Skip to content
BIASED

The multivariate Gaussian

The mean vector and covariance matrix, the density and the Mahalanobis distance, ellipses and eigenvectors, affine maps and sampling, why uncorrelated coordinates are independent, and the conditioning formula.

Random vectors and the covariance matrix

The topic Joint distributions and independence gave us the words for several variables at once: a joint density, marginals, conditional distributions, and covariance as a measure of how two variables move together. It also showed the price. A joint distribution of dd variables is a function of dd arguments, and nothing forces it to be simple. This topic studies the one family that machine learning reaches for first, because a vector of means and a table of covariances describe it completely.

Stack the variables into a random vector X=(X1,…,Xd)⊤X = (X_1, \dots, X_d)^\top, for example dd features of one object in a dataset. Its mean vector collects the means, μi=E[Xi]\mu_i = \mathbb E[X_i]. Its covariance matrix collects all the covariances:

Σij=Cov⁡(Xi,Xj),Σ=E[(X−μ)(X−μ)⊤].\Sigma_{ij} = \operatorname{Cov}(X_i, X_j), \qquad \Sigma = \mathbb E\big[(X - \mu)(X - \mu)^\top\big].

A one-line reminder on matrices: v⊤v^\top turns a column into a row, and a column times a row, vw⊤vw^\top, is a table with entries viwjv_i w_j. So the second formula is just the first one written for all pairs (i,j)(i, j) at once. The diagonal holds the variances, since Cov⁡(Xi,Xi)=Var⁡(Xi)\operatorname{Cov}(X_i, X_i) = \operatorname{Var}(X_i).

The running example of this topic has two features measured from their averages, so μ=0\mu = 0, and

Σ=(21.21.21).\Sigma = \begin{pmatrix} 2 & 1.2 \\ 1.2 & 1 \end{pmatrix}.

Feature 1 varies more (Var⁡(X1)=2\operatorname{Var}(X_1) = 2), feature 2 less (Var⁡(X2)=1\operatorname{Var}(X_2) = 1), and the positive covariance 1.21.2 says they tend to rise together.

Not every table of numbers can be a covariance matrix. It is symmetric, because Cov⁡(Xi,Xj)=Cov⁡(Xj,Xi)\operatorname{Cov}(X_i, X_j) = \operatorname{Cov}(X_j, X_i). And for any fixed weights aa, the number a⊤X=a1X1+⋯+adXda^\top X = a_1 X_1 + \dots + a_d X_d has variance

Var⁡(a⊤X)=∑i,jaiajΣij=a⊤Σa,\operatorname{Var}(a^\top X) = \sum_{i, j} a_i a_j \Sigma_{ij} = a^\top \Sigma a,

which is the rule Var⁡(X+Y)=Var⁡(X)+Var⁡(Y)+2Cov⁡(X,Y)\operatorname{Var}(X + Y) = \operatorname{Var}(X) + \operatorname{Var}(Y) + 2\operatorname{Cov}(X, Y) from the previous topic, extended to dd weighted terms. A variance is never negative, so a⊤Σa≥0a^\top \Sigma a \ge 0 for every aa: the matrix is positive semidefinite. For a symmetric 2×22 \times 2 matrix this comes down to a nonnegative diagonal and a nonnegative determinant Σ11Σ22−Σ122\Sigma_{11}\Sigma_{22} - \Sigma_{12}^2, which is the familiar ∣ρ∣≤1\lvert\rho\rvert \le 1 once more. The course Mathematics for ML covers such matrices in the topic Symmetric matrices and quadratic forms.

The density and the Mahalanobis distance

When Σ\Sigma is positive definite (a⊤Σa>0a^\top \Sigma a > 0 for every a≠0a \ne 0, which also makes it invertible), the multivariate Gaussian N(μ,Σ)\mathcal N(\mu, \Sigma) is the distribution with density

