The multivariate Gaussian
The mean vector and covariance matrix, the density and the Mahalanobis distance, ellipses and eigenvectors, affine maps and sampling, why uncorrelated coordinates are independent, and the conditioning formula.
Random vectors and the covariance matrix
The topic Joint distributions and independence gave us the words for several variables at once: a joint density, marginals, conditional distributions, and covariance as a measure of how two variables move together. It also showed the price. A joint distribution of variables is a function of arguments, and nothing forces it to be simple. This topic studies the one family that machine learning reaches for first, because a vector of means and a table of covariances describe it completely.
Stack the variables into a random vector , for example features of one object in a dataset. Its mean vector collects the means, . Its covariance matrix collects all the covariances:
A one-line reminder on matrices: turns a column into a row, and a column times a row, , is a table with entries . So the second formula is just the first one written for all pairs at once. The diagonal holds the variances, since .
The running example of this topic has two features measured from their averages, so , and
Feature 1 varies more (), feature 2 less (), and the positive covariance says they tend to rise together.
Not every table of numbers can be a covariance matrix. It is symmetric, because . And for any fixed weights , the number has variance
which is the rule from the previous topic, extended to weighted terms. A variance is never negative, so for every : the matrix is positive semidefinite. For a symmetric matrix this comes down to a nonnegative diagonal and a nonnegative determinant , which is the familiar once more. The course Mathematics for ML covers such matrices in the topic Symmetric matrices and quadratic forms.
The density and the Mahalanobis distance
When is positive definite ( for every , which also makes it invertible), the multivariate Gaussian is the distribution with density
Here is the determinant and the inverse matrix, . For a matrix both fit in one line:
With and the formula is the normal density from Continuous distributions.
The density depends on only through the number in the exponent,
the squared Mahalanobis distance from to the mean. In one dimension , the distance in standard deviations, the same -score as in standardization from Functions of random variables. In several dimensions it does the same job, but it measures every direction by the spread in that direction and takes the correlations into account. The smaller , the higher the density.
In the running example and . Take two points at the same Euclidean distance from the mean:
- : . Both features are above average together, which the correlation makes typical.
- : . One feature up and the other down goes against the correlation and is rare.
This is why anomaly detectors on correlated features use the Mahalanobis distance rather than the Euclidean one.
Ellipses and eigenvectors
The points with equal density satisfy . For a Gaussian these level sets are ellipses, and ellipsoids when . Their axes come from the covariance matrix.
A reminder: a vector is an eigenvector of with eigenvalue if , so the matrix only stretches it. A symmetric matrix has mutually perpendicular unit eigenvectors, and for a positive semidefinite one all . More in Mathematics for ML, topic Determinants and eigenvalues.
Along a unit eigenvector the projection has variance . So is the standard deviation in that direction, and the ellipse has its semi-axes along the with lengths .
In the running example and . Their sum is , the total variance, and their product is . The long axis points along with semi-axis , the short one along with . The point from the last block lies close to the long axis, and close to the short one; hence their very different distances. The long axis carries of the total variance. This direction is the first principal component, the starting point of PCA.
How much probability does an ellipse hold? In one dimension, has probability . In two dimensions is itself a random variable, and it has the exponential distribution with rate ; the next block shows where that comes from. So
For that is , noticeably less than the of a 1-D two-sigma interval. In dimensions follows the chi-squared distribution with degrees of freedom, and a fixed ellipse holds less and less as grows.
Affine maps and sampling
The property everything else in this topic rests on: if , is a fixed matrix and a fixed vector, then
The mean follows from linearity of expectation. For the covariance, , and comes out of the expectation on both sides: . This is the matrix version of . The real content is that is again Gaussian. For an invertible square it follows from the change of variables in several dimensions, the analogue of the 1-D formula from Functions of random variables.
With one row, , we get . In the running example , while : the sum goes along the long axis, the difference across it.
Now run the rule backwards to sample. Let be independent standard normals; the product of their 1-D densities is exactly the formula above with , . Pick any with , for example the lower-triangular Cholesky factor. Then
This is from standardization, with a matrix in place of . For the running example (rounded). The same construction shows that every symmetric positive semidefinite matrix is the covariance of some Gaussian vector.
Backwards again, turns any Gaussian into ; this is called whitening. Since , the Mahalanobis distance becomes ordinary length: , a sum of squared independent standard normals. That sum is the chi-squared law promised in the previous block.
Uncorrelated means independent
The previous topic warned that uncorrelated does not mean independent: and have zero covariance, yet one determines the other. For a Gaussian vector the warning does not apply.
Let , all covariances zero. Then , the determinant is , and the quadratic form falls apart into a sum. The exponential of a sum is a product, so
The joint density is the product of the marginal densities, which is the definition of independence. The ellipses of such a Gaussian are aligned with the coordinate axes. The same argument works for groups: if every covariance between one group of coordinates and another is zero, splits into two blocks, and the two groups are independent.
The condition is that the whole vector is Gaussian. A vector is jointly Gaussian if it has the distribution ; an equivalent test is that every linear combination is normal. Normal marginals are not enough. Take and an independent fair sign , and set . By symmetry . Its covariance with is
yet always, so the two are strongly dependent. The pair is simply not jointly Gaussian.
Conditioning on part of the vector
Suppose we observe feature 1, . What do we now know about feature 2?
The marginal first. Keeping one coordinate is a linear map, so : just read the entries off. In the running example and .
For the conditional distribution there is a short way. Remove from the part that explains:
Then . The pair is an affine image of , hence jointly Gaussian, hence independent by the previous block. So with independent of . Fixing leaves the distribution of untouched, and computing the mean and variance of gives
In the running example and the variance is , so . Observing gives .
Three things to notice.
- The conditional mean is linear in , a regression line with slope .
- The conditional variance does not depend on .
- It is smaller than . In general it equals , so knowing removes the share of the variance of .
The same proof works for groups of coordinates, with matrices in place of numbers. Observing a block leaves the block Gaussian:
The covariance here is called the Schur complement. This one formula drives Gaussian processes, the Kalman filter and Bayesian linear regression.
Where the Gaussian stops, and what comes next
The multivariate Gaussian is fixed by numbers, and it stays Gaussian under affine maps, marginals and conditioning. That is a lot of convenience, and it has limits.
- Size. For features the covariance alone has entries. Estimating it needs many samples, and inverting it costs on the order of operations. Practical models often restrict to be diagonal or low-rank.
- Shape. Real data have heavy tails, clusters and curved structure; a cloud of face images is not an ellipsoid. Mixtures of Gaussians, normalizing flows and diffusion models start from Gaussians and bend them. The course Generative Models does this, beginning with its topic Multivariate Gaussians.
One thread is left loose. The conditional mean is a number for each observed value: the best guess for feature 2 once feature 1 is known. Before we observe anything, is random, and so is the guess . It is a random variable with its own mean and variance, and together with the leftover it rebuilds .
The next topic, Conditional expectation, defines this object, , for any joint distribution. It shows why it is the best predictor under squared error and how the variance of splits into an explained part and a leftover part. The Gaussian is the special case where that predictor is a straight line.
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.