Guides / Probability

The multivariate Gaussian: invert Σ in the exponent, transform with AΣAᵀ, and condition with the Schur complement

The multivariate Gaussian describes several quantities at once through a mean vector μ and a covariance matrix Σ. Almost every calculation with it is one of four moves: evaluate the density through the Mahalanobis distance, push it through a linear map, condition on some coordinates, or draw a sample. The mistakes come from putting Σ in the exponent where Σ−1 belongs, from dropping the covariance terms when combining coordinates, and from forgetting to center the observed value when conditioning.

Short answer

Density in d dimensions: p(x) = (2π)−d/2 (det Σ)−1/2 exp(−½ q), where q = (x − μ)TΣ−1(x − μ) is the squared Mahalanobis distance. Linear maps: if y = Ax + b, then y ~ N(Aμ + b, AΣAT); a single coordinate is the case where A picks it out, so every marginal is Gaussian with its own mean and diagonal entry. Conditioning on x2 in two dimensions: x1 | x2 ~ N(μ1 + (Σ12/Σ22)(x2 − μ2), Σ11 − Σ122/Σ22). Sampling: factor Σ = LLT (Cholesky) and set x = μ + Lε with ε standard normal.

Why it works

In one dimension the exponent is −(x − μ)2/(2σ2): the squared distance from the mean, measured in standard deviations. With several correlated coordinates, "measured in standard deviations" has to account for the shape of the cloud, and dividing by σ2 becomes multiplying by Σ−1. That is why q stretches distances along directions where the data are tight and shrinks them where the data are spread out. The factor (det Σ)−1/2 plays the role of 1/σ: det Σ is the product of the eigenvalues of Σ, the variances along the principal axes, so its square root is the product of the standard deviations.

The linear-map rule is the covariance rule Cov(Ax) = AΣAT, plus the fact that a linear combination of jointly Gaussian variables is again Gaussian. Conditioning is a regression: x1 − (Σ12/Σ22)x2 is uncorrelated with x2, hence independent of it, so fixing x2 shifts the mean along the regression line and leaves the leftover variance Σ11 − Σ122/Σ22, the Schur complement. Sampling works because Cov(Lε) = LILT = Σ.

Example 1: the density and the Mahalanobis distance

Let μ = (1, 2) and Σ = 4223. Find the squared Mahalanobis distance of x = (3, 2) from μ and the density there.

  1. det Σ = 4 · 3 − 2 · 2 = 8Positive, as it must be for a covariance matrix.
  2. Σ−1 = 183−2−24Swap the diagonal, negate the off-diagonal, divide by the determinant.
  3. x − μ = (2, 0)Subtract the mean.
  4. q = (3 · 2 · 2)/8 = 1.5With a zero second entry only the (1, 1) entry of Σ−1, 3/8, is used: q = (3/8) · 22.
  5. √q ≈ 1.225The point is 2 units from the mean in plain distance, but only about 1.22 "standard deviations" once the spread of 4 along the first axis is accounted for.
  6. p(x) = e−0.75/(2π√8)The exponent is −q/2 = −0.75.
  7. ≈ 0.4724 × 0.05627e−0.75 ≈ 0.4724 and 1/(2π√8) ≈ 0.05627.
  8. p(x) ≈ 0.02658With d = 2 the constant is (2π)−1(det Σ)−1/2. The log-density is −ln 2π − ½ ln 8 − 0.75 ≈ −3.628.

Example 2: a linear map with AΣAᵀ

With the same μ and Σ, find the joint distribution of the sum s = x1 + x2 and the difference t = x1 − x2.

  1. A = 111−1Each row of A is one new variable.
  2. Aμ = (1 + 2, 1 − 2) = (3, −1)Means map straight through.
  3. Var(s) = 4 + 3 + 2 · 2 = 11The diagonal of AΣAT: each is aTΣa for one row a, which includes the covariance twice.
  4. Var(t) = 4 + 3 − 2 · 2 = 3The second row of A, with the covariance subtracted twice.
  5. Cov(s, t) = Var(x1) − Var(x2)The cross terms Cov(x1, x2) cancel.
  6. = 4 − 3 = 1The two variances.
  7. (s, t) ~ N((3, −1), 11113)Reading off one coordinate gives a marginal: s alone is N(3, 11). Likewise x1 alone is N(1, 4), with no integration needed.

Example 3: conditioning with the Schur complement

