Skip to content
AI360XpertBeta

Multivariate Gaussian Distribution

The bell curve in many dimensions at once: a center plus a covariance matrix that stretches the contours into tilted ellipses and sets the ruler for distance.

With mean at the origin and covariance 5 3 3 5, the 1 sigma ellipse reaches (2, 2) exactly, so that point sits one Mahalanobis unit out with density 0.0241
With mean at the origin and covariance 5 3 3 5, the 1 sigma ellipse reaches (2, 2) exactly, so that point sits one Mahalanobis unit out with density 0.0241

Why Does This Exist?

Data rarely arrives one number at a time. A sensor reports position and velocity together, a model embeds a word as hundreds of coordinates, a filter tracks a whole state vector. The general joint law of a vector is taught in multivariate distributions: one rule weighing every combination, organized by a mean vector and a covariance matrix. That page is the family; this page is its most-used member.

The Gaussian earns its own page because it is the only joint law fully described by those two summaries. Give it a mean vector μ\mu and a covariance matrix Σ\Sigma and every marginal, every conditional, and every contour is fixed, with closed forms throughout. That is why Kalman filters track Gaussians, why variational methods fit Gaussians first, and why central limit theorem averages converge to one.

The one idea you need first

A random vector stacks several random variables into X=(X1,…,Xd)X = (X_1, \ldots, X_d), and its joint law answers combination questions. The mean vector collects the centers μi=E[Xi]\mu_i = E[X_i], and the covariance matrix collects the pairwise leans Σij=E[(Xi−μi)(Xj−μj)]\Sigma_{ij} = E[(X_i - \mu_i)(X_j - \mu_j)], built from the averaging in expectation, variance and covariance. The Gaussian is the special case where these two objects are the entire distribution.

Think of It Like This

An archery target printed on stretched rubber

Take a round archery target: concentric circles around a bullseye, with rings marking one, two and three standard deviations. Now stretch the rubber sheet wider in one direction than the other, then twist it 45 degrees and pin it down. The circles become tilted ellipses, but the bullseye stays the bullseye and the rings still mark the same probabilities.

The bullseye is the mean vector μ\mu: where the cloud centers. The stretch plus twist is the covariance matrix Σ\Sigma: variances set how far each axis stretches, covariance sets the twist. A point on the first ellipse is one Mahalanobis unit out no matter which direction it lies in, exactly as every point on the original first circle was one standard deviation out.

The analogy stops where shape stops. Rubber stretches a circle into exactly one ellipse family, and the Gaussian likewise produces only ellipses. Real data forms bananas, rings and separate clusters that no amount of stretching a circle will match. When the contours need more than one center, reach for a mixture, not a bigger ellipse.

How It Actually Works

Write the density for x∈Rdx \in \mathbb{R}^d with mean vector μ∈Rd\mu \in \mathbb{R}^d and covariance matrix Σ∈Rd×d\Sigma \in \mathbb{R}^{d \times d}:

N(x∣μ,Σ)=1(2π)d/2det⁡Σexp⁡(−12(x−μ)⊤Σ−1(x−μ))\mathcal{N}(x \mid \mu, \Sigma) = \frac{1}{(2\pi)^{d/2}\sqrt{\det \Sigma}} \exp\left(-\tfrac{1}{2}(x - \mu)^\top \Sigma^{-1}(x - \mu)\right)

In plain words: μ\mu is the center of the cloud, Σ\Sigma is its shape and tilt, det⁡Σ\det \Sigma sets the normalizing volume, and the exponent measures squared distance in the curved ruler Σ\Sigma defines. Every symbol is used below, none is decorative.

Three readings of Σ\Sigma that practitioners actually use:

  • Variances on the diagonal Σii\Sigma_{ii} set the spread along each axis. Standard deviations are their square roots.
  • Covariances off the diagonal Σij\Sigma_{ij} set the tilt. Divided by both standard deviations they become correlations in [−1,1][-1, 1], which speak strength instead of units.
  • Eigen-structure sets the ellipse axes. The eigenvectors of Σ\Sigma point along the ellipse axes and the square roots of the eigenvalues are the half-lengths of the 1-sigma contour.

The exponent has its own name because it is used constantly. The Mahalanobis distance is DM(x)=(x−μ)⊤Σ−1(x−μ)D_M(x) = \sqrt{(x - \mu)^\top \Sigma^{-1}(x - \mu)}: Euclidean distance after whitening by Σ\Sigma. Contours of equal density are contours of equal Mahalanobis distance, which is why the diagram draws ellipses and labels one of them M=1M = 1.

Worked example

Take d=2d = 2 with μ=[0,0]\mu = [0, 0] and Σ=[[5,3],[3,5]]\Sigma = [[5, 3], [3, 5]].