p(x)=1(2π)d/2 ∣Σ∣1/2exp⁡ ⁣(−12 (x−μ)⊤Σ−1(x−μ)).p(x) = \frac{1}{(2\pi)^{d/2}\,\lvert\Sigma\rvert^{1/2}} \exp\!\Big(-\tfrac12\,(x - \mu)^\top \Sigma^{-1} (x - \mu)\Big).

Here ∣Σ∣\lvert\Sigma\rvert is the determinant and Σ−1\Sigma^{-1} the inverse matrix, ΣΣ−1=I\Sigma\Sigma^{-1} = I. For a 2×22 \times 2 matrix both fit in one line:

(accb)−1=1ab−c2(b−c−ca),determinant ab−c2.\begin{pmatrix} a & c \\ c & b \end{pmatrix}^{-1} = \frac{1}{ab - c^2}\begin{pmatrix} b & -c \\ -c & a \end{pmatrix}, \qquad \text{determinant } ab - c^2.

With d=1d = 1 and Σ=σ2\Sigma = \sigma^2 the formula is the normal density from Continuous distributions.

The density depends on xx only through the number in the exponent,

Δ2(x)=(x−μ)⊤Σ−1(x−μ),\Delta^2(x) = (x - \mu)^\top \Sigma^{-1} (x - \mu),

the squared Mahalanobis distance from xx to the mean. In one dimension Δ=∣x−μ∣/σ\Delta = \lvert x - \mu\rvert / \sigma, the distance in standard deviations, the same zz-score as in standardization from Functions of random variables. In several dimensions it does the same job, but it measures every direction by the spread in that direction and takes the correlations into account. The smaller Δ\Delta, the higher the density.

In the running example ∣Σ∣=2⋅1−1.22=0.56\lvert\Sigma\rvert = 2 \cdot 1 - 1.2^2 = 0.56 and Σ−1=10.56(1−1.2−1.22)\Sigma^{-1} = \frac{1}{0.56}\begin{pmatrix} 1 & -1.2 \\ -1.2 & 2 \end{pmatrix}. Take two points at the same Euclidean distance 2\sqrt2 from the mean:

  • (1,1)(1, 1): Δ2=(1−2.4+2)/0.56≈1.07\Delta^2 = (1 - 2.4 + 2)/0.56 \approx 1.07. Both features are above average together, which the correlation makes typical.
  • (1,−1)(1, -1): Δ2=(1+2.4+2)/0.56≈9.64\Delta^2 = (1 + 2.4 + 2)/0.56 \approx 9.64. One feature up and the other down goes against the correlation and is rare.

This is why anomaly detectors on correlated features use the Mahalanobis distance rather than the Euclidean one.

Ellipses and eigenvectors

The points with equal density satisfy Δ2(x)=k2\Delta^2(x) = k^2. For a Gaussian these level sets are ellipses, and ellipsoids when d>2d > 2. Their axes come from the covariance matrix.

A reminder: a vector u≠0u \ne 0 is an eigenvector of Σ\Sigma with eigenvalue λ\lambda if Σu=λu\Sigma u = \lambda u, so the matrix only stretches it. A symmetric d×dd \times d matrix has dd mutually perpendicular unit eigenvectors, and for a positive semidefinite one all λ≥0\lambda \ge 0. More in Mathematics for ML, topic Determinants and eigenvalues.

Along a unit eigenvector uiu_i the projection ui⊤Xu_i^\top X has variance ui⊤Σui=λiu_i^\top \Sigma u_i = \lambda_i. So λi\sqrt{\lambda_i} is the standard deviation in that direction, and the ellipse Δ=1\Delta = 1 has its semi-axes along the uiu_i with lengths λi\sqrt{\lambda_i}.

