Search

Search papers and lessons

Covariance Matrix

Level 2 · 9 min read

A covariance matrix collects the variance of each coordinate on its diagonal and the covariance of each pair of coordinates off the diagonal, describing the spread and tilt of a cloud of vectors.

On this page

  • Colour = topic mix
  • Probability
  • Linear algebra
  • Discrete math
  • Sign in to see your progress

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 2×22 \times 2 grid, and for dd variables it is d×dd \times d.

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.

0123450246x (first variable)y (second variable)
  • people
Four points that rise together. Their covariance is positive, so the cloud leans up to the right.

The math

Covariance of two variables

Let XX and YY be random variables with means μX=E[X]\mu_X = \mathbb{E}[X] and μY=E[Y]\mu_Y = \mathbb{E}[Y]. Recall that E\mathbb{E} is the expected value, the probability-weighted average.

Definition: Covariance
Cov⁡(X,Y)=E[(X−μX)(Y−μY)].\operatorname{Cov}(X, Y) = \mathbb{E}\big[(X - \mu_X)(Y - \mu_Y)\big].

Here X−μXX - \mu_X is how far XX sits from its mean, and Y−μYY - \mu_Y is the same for YY. 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 Y=XY = X gives Cov⁡(X,X)=E[(X−μX)2]\operatorname{Cov}(X, X) = \mathbb{E}[(X - \mu_X)^2], which is the variance of XX. 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:

Cov⁡(X,Y)=E[XY]−μXμY.\operatorname{Cov}(X, Y) = \mathbb{E}[XY] - \mu_X \mu_Y.

The covariance matrix

Let X=(X1,…,Xd)⊤\mathbf{X} = (X_1, \dots, X_d)^\top be a random vector, with mean vector μ=E[X]\boldsymbol{\mu} = \mathbb{E}[\mathbf{X}].

Definition: Covariance matrix
Σ=E[(X−μ)(X−μ)⊤],Σij=Cov⁡(Xi,Xj).\boldsymbol{\Sigma} = \mathbb{E}\big[(\mathbf{X} - \boldsymbol{\mu})(\mathbf{X} - \boldsymbol{\mu})^\top\big], \qquad \Sigma_{ij} = \operatorname{Cov}(X_i, X_j).

The vector X−μ\mathbf{X} - \boldsymbol{\mu} is a column of deviations from the mean. Multiplying a column by its own transpose, as in vv⊤\mathbf{v}\mathbf{v}^\top, gives a square matrix, and the entry in row ii, column jj is vivjv_i v_j. Taking the expectation of this matrix gives the entry E[(Xi−μi)(Xj−μj)]\mathbb{E}[(X_i - \mu_i)(X_j - \mu_j)]. That is the covariance of XiX_i and XjX_j. The diagonal entries are the variances of the coordinates.

Three properties follow directly from the definition:

  1. Symmetry. Σij=Σji\Sigma_{ij} = \Sigma_{ji}, because multiplication of numbers does not depend on order. So Σ⊤=Σ\boldsymbol{\Sigma}^\top = \boldsymbol{\Sigma}.
  2. Non-negative variances. The diagonal entries Σii=Var⁡(Xi)\Sigma_{ii} = \operatorname{Var}(X_i) are never negative.
  3. Positive semidefinite. For any vector a\mathbf{a}, let Z=a⊤XZ = \mathbf{a}^\top \mathbf{X}, a single number per outcome. Then a⊤Σa=Var⁡(Z)≥0\mathbf{a}^\top \boldsymbol{\Sigma} \mathbf{a} = \operatorname{Var}(Z) \geq 0. So no direction can have negative spread.

Transforming a cloud

The covariance matrix changes in a simple way under a linear map. If Y=AX\mathbf{Y} = A\mathbf{X} for a fixed matrix AA, then

ΣY=A ΣX A⊤.\boldsymbol{\Sigma}_{\mathbf{Y}} = A\,\boldsymbol{\Sigma}_{\mathbf{X}}\,A^\top.

This follows from the definition by pulling AA out of the expectation, since (AX−Aμ)(AX−Aμ)⊤=A(X−μ)(X−μ)⊤A⊤(A\mathbf{X} - A\boldsymbol{\mu})(A\mathbf{X} - A\boldsymbol{\mu})^\top = A(\mathbf{X} - \boldsymbol{\mu})(\mathbf{X} - \boldsymbol{\mu})^\top A^\top. 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 AA⊤A A^\top in that case.

Worked examples

Example
Example 1: computing a 2 by 2 covariance matrix

Take four paired measurements.

