MultivariateNormalScore#
- class bayesflow.scores.MultivariateNormalScore(dim: int = None, links: dict = None, **kwargs)[source]#
Bases:
ParametricDistributionScore\(S(\hat p_{\mu, \Sigma}, \theta; k) = -\log( \mathcal N (\theta; \mu, \Sigma))\)
Scores a predicted mean and lower-triangular Cholesky factor \(L\) of the precision matrix \(P\) with the log-score of the probability of the materialized value. The precision matrix is the inverse of the covariance matrix, \(L^T L = P = \Sigma^{-1}\).
- NOT_TRANSFORMING_LIKE_VECTOR_WARNING: tuple[str] = ('precision_cholesky_factor',)#
Marks head for precision matrix Cholesky factor as an exception for adapter transformations.
This variable contains names of prediction heads that should lead to a warning when the adapter is applied in inverse direction to them.
For more information see
ScoringRule.
- TRANSFORMATION_TYPE: dict[str, str] = {'precision_cholesky_factor': 'right_side_scale_inverse'}#
Marks precision Cholesky factor head to handle de-standardization appropriately.
See
bayesflow.networks.Standardizationfor more information on supported de-standardization options.For the mean head the default (“location_scale”) is not overridden.
- get_head_shapes_from_target_shape(target_shape: tuple[int, ...]) dict[str, tuple[int, ...]][source]#
Request a dictionary of names and output shapes of required heads from the score.
- classmethod from_config(config)#
- get_head(key: str, output_shape: tuple[int, ...]) Sequential#
For a specified head key and output shape, request corresponding head network.
A head network has the following components that are called sequentially:
subnet: A keras.Layer.
dense: A trainable linear projection with as many units as are required by the next component.
reshape: Changes shape of output of projection to match requirements of next component.
link: Transforms unconstrained values into a constrained space for the final estimator. See
linksfor examples.
This method initializes the components in reverse order to meet all requirements and returns them.
- Parameters:
- keystr
Name of head for which to request a link.
- output_shape: Shape
The necessary shape of estimated values for the given key as returned by
get_head_shapes_from_target_shape().
- Returns:
- headkeras.Sequential
Head network consisting of a learnable projection, a reshape and a link operation to parameterize estimates.
- get_link(key: str) Layer#
For a specified key, request a link from network output to estimation target.
If no link was specified for the key (e.g. upon initialization), return a linear activation.
- Parameters:
- keystr
Name of head for which to request a link.
- Returns:
- linkkeras.Layer
Activation function linking network output to estimation target.
- get_subnet(key: str) Layer#
For a specified key, request a subnet to be used for projecting the shared condition embedding before further projection and reshaping to the heads output shape.
If no subnet was specified for the key (e.g. upon initialization), return just an instance of keras.layers.Identity.
- Parameters:
- keystr
Name of head for which to request a subnet.
- Returns:
- linkkeras.Layer
Subnet projecting the shared condition embedding.
- log_prob(x: Tensor, mean: Tensor, precision_cholesky_factor: Tensor) Tensor[source]#
Compute the log probability density of a multivariate Gaussian distribution.
This function calculates the log probability density for each sample in x under a multivariate Gaussian distribution with the given mean and precision_cholesky_factor.
The computation includes the determinant of the precision matrix, its inverse, and the quadratic form in the exponential term of the Gaussian density function.
- Parameters:
- xTensor
A tensor of input samples for which the log probability density is computed. The shape should be compatible with broadcasting against mean.
- meanTensor
A tensor representing the mean of the multivariate Gaussian distribution.
- precision_cholesky_factorTensor
A tensor representing the lower-triangular Cholesky factor of the precision matrix of the multivariate Gaussian distribution.
- Returns:
- Tensor
A tensor containing the log probability densities for each sample in x under the given Gaussian distribution.
- score(estimates: dict[str, Tensor], targets: Tensor, weights: Tensor = None) Tensor#
Computes the log-score for a predicted parametric probability distribution given realized targets.
\(S(\hat p_\phi, \theta; k) = -\log(\hat p_\phi(\theta))\)
- sample(batch_shape: tuple[int, ...], mean: Tensor, precision_cholesky_factor: Tensor) Tensor[source]#
Generate samples from a multivariate Gaussian distribution.
Independent standard normal samples are transformed using the Cholesky factor of the precision matrix to generate correlated samples.
- Parameters:
- batch_shapeShape
A tuple specifying the batch size and the number of samples to generate.
- meanTensor
A tensor representing the mean of the multivariate Gaussian distribution. Must have shape (batch_size, D), where D is the dimensionality of the distribution.
- precision_cholesky_factorTensor
A tensor representing the lower-triangular Cholesky factor of the precision matrix of the multivariate Gaussian distribution. Must have shape (batch_size, D, D), where D is the dimensionality.
- Returns:
- Tensor
A tensor of shape (batch_size, num_samples, D) containing the generated samples.