In the running example λ1=2.8\lambda_1 = 2.8 and λ2=0.2\lambda_2 = 0.2. Their sum is 3=2+13 = 2 + 1, the total variance, and their product is 0.56=∣Σ∣0.56 = \lvert\Sigma\rvert. The long axis points along u1≈(0.83,0.55)u_1 \approx (0.83, 0.55) with semi-axis 2.8≈1.67\sqrt{2.8} \approx 1.67, the short one along u2≈(−0.55,0.83)u_2 \approx (-0.55, 0.83) with 0.2≈0.45\sqrt{0.2} \approx 0.45. The point (1,1)(1, 1) from the last block lies close to the long axis, and (1,−1)(1, -1) close to the short one; hence their very different distances. The long axis carries 2.8/3≈93%2.8/3 \approx 93\% of the total variance. This direction is the first principal component, the starting point of PCA.

How much probability does an ellipse hold? In one dimension, ∣X−μ∣≤σ\lvert X - \mu\rvert \le \sigma has probability 0.6830.683. In two dimensions Δ2\Delta^2 is itself a random variable, and it has the exponential distribution with rate 1/21/2; the next block shows where that comes from. So

P(Δ≤k)=1−e−k2/2,d=2.P(\Delta \le k) = 1 - e^{-k^2/2}, \qquad d = 2.

For k=2k = 2 that is 1−e−2≈0.8651 - e^{-2} \approx 0.865, noticeably less than the 0.9540.954 of a 1-D two-sigma interval. In dd dimensions Δ2\Delta^2 follows the chi-squared distribution with dd degrees of freedom, and a fixed ellipse holds less and less as dd grows.

Affine maps and sampling

The property everything else in this topic rests on: if X∼N(μ,Σ)X \sim \mathcal N(\mu, \Sigma), BB is a fixed m×dm \times d matrix and cc a fixed vector, then

Y=BX+c∼N(Bμ+c,  BΣB⊤).Y = BX + c \sim \mathcal N\big(B\mu + c,\; B\Sigma B^\top\big).

The mean follows from linearity of expectation. For the covariance, Y−E[Y]=B(X−μ)Y - \mathbb E[Y] = B(X - \mu), and BB comes out of the expectation on both sides: E[B(X−μ)(X−μ)⊤B⊤]=BΣB⊤\mathbb E[B(X - \mu)(X - \mu)^\top B^\top] = B\Sigma B^\top. This is the matrix version of Var⁡(aX+b)=a2Var⁡(X)\operatorname{Var}(aX + b) = a^2 \operatorname{Var}(X). The real content is that YY is again Gaussian. For an invertible square BB it follows from the change of variables in several dimensions, the analogue of the 1-D formula from Functions of random variables.

With one row, B=a⊤B = a^\top, we get a⊤X∼N(a⊤μ, a⊤Σa)a^\top X \sim \mathcal N(a^\top\mu,\ a^\top\Sigma a). In the running example X1+X2∼N(0, 2+1+2.4)=N(0,5.4)X_1 + X_2 \sim \mathcal N(0,\ 2 + 1 + 2.4) = \mathcal N(0, 5.4), while X1−X2∼N(0, 2+1−2.4)=N(0,0.6)X_1 - X_2 \sim \mathcal N(0,\ 2 + 1 - 2.4) = \mathcal N(0, 0.6): the sum goes along the long axis, the difference across it.

Now run the rule backwards to sample. Let Z∼N(0,I)Z \sim \mathcal N(0, I) be dd independent standard normals; the product of their 1-D densities is exactly the formula above with μ=0\mu = 0, Σ=I\Sigma = I. Pick any LL with LL⊤=ΣLL^\top = \Sigma, for example the lower-triangular Cholesky factor. Then

X=μ+LZ∼N(μ, LIL⊤)=N(μ,Σ).X = \mu + LZ \sim \mathcal N(\mu,\ LIL^\top) = \mathcal N(\mu, \Sigma).

