Skip to main content
© 2026 ePowerAI — free, open courseware. No login required.
CoursesContact
ePOWERAI
CoursesContact
Week 6: Multivariate Gaussians
Prob & Stats

Foundations

  1. 01Week 1: Probability Spaces and Events
  2. 02Week 2: Random Variables and Distributions
  3. 03Week 3: Expectation, Variance, and Covariance
  4. 04Week 4: Conditioning and Independence
  5. 05Week 5: Bayes' Rule and Generative Classification
  6. 06Week 6: Multivariate Gaussians

Inference

  1. 07Week 7: Laws of Large Numbers and the CLT
  2. 08Week 8: Point Estimation — Maximum Likelihood
  3. 09Week 9: Bayesian Estimation and MAP
  4. 10Week 10: Bias, Variance, and Uncertainty

ML interface

  1. 11Week 11: Information Theory for ML
  2. 12Week 12: Exponential Families (lite)
  3. 13Week 13: Graphical Models (lite)

Capstone

  1. 14Week 14: Capstone — Probabilistic Language of ML
Week 06· Prob & Stats· Foundations8 min read

Week 6: Multivariate Gaussians

Learning Outcomes
  • Write the multivariate Gaussian density and read the roles of μ\muμ and Σ\SigmaΣ from its shape
  • Compute a conditional X2∣X1=x1X_2 \mid X_1 = x_1X2​∣X1​=x1​ and explain why its mean moves along a line and its variance does not move
  • Read a marginal off μ\muμ and Σ\SigmaΣ with no integration, and explain why Σ\SigmaΣ must be PSD
  • Explain whitening and the Mahalanobis distance
Prerequisites

Required: Weeks 2–3 (random variables, expectation, variance, covariance). Linear Algebra (course index) — eigenvalues and quadratic forms xTΣxx^T \Sigma xxTΣx.

Later use: Multivariate Gaussians underpin Kalman filters, diffusion models and Gaussian processes.

  • Also see: Robot Learning Week 2: Dynamics and State Estimation — Gaussians doing real work in a Kalman filter.

A sensor reads x1=5x_1 = 5x1​=5. A second quantity X2X_2X2​ moves with the reading, and the pair is jointly Gaussian with mean μ=(3,1)\mu = (3, 1)μ=(3,1), standard deviations σ1=2\sigma_1 = 2σ1​=2 and σ2=1\sigma_2 = 1σ2​=1, and correlation ρ\rhoρ. What should you now believe about X2X_2X2​? The surface below is the joint density. The amber curve is a vertical slice at the observed x1x_1x1​.

The joint density of (X₁, X₂) ~ N((3, 1), Σ) with σ₁ = 2, σ₂ = 1 and correlation ρ = 0.5, so Σ = [[4, 1], [1, 1]]. A vertical slice at the observed x₁ cuts the surface along a curve proportional to the conditional density of X₂. At x₁ = 5.0 the slice has the shape of X₂ | X₁ = 5.0 ~ N(1.50, 0.87²). Its centre is 1 + 0.25 × (5.0 − 3) and its width does not depend on x₁.

At x₁ = 5.0 the slice has the shape of X₂ | X₁ = 5.0 ~ N(1.50, 0.87²). Its centre is 1 + 0.25 × (5.0 − 3) and its width does not depend on x₁.

Slide x1x_1x1​. The slice's peak follows the line on the floor, and its height falls as x1x_1x1​ leaves the mean. Its width stays the same. Switch ρ\rhoρ to 0.9 and the surface narrows into a ridge, and every slice gets thinner. At ρ=0\rho = 0ρ=0 the slice is centred at x2=1x_2 = 1x2​=1 wherever you put it.

Mean and covariance matrix#

A multivariate Gaussian in Rd\mathbb{R}^dRd has a mean vector μ\muμ and a covariance matrix Σ\SigmaΣ:

f(x)=1(2π)d/2∣Σ∣1/2exp⁡(−12(x−μ)TΣ−1(x−μ)).f(x) = \frac{1}{(2\pi)^{d/2} |\Sigma|^{1/2}} \exp\left(-\frac{1}{2}(x-\mu)^T \Sigma^{-1}(x-\mu)\right).f(x)=(2π)d/2∣Σ∣1/21​exp(−21​(x−μ)TΣ−1(x−μ)).

