Skip to content
BIASED

Multivariate Gaussians

The distribution almost every model starts from: density, covariance and its ellipses, affine maps, sampling as x = μ + Lε, and why we work with log-densities.

One dimension: shift and scale

The topic What generative modeling is left two things to build: the Gaussian noise that most models start from, and a way to measure how far a model is from the data. This topic builds the first; the second is the next topic.

In one dimension a Gaussian, or normal distribution, has two parameters, a mean μ\mu and a variance σ2\sigma^2:

N(x; μ,σ2)=12πσ2 exp⁡ ⁣(−(x−μ)22σ2).\mathcal N(x;\, \mu, \sigma^2) = \frac{1}{\sqrt{2\pi\sigma^2}}\, \exp\!\Big(-\frac{(x - \mu)^2}{2\sigma^2}\Big).

The second argument is always the variance, never the standard deviation: N(0,4)\mathcal N(0, 4) has σ=2\sigma = 2. The standard normal N(0,1)\mathcal N(0, 1) is the one with μ=0\mu = 0 and σ=1\sigma = 1.

Every Gaussian is a shifted and stretched standard normal. Standardizing sends x∼N(μ,σ2)x \sim \mathcal N(\mu, \sigma^2) to ε=(x−μ)/σ∼N(0,1)\varepsilon = (x - \mu)/\sigma \sim \mathcal N(0, 1), and the inverse map goes back:

x=μ+σε,ε∼N(0,1).x = \mu + \sigma\varepsilon, \qquad \varepsilon \sim \mathcal N(0, 1).

So one random number generator, for N(0,1)\mathcal N(0, 1), serves every 1-D Gaussian. With μ=2\mu = 2 and σ=1.5\sigma = 1.5 the draw ε=1\varepsilon = 1 becomes x=3.5x = 3.5: one standard deviation above its mean, just as ε\varepsilon was above its own.

Mean vector and covariance matrix

A data point is a vector x∈RDx \in \mathbb R^D, so two numbers are no longer enough. The mean becomes a vector, μ=E[x]\mu = \mathbb E[x], and the variance becomes the covariance matrix

Σ=E[(x−μ)(x−μ)⊤],Σij=Cov⁡(xi,xj).\Sigma = \mathbb E\big[(x - \mu)(x - \mu)^\top\big], \qquad \Sigma_{ij} = \operatorname{Cov}(x_i, x_j).

The diagonal holds the variances of the coordinates; the off-diagonal entries say how pairs of coordinates move together. In two dimensions, with correlation ρ\rho,

Σ=(σ12ρ σ1σ2ρ σ1σ2σ22).\Sigma = \begin{pmatrix} \sigma_1^2 & \rho\,\sigma_1\sigma_2 \\ \rho\,\sigma_1\sigma_2 & \sigma_2^2 \end{pmatrix}.

Not every matrix qualifies. Σ\Sigma is symmetric, since Cov⁡(xi,xj)=Cov⁡(xj,xi)\operatorname{Cov}(x_i, x_j) = \operatorname{Cov}(x_j, x_i). It is also positive semidefinite: for any fixed vector aa the number a⊤xa^\top x has variance a⊤Σaa^\top \Sigma a, and a variance cannot be negative.

The running example of this topic is

μ=(00),Σ=(10.50.51).\mu = \begin{pmatrix} 0 \\ 0 \end{pmatrix}, \qquad \Sigma = \begin{pmatrix} 1 & 0.5 \\ 0.5 & 1 \end{pmatrix}.

Both coordinates have variance 11, exactly like the round cloud N(0,I)\mathcal N(0, I). Only the covariance 0.50.5 differs, and it tilts the cloud along the diagonal x1=x2x_1 = x_2. Two variances alone could not tell the two clouds apart.

The price is size: Σ\Sigma has D(D+1)/2D(D+1)/2 free numbers, which for a 28×2828 \times 28 image (D=784D = 784) is 307,720307{,}720.

The density and its ellipses

When Σ\Sigma is positive definite, and so invertible, the Gaussian N(μ,Σ)\mathcal N(\mu, \Sigma) has the density

N(x; μ,Σ)=1(2π)D/2 ∣Σ∣1/2 exp⁡ ⁣(−12 (x−μ)⊤Σ−1(x−μ)),\mathcal N(x;\, \mu, \Sigma) = \frac{1}{(2\pi)^{D/2}\,|\Sigma|^{1/2}}\, \exp\!\Big(-\tfrac12\,(x - \mu)^\top \Sigma^{-1} (x - \mu)\Big),

