Course contents 10 / 22

10 / 22 · Several Random Variables

The multivariate Gaussian

The joint distribution described completely by a mean vector and a covariance matrix: the density and the Mahalanobis distance, the covariance matrix as an ellipse through its eigenvectors, affine maps, marginals and conditionals in closed form (the conditional mean is a linear regression), uncorrelated meaning independent only for jointly Gaussian vectors, and the thin shell where high-dimensional Gaussian samples live.

On this page 12

A joint distribution of dd variables is in general a function of dd arguments that no table can hold. One family is different. It is described completely by a mean vector and a covariance matrix, it stays in the family under linear maps, marginalization and conditioning, and all of these are a few lines of linear algebra. That family, the multivariate Gaussian, is the noise model of linear regression, the prior of Gaussian processes, the state of a Kalman filter and the latent space of most generative models. This note builds it from the one-dimensional normal and the covariance of the previous note.

Notation. X∈RdX \in \mathbb R^d is a random vector with mean μ=E[X]\mu = E[X] and covariance matrix Σ\Sigma, Σij=Cov⁡(Xi,Xj)\Sigma_{ij} = \operatorname{Cov}(X_i, X_j), symmetric and positive definite. X∼N(μ,Σ)X \sim N(\mu, \Sigma); Z∼N(0,I)Z \sim N(0, I) is the standard normal vector. ∣Σ∣\lvert \Sigma \rvert is the determinant, A⊤A^\top the transpose. The note uses a little linear algebra: matrix products, inverses and determinants, the quadratic form x⊤Axx^\top A x, and the eigenvectors of a symmetric matrix.

The density#

X∼N(μ,Σ)X \sim N(\mu, \Sigma) has the density

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

For d=1d = 1 it is the normal density of the continuous distributions note, with Σ=σ2\Sigma = \sigma^2. The quadratic form in the exponent, Δ2(x)=(x−μ)⊤Σ−1(x−μ)\Delta^2(x) = (x - \mu)^\top \Sigma^{-1} (x - \mu), is the squared Mahalanobis distance: the distance from the mean measured in standard deviations along each direction. The density is constant on the sets where Δ\Delta is constant, which are ellipsoids.

If Σ\Sigma is diagonal, the quadratic form is a sum ∑i(xi−μi)2/σi2\sum_i (x_i - \mu_i)^2 / \sigma_i^2, the exponential of a sum is a product, and the density factorizes into one normal density per coordinate. The standard normal vector Z∼N(0,I)Z \sim N(0, I) is simply dd independent N(0,1)N(0, 1) coordinates.

Covariance as geometry#

A symmetric positive definite matrix has an orthonormal basis of eigenvectors q1,…,qdq_1, \dots, q_d with positive eigenvalues λ1,…,λd\lambda_1, \dots, \lambda_d: Σ=QΛQ⊤\Sigma = Q \Lambda Q^\top. In the coordinates of that basis the covariance is diagonal, so the density factorizes there, with variance λi\lambda_i along qiq_i. The contours are ellipses with axes along the eigenvectors and half-lengths kλik\sqrt{\lambda_i} at Mahalanobis distance kk. For

Σ=(21.21.21),λ=2.8, 0.2,ρ=1.22⋅1=0.85,\Sigma = \begin{pmatrix} 2 & 1.2 \\ 1.2 & 1 \end{pmatrix}, \qquad \lambda = 2.8,\ 0.2, \qquad \rho = \frac{1.2}{\sqrt{2 \cdot 1}} = 0.85 ,

the long axis points along (0.83,0.55)(0.83, 0.55):

Figure

A covariance matrix as an ellipse

A point cloud, two ellipses and the principal axesAn elongated cloud along the diagonal; ellipses of distance 1 and 2; principal-axis arrows of length 1.67 and 0.45.√2.8√0.239%86%A point cloud, two ellipses and the principal axesAn elongated cloud along the diagonal; ellipses of distance 1 and 2; principal-axis arrows of length 1.67 and 0.45.√2.8√0.239%86%
600 points from N(0, Σ), Σ = [[2, 1.2], [1.2, 1]], correlation 0.85. The eigenvectors of Σ give the axes of the ellipses and the square roots of the eigenvalues their half-lengths: √2.8 ≈ 1.67 along the long axis and √0.2 ≈ 0.45 along the short one. The inner ellipse (Mahalanobis distance 1) holds 39% of the points against 39.3% in theory, the outer one (distance 2) 87% against 86.5%: in two dimensions "one sigma" does not cover 68%.

