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 and a variance :
The second argument is always the variance, never the standard deviation: has . The standard normal is the one with and .
Every Gaussian is a shifted and stretched standard normal. Standardizing sends to , and the inverse map goes back:
So one random number generator, for , serves every 1-D Gaussian. With and the draw becomes : one standard deviation above its mean, just as was above its own.
Mean vector and covariance matrix
A data point is a vector , so two numbers are no longer enough. The mean becomes a vector, , and the variance becomes the covariance matrix
The diagonal holds the variances of the coordinates; the off-diagonal entries say how pairs of coordinates move together. In two dimensions, with correlation ,
Not every matrix qualifies. is symmetric, since . It is also positive semidefinite: for any fixed vector the number has variance , and a variance cannot be negative.
The running example of this topic is
Both coordinates have variance , exactly like the round cloud . Only the covariance differs, and it tilts the cloud along the diagonal . Two variances alone could not tell the two clouds apart.
The price is size: has free numbers, which for a image () is .
The density and its ellipses
When is positive definite, and so invertible, the Gaussian has the density
where is the determinant. With and this is the 1-D formula again. The exponent is a single number: a row times a matrix times a column.
The quadratic form is the squared Mahalanobis distance: the distance from the mean, counted in standard deviations of each direction. The density depends on only through , so its level sets are ellipses (ellipsoids when ), the and contours for .
The axes of the ellipses point along the eigenvectors of , and the semi-axis along has length , the standard deviation in that direction. For the running example , the peak density is , and
The point lies on the long axis and has , inside the 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 ellipse holds only of the probability, not the of a 1-D interval.
Diagonal covariance and the standard normal
Let . Then , , and the quadratic form falls apart into a sum. The exponential of a sum is a product, so
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: and 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 numbers instead of , which is why later models use it so often.
The most important diagonal Gaussian is the standard normal , the course's noise and prior :
Its density depends only on the length , so its contours are circles (spheres), and rotating it changes nothing. Its coordinates are independent draws — exactly what torch.randn(D) returns.
Affine maps keep Gaussians Gaussian
The property everything else rests on: if and for a fixed matrix and vector , then
The mean follows from linearity of expectation. For the covariance, , and comes out of the expectation on both sides. The non-obvious part is that 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 , the number has variance . In the running example the difference has variance .
- 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 and the matching block of . In the running example : the covariance simply drops out.
- Adding independent noise. Independent Gaussians and stack into one Gaussian vector with a block-diagonal covariance, and their sum is a linear map of it: . Variances add; standard deviations do not. For independent , the mixture is again .
Sampling: x = μ + Lε
Now run the affine rule the other way. Take noise and a square matrix :
To sample we need any with — a matrix that plays the role of in .
The standard choice is the Cholesky factor. For a positive definite there is exactly one lower-triangular with a positive diagonal and ; it costs about operations and is computed once (Golub and Van Loan, 2013, §4.2). For the running example
The noise becomes : a step along drags halfway along, which is what correlation means. Other square roots, such as the symmetric 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 , and the line shrinks to . 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 once we have observed ? For a Gaussian the answer is again a Gaussian. In two dimensions
and the same formulas hold for blocks of coordinates, with in place of the division (Bishop, 2006, §2.3.1). One way to see it: write as a multiple of plus a residual that has zero covariance with . For a Gaussian, zero covariance means independence, so fixing 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 carries information about , and it does not depend on at all. In the running example, observing gives : the mean follows halfway, and the variance drops from to .
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
In practice we always work with this, never with the density itself.
- Range. Densities in high dimension are absurd numbers. The standard normal in has density at its own mean, far below the smallest float32, and about at a typical sample, below float64 too. Their logarithms, about and , 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, is a sum of one-dimensional terms.
- Shape. The log-density is a quadratic in : a squared error weighted by , plus terms that depend only on . With a fixed , is plus a constant: squared error is a Gaussian log-likelihood in disguise.
A Cholesky factor gives the whole log-density without inverting . From we get , so the quadratic form is , one triangular solve away. The determinant of a triangular matrix is just the product of its diagonal, so and :
MultivariateNormal.log_prob works this way. The factor is also how a trainable Gaussian is stored. An optimizer that moves the entries of directly can leave the valid matrices: one step that takes the off-diagonal entries of the running example from to gives eigenvalues and , a negative variance. If it moves a lower-triangular whose diagonal is stored as , every step lands on a valid covariance, since for every . 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 to data from we still need a number that says how far is from . The next topic, Entropy, cross-entropy and KL, builds it out of of probabilities — the quantity we just learned to compute.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.