where ∣Σ∣|\Sigma| is the determinant. With D=1D = 1 and Σ=σ2\Sigma = \sigma^2 this is the 1-D formula again. The exponent is a single number: a 1×D1 \times D row times a D×DD \times D matrix times a D×1D \times 1 column.

The quadratic form Δ2=(x−μ)⊤Σ−1(x−μ)\Delta^2 = (x - \mu)^\top \Sigma^{-1} (x - \mu) is the squared Mahalanobis distance: the distance from the mean, counted in standard deviations of each direction. The density depends on xx only through Δ2\Delta^2, so its level sets Δ2=k2\Delta^2 = k^2 are ellipses (ellipsoids when D>2D > 2), the 1σ1\sigma and 2σ2\sigma contours for k=1,2k = 1, 2.

The axes of the ellipses point along the eigenvectors uiu_i of Σ\Sigma, and the 1σ1\sigma semi-axis along uiu_i has length λi\sqrt{\lambda_i}, the standard deviation in that direction. For the running example ∣Σ∣=0.75|\Sigma| = 0.75, the peak density is 1/(2π0.75)≈0.1841/(2\pi\sqrt{0.75}) \approx 0.184, and

Σ−1=10.75(1−0.5−0.51),λ1=1.5 along 12(1,1),λ2=0.5 along 12(1,−1).\Sigma^{-1} = \frac{1}{0.75}\begin{pmatrix} 1 & -0.5 \\ -0.5 & 1 \end{pmatrix}, \qquad \lambda_1 = 1.5 \ \text{along}\ \tfrac{1}{\sqrt2}(1, 1), \quad \lambda_2 = 0.5 \ \text{along}\ \tfrac{1}{\sqrt2}(1, -1).

The point (1,1)(1, 1) lies on the long axis and has Δ2=10.75(1−0.5−0.5+1)=43\Delta^2 = \tfrac{1}{0.75}(1 - 0.5 - 0.5 + 1) = \tfrac43, inside the 2σ2\sigma ellipse. A step of the same Euclidean length across the long axis counts for more, because the cloud is narrow there.

One caution: in two dimensions the 1σ1\sigma ellipse holds only 1−e−1/2≈39%1 - e^{-1/2} \approx 39\% of the probability, not the 68%68\% of a 1-D interval.

Diagonal covariance and the standard normal

Let Σ=diag⁡(σ12,…,σD2)\Sigma = \operatorname{diag}(\sigma_1^2, \dots, \sigma_D^2). Then ∣Σ∣=∏iσi2|\Sigma| = \prod_i \sigma_i^2, Σ−1=diag⁡(1/σ12,…,1/σD2)\Sigma^{-1} = \operatorname{diag}(1/\sigma_1^2, \dots, 1/\sigma_D^2), and the quadratic form falls apart into a sum. The exponential of a sum is a product, so

N(x; μ,diag⁡(σ2))=∏i=1DN(xi; μi,σi2).\mathcal N\big(x;\, \mu, \operatorname{diag}(\sigma^2)\big) = \prod_{i=1}^{D} \mathcal N(x_i;\, \mu_i, \sigma_i^2).

A density that factorizes over coordinates means the coordinates are independent. For a Gaussian vector, zero covariance therefore means independence. That is special to Gaussians: x∼N(0,1)x \sim \mathcal N(0, 1) and x2x^2 have zero covariance, yet one determines the other. Geometrically, the ellipses of a diagonal Gaussian are aligned with the coordinate axes.

A diagonal Gaussian costs 2D2D numbers instead of D+D(D+1)/2D + D(D+1)/2, which is why later models use it so often.

The most important diagonal Gaussian is the standard normal N(0,I)\mathcal N(0, I), the course's noise ε\varepsilon and prior p(z)p(z):

N(ε; 0,I)=(2π)−D/2exp⁡ ⁣(−12 ∥ε∥2).\mathcal N(\varepsilon;\, 0, I) = (2\pi)^{-D/2} \exp\!\big(-\tfrac12\,\lVert\varepsilon\rVert^2\big).

Its density depends only on the length ∥ε∥\lVert\varepsilon\rVert, so its contours are circles (spheres), and rotating it changes nothing. Its coordinates are DD independent N(0,1)\mathcal N(0, 1) draws — exactly what torch.randn(D) returns.

Affine maps keep Gaussians Gaussian

The property everything else rests on: if x∼N(μ,Σ)x \sim \mathcal N(\mu, \Sigma) and y=Ax+by = Ax + b for a fixed M×DM \times D matrix AA and vector bb, then

y∼N(Aμ+b, AΣA⊤).y \sim \mathcal N\big(A\mu + b,\ A \Sigma A^\top\big).

