Search

Search papers and lessons

Covariance

Level 2 · 9 min read

Covariance measures whether two quantities tend to rise and fall together: it is the average of the product of their deviations from their means.

On this page

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

TL;DR

Covariance tells you whether two quantities tend to move together. Take each quantity's distance from its own average. If the two distances usually have the same sign, the covariance is positive: when one is above average the other tends to be too. If they usually have opposite signs, it is negative. If the signs are mixed, it is near zero.

Covariance is the building block for how spread adds up when quantities are linked, for measuring association between variables, and for describing the shape of multivariate data.

Where you'll see it: in the variance of a sum of correlated measurements, in the correlation coefficient, in principal component analysis, and in the covariance matrix that describes a cloud of data in several dimensions.

This lesson explains a statistical quantity. It is not advice about any particular data set or decision.

Intuition

Do they rise together?

Imagine recording, for a group of students, the hours each one studied and the score each one got. Some students studied more than average and scored more than average. Others studied less and scored less. Those students land in the same quadrant of the plot, and the cloud of points tilts upward.

Covariance is a single number that summarises that tilt. For every student, multiply the study deviation (hours minus average hours) by the score deviation (score minus average score). A student above average on both gives a positive product. A student below average on both also gives a positive product, because a negative times a negative is positive. Students above on one and below on the other give negative products. Average all the products, and the sign tells you the direction of the tilt.

012345650607080hours studiedexam score
  • students
Five students. The points rise from lower left to upper right, so the covariance of hours and score is positive. The worked example below computes it.

Why the variance is a special case

The variance from Mean and Variance asks how far one quantity is from its average. Covariance asks how two quantities are far from their averages together. Put the same quantity in both places and you get the variance back. So covariance generalises variance, and the two share the same rules.

The math

Definition

Definition: Covariance

For random variables XX and YY (described together by their joint distribution) with means μX=E[X]\mu_X = \mathbb{E}[X] and μY=E[Y]\mu_Y = \mathbb{E}[Y], the covariance is

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

Here E\mathbb{E} is the expected value, the probability-weighted average. The term X−μXX - \mu_X is how far XX sits from its mean, and likewise for YY. Their product is positive when both are on the same side of their means, and negative otherwise. Covariance is the average of that product.

A shortcut formula

Expand the product and use linearity of expectation:

  1. (X−μX)(Y−μY)=XY−μYX−μXY+μXμY(X - \mu_X)(Y - \mu_Y) = XY - \mu_Y X - \mu_X Y + \mu_X \mu_Y.
  2. Take expectations. The terms μYE[X]\mu_Y \mathbb{E}[X] and μXE[Y]\mu_X \mathbb{E}[Y] each equal μXμY\mu_X \mu_Y, and the last term is also μXμY\mu_X \mu_Y.
  3. Four terms, two cancel: Cov(X,Y)=E[XY]−μXμY\mathrm{Cov}(X, Y) = \mathbb{E}[XY] - \mu_X \mu_Y.

This form is often easier to compute. It says covariance is the average of the product minus the product of the averages. If the two were unrelated, those would be equal and the covariance would be zero.

Basic rules

Covariance has three properties that you use constantly.

  • Symmetry. Cov(X,Y)=Cov(Y,X)\mathrm{Cov}(X, Y) = \mathrm{Cov}(Y, X).
  • Variance as a special case. Cov(X,X)=Var(X)\mathrm{Cov}(X, X) = \mathrm{Var}(X).
  • Scaling and shifting. For constants a,b,c,da, b, c, d, Cov(aX+b,cY+d)=ac Cov(X,Y)\mathrm{Cov}(aX + b, cY + d) = ac\,\mathrm{Cov}(X, Y). Adding a constant does not change the covariance, because it shifts the mean by the same amount.

The most useful consequence is for sums. Expanding Var(X+Y)\mathrm{Var}(X + Y) gives