Pointxxyy
112
223
335
446
  1. Means: xˉ=(1+2+3+4)/4=2.5\bar{x} = (1 + 2 + 3 + 4)/4 = 2.5 and yˉ=(2+3+5+6)/4=4\bar{y} = (2 + 3 + 5 + 6)/4 = 4.
  2. Deviations of xx from 2.52.5: −1.5,−0.5,0.5,1.5-1.5, -0.5, 0.5, 1.5. Deviations of yy from 44: −2,−1,1,2-2, -1, 1, 2.
  3. Products of paired deviations: (−1.5)(−2)=3(-1.5)(-2) = 3, (−0.5)(−1)=0.5(-0.5)(-1) = 0.5, (0.5)(1)=0.5(0.5)(1) = 0.5, (1.5)(2)=3(1.5)(2) = 3. Sum: 77.
  4. Squared deviations of xx: 2.25,0.25,0.25,2.252.25, 0.25, 0.25, 2.25, sum 55. Of yy: 4,1,1,44, 1, 1, 4, sum 1010.
  5. Dividing by n=4n = 4 (the population form): Var⁡(x)=1.25\operatorname{Var}(x) = 1.25, Var⁡(y)=2.5\operatorname{Var}(y) = 2.5, Cov⁡(x,y)=1.75\operatorname{Cov}(x, y) = 1.75.
  6. So Σ=(1.251.751.752.5)\boldsymbol{\Sigma} = \begin{pmatrix} 1.25 & 1.75 \\ 1.75 & 2.5 \end{pmatrix}.
  7. Correlation, the covariance scaled to the range −1-1 to 11: 1.75/1.25×2.5=1.75/1.768≈0.991.75 / \sqrt{1.25 \times 2.5} = 1.75 / 1.768 \approx 0.99.

The off-diagonal entry is positive and large, which matches the picture of four points almost on a rising line. Dividing by n−1=3n - 1 = 3 instead gives the sample form, with every entry multiplied by 4/34/3. Software usually reports the sample form.

Example
Example 2: rotating a cloud

Start with two independent coordinates, the first with variance 1 and the second with variance 4. Then

Σ=(1004).\boldsymbol{\Sigma} = \begin{pmatrix} 1 & 0 \\ 0 & 4 \end{pmatrix}.

The cloud is wide along the second axis and has no lean.

  1. Rotate every point by 45∘45^\circ with A=(a−aaa)A = \begin{pmatrix} a & -a \\ a & a \end{pmatrix}, where a=1/2≈0.7071a = 1/\sqrt{2} \approx 0.7071.
  2. Compute AΣ=(0.7071−2.82840.70712.8284)A\boldsymbol{\Sigma} = \begin{pmatrix} 0.7071 & -2.8284 \\ 0.7071 & 2.8284 \end{pmatrix}.
  3. Multiply by A⊤=(0.70710.7071−0.70710.7071)A^\top = \begin{pmatrix} 0.7071 & 0.7071 \\ -0.7071 & 0.7071 \end{pmatrix}.
  4. Entry (1,1)(1,1): 0.7071×0.7071+(−2.8284)(−0.7071)=0.5+2.0=2.50.7071 \times 0.7071 + (-2.8284)(-0.7071) = 0.5 + 2.0 = 2.5.
  5. Entry (1,2)(1,2): 0.7071×0.7071+(−2.8284)(0.7071)=0.5−2.0=−1.50.7071 \times 0.7071 + (-2.8284)(0.7071) = 0.5 - 2.0 = -1.5.
  6. Entry (2,2)(2,2): 0.5+2.0=2.50.5 + 2.0 = 2.5.
  7. So the new covariance is (2.5−1.5−1.52.5)\begin{pmatrix} 2.5 & -1.5 \\ -1.5 & 2.5 \end{pmatrix}.

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 55 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 XX uniform on {−1,0,1}\{-1, 0, 1\} and Y=X2Y = X^2. Here Cov⁡(X,Y)=0\operatorname{Cov}(X, Y) = 0 because E[XY]=E[X3]=0\mathbb{E}[XY] = \mathbb{E}[X^3] = 0 and E[X]=0\mathbb{E}[X] = 0, yet YY is completely determined by XX. 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 100100. Correlation avoids this by dividing each covariance by the two standard deviations.

For estimation from data, the number of entries grows quickly. A dd-dimensional covariance matrix has d(d+1)/2d(d+1)/2 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.

SituationProblemBetter choice
Features move together in a non-linear wayCovariance reports near zeroMutual information or a non-linear measure
Many features, few samplesEstimate is singular or noisyShrinkage estimator, or a diagonal approximation
Outliers in the dataA few points dominate the productsRobust covariance estimate
Need the scale-free strength of associationCovariance depends on unitsCorrelation 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 Var⁡(X)=4\operatorname{Var}(X) = 4, Var⁡(Y)=9\operatorname{Var}(Y) = 9 and Cov⁡(X,Y)=3\operatorname{Cov}(X, Y) = 3, what is the correlation?Show answer

ρ=3/4×9=3/6=0.5\rho = 3 / \sqrt{4 \times 9} = 3 / 6 = 0.5. 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 Cov⁡(Xi,Xj)=Cov⁡(Xj,Xi)\operatorname{Cov}(X_i, X_j) = \operatorname{Cov}(X_j, X_i), since multiplying two deviations does not depend on their order. Positive semidefiniteness holds because for any vector a\mathbf{a}, the quantity a⊤Σa\mathbf{a}^\top \boldsymbol{\Sigma} \mathbf{a} is the variance of the single number a⊤X\mathbf{a}^\top \mathbf{X}, 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 50×5050 \times 50 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.

Sources