The exponent holds the squared Mahalanobis distance (x−μ)TΣ−1(x−μ)(x-\mu)^T \Sigma^{-1}(x-\mu)(x−μ)TΣ−1(x−μ), the multivariate form of the squared z-score (x−μ)2/σ2(x-\mu)^2/\sigma^2(x−μ)2/σ2. Its level sets are ellipses centred at μ\muμ. The scene's default is the worked example

μ=(31),Σ=(4111),\mu = \begin{pmatrix} 3 \\ 1 \end{pmatrix}, \quad \Sigma = \begin{pmatrix} 4 & 1 \\ 1 & 1 \end{pmatrix},μ=(31​),Σ=(41​11​),

with Σ12=ρ σ1σ2\Sigma_{12} = \rho\,\sigma_1\sigma_2Σ12​=ρσ1​σ2​, so ρ=1/4⋅1=0.5\rho = 1/\sqrt{4 \cdot 1} = 0.5ρ=1/4⋅1​=0.5. The ellipses stretch along x1x_1x1​ and tilt because the covariance is positive.

The slice is the conditional#

Fix x1x_1x1​ and read f(x1,x2)f(x_1, x_2)f(x1​,x2​) as a function of x2x_2x2​. That is the slice. The conditional density is

f(x2∣x1)=f(x1,x2)f1(x1),f(x_2 \mid x_1) = \frac{f(x_1, x_2)}{f_1(x_1)},f(x2​∣x1​)=f1​(x1​)f(x1​,x2​)​,

and the marginal f1(x1)f_1(x_1)f1​(x1​) is one number for a fixed x1x_1x1​. So the slice is the conditional scaled by f1(x1)f_1(x_1)f1​(x1​): the same centre and width, a different height. For two variables,

X2∣X1=x1∼N ⁣(μ2+ρσ2σ1(x1−μ1),  σ22(1−ρ2)).X_2 \mid X_1 = x_1 \sim \mathcal{N}\!\left(\mu_2 + \rho\frac{\sigma_2}{\sigma_1}(x_1 - \mu_1),\; \sigma_2^2(1-\rho^2)\right).X2​∣X1​=x1​∼N(μ2​+ρσ1​σ2​​(x1​−μ1​),σ22​(1−ρ2)).

At ρ=0.5\rho = 0.5ρ=0.5 the mean is 1+0.25 (x1−3)1 + 0.25\,(x_1 - 3)1+0.25(x1​−3), which is 1.5 at x1=5x_1 = 5x1​=5 and 0.25 at x1=0x_1 = 0x1​=0. The variance is 1⋅(1−0.25)=0.751 \cdot (1 - 0.25) = 0.751⋅(1−0.25)=0.75, a standard deviation of 0.87, at every x1x_1x1​. At ρ=0.9\rho = 0.9ρ=0.9 the slope is 0.45 and the standard deviation 0.19=0.44\sqrt{0.19} = 0.440.19​=0.44. At ∣ρ∣=1|\rho| = 1∣ρ∣=1 the variance is 0.

The common mistake. A surprising reading, far from the mean, feels as if it should leave you less certain. It does not. At x1=0x_1 = 0x1​=0 the marginal density is 0.065, against 0.121 at x1=5x_1 = 5x1​=5, so the slice is about half as tall. Its width is the same. The reading moves only the centre. Not every joint does this: a multivariate t widens its conditional after a surprising reading.

The general partitioned form, for blocks X=(Xa,Xb)X = (X_a, X_b)X=(Xa​,Xb​), is

Xa∣Xb=xb∼N ⁣(μa+ΣabΣbb−1(xb−μb),  Σaa−ΣabΣbb−1Σba).X_a \mid X_b = x_b \sim \mathcal{N}\!\left(\mu_a + \Sigma_{ab}\Sigma_{bb}^{-1}(x_b - \mu_b),\; \Sigma_{aa} - \Sigma_{ab}\Sigma_{bb}^{-1}\Sigma_{ba}\right).Xa​∣Xb​=xb​∼N(μa​+Σab​Σbb−1​(xb​−μb​),Σaa​−Σab​Σbb−1​Σba​).

The mean is linear in xbx_bxb​. The covariance is the Schur complement of Σbb\Sigma_{bb}Σbb​ in Σ\SigmaΣ. It and Σaa\Sigma_{aa}Σaa​ minus it are both PSD, and it does not depend on xbx_bxb​. The Kalman filter's update step uses the same formula.

Marginals#