Var(X+Y)=Var(X)+Var(Y)+2 Cov(X,Y).\mathrm{Var}(X + Y) = \mathrm{Var}(X) + \mathrm{Var}(Y) + 2\,\mathrm{Cov}(X, Y).

If XX and YY are independent, the covariance is zero and the variances simply add. Correlated quantities can make the variance of their sum larger or smaller than that.

Independence and zero covariance

If XX and YY are independent, then E[XY]=E[X] E[Y]\mathbb{E}[XY] = \mathbb{E}[X]\,\mathbb{E}[Y], so Cov(X,Y)=0\mathrm{Cov}(X, Y) = 0. The reverse is false. Zero covariance only rules out a linear relationship. Take XX equally likely to be −1-1, 00 or 11, and let Y=X2Y = X^2. Then E[X]=0\mathbb{E}[X] = 0 and E[X3]=0\mathbb{E}[X^3] = 0, so Cov(X,Y)=E[X3]−E[X] E[X2]=0\mathrm{Cov}(X, Y) = \mathbb{E}[X^3] - \mathbb{E}[X]\,\mathbb{E}[X^2] = 0. Yet YY is completely determined by XX.

Worked examples

The first two examples use the probability definition. The third uses a data set, where the average is taken over the observed points and (as is standard for samples) divided by n−1n - 1 instead of nn.

Example
Two binary variables, by hand

Let XX and YY each take the values 0 or 1, with joint probabilities P(0,0)=0.4P(0,0) = 0.4, P(0,1)=0.1P(0,1) = 0.1, P(1,0)=0.1P(1,0) = 0.1 and P(1,1)=0.4P(1,1) = 0.4.

  1. Marginal means: P(X=1)=0.1+0.4=0.5P(X = 1) = 0.1 + 0.4 = 0.5, so μX=0.5\mu_X = 0.5. By symmetry μY=0.5\mu_Y = 0.5.
  2. Compute E[XY]\mathbb{E}[XY]. Only the outcome (1,1)(1,1) has XY=1XY = 1, so E[XY]=0.4\mathbb{E}[XY] = 0.4.
  3. Apply the shortcut: Cov(X,Y)=0.4−0.5×0.5=0.15\mathrm{Cov}(X, Y) = 0.4 - 0.5 \times 0.5 = 0.15.
  4. Check with the definition. The four products (X−0.5)(Y−0.5)(X - 0.5)(Y - 0.5) are 0.250.25, −0.25-0.25, −0.25-0.25 and 0.250.25, weighted by 0.40.4, 0.10.1, 0.10.1 and 0.40.4. The sum is 0.1−0.025−0.025+0.1=0.150.1 - 0.025 - 0.025 + 0.1 = 0.15.

Interpretation. The positive value says the two variables agree more often than they disagree. Both are 1 together more often than chance would give.

Example
A standard linear case

Let XX and ZZ be independent, each with variance 1 and mean 0. Define Y=2X+ZY = 2X + Z.

  1. Cov(X,Y)=Cov(X,2X+Z)=2 Cov(X,X)+Cov(X,Z)=2×1+0=2\mathrm{Cov}(X, Y) = \mathrm{Cov}(X, 2X + Z) = 2\,\mathrm{Cov}(X, X) + \mathrm{Cov}(X, Z) = 2 \times 1 + 0 = 2.
  2. Var(Y)=22×1+1=5\mathrm{Var}(Y) = 2^2 \times 1 + 1 = 5, since XX and ZZ are independent.
  3. Check with the sum rule. X+Y=3X+ZX + Y = 3X + Z, so Var(X+Y)=32×1+1=10\mathrm{Var}(X + Y) = 3^2 \times 1 + 1 = 10. The formula gives Var(X)+Var(Y)+2 Cov(X,Y)=1+5+2×2=10\mathrm{Var}(X) + \mathrm{Var}(Y) + 2\,\mathrm{Cov}(X, Y) = 1 + 5 + 2 \times 2 = 10. The two agree.