The long axis carries 2.8/3=93%2.8 / 3 = 93\% of the total variance tr⁡Σ=3\operatorname{tr} \Sigma = 3, and projecting onto it gives the first principal component. The ellipses also correct a common intuition. In one dimension, one standard deviation around the mean holds 68% of the probability; in two dimensions the ellipse of Mahalanobis distance 1 holds only 1−e−1/2=39.3%1 - e^{-1/2} = 39.3\%, and distance 2 holds 86.5%86.5\%, because Δ2\Delta^2 is a sum of dd squared standard normals and has the chi-squared distribution with dd degrees of freedom.

Affine maps#

Linear maps keep Gaussians Gaussian. If X∼N(μ,Σ)X \sim N(\mu, \Sigma), then for any matrix BB and vector cc

BX+c∼N(Bμ+c, BΣB⊤).BX + c \sim N(B\mu + c,\ B \Sigma B^\top) .

The mean follows from linearity of expectation and the covariance from bilinearity of covariance; that the result is again Gaussian follows, for an invertible square BB, from the change-of-variables formula of the functions of random variables note in dd dimensions, and for any BB from the characteristic function; when BΣB⊤B \Sigma B^\top is singular, the result is a degenerate Gaussian concentrated on a subspace, without a density. Three consequences are used constantly:

  • Sampling. X=μ+LZX = \mu + LZ with LL⊤=ΣLL^\top = \Sigma (a Cholesky factor) has distribution N(μ,Σ)N(\mu, \Sigma); the Gaussian distributions note of the generative models course works through it in PyTorch.
  • Whitening. Conversely, L−1(X−μ)∼N(0,I)L^{-1}(X - \mu) \sim N(0, I): any Gaussian is a standard one in suitable coordinates.
  • Linear combinations. a⊤X∼N(a⊤μ,a⊤Σa)a^\top X \sim N(a^\top \mu, a^\top \Sigma a). For the example above, whose mean is 0, X1+X2∼N(0,2+1+2⋅1.2)=N(0,5.4)X_1 + X_2 \sim N(0, 2 + 1 + 2 \cdot 1.2) = N(0, 5.4).

Marginals and conditionals#

Split XX into two blocks X1X_1, X2X_2, with the mean and covariance split accordingly:

μ=(μ1μ2),Σ=(Σ11Σ12Σ21Σ22).\mu = \begin{pmatrix} \mu_1 \\ \mu_2 \end{pmatrix}, \qquad \Sigma = \begin{pmatrix} \Sigma_{11} & \Sigma_{12} \\ \Sigma_{21} & \Sigma_{22} \end{pmatrix} .

The marginal of X2X_2 is read off: X2∼N(μ2,Σ22)X_2 \sim N(\mu_2, \Sigma_{22}), a special case of an affine map that selects coordinates. The conditional of X2X_2 given X1=x1X_1 = x_1 is Gaussian as well, with

E[X2∣X1=x1]=μ2+Σ21Σ11−1(x1−μ1),Cov⁡(X2∣X1=x1)=Σ22−Σ21Σ11−1Σ12.E[X_2 \mid X_1 = x_1] = \mu_2 + \Sigma_{21} \Sigma_{11}^{-1} (x_1 - \mu_1), \qquad \operatorname{Cov}(X_2 \mid X_1 = x_1) = \Sigma_{22} - \Sigma_{21} \Sigma_{11}^{-1} \Sigma_{12} .

For two scalar coordinates the blocks are numbers, and the formulas read E[X2∣X1=x1]=μ2+(Σ21/Σ11)(x1−μ1)E[X_2 \mid X_1 = x_1] = \mu_2 + (\Sigma_{21} / \Sigma_{11})(x_1 - \mu_1) and Var⁡(X2∣X1=x1)=Σ22−Σ212/Σ11\operatorname{Var}(X_2 \mid X_1 = x_1) = \Sigma_{22} - \Sigma_{21}^2 / \Sigma_{11}. For the example, Σ21/Σ11=1.2/2=0.6\Sigma_{21}/\Sigma_{11} = 1.2/2 = 0.6, so given X1=x1X_1 = x_1 the second coordinate is N(0.6 x1, 1−1.22/2)=N(0.6 x1,0.28)N(0.6\, x_1,\ 1 - 1.2^2/2) = N(0.6\, x_1, 0.28):

Figure

Conditioning a Gaussian vector