This is x=μ+σzx = \mu + \sigma z from standardization, with a matrix in place of σ\sigma. For the running example L=(1.4100.850.53)L = \begin{pmatrix} 1.41 & 0 \\ 0.85 & 0.53 \end{pmatrix} (rounded). The same construction shows that every symmetric positive semidefinite matrix is the covariance of some Gaussian vector.

Backwards again, Z=L−1(X−μ)Z = L^{-1}(X - \mu) turns any Gaussian into N(0,I)\mathcal N(0, I); this is called whitening. Since Σ−1=(L−1)⊤L−1\Sigma^{-1} = (L^{-1})^\top L^{-1}, the Mahalanobis distance becomes ordinary length: Δ2=Z⊤Z=Z12+⋯+Zd2\Delta^2 = Z^\top Z = Z_1^2 + \dots + Z_d^2, a sum of dd squared independent standard normals. That sum is the chi-squared law promised in the previous block.

Uncorrelated means independent

The previous topic warned that uncorrelated does not mean independent: ZZ and Z2Z^2 have zero covariance, yet one determines the other. For a Gaussian vector the warning does not apply.

Let Σ=diag⁡(σ12,…,σd2)\Sigma = \operatorname{diag}(\sigma_1^2, \dots, \sigma_d^2), all covariances zero. Then Σ−1=diag⁡(1/σ12,…,1/σd2)\Sigma^{-1} = \operatorname{diag}(1/\sigma_1^2, \dots, 1/\sigma_d^2), the determinant is σ12⋯σd2\sigma_1^2 \cdots \sigma_d^2, and the quadratic form falls apart into a sum. The exponential of a sum is a product, so

p(x)=∏i=1d12πσi2exp⁡ ⁣(−(xi−μi)22σi2).p(x) = \prod_{i=1}^{d} \frac{1}{\sqrt{2\pi\sigma_i^2}} \exp\!\Big(-\frac{(x_i - \mu_i)^2}{2\sigma_i^2}\Big).

The joint density is the product of the marginal densities, which is the definition of independence. The ellipses of such a Gaussian are aligned with the coordinate axes. The same argument works for groups: if every covariance between one group of coordinates and another is zero, Σ\Sigma splits into two blocks, and the two groups are independent.

The condition is that the whole vector is Gaussian. A vector is jointly Gaussian if it has the distribution N(μ,Σ)\mathcal N(\mu, \Sigma); an equivalent test is that every linear combination a⊤Xa^\top X is normal. Normal marginals are not enough. Take X∼N(0,1)X \sim \mathcal N(0, 1) and an independent fair sign S=±1S = \pm 1, and set Y=SXY = SX. By symmetry Y∼N(0,1)Y \sim \mathcal N(0, 1). Its covariance with XX is

Cov⁡(X,Y)=E[SX2]=E[S] E[X2]=0,\operatorname{Cov}(X, Y) = \mathbb E[S X^2] = \mathbb E[S]\,\mathbb E[X^2] = 0,

yet ∣Y∣=∣X∣\lvert Y\rvert = \lvert X\rvert always, so the two are strongly dependent. The pair is simply not jointly Gaussian.

Conditioning on part of the vector

Suppose we observe feature 1, X1=x1X_1 = x_1. What do we now know about feature 2?

The marginal first. Keeping one coordinate is a linear map, so X2∼N(μ2,Σ22)X_2 \sim \mathcal N(\mu_2, \Sigma_{22}): just read the entries off. In the running example X1∼N(0,2)X_1 \sim \mathcal N(0, 2) and X2∼N(0,1)X_2 \sim \mathcal N(0, 1).

For the conditional distribution there is a short way. Remove from X2X_2 the part that X1X_1 explains:

R=X2−βX1,β=Σ21Σ11.R = X_2 - \beta X_1, \qquad \beta = \frac{\Sigma_{21}}{\Sigma_{11}}.