The marginal of a block is read off the parameters, with no integration: Xa∼N(μa,Σaa)X_a \sim \mathcal{N}(\mu_a, \Sigma_{aa})Xa​∼N(μa​,Σaa​). In the example X1∼N(3,4)X_1 \sim \mathcal{N}(3, 4)X1​∼N(3,4), and its density at x1=5x_1 = 5x1​=5 is 0.121. This is the factor that sets the slice's height.

Why Σ\SigmaΣ must be PSD#

The covariance Σij=Cov(Xi,Xj)\Sigma_{ij} = \mathrm{Cov}(X_i, X_j)Σij​=Cov(Xi​,Xj​) is symmetric. For any vvv, the scalar vTXv^T XvTX has variance

Var(vTX)=vTΣv≥0,\mathrm{Var}(v^T X) = v^T \Sigma v \ge 0,Var(vTX)=vTΣv≥0,

so Σ\SigmaΣ is positive semidefinite. The density needs Σ−1\Sigma^{-1}Σ−1, so it needs Σ\SigmaΣ positive definite. The eigenvectors of Σ\SigmaΣ are the axes of the ellipses, and the square roots of its eigenvalues set their lengths. For the example these eigenvalues are about 4.30 and 0.70.

eigvec λ₁ eigvec λ₂ a multivariate Gaussian is a mean plus a covariance ellipsoid the eigenvectors of Σ give the axes, √eigenvalues the lengths

The eigenvectors of Σ\SigmaΣ are the ellipse axes. Whitening inverts this picture.

Whitening and Mahalanobis distance#

Whitening maps correlated data to independent unit-variance components:

Z=Σ−1/2(X−μ)∼N(0,Id).Z = \Sigma^{-1/2}(X - \mu) \sim \mathcal{N}(0, I_d).Z=Σ−1/2(X−μ)∼N(0,Id​).

The Mahalanobis distance DM(x)=(x−μ)TΣ−1(x−μ)D_M(x) = \sqrt{(x-\mu)^T \Sigma^{-1}(x-\mu)}DM​(x)=(x−μ)TΣ−1(x−μ)​ is the Euclidean distance after whitening. A step along a high-variance direction costs less than the same step along a low-variance one.

Why Gaussians dominate#

Sums of many independent effects tend to Gaussians (the CLT, Week 7). Linear maps keep them Gaussian: AX+b∼N(Aμ+b,AΣAT)AX + b \sim \mathcal{N}(A\mu + b, A\Sigma A^T)AX+b∼N(Aμ+b,AΣAT). A Gaussian prior and likelihood give a Gaussian posterior (Week 9). Real data is often heavier-tailed or multimodal, but these closed forms keep Gaussians the default.

Knowledge Check#

Exercise · Multiple choice

Is Σ = [[1, 2], [2, 1]] a valid covariance matrix?

Yes — it is symmetric with a positive diagonal
No — v = (1, −1) gives vᵀΣv = −2, a negative variance
Yes — any symmetric matrix is PSD
No — variances must be equal
Question 1 of 4

Set ρ = 0.5 and slide x₁ from 5 to 0. What happens to the slice's standard deviation?

It stays at 0.87
It grows, because x₁ = 0 is far from the mean
It shrinks to 0.44
It drops to 0

Browser lab: the slice from samples#

Sample the worked example, draw the level sets, and compare histograms of samples near x1=0x_1 = 0x1​=0 and x1=5x_1 = 5x1​=5 with the conditionals from the slice.

python · runs in browser
import numpy as np
import matplotlib.pyplot as plt
from numpy.linalg import eigh

rng = np.random.default_rng(42)
n = 100000

mu = np.array([3.0, 1.0])
Sigma = np.array([[4.0, 1.0],
                   [1.0, 1.0]])

samples = rng.multivariate_normal(mu, Sigma, size=n)

def plot_ellipse(ax, mu, Sigma, n_std=2, **kwargs):
    vals, vecs = eigh(Sigma)
    theta = np.linspace(0, 2*np.pi, 200)
    circle = np.array([np.cos(theta), np.sin(theta)])
    ellipse = (vecs @ np.diag(np.sqrt(vals) * n_std)) @ circle + mu[:, None]
    ax.plot(ellipse[0], ellipse[1], **kwargs)

fig, axes = plt.subplots(1, 3, figsize=(14, 4.5))