Ellipses, the regression line and a sliceTilted ellipses of the Gaussian, the line x₂ = 0.6 x₁ and a vertical slice at x₁ = 2 with a narrow normal density around 1.2; on the right the wide marginal density of X₂.1E[X₂ | X₁] = 0.6 X₁2N(1.2, 0.28)X₁ = 2N(0, 1)3Ellipses, the regression line and a sliceTilted ellipses of the Gaussian, the line x₂ = 0.6 x₁ and a vertical slice at x₁ = 2 with a narrow normal density around 1.2; on the right the wide marginal density of X₂.1E[X₂ | X₁] = 0.6 X₁2N(1.2, 0.28)X₁ = 2N(0, 1)3
The same distribution N(0, Σ), Σ = [[2, 1.2], [1.2, 1]] (1). Given X₁ = x₁, X₂ is again normal; its mean Σ₁₂/Σ₁₁ · x₁ = 0.6 x₁ lies on the regression line (2), and its variance Σ₂₂ − Σ₁₂²/Σ₁₁ = 0.28 does not depend on x₁. The slice at X₁ = 2 gives N(1.2, 0.28) (3); on the right, for comparison, the marginal distribution of X₂, N(0, 1): knowing X₁ moves the centre and narrows the spread.

Two features of the formulas matter far beyond this example. The conditional mean is a linear function of the observed value, so for jointly Gaussian data linear regression is not an approximation but the exact best predictor, the subject of the conditional expectation note. And the conditional covariance, the Schur complement Σ22−Σ21Σ11−1Σ12\Sigma_{22} - \Sigma_{21}\Sigma_{11}^{-1}\Sigma_{12}, does not depend on the observed value: observing X1X_1 shrinks the uncertainty about X2X_2 by the same amount wherever X1X_1 lands. Gaussian process regression and the Kalman filter are these two formulas applied to large covariance matrices.

Uncorrelated means independent, for Gaussian vectors#

For a jointly Gaussian vector, zero covariance between two blocks makes Σ\Sigma block-diagonal, and the density factorizes: uncorrelated implies independent. This is a property of the joint distribution, not of the marginals. Let X∼N(0,1)X \sim N(0, 1) and let Y=SXY = SX, where S=±1S = \pm 1 is an independent fair sign. Then Y∼N(0,1)Y \sim N(0, 1) as well, and Cov⁡(X,Y)=E[S] E[X2]=0\operatorname{Cov}(X, Y) = E[S]\, E[X^2] = 0, but ∣Y∣=∣X∣\lvert Y \rvert = \lvert X \rvert: the two are strongly dependent. The pair is not jointly Gaussian: every linear combination of a Gaussian vector is normal, and X+YX + Y equals 0 with probability 1/21/2, which no normal variable does.

High dimensions: the shell#

Optional depth: why high-dimensional Gaussian samples avoid the centre. The rest of the note does not depend on it.

The density of N(0,I)N(0, I) is largest at the origin, and yet samples in high dimension are never near it. The squared length ∥Z∥2=Z12+⋯+Zd2\lVert Z \rVert^2 = Z_1^2 + \dots + Z_d^2 has mean dd and variance 2d2d, so the length concentrates around d\sqrt d with a spread that stays near 0.70.7:

Figure

The length of a standard normal vector in d dimensions

Three densities of the vector lengthThree bumps of similar width centred near 1.25, 3.08 and 9.98.024681012‖Z‖d = 2d = 10d = 100Three densities of the vector lengthThree bumps of similar width centred near 1.25, 3.08 and 9.98.024681012‖Z‖d = 2d = 10d = 100
The density of the length ‖Z‖ of Z ~ N(0, I) in dimension d = 2, 10, 100. The means 1.25, 3.08, 9.98 are close to √d, and the spread, about 0.7, barely grows: in high dimension almost all the probability lies in a thin shell of radius √d, and the centre, the point of highest density, practically never occurs.

In 100 dimensions the length is 10±0.710 \pm 0.7, and a length below 5 has probability about 10−1510^{-15}. The density is high near the centre, but the centre has almost no volume; the volume, and with it the probability, is in a thin shell. Two practical consequences: interpolating linearly between two latent samples of a generative model passes through a region the model never saw during training, which is why spherical interpolation is often used instead; and in high dimensions, distances between independent Gaussian points are all about 2d\sqrt{2d}, so nearest neighbours become less informative.

What changed#

A general joint distributionThe multivariate Gaussian
Parametersa function of dd argumentsμ\mu and Σ\Sigma: d+d(d+1)/2d + d(d+1)/2 numbers
Contoursanythingellipsoids, axes from the eigenvectors of Σ\Sigma
Linear maps, marginalsnew distributions to computeN(Bμ+c,BΣB⊤)N(B\mu + c, B\Sigma B^\top); read off blocks
Conditionalsrenormalized slicesGaussian, linear mean, Schur complement covariance
Uncorrelatednot enough for independenceequivalent to independence