The mean follows from linearity of expectation. For the covariance, y−E[y]=A(x−μ)y - \mathbb E[y] = A(x - \mu), and AA comes out of the expectation on both sides. The non-obvious part is that yy is again Gaussian, not just some distribution with these moments; this holds for every affine map (Bishop, 2006, §2.3).

Three special cases do most of the work later.

  • One direction. For a single row A=a⊤A = a^\top, the number a⊤xa^\top x has variance a⊤Σaa^\top \Sigma a. In the running example the difference x1−x2x_1 - x_2 has variance 1+1−2⋅0.5=11 + 1 - 2 \cdot 0.5 = 1.
  • Marginals. Keeping some of the coordinates is a linear map that selects them, so any subset of coordinates is Gaussian, with the matching entries of μ\mu and the matching block of Σ\Sigma. In the running example x1∼N(0,1)x_1 \sim \mathcal N(0, 1): the covariance simply drops out.
  • Adding independent noise. Independent Gaussians xx and ε\varepsilon stack into one Gaussian vector with a block-diagonal covariance, and their sum is a linear map of it: x+ε∼N(μx+με, Σx+Σε)x + \varepsilon \sim \mathcal N(\mu_x + \mu_\varepsilon,\ \Sigma_x + \Sigma_\varepsilon). Variances add; standard deviations do not. For independent x,ε∼N(0,1)x, \varepsilon \sim \mathcal N(0, 1), the mixture 0.6 x+0.8 ε0.6\,x + 0.8\,\varepsilon is again N(0,0.36+0.64)=N(0,1)\mathcal N(0, 0.36 + 0.64) = \mathcal N(0, 1).

Sampling: x = μ + Lε

Now run the affine rule the other way. Take noise ε∼N(0,I)\varepsilon \sim \mathcal N(0, I) and a square matrix LL:

x=μ+Lε∼N(μ, LL⊤).x = \mu + L\varepsilon \sim \mathcal N\big(\mu,\ LL^\top\big).

To sample N(μ,Σ)\mathcal N(\mu, \Sigma) we need any LL with LL⊤=ΣLL^\top = \Sigma — a matrix that plays the role of σ\sigma in x=μ+σεx = \mu + \sigma\varepsilon.

The standard choice is the Cholesky factor. For a positive definite Σ\Sigma there is exactly one lower-triangular LL with a positive diagonal and LL⊤=ΣLL^\top = \Sigma; it costs about D3/3D^3/3 operations and is computed once (Golub and Van Loan, 2013, §4.2). For the running example

L=(100.50.75)≈(100.50.866),LL⊤=(10.50.50.25+0.75)=Σ.L = \begin{pmatrix} 1 & 0 \\ 0.5 & \sqrt{0.75} \end{pmatrix} \approx \begin{pmatrix} 1 & 0 \\ 0.5 & 0.866 \end{pmatrix}, \qquad LL^\top = \begin{pmatrix} 1 & 0.5 \\ 0.5 & 0.25 + 0.75 \end{pmatrix} = \Sigma .

The noise ε=(1,0)\varepsilon = (1, 0) becomes Lε=(1,0.5)L\varepsilon = (1, 0.5): a step along x1x_1 drags x2x_2 halfway along, which is what correlation 0.50.5 means. Other square roots, such as the symmetric Σ1/2\Sigma^{1/2} from the eigendecomposition, give the same distribution; they only pair noise values with samples differently.

In PyTorch, with one sample per row:

L = torch.linalg.cholesky(Sigma)   # Sigma = L @ L.T
eps = torch.randn(n, D)            # n draws of ε ~ N(0, I)
x = mu + eps @ L.T                 # each row is μ + Lε

torch.distributions.MultivariateNormal samples exactly this way. For a diagonal Σ\Sigma, L=diag⁡(σ)L = \operatorname{diag}(\sigma) and the line shrinks to x=μ+σ⊙εx = \mu + \sigma \odot \varepsilon. Keep its shape in mind: the randomness enters as parameter-free noise, and the parameters enter through a deterministic, differentiable map. When we need gradients through samples, this line returns as the reparameterization trick.

Conditioning on part of a Gaussian

Marginals drop coordinates. Conditionals fix them: what is the distribution of x2x_2 once we have observed x1=ax_1 = a? For a Gaussian the answer is again a Gaussian. In two dimensions

x2∣x1=a  ∼  N(μ2+Σ21Σ11 (a−μ1),  Σ22−Σ212Σ11),x_2 \mid x_1 = a \;\sim\; \mathcal N\Big(\mu_2 + \frac{\Sigma_{21}}{\Sigma_{11}}\,(a - \mu_1),\ \ \Sigma_{22} - \frac{\Sigma_{21}^2}{\Sigma_{11}}\Big),