ax = axes[0]
ax.scatter(samples[:800, 0], samples[:800, 1], alpha=0.3, s=5)
plot_ellipse(ax, mu, Sigma, n_std=1, color="tab:red", linewidth=2, label="1σ")
plot_ellipse(ax, mu, Sigma, n_std=2, color="tab:red", linewidth=1.5, linestyle="--", label="2σ")
ax.scatter(*mu, color="black", marker="x", s=80, label="μ")
ax.set_aspect("equal")
ax.set_xlabel("X₁"); ax.set_ylabel("X₂")
ax.set_title("Scatter + level sets")
ax.legend()

ax = axes[1]
rho = Sigma[0, 1] / np.sqrt(Sigma[0, 0] * Sigma[1, 1])
cond_var = Sigma[1, 1] * (1 - rho**2)
x2_grid = np.linspace(mu[1] - 4, mu[1] + 4, 200)
for cond_val, color in [(0.0, "tab:orange"), (5.0, "tab:red")]:
    near = samples[np.abs(samples[:, 0] - cond_val) < 0.2]
    # Conditional mean moves along a line in x1; the variance does not move
    cond_mean = mu[1] + rho * np.sqrt(Sigma[1, 1] / Sigma[0, 0]) * (cond_val - mu[0])
    cond_pdf = np.exp(-0.5 * (x2_grid - cond_mean)**2 / cond_var) / np.sqrt(2 * np.pi * cond_var)
    ax.hist(near[:, 1], bins=30, density=True, alpha=0.4, color=color,
            label=f"X₂ | X₁≈{cond_val} ({len(near)} samples)")
    ax.plot(x2_grid, cond_pdf, color=color, linewidth=2)
    print(f"x₁ = {cond_val}: {len(near):4d} samples, mean {near[:, 1].mean():.3f} "
          f"(exact {cond_mean:.3f}), sd {near[:, 1].std():.3f} (exact {np.sqrt(cond_var):.3f})")
ax.set_title("Two slices, one width")
ax.set_xlabel("X₂"); ax.legend(fontsize=8)

ax = axes[2]
x1_marginal = np.linspace(mu[0] - 4, mu[0] + 4, 200)
marg_pdf = (1 / np.sqrt(2 * np.pi * Sigma[0, 0])) * np.exp(-0.5 * (x1_marginal - mu[0])**2 / Sigma[0, 0])
ax.hist(samples[:, 0], bins=50, density=True, alpha=0.5, label="Empirical marginal")
ax.plot(x1_marginal, marg_pdf, "tab:blue", linewidth=2, label="True marginal")
ax.set_title("Marginal X₁")
ax.set_xlabel("X₁"); ax.legend()

plt.tight_layout()
plt.show()

print(f"Sample mean: {samples.mean(axis=0)}")
print(f"Sample cov:\n{np.cov(samples.T)}")
print("Notice: both slices have sd near the exact 0.87, the scene's width at ρ = 0.5. "
      "x₁ = 0 has about half as many samples (a lower slice), not a wider spread.")

Key takeaways#

  • A multivariate Gaussian is a mean and a covariance. Its level sets are ellipses whose axes are the eigenvectors of Σ\SigmaΣ.
  • A slice of the joint at x1x_1x1​ has the shape of the conditional. Its centre moves along a line in x1x_1x1​, and its width is set by Σ\SigmaΣ alone.
  • The general conditional covariance is the Schur complement. It is PSD and never larger than the marginal covariance.
  • Marginals are read off μ\muμ and Σ\SigmaΣ. Σ\SigmaΣ is PSD because vTΣvv^T \Sigma vvTΣv is a variance.
  • Whitening turns correlated components into independent standard normals, and the Mahalanobis distance is Euclidean distance after whitening.

Further reading#

  • Christopher M. Bishop, Pattern Recognition and Machine Learning (Springer, 2006): §2.3 is the standard treatment of the multivariate Gaussian, marginals, and conditionals.
  • Kevin P. Murphy, Machine Learning: A Probabilistic Perspective (MIT Press, 2012): Chapter 4 covers Gaussian models, covariance, and linear-Gaussian systems.

Reading is free. PDF and ePub unlock with the newsletter. Join once and every lesson opens in this browser.

← Previous
Week 5: Bayes' Rule and Generative Classification
Next →
Week 7: Laws of Large Numbers and the CLT
On this page
  • Mean and covariance matrix
  • The slice is the conditional
  • Marginals
  • Why Sigma must be PSD
  • Whitening and Mahalanobis distance
  • Why Gaussians dominate
  • Knowledge Check
  • Browser lab: the slice from samples
  • Key takeaways
  • Further reading