Variances are 55 and 55, so each standard deviation is 5≈2.236\sqrt{5} \approx 2.236. Covariance 33 divided by 55 gives correlation 0.60.6: a clear rising tilt. Determinant det⁡Σ=25−9=16\det \Sigma = 25 - 9 = 16, so det⁡Σ=4\sqrt{\det \Sigma} = 4 and the inverse is 116[[5,−3],[−3,5]]\frac{1}{16}[[5, -3], [-3, 5]]. Eigenvalues are 88 and 22 since the trace is 1010 and (10±100−64)/2=8,2(10 \pm \sqrt{100 - 64})/2 = 8, 2, so the 1-sigma ellipse has half-lengths 8≈2.828\sqrt{8} \approx 2.828 along (1,1)(1, 1) and 2≈1.414\sqrt{2} \approx 1.414 along (1,−1)(1, -1): a 45-degree tilt, exactly as drawn.

Test the point x=(2,2)x = (2, 2) with displacement dx=[2,2]dx = [2, 2]:

DM2=116(5×4+5×4−2×3×2×2)=116(20+20−24)=1D_M^2 = \tfrac{1}{16}(5 \times 4 + 5 \times 4 - 2 \times 3 \times 2 \times 2) = \tfrac{1}{16}(20 + 20 - 24) = 1

so DM=1D_M = 1: the point sits exactly on the 1-sigma contour, which is why the diagram places it at the tip of the major axis. Its density is 1/(8π)×e−0.5≈0.6065/25.1327≈0.02411/(8\pi) \times e^{-0.5} \approx 0.6065 / 25.1327 \approx 0.0241.

Marginals and conditionals fall out without integration. Each marginal is Gaussian: X1∼N(0,5)X_1 \sim \mathcal{N}(0, 5) and X2∼N(0,5)X_2 \sim \mathcal{N}(0, 5). Given X1=2X_1 = 2, the conditional is X2∣X1=2∼N(1.2,3.2)X_2 \mid X_1 = 2 \sim \mathcal{N}(1.2, 3.2), since the conditional mean is (3/5)×2=1.2(3/5) \times 2 = 1.2 and the conditional variance is 5−9/5=16/5=3.25 - 9/5 = 16/5 = 3.2. Conditioning shrinks the variance from 55 to 3.23.2 because part of X2X_2 is explained by X1X_1.

Code

The snippet evaluates the worked fixture directly: squared Mahalanobis distance and density at (2,2)(2, 2), with no library beyond NumPy.

import numpy as np
mu = np.array([0.0, 0.0])Sigma = np.array([[5.0, 3.0], [3.0, 5.0]])x = np.array([2.0, 2.0])
dx = x - muinv = np.linalg.inv(Sigma)m2 = float(dx @ inv @ dx)dens = float(np.exp(-0.5 * m2) / (2 * np.pi * np.sqrt(np.linalg.det(Sigma))))
print(round(m2, 4))    # -> 1.0print(round(dens, 4))  # -> 0.0241

One and 0.02410.0241, matching the hand calculation. The dx @ inv @ dx line is the Mahalanobis form; everything else is the normalizer.

Watch Out For

Fitting one Gaussian to data with two centers

A single Gaussian has one μ\mu, so on bimodal data the fitted mean lands between the clusters where no data lives. The classic case is two well-separated blobs: the maximum-likelihood center sits in the empty middle, the covariance inflates to cover both, and the density peaks exactly where the data is absent. Samples from the fit then look nothing like the training set, and downstream thresholds built from the contours misfire.

The symptom is a mean with low data density around it and residuals that cluster rather than scatter. The fix is a mixture with one component per center, or a check before fitting: if a histogram or a 2-D scatter shows two humps, do not fit one bell. The diagram's limit line states the same boundary: one Gaussian cannot fit separate clusters.

Inverting a singular or near-singular covariance

When two features are nearly perfectly correlated, rows of Σ\Sigma nearly repeat and det⁡Σ\det \Sigma approaches zero. The inverse then explodes: tiny measurement noise in x−μx - \mu multiplies into enormous Mahalanobis distances, densities swing wildly, and np.linalg.inv may warn or return garbage. Duplicated columns, one-hot groups with a dropped-category bug, or more dimensions than samples all create this.

The symptom is a determinant near zero, a condition number in the millions, or Mahalanobis distances that change drastically when one sample is removed. The fixes in order: remove the duplicated feature, add a small jitter to the diagonal (Σ+10−6I\Sigma + 10^{-6}I), or work with the pseudo-inverse and a rank check. Never trust a density whose normalizer divides by a near-zero determinant.

The Quick Version

  • The multivariate Gaussian is the joint law fully described by a mean vector μ\mu and a covariance matrix Σ\Sigma.
  • Diagonal entries of Σ\Sigma set axis spreads; off-diagonals set tilt; correlation rescales tilt into [−1,1][-1, 1].
  • Contours of equal density are ellipses of equal Mahalanobis distance DM=(x−μ)⊤Σ−1(x−μ)D_M = \sqrt{(x-\mu)^\top \Sigma^{-1}(x-\mu)}.
  • With Σ=[[5,3],[3,5]]\Sigma = [[5, 3], [3, 5]], correlation is 0.60.6, the determinant is 1616, and (2,2)(2, 2) sits exactly at DM=1D_M = 1 with density about 0.02410.0241.
  • Marginals and conditionals of a Gaussian are Gaussian, with conditioning shrinking variance, here from 55 to 3.23.2.
  • One Gaussian cannot fit separate clusters, and a near-singular Σ\Sigma breaks the inverse, so check modality and the determinant before trusting a fit.