With the same μ and Σ, find the distribution of x1 given that x2 = 4.

  1. x2 − μ2 = 4 − 2 = 2Center the observed value first: conditioning reacts to how surprising x2 is, not to its raw size.
  2. Σ12/Σ22 = 2/3The slope of the regression of x1 on x2.
  3. mean = 1 + (2/3) · 2 = 7/3x2 came out above its mean and the covariance is positive, so x1 is pulled up too: 7/3 ≈ 2.333.
  4. variance = 4 − 22/3 = 8/3That is about 2.667, smaller than Σ11 = 4: knowing x2 removes some uncertainty. Two checks: it equals det Σ/Σ22 = 8/3, and it is 1/(Σ−1)11 = 1/(3/8). It does not depend on the observed value 4 at all.

Example 4: sampling with a Cholesky factor

Turn the standard normal draw ε = (0.5, −1) into a sample from N(μ, Σ) with the same μ and Σ.

  1. L11 = √4 = 2, L21 = 2/2 = 1The first column of L is the first column of Σ divided by √Σ11.
  2. L22 = √(3 − 12) = √2What is left of Σ22 after the first column's share.
  3. LLT = 4223Check the factor before using it: 2 · 1 = 2 and 1 + 2 = 3.
  4. (Lε)1 = 2 · 0.5 = 1L is lower-triangular, so the first coordinate uses only ε1.
  5. (Lε)2 = 1 · 0.5 + √2 · (−1)The second row of L uses both entries of ε …
  6. = 0.5 − √2… so Lε = (1, 0.5 − √2).
  7. x = (1 + 1, 2 + 0.5 − √2)Add the mean last.
  8. x ≈ (2, 1.086)Since 2.5 − √2 ≈ 1.086. Repeating with many ε gives draws whose sample covariance approaches Σ.

Common mistakes, and the exact line they happen on

1. Σ in the exponent instead of Σ⁻¹

The quadratic form of Example 1.

  1. x − μ = (2, 0)Right so far.
  2. q = (x − μ)TΣ(x − μ) = 16This is the mistake. This uses Σ itself, giving 4 · 22 = 16. In one dimension the exponent divides by σ2, so the matrix version multiplies by the inverse. With Σ the density becomes e−8/(2π√8) ≈ 0.0000189 instead of 0.02658, and the error gets worse the more spread out the data are, which is backward. The right value is q = 1.5.

2. Dropping the covariance when combining coordinates

The variance of t = x1 − x2 in Example 2.

  1. Var(x1) = 4, Var(x2) = 3Right so far: the diagonal of Σ.
  2. Var(x1 − x2) = 4 + 3 = 7This is the mistake. Adding the variances is right only for uncorrelated coordinates. aTΣa with a = (1, −1) uses every entry of Σ, including the off-diagonal 2 twice: 4 + 3 − 2 · 2 = 3. Positively correlated coordinates move together, so their difference varies less, not more.

3. Conditioning on the raw value instead of its deviation

The conditional mean of Example 3.

  1. Σ12/Σ22 = 2/3Right so far.
  2. mean = μ1 + (2/3) · 4 = 11/3This is the mistake. The regression term must use x2 − μ2. A quick test: observing x2 = μ2 = 2 tells you nothing, so the conditional mean should stay at μ1 = 1, but this formula gives 1 + 4/3. The right mean is 1 + (2/3)(4 − 2) = 7/3.

Practice

  1. μ = (0, 0) and Σ = diag(4, 9). Find the squared Mahalanobis distance of x = (2, 3).

    Show answer

    With a diagonal Σ, q = 22/4 + 32/9 = 1 + 1 = 2: each coordinate is one standard deviation out.

  2. Σ = 5222. Find det Σ and Var(x1 + x2).

    Show answer

    det Σ = 10 − 4 = 6, and Var(x1 + x2) = 5 + 2 + 2 · 2 = 11.

  3. With μ = (0, 0) and the Σ of Problem 2, find the distribution of x1 given x2 = 1.

    Show answer

    Mean (2/2) · 1 = 1, variance 5 − 22/2 = 3, so N(1, 3). Check: det Σ/Σ22 = 6/2 = 3.

  4. Find the Cholesky factor of 9335.

    Show answer

    L11 = 3, L21 = 3/3 = 1, L22 = √(5 − 1) = 2, so L has rows (3, 0) and (1, 2).

  5. Find the density at the mean of a 2-D Gaussian with Σ = diag(1, 4).

    Show answer

    At the mean q = 0, so p = 1/(2π√4) = 1/(4π) ≈ 0.0796.