MultivariateNormal
DistributionMultivariateNormal(loc: Tensor, covariance_matrix: Tensor | None = None, precision_matrix: Tensor | None = None, scale_tril: Tensor | None = None, validate_args: bool | None = None)Multivariate Normal (Gaussian) distribution in .
MultivariateNormal(loc=μ, ...) defines the -dimensional
Gaussian with mean vector and a covariance structure
expressed in one of three equivalent forms. Internally all forms are
converted to the lower-triangular Cholesky factor of
(i.e. ), which is the numerically
preferred representation for both sampling and log-probability evaluation.
Specify exactly one of:
covariance_matrix(positive-definite, ),precision_matrix(positive-definite, inverted via Cholesky internally), orscale_tril(lower-triangular with positive diagonal).
Parameters
locTensor(..., D).covariance_matrixTensor | None= None(..., D, D).precision_matrixTensor | None= None(..., D, D).scale_trilTensor | None= None(..., D, D)
with positive diagonal entries.validate_argsbool | None= NoneTrue, validate parameter constraints at construction time.Attributes
locTensorscale_trilTensorNotes
PDF:
Log-PDF (numerically stable via Cholesky, avoiding explicit inversion):
where is the squared Mahalanobis distance computed via triangular solve (no explicit matrix inversion).
Moments:
- Mean:
- Mode: (Gaussian is unimodal)
- Marginal variances: diagonal of
Entropy:
Reparameterised sampling uses the Cholesky factorisation:
Gradients propagate through and unobstructed.
Examples
>>> import lucid
>>> from lucid.distributions import MultivariateNormal
>>> # 2-d isotropic Gaussian
>>> dist = MultivariateNormal(
... loc=lucid.zeros(2),
... covariance_matrix=lucid.eye(2),
... )
>>> samples = dist.rsample((50,))
>>> samples.shape # (50, 2)
(50, 2)
>>> # Log-prob at the mean (maximum)
>>> dist.log_prob(lucid.zeros(2))Methods (8)
__init__
→None__init__(loc: Tensor, covariance_matrix: Tensor | None = None, precision_matrix: Tensor | None = None, scale_tril: Tensor | None = None, validate_args: bool | None = None)Initialise a MultivariateNormal distribution.
Exactly one of covariance_matrix, precision_matrix, or
scale_tril must be provided. All parameterisations are
internally converted to the lower-triangular Cholesky factor L
via lucid.linalg.cholesky or triangular inversion.
Parameters
locTensor(..., D).covariance_matrixTensor | None= None(..., D, D).precision_matrixTensor | None= None(..., D, D).scale_trilTensor | None= None(..., D, D)
with positive diagonal.validate_argsbool | None= NoneTrue, validate parameter constraints at construction time.Raises
ValueErrorcovariance_matrix
→Tensorcovariance_matrix: TensorFull covariance matrix .
Returns
TensorPositive-definite covariance matrix of shape (*batch_shape, D, D).
mean
→Tensormean: TensorMean of the MultivariateNormal: .
Returns
TensorMean vector of shape (*batch_shape, D).
mode
→Tensormode: TensorMode of the MultivariateNormal: equal to the mean .
The Gaussian is unimodal; its unique maximum is at the mean.
Returns
TensorMode vector of shape (*batch_shape, D).
variance
→Tensorvariance: TensorMarginal variances — diagonal entries of .
Returns
TensorVariance vector of shape (*batch_shape, D).
rsample
→Tensorrsample(sample_shape: tuple[int, ...] = ())Draw reparameterised samples via the Cholesky factorisation.
Gradients propagate through both and .
Parameters
sample_shapetuple[int, ...]= ()Returns
TensorSamples of shape (*sample_shape, *batch_shape, D).
log_prob
→Tensorlog_prob(value: Tensor)Log-probability density of the MultivariateNormal distribution.
Computed via the Cholesky factor to avoid explicit matrix inversion:
Parameters
valueTensor(*batch_shape, D).Returns
TensorLog-density values of shape batch_shape.
entropy
→Tensorentropy()Entropy of the MultivariateNormal distribution.
Returns
TensorEntropy values of shape batch_shape (nats).