Then Cov⁡(R,X1)=Σ21−β Σ11=0\operatorname{Cov}(R, X_1) = \Sigma_{21} - \beta\,\Sigma_{11} = 0. The pair (X1,R)(X_1, R) is an affine image of XX, hence jointly Gaussian, hence independent by the previous block. So X2=βX1+RX_2 = \beta X_1 + R with RR independent of X1X_1. Fixing X1=x1X_1 = x_1 leaves the distribution of RR untouched, and computing the mean and variance of RR gives

X2∣X1=x1  ∼  N ⁣(μ2+Σ21Σ11(x1−μ1),    Σ22−Σ212Σ11).X_2 \mid X_1 = x_1 \;\sim\; \mathcal N\!\Big(\mu_2 + \frac{\Sigma_{21}}{\Sigma_{11}}(x_1 - \mu_1),\;\; \Sigma_{22} - \frac{\Sigma_{21}^2}{\Sigma_{11}}\Big).

In the running example β=1.2/2=0.6\beta = 1.2/2 = 0.6 and the variance is 1−1.44/2=0.281 - 1.44/2 = 0.28, so X2∣X1=x1∼N(0.6 x1, 0.28)X_2 \mid X_1 = x_1 \sim \mathcal N(0.6\,x_1,\ 0.28). Observing x1=2x_1 = 2 gives N(1.2, 0.28)\mathcal N(1.2,\ 0.28).

Three things to notice.

  • The conditional mean is linear in x1x_1, a regression line with slope Cov⁡/Var⁡\operatorname{Cov}/\operatorname{Var}.
  • The conditional variance does not depend on x1x_1.
  • It is smaller than Σ22\Sigma_{22}. In general it equals Σ22(1−ρ2)\Sigma_{22}(1 - \rho^2), so knowing X1X_1 removes the share ρ2\rho^2 of the variance of X2X_2.

The same proof works for groups of coordinates, with matrices in place of numbers. Observing a block XA=xAX_A = x_A leaves the block XBX_B Gaussian:

XB∣XA=xA  ∼  N(μB+ΣBAΣAA−1(xA−μA),    ΣBB−ΣBAΣAA−1ΣAB).X_B \mid X_A = x_A \;\sim\; \mathcal N\big(\mu_B + \Sigma_{BA}\Sigma_{AA}^{-1}(x_A - \mu_A),\;\; \Sigma_{BB} - \Sigma_{BA}\Sigma_{AA}^{-1}\Sigma_{AB}\big).

The covariance here is called the Schur complement. This one formula drives Gaussian processes, the Kalman filter and Bayesian linear regression.

Where the Gaussian stops, and what comes next

The multivariate Gaussian is fixed by d+d(d+1)/2d + d(d+1)/2 numbers, and it stays Gaussian under affine maps, marginals and conditioning. That is a lot of convenience, and it has limits.

  • Size. For d=1000d = 1000 features the covariance alone has 500,500500{,}500 entries. Estimating it needs many samples, and inverting it costs on the order of d3d^3 operations. Practical models often restrict Σ\Sigma to be diagonal or low-rank.
  • Shape. Real data have heavy tails, clusters and curved structure; a cloud of face images is not an ellipsoid. Mixtures of Gaussians, normalizing flows and diffusion models start from Gaussians and bend them. The course Generative Models does this, beginning with its topic Multivariate Gaussians.

One thread is left loose. The conditional mean m(x1)=0.6 x1m(x_1) = 0.6\,x_1 is a number for each observed value: the best guess for feature 2 once feature 1 is known. Before we observe anything, X1X_1 is random, and so is the guess 0.6 X10.6\,X_1. It is a random variable with its own mean and variance, and together with the leftover RR it rebuilds X2=0.6 X1+RX_2 = 0.6\,X_1 + R.

The next topic, Conditional expectation, defines this object, E[X2∣X1]\mathbb E[X_2 \mid X_1], for any joint distribution. It shows why it is the best predictor under squared error and how the variance of X2X_2 splits into an explained part and a leftover part. The Gaussian is the special case where that predictor is a straight line.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.