torch.distributions.MultivariateNormal
class MultivariateNormal extends Distributionnew MultivariateNormal(loc: number[] | Tensor, options: {
covariance_matrix?: Tensor;
precision_matrix?: Tensor;
scale_tril?: Tensor;
} & DistributionOptions)
- readonly
loc(Tensor) - – Mean of the distribution.
- readonly
arg_constraints(unknown) - readonly
support(unknown) - readonly
has_rsample(unknown) - readonly
scale_tril(Tensor) - – Get the Cholesky factor of the covariance matrix.
- readonly
covariance_matrix(Tensor) - – Get the covariance matrix.
- readonly
precision_matrix(Tensor) - – Get the precision matrix (inverse of covariance).
- readonly
mean(Tensor) - readonly
mode(Tensor) - readonly
variance(Tensor)
Multivariate Normal distribution: joint distribution over multiple dimensions.
Generalization of normal distribution to multiple correlated variables. Fundamental distribution for multivariate statistics and machine learning. Essential for:
- Multivariate regression and Gaussian processes
- Bayesian neural networks (weight priors)
- Variational autoencoders (VAE latent distributions)
- Gaussian mixture models and clustering
- Kalman filters and state-space models
- Principal component analysis (PCA) modeling
- Multivariate hypothesis testing
- Latent variable models and factor analysis
Parameterized by mean vector μ ∈ ℝ^d and covariance matrix Σ ∈ ℝ^(d×d) (positive definite) PDF: f(x) = (2π)^(-d/2) |Σ|^(-1/2) exp(-0.5 * (x-μ)^T Σ^(-1) (x-μ))
- Cholesky form: scale_tril is most numerically stable for sampling
- Marginals: Each dimension marginally is univariate N(μ_i, Σ_ii)
- Conditionals: Conditioning on subset dimensions yields normal
- Linear transforms: A X + b has MVN(A μ + b, A Σ A^T)
- Independence: Uncorrelated (zero covariance) implies independence for joint normal
- Mahalanobis distance: (x-μ)^T Σ^(-1) (x-μ) ~ Chi2(d) for x ~ MVN
- Positive definite: Covariance/precision must be positive definite
- Dimension mismatch: Covariance must be d×d for d-dim mean
- Singularity: Singular covariance causes numerical issues
- Computation: Computing covariance inverse can be numerically unstable
- High dimensions: Covariance becomes dense; consider factorizations
Examples
// Standard bivariate normal: independent normals N(0,1)
const m = new torch.distributions.MultivariateNormal(
torch.tensor([0.0, 0.0]),
{ covariance_matrix: torch.eye(2) }
);
m.sample(); // 2D sample from standard normal
// Correlated bivariate normal
const mean = torch.tensor([1.0, 2.0]);
const cov = torch.tensor([
[1.0, 0.5], // variance=1, correlation=0.5
[0.5, 1.0] // variance=1
]);
const correlated = new torch.distributions.MultivariateNormal(mean, { covariance_matrix: cov });
const sample = correlated.sample(); // Positively correlated pair
// Using Cholesky factorization (numerically stable)
// More efficient: Σ = L @ L.T
const L = torch.tensor([[1.0, 0.0], [0.5, Math.sqrt(0.75)]]);
const chol_dist = new torch.distributions.MultivariateNormal(
torch.zeros([2]),
{ scale_tril: L }
); // Cholesky parameterization
// Using precision matrix (inverse covariance)
// Useful in graphical models where precision encodes sparsity
const precision = torch.inverse(cov);
const precision_dist = new torch.distributions.MultivariateNormal(
mean,
{ precision_matrix: precision }
);
// Gaussian process: predict at multiple points
// Joint distribution over function values at d points
const mean_vector = torch.randn([100]); // Mean at 100 points
const K = kernel_matrix(x_train, x_train); // Kernel/covariance matrix
const gp_dist = new torch.distributions.MultivariateNormal(mean_vector, { covariance_matrix: K });
const f_samples = gp_dist.sample([10]); // 10 GP function samples
// Variational autoencoder (VAE): latent distribution
const z_dim = 32; // Latent dimension
const z_mean = encoder(x); // Learned means
const z_logvar = log_encoder(x); // Learned log-variances
const z_cov = torch.diag(z_logvar.exp()); // Diagonal covariance
const z_dist = new torch.distributions.MultivariateNormal(
z_mean,
{ covariance_matrix: z_cov }
);
const z = z_dist.rsample(); // Reparameterized sample
// Batched: multiple multivariate normals
const batch_means = torch.randn([10, 5]); // 10 distributions, dimension 5
const cov_batch = torch.eye(5).unsqueeze(0).expand([10, 5, 5]); // Same cov for all
const batch_dist = new torch.distributions.MultivariateNormal(
batch_means,
{ covariance_matrix: cov_batch }
);