Probability and Statistics for Machine Learning

Multivariate Gaussian

The multivariate Gaussian as geometry plus probability: covariance creates ellipsoids, the precision matrix weights distance, and whitening turns the distribution spherical.

Lesson
15

Goal

Understand the multivariate Gaussian as a geometric probability distribution: covariance determines the ellipsoidal shape, the precision matrix determines how distance is measured, and whitening reveals that the distribution is just a spherical Gaussian viewed in transformed coordinates.

Motivation

Many machine learning models assume that some source of noise, uncertainty, or latent variation is Gaussian.

That can feel like a memorized convention until you see the geometry. Once covariance defines an ellipsoid, the multivariate Gaussian becomes the natural probability density built from that geometry.

Prerequisites

Notation / Symbol Table

SymbolPlain-English nameMeaning
xxevaluation pointPoint in Rn\mathbb{R}^n where the density is evaluated.
μ\mumean vectorCenter of the Gaussian.
Σ\Sigmacovariance matrixDescribes spread and orientation.
Σ1\Sigma^{-1}precision matrixWeights distance by confidence instead of uncertainty.
dM2d_M^2squared Mahalanobis distanceDistance from xx to μ\mu in covariance-scaled geometry.
Σ\lvert\Sigma\rvertdeterminant of covarianceVolume-scaling term used in normalization.
λi\lambda_iprincipal varianceVariance along principal direction ii.
zzwhitened coordinateCoordinate after removing covariance structure.

The squared Mahalanobis distance is

dM2=(xμ)TΣ1(xμ).d_M^2 = (x - \mu)^T \Sigma^{-1} (x - \mu).

Core Ideas

Covariance turns circles into ellipses

In ordinary Euclidean geometry, points at fixed squared distance from the origin satisfy

xTx=r2.x^T x = r^2.

That gives a circle in two dimensions or a sphere in higher dimensions.

When covariance enters, the relevant quadratic form becomes

(xμ)TΣ1(xμ).(x - \mu)^T \Sigma^{-1} (x - \mu).

Level sets of this expression are ellipses or ellipsoids. The covariance matrix stretches and rotates the geometry.

Why the inverse covariance appears

Large variance means motion in that direction is unsurprising. Small variance means motion in that direction is surprising.

So the distance metric should:

That is exactly what the inverse covariance does. If one direction has variance 100100, the corresponding precision is 1/1001/100, which makes movement in that direction contribute less to the distance.

Mahalanobis distance is Euclidean distance in whitened space

Whitening uses

z=Σ1/2(xμ).z = \Sigma^{-1/2}(x - \mu).

Then

zTz=(xμ)TΣ1(xμ).z^T z = (x - \mu)^T \Sigma^{-1} (x - \mu).

So the Mahalanobis distance is not a brand-new species of distance. It is ordinary Euclidean distance after the right change of coordinates.

The Gaussian density is the right exponential of distance

The one-dimensional Gaussian uses squared distance divided by variance:

p(x)=12πσ2exp((xμ)22σ2).p(x) = \frac{1}{\sqrt{2\pi \sigma^2}} \exp\left( -\frac{(x - \mu)^2}{2\sigma^2} \right).

The multivariate version replaces ordinary squared distance with squared Mahalanobis distance:

p(x)=1(2π)n/2Σ1/2exp(12(xμ)TΣ1(xμ)).p(x) = \frac{1}{(2\pi)^{n/2} |\Sigma|^{1/2}} \exp\left( -\frac{1}{2}(x - \mu)^T \Sigma^{-1} (x - \mu) \right).

Nothing mystical happened. We took the one-dimensional idea and upgraded the geometry.

The determinant normalizes by volume

The determinant Σ|\Sigma| measures how covariance stretches volume.

If covariance spreads the distribution out across a larger ellipsoid, the peak density must be lower so the total probability still integrates to 11.

So the determinant is doing the same kind of job it always does in linear algebra: measuring how a transformation scales volume.

Eigenvectors and eigenvalues explain the shape

If

Σ=VΛVT,\Sigma = V \Lambda V^T,

then the eigenvectors give the principal axes of the Gaussian ellipsoid. The eigenvalues give the variances along those axes.

The semi-axis lengths are proportional to

λi.\sqrt{\lambda_i}.

So large eigenvalues correspond to broad directions of uncertainty, while small eigenvalues correspond to narrow directions of confidence.

Worked Example

Let

μ=[00],Σ=[9004],x=[32].\mu = \begin{bmatrix} 0 \\ 0 \end{bmatrix}, \qquad \Sigma = \begin{bmatrix} 9 & 0 \\ 0 & 4 \end{bmatrix}, \qquad x = \begin{bmatrix} 3 \\ 2 \end{bmatrix}.

Then

Σ1=[190014].\Sigma^{-1} = \begin{bmatrix} \frac{1}{9} & 0 \\ 0 & \frac{1}{4} \end{bmatrix}.

Now compute the squared Mahalanobis distance:

dM2=xTΣ1x=[32][190014][32]=1+1=2.d_M^2 = x^T \Sigma^{-1} x = \begin{bmatrix} 3 & 2 \end{bmatrix} \begin{bmatrix} \frac{1}{9} & 0 \\ 0 & \frac{1}{4} \end{bmatrix} \begin{bmatrix} 3 \\ 2 \end{bmatrix} = 1 + 1 = 2.

Interpretation:

So this point sits one standard deviation away along each principal axis.

Why This Shows Up In ML

Gaussians show up all over machine learning because they are mathematically tractable and geometrically expressive.

Examples include:

The broader lesson is that linear algebra is now describing uncertainty rather than only transformations.

Exercises

  1. For
Σ=[16001],\Sigma = \begin{bmatrix} 16 & 0 \\ 0 & 1 \end{bmatrix},

sketch the covariance ellipse and label its semi-axis lengths.

  1. If an eigenvalue of Σ\Sigma becomes extremely small, what happens to:
  1. Explain in words why the Gaussian formula uses Σ1\Sigma^{-1} in the exponent and Σ|\Sigma| in the normalization constant.

  2. Why does whitening turn a multivariate Gaussian into a spherical one?

Implementation Exercise

Write a TypeScript function that evaluates the log density of a two-dimensional Gaussian.

Start with a diagonal covariance matrix so the inverse and determinant are easy to compute by hand. Then extend the function so it accepts:

Keep the implementation explicit about each mathematical piece: centered vector, precision-weighted quadratic form, and determinant-based normalization.

Reflection Questions