TL;DR
Take a single number per example, such as a height, and its variance tells you how spread out the values are. Take two numbers per example, such as height and weight, and you also need to know whether they rise together. The covariance matrix holds all of that in one table: the variance of each coordinate on the diagonal, and the covariance of each pair of coordinates off the diagonal.
Where you'll see it: the shape of a multivariate Gaussian, the correlations between features in a dataset, and the shape and orientation of each 3D Gaussian in Gaussian Splatting.
Intuition
Two numbers that move together
Suppose you measure the height and weight of many adults. Plot each person as a dot, with height on one axis and weight on the other. The dots form a cloud. The cloud is wider in some directions than in others, and it is usually tilted, because taller people tend to be heavier.
Two facts describe the cloud. The first is how wide it is along each axis. That is the variance of height and the variance of weight. The second is whether it leans. A lean is measured by the covariance of height and weight: positive when the two rise together, negative when one rises as the other falls, and zero when knowing one tells you nothing about the other.
The covariance matrix packs both facts into a square table. For two variables it is a grid, and for variables it is .
Why a table and not a list of numbers
Each variable has a variance, and each pair of variables has a covariance. Listing them separately loses the structure. The matrix form keeps every pair in a fixed place, which is what lets us apply linear algebra to it. Rotating the cloud, stretching it along one axis, or measuring its spread in some chosen direction all become simple matrix operations.
- people
The math
Covariance of two variables
Let and be random variables with means and . Recall that is the expected value, the probability-weighted average.
Here is how far sits from its mean, and is the same for . Their product is positive when both are above their means or both below, and negative when one is above while the other is below. The covariance averages these products. Setting gives , which is the variance of . So variance is the special case of covariance with a variable paired with itself.
Expanding the product gives a formula that is often easier to compute:
The covariance matrix
Let be a random vector, with mean vector .
The vector is a column of deviations from the mean. Multiplying a column by its own transpose, as in , gives a square matrix, and the entry in row , column is . Taking the expectation of this matrix gives the entry . That is the covariance of and . The diagonal entries are the variances of the coordinates.
Three properties follow directly from the definition:
- Symmetry. , because multiplication of numbers does not depend on order. So .
- Non-negative variances. The diagonal entries are never negative.
- Positive semidefinite. For any vector , let , a single number per outcome. Then . So no direction can have negative spread.
Transforming a cloud
The covariance matrix changes in a simple way under a linear map. If for a fixed matrix , then
This follows from the definition by pulling out of the expectation, since . The rule is the reason the covariance matrix is central to Gaussian models: a standard Gaussian can be turned into any Gaussian by a linear map, and the covariance of the result is in that case.
Worked examples
Take four paired measurements.
| Point | ||
|---|---|---|
| 1 | 1 | 2 |
| 2 | 2 | 3 |
| 3 | 3 | 5 |
| 4 | 4 | 6 |
- Means: and .
- Deviations of from : . Deviations of from : .
- Products of paired deviations: , , , . Sum: .
- Squared deviations of : , sum . Of : , sum .
- Dividing by (the population form): , , .
- So .
- Correlation, the covariance scaled to the range to : .
The off-diagonal entry is positive and large, which matches the picture of four points almost on a rising line. Dividing by instead gives the sample form, with every entry multiplied by . Software usually reports the sample form.
Start with two independent coordinates, the first with variance 1 and the second with variance 4. Then
The cloud is wide along the second axis and has no lean.
- Rotate every point by with , where .
- Compute .
- Multiply by .
- Entry : .
- Entry : .
- Entry : .
- So the new covariance is .
The diagonal is now equal, because the cloud is no longer aligned with an axis. The off-diagonal entry is negative, so the cloud now leans the other way. The trace, the sum of the diagonal, is before and after, because rotation does not change total spread. The lesson is that a covariance matrix describes the cloud's shape, and rotating the cloud changes the table while keeping the total spread.
Tradeoffs and failure modes
Covariance measures linear association only. A variable can depend strongly on another and still have zero covariance with it. The standard example is uniform on and . Here because and , yet is completely determined by . A zero entry means no linear lean, not independence.
Covariance is also sensitive to scale. Measuring height in centimetres instead of metres multiplies the covariance by . Correlation avoids this by dividing each covariance by the two standard deviations.
For estimation from data, the number of entries grows quickly. A -dimensional covariance matrix has distinct entries. The sample version is singular when the number of samples is smaller than the dimension, which breaks methods that need to invert it.
| Situation | Problem | Better choice |
|---|---|---|
| Features move together in a non-linear way | Covariance reports near zero | Mutual information or a non-linear measure |
| Many features, few samples | Estimate is singular or noisy | Shrinkage estimator, or a diagonal approximation |
| Outliers in the data | A few points dominate the products | Robust covariance estimate |
| Need the scale-free strength of association | Covariance depends on units | Correlation matrix |
History
The idea of co-variation came from the statistics of heredity in the late nineteenth century. Francis Galton studied how the heights of parents and children move together, and he called the relationship "co-relation". Karl Pearson gave the modern formula for the correlation coefficient in the 1890s, and the covariance is the unscaled quantity underneath it. The matrix form is the standard way to handle several variables at once.
The covariance matrix matters in machine learning because the multivariate Gaussian is defined by its mean vector and covariance matrix.
Check yourself
QuestionRecall: what does a positive off-diagonal entry in a covariance matrix tell you about the two variables?Show answer
It tells you the two variables tend to be above their means together and below their means together. In a cloud of points, it means the cloud leans up to the right. The entry is zero when there is no linear lean, and negative when one variable tends to be high while the other is low.
QuestionComputation: for two variables with , and , what is the correlation?Show answer
. The correlation is half the maximum possible, so the two variables have a moderate positive linear relationship.
QuestionWhy: why must every covariance matrix be symmetric and positive semidefinite?Show answer
Symmetry holds because , since multiplying two deviations does not depend on their order. Positive semidefiniteness holds because for any vector , the quantity is the variance of the single number , and a variance cannot be negative. A matrix that gave a negative value would describe a direction with negative spread, which makes no sense.
QuestionApplied scenario: a dataset has 50 features and 30 samples. You want the covariance matrix to decide which features are redundant. What is the first problem, and what would you do?Show answer
The sample covariance matrix is but built from only 30 samples, so its rank is at most 29 and it is singular. Its estimates of the small off-diagonal entries are noisy. A sensible response is to use a shrinkage estimator, which pulls the matrix towards a simple target such as a diagonal, or to reduce the feature count first. Treating the raw estimate as exact would make the redundancy decisions unreliable.