and the same formulas hold for blocks of coordinates, with Σ11−1\Sigma_{11}^{-1} in place of the division (Bishop, 2006, §2.3.1). One way to see it: write x2x_2 as a multiple of x1x_1 plus a residual that has zero covariance with x1x_1. For a Gaussian, zero covariance means independence, so fixing x1x_1 leaves the residual's distribution untouched.

Two things to read off. The conditional mean moves linearly with the observed value. The conditional variance shrinks, because x1x_1 carries information about x2x_2, and it does not depend on aa at all. In the running example, observing x1=2x_1 = 2 gives x2∣x1=2∼N(1,0.75)x_2 \mid x_1 = 2 \sim \mathcal N(1, 0.75): the mean follows halfway, and the variance drops from 11 to 0.750.75.

Gaussians whose mean depends on another variable are how most later models write their likelihoods.

Working in logs

Taking the logarithm of the density gives

log⁡N(x; μ,Σ)=−12 (x−μ)⊤Σ−1(x−μ)  −  12log⁡∣Σ∣  −  D2log⁡2π.\log \mathcal N(x;\, \mu, \Sigma) = -\tfrac12\,(x - \mu)^\top \Sigma^{-1} (x - \mu) \;-\; \tfrac12 \log|\Sigma| \;-\; \tfrac D2 \log 2\pi .

In practice we always work with this, never with the density itself.

  • Range. Densities in high dimension are absurd numbers. The standard normal in D=784D = 784 has density (2π)−392≈10−313(2\pi)^{-392} \approx 10^{-313} at its own mean, far below the smallest float32, and about 10−48310^{-483} at a typical sample, below float64 too. Their logarithms, about −720-720 and −1112-1112, are ordinary numbers.
  • Sums. Independent coordinates and independent data points multiply densities; the logarithm turns the product into a sum, which is stable and easy to differentiate. For a diagonal Gaussian, log⁡p\log p is a sum of DD one-dimensional terms.
  • Shape. The log-density is a quadratic in xx: a squared error weighted by Σ−1\Sigma^{-1}, plus terms that depend only on Σ\Sigma. With a fixed Σ=s2I\Sigma = s^2 I, −log⁡p-\log p is ∥x−μ∥2/(2s2)\lVert x - \mu\rVert^2 / (2s^2) plus a constant: squared error is a Gaussian log-likelihood in disguise.

A Cholesky factor gives the whole log-density without inverting Σ\Sigma. From Σ=LL⊤\Sigma = LL^\top we get Σ−1=L−⊤L−1\Sigma^{-1} = L^{-\top}L^{-1}, so the quadratic form is ∥L−1(x−μ)∥2\lVert L^{-1}(x - \mu)\rVert^2, one triangular solve away. The determinant of a triangular matrix is just the product of its diagonal, so ∣Σ∣=∏iLii2|\Sigma| = \prod_i L_{ii}^2 and log⁡∣Σ∣=2∑ilog⁡Lii\log|\Sigma| = 2\sum_i \log L_{ii}:

log⁡N(x; μ,Σ)=−12 ∥L−1(x−μ)∥2  −  ∑i=1Dlog⁡Lii  −  D2log⁡2π.\log \mathcal N(x;\, \mu, \Sigma) = -\tfrac12\,\big\lVert L^{-1}(x - \mu)\big\rVert^2 \;-\; \sum_{i=1}^{D} \log L_{ii} \;-\; \tfrac D2 \log 2\pi .

MultivariateNormal.log_prob works this way. The factor is also how a trainable Gaussian is stored. An optimizer that moves the entries of Σ\Sigma directly can leave the valid matrices: one step that takes the off-diagonal entries of the running example from 0.50.5 to 1.21.2 gives eigenvalues 2.22.2 and −0.2-0.2, a negative variance. If it moves a lower-triangular LL whose diagonal is stored as Lii=esiL_{ii} = e^{s_i}, every step lands on a valid covariance, since v⊤LL⊤v=∥L⊤v∥2>0v^\top LL^\top v = \lVert L^\top v\rVert^2 > 0 for every v≠0v \ne 0. The triangular determinant returns in The change of variables, where it makes normalizing flows affordable.

We can now draw samples from a Gaussian and score any point under one. To fit a model QθQ_\theta to data from PP we still need a number that says how far QθQ_\theta is from PP. The next topic, Entropy, cross-entropy and KL, builds it out of −log⁡-\log of probabilities — the quantity we just learned to compute.

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.