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 covariance matrix with the log-score of the probability of the materialized value.

NOT_TRANSFORMING_LIKE_VECTOR_WARNING = ('covariance',)#

Marks head for covariance matrix 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.

get_config()[source]#
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.

log_prob(x: Tensor, mean: Tensor, covariance: 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 covariance.

The computation includes the determinant of the covariance 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.

covarianceTensor

A tensor representing the covariance 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.

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:

  1. subnet: A keras.Layer.

  2. dense: A trainable linear projection with as many units as are required by the next component.

  3. reshape: Changes shape of output of projection to match requirements of next component.

  4. link: Transforms unconstrained values into a constrained space for the final estimator. See links for 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.

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.

sample(batch_shape: tuple[int, ...], mean: Tensor, covariance: Tensor) Tensor[source]#

Generate samples from a multivariate Gaussian distribution.

This function samples from a multivariate Gaussian distribution with the given mean and covariance using the Cholesky decomposition method. Independent standard normal samples are transformed using the Cholesky factor of the covariance 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.

covarianceTensor

A tensor representing the covariance 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.

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))\)