Limitations#

  • Thin tails and one mode. Real data in high dimension are rarely Gaussian: images, text embeddings and returns have heavy tails, clusters and curved structure; mixtures, flows and diffusion models exist for that reason.
  • The covariance matrix is expensive. It has d(d+1)/2d(d+1)/2 free entries, 500,500 for d=1,000d = 1{,}000; estimating it needs many more samples than dimensions, and inverting it costs O(d3)O(d^3). Diagonal, low-rank or sparse-precision structures are used in practice.
  • Marginal normality proves nothing about the joint. Each coordinate can be normal while the vector is not, as the sign example shows; formulas for conditionals and independence apply only when the vector is jointly Gaussian.

What you should remember#

  • N(μ,Σ)N(\mu, \Sigma) has density ∝exp⁡(−12(x−μ)⊤Σ−1(x−μ))\propto \exp(-\frac12 (x - \mu)^\top \Sigma^{-1} (x - \mu)); contours are ellipsoids of constant Mahalanobis distance.
  • The eigenvectors of Σ\Sigma give the axes, the square roots of its eigenvalues the half-lengths; in 2D one Mahalanobis unit holds 39%, not 68%.
  • BX+c∼N(Bμ+c,BΣB⊤)BX + c \sim N(B\mu + c, B\Sigma B^\top); X=μ+LZX = \mu + LZ with LL⊤=ΣLL^\top = \Sigma samples any Gaussian.
  • Marginals are blocks of μ\mu and Σ\Sigma; conditionals are Gaussian with a mean linear in the observation and covariance Σ22−Σ21Σ11−1Σ12\Sigma_{22} - \Sigma_{21}\Sigma_{11}^{-1}\Sigma_{12}.
  • For jointly Gaussian vectors, uncorrelated means independent.
  • A standard normal vector in dd dimensions has length about d±0.7\sqrt d \pm 0.7.

Code#

The example of the figures checked by sampling, with NumPy:

import numpy as np
from scipy import stats

S = np.array([[2.0, 1.2], [1.2, 1.0]])
w, V = np.linalg.eigh(S)
print(w.round(3), np.abs(V[:, 1]).round(3))                         # [0.2 2.8] [0.832 0.555]: the long axis
print(round(1.2 / np.sqrt(2), 2), [round(float(stats.chi2(2).cdf(k * k)), 3) for k in (1, 2)])   # 0.85 [0.393, 0.865]

rng = np.random.default_rng(0)
L = np.linalg.cholesky(S)                                            # sampling: X = L Z
X = rng.normal(size=(1_000_000, 2)) @ L.T
print(np.cov(X.T).round(2).tolist())                                 # [[2.0, 1.2], [1.2, 1.0]]
d2 = np.einsum('ij,jk,ik->i', X, np.linalg.inv(S), X)                # squared Mahalanobis distances
print(round(float((d2 <= 1).mean()), 3), round(float((d2 <= 4).mean()), 3))   # 0.393 0.865
print(round(float(X.sum(axis=1).var()), 2))                          # 5.39, against 2 + 1 + 2 * 1.2 = 5.4

# conditioning on X1 = 2: the samples with X1 close to 2
near = X[np.abs(X[:, 0] - 2) < 0.02, 1]
print(round(1.2 / 2 * 2, 2), round(1 - 1.2**2 / 2, 2), round(float(near.mean()), 2), round(float(near.var()), 2))   # 1.2 0.28 1.2 0.28

# normal marginals, zero correlation, and still dependent: Y = S X with a random sign S
x = rng.normal(size=1_000_000)
y = rng.choice([-1, 1], size=x.size) * x
print(round(float(np.corrcoef(x, y)[0, 1]), 3), round(float((x + y == 0).mean()), 3))   # -0.001 0.501

# the shell: the length of a standard normal vector in 100 dimensions
r = np.linalg.norm(rng.normal(size=(100_000, 100)), axis=1)
print(round(float(r.mean()), 2), round(float(r.std()), 2), f'{stats.chi(100).cdf(5):.1e}')   # 9.97 0.71 1.1e-15

Next#

For Gaussians the best prediction of X2X_2 from X1X_1 turned out to be linear, with a known variance left over. The question makes sense for any pair of random variables: what function of XX predicts YY best, and how much error is left? The next note answers it with the conditional expectation E[Y∣X]E[Y \mid X], the regression function that a model trained with squared error approximates; other losses target other features of the conditional distribution, such as its median or a quantile.

References#