Interpretation. The positive covariance comes entirely from the 2X2X term that YY borrows from XX. The independent noise ZZ adds variance but no covariance.

Example
Study hours and exam scores

Use the five students from the plot. Hours x=1,2,3,4,5x = 1, 2, 3, 4, 5 and scores y=52,58,61,70,74y = 52, 58, 61, 70, 74.

  1. Means: xˉ=3\bar{x} = 3 and yˉ=315/5=63\bar{y} = 315 / 5 = 63.
  2. Deviations from the means: x−xˉ=−2,−1,0,1,2x - \bar{x} = -2, -1, 0, 1, 2 and y−yˉ=−11,−5,−2,7,11y - \bar{y} = -11, -5, -2, 7, 11.
  3. Products of deviations: 22,5,0,7,2222, 5, 0, 7, 22. Their sum is 56.
  4. Sample covariance, dividing by n−1=4n - 1 = 4: 56/4=1456 / 4 = 14.

Interpretation. The value 14 is positive, so more study goes with higher scores in this group. Its size is in units of hours times points, which is hard to read on its own. That is why the correlation coefficient, which rescales covariance to lie between −1-1 and 11, is often reported instead.

Tradeoffs and failure modes

SituationProblemBetter choice
Comparing association across variables with different unitsCovariance depends on the units, so 14 hours-points means nothing on its ownCorrelation coefficient, which divides out the spreads
Relationship is curved or non-monotoneCovariance can be zero even when the variables are strongly linked[[mutual-information
A few extreme pointsA single outlier can dominate the sum of productsRank-based measures such as Spearman's correlation, or robust estimates
Small sampleThe sample covariance varies a lot from sample to sampleReport a [[confidence-interval

The main failure is reading zero covariance as independence. It only says there is no straight-line tilt. Covariance is also not evidence of cause: two variables can rise together because both are driven by a third.

History

Covariance grew out of the study of correlation in the late nineteenth century. Francis Galton, working on heredity in the 1880s, looked at how the heights of parents and children moved together. Karl Pearson then developed this into the product-moment correlation in the 1890s, which is covariance divided by the two standard deviations. Covariance as a quantity in its own right came into common use because it is the form that adds up cleanly when variables are combined. It caught on as statistics moved from comparing single measurements to modelling several at once.

Check yourself

QuestionTwo variables are both above their means in most observations. What is the sign of their covariance, and why?Show answer

The covariance is positive. Each observation where both are above their means contributes a positive product of deviations. Observations where both are below their means also contribute positively. So the average of the products is positive.

QuestionFor XX and YY with Var(X)=4\mathrm{Var}(X) = 4, Var(Y)=9\mathrm{Var}(Y) = 9 and Cov(X,Y)=3\mathrm{Cov}(X, Y) = 3, what is Var(X+Y)\mathrm{Var}(X + Y)?Show answer

Var(X+Y)=4+9+2×3=19\mathrm{Var}(X + Y) = 4 + 9 + 2 \times 3 = 19. The covariance term is added twice because both orders of the product appear when the square is expanded.

QuestionIf XX and YY are independent, what is Cov(X,Y)\mathrm{Cov}(X, Y)? Explain why the converse does not hold.Show answer

It is zero, because independence gives E[XY]=E[X] E[Y]\mathbb{E}[XY] = \mathbb{E}[X]\,\mathbb{E}[Y]. The converse fails because covariance only measures linear association. In the example Y=X2Y = X^2 with XX equally likely to be −1-1, 00 or 11, the covariance is zero but YY depends on XX completely.

QuestionA researcher reports a covariance of 1200 between house size in square feet and price in dollars, and another of 0.8 between two standardized scores. Why can they not be compared directly, and what would you report instead?Show answer

Covariance depends on the units of both variables. Square feet times dollars is not on the same scale as a pure number from standardized scores, so the two values cannot be ranked by size. Report the correlation coefficient for each pair, which divides out the standard deviations and is unit-free, so the two associations can be compared.

Sources