Skip to content

Covariance and correlation

Add up signed rectangles around the means to see whether two things rise together, then scale the sum into a correlation between −1 and 1.

Helps to know first
This topic

First, the picture

Do two things rise together? Five points answer it with rectangles. Watch each one draw its rectangle to the means.

Do two things rise together?

Five pairs of numbers: each point's rectangle to the means, signed by its quadrant, and their sum.

Five points. The dashed lines mark the mean of the first numbers, 3, and of the second numbers, 3.

means
3.0, 3.0
sum of signed areas
0.00
0.00 / 9.50 s
Describe this picture

Five points on a grid from 0 to 6, drawn to one scale across and up: (1,2)(1,2), (2,1)(2,1), (3,4)(3,4), (4,3)(4,3) and (5,5)(5,5). A dashed vertical line at the mean of the first numbers, 3, and a dashed horizontal line at the mean of the second numbers, 3, cut the plane into four quadrants. One point at a time, each point draws the rectangle between itself and the crossing of the dashed lines, and the caption gives its sides and area. A rectangle in the top-right or bottom-left quadrant is shaded in the accent colour and marked + as positive; one in the other two quadrants is shaded in a second colour and marked − as negative; the two points on a dashed line draw flat rectangles that add 0. A bar beside the plot, from −12 to 12, adds up the signed areas as they appear: 2, 4, 4, 4, then 8. Readouts give the means and the sum. When the clip has finished, any point can be dragged, or focused and moved with the arrow keys in steps of 0.5, and its rectangle, the dashed lines, the bar and the readouts follow it.

Rectangles around the means

Take five pairs of measurements, say a plant’s height and its number of leaves. Plot each pair as a point. Do the two numbers go up together?

Here is a way to measure it. Draw a line at the mean of each number; for these five points both means are 3. Every point now sits in one of four quadrants. A point to the top right is above average in both numbers. A point to the bottom left is below average in both. Both agree, so I count their rectangle as positive. A point to the top left or bottom right disagrees, so its rectangle counts as negative.

The rectangle’s sides are the point’s distances from the two means, so its area is the product of the two deviations, with its sign. For the point (5,5)(5,5) that is (5−3)(5−3)=4(5-3)(5-3)=4. For (1,2)(1,2) it is (1−3)(2−3)=2(1-3)(2-3)=2: both deviations are negative, and their product is positive. The points (3,4)(3,4) and (4,3)(4,3) sit on a dashed line, so one side of their rectangle is 0.

Add the five areas, 2+2+0+0+4=82+2+0+0+4=8. A large positive total says the points mostly agree: when one number is high, so is the other. A large negative total says they mostly disagree. A total near 0 says neither wins.

After the clip, drag a point into a disagreeing quadrant and watch the total drop.

Covariance

Write the points as (xk,yk)(x_k,y_k) for k=1,…,nk=1,\dots,n, with means xˉ\bar x and yˉ\bar y. The sum of the signed rectangles is

Sxy=∑k=1n(xk−xˉ)(yk−yˉ).S_{xy}=\sum_{k=1}^{n}(x_k-\bar x)(y_k-\bar y).

It grows with the number of points, so I divide by it. The average rectangle, Sxy/nS_{xy}/n, is the covariance of the data: 8/5=1.68/5=1.6 here. Some books divide by n−1=4n-1=4 instead and get 2.02.0; the reason belongs to estimation, and it does not change anything on this page.

For two random variables XX and YY, with means μX\mu_X and μY\mu_Y, the covariance is the expected rectangle:

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

The second form comes from multiplying out the brackets. The covariance of XX with itself is its variance, from Variance and standard deviation: every rectangle is then a square.

Correlation

Covariance has a problem: it depends on the units. Measure the heights in centimetres instead of metres and every rectangle gets 100 times wider, so the covariance is 100 times larger. The points have not changed at all.

So scale it. Divide by the spread of each number, and the units cancel. The result is the correlation. For data,

r=SxySxx Syy,r=\frac{S_{xy}}{\sqrt{S_{xx}\,S_{yy}}},

where Sxx=∑k(xk−xˉ)2S_{xx}=\sum_k(x_k-\bar x)^2 and Syy=∑k(yk−yˉ)2S_{yy}=\sum_k(y_k-\bar y)^2. For the five points, Sxx=4+1+0+1+4=10S_{xx}=4+1+0+1+4=10 and Syy=1+4+1+0+4=10S_{yy}=1+4+1+0+4=10, so r=8/100=0.8r=8/\sqrt{100}=0.8. For random variables,

ρXY=Cov{X,Y}σX σY.\rho_{XY}=\frac{\mathrm{Cov}\{X,Y\}}{\sigma_X\,\sigma_Y}.

The correlation always lies between −1-1 and 11. It is 11 when the points lie on a rising straight line, and −1-1 on a falling one. For y=2x+1y=2x+1, r=1r=1; for y=−3xy=-3x, r=−1r=-1. Changing units, centimetres to metres, leaves rr alone.

Why can it never pass 1? Collect the deviations into two lists: (−2,−1,0,1,2)(-2,-1,0,1,2) for the first numbers and (−1,−2,1,0,2)(-1,-2,1,0,2) for the second. Treat each list as an arrow with five entries. Then SxyS_{xy} is their dot product, and Sxx\sqrt{S_{xx}} and Syy\sqrt{S_{yy}} are their lengths. So rr is the cosine of the angle between the two arrows, and a cosine stays between −1-1 and 11. Here the angle is 36.87°, which is 0.6435 radians, and its cosine is 0.80.8.

Where this leads · angles between data

The cosine comes from Length and the dot product in the linear algebra course. A correlation of 0 means the two lists of deviations are at a right angle.

Correlation is not the slope of the line through the points. The best line through them rises by r sy/sxr\,s_y/s_x for each step in xx, where sxs_x and sys_y are the standard deviations of the xx and yy values. For the five points, the two spreads are equal, so the slope happens to be 0.80.8 too. If r=0.8r=0.8 but the spread of yy is twice the spread of xx, the slope is 1.61.6.

What correlation misses

Correlation measures only how close the points come to a straight line. Two variables can depend on each other completely and still have correlation 0. Let XX be one of −2,−1,0,1,2-2,-1,0,1,2, each with chance 15\tfrac15, and let Y=X2Y=X^2. Knowing XX tells you YY exactly. But the points (−2,4)(-2,4) and (2,4)(2,4) cancel, and so do (−1,1)(-1,1) and (1,1)(1,1): E{XY}=E{X3}=0\mathbb{E}\{XY\}=\mathbb{E}\{X^3\}=0 and E{X}=0\mathbb{E}\{X\}=0, so the covariance is 0. The points lie on a curve, not a line.

So zero correlation does not mean independent. The other way round does hold: independent variables have covariance 0, because the expected product of independent variables is the product of their means.

Correlation can also point the wrong way. Watch six points, first in one colour, then in two.

Two falling groups, one rising cloud

Six pairs of numbers, first as one group, then as the two groups they came from.

Six points rising from bottom left to top right: their correlation is 0.632.

correlation, all six
0.632
first group
–
second group
–
0.00 / 7.40 s
Describe this picture

Six points in one colour on a grid of first numbers 0 to 7 and second numbers 2 to 10, rising from bottom left to top right, with a readout of their correlation, 0.632. Then the points split into two groups by colour: (1,5)(1,5), (2,4)(2,4), (3,3)(3,3) keep the first colour and (4,9)(4,9), (5,8)(5,8), (6,7)(6,7) take a second. A line falls through each group, and a readout for each group shows a correlation of −1.000-1.000. The picture has no control.

All six points together have r=0.632r=0.632: they seem to rise together. Inside each group, they fall together perfectly. A third thing, the group, raises both numbers at once, and that makes the pooled cloud rise. Imagine the groups are two schools, xx is hours of homework and yy is a test score. In the pooled data more homework goes with a higher score, while inside each school more homework goes with a lower score.

This is why correlation is not cause. A correlation says the numbers move together in this data. It does not say that one moves the other, and a hidden third variable can create or reverse it.

Worked example

  1. Five points. Sxy=8S_{xy}=8, Sxx=Syy=10S_{xx}=S_{yy}=10, so r=8/100=0.8r=8/\sqrt{100}=0.8. The covariance is 1.61.6 dividing by n=5n=5, or 2.02.0 dividing by n−1=4n-1=4.
  2. Dependent but uncorrelated. XX equally likely to be −2,−1,0,1,2-2,-1,0,1,2 and Y=X2Y=X^2: covariance 0, though YY is fixed by XX.
  3. Two groups. The six points have Sxy=14S_{xy}=14, Sxx=17.5S_{xx}=17.5 and Syy=28S_{yy}=28, so r=14/490=0.6325r=14/\sqrt{490}=0.6325, although each group alone has r=−1r=-1.
  4. Lines. y=2x+1y=2x+1 has r=1r=1; y=−3xy=-3x has r=−1r=-1.
  5. Slope. With r=0.8r=0.8 and the spread of yy twice that of xx, the best line has slope 1.61.6, not 0.80.8.

Where you’ll meet this

An investor who holds two shares whose prices have a negative correlation tends to lose less on a bad day, because one tends to rise when the other falls. That is the idea behind spreading money over many investments.

A robot that combines two sensors, say a wheel counter and a camera, needs to know how their errors are correlated; errors that move together cannot cancel. And a data analyst choosing which measurements to feed a model often drops one of two measurements that are almost perfectly correlated, because the second adds little.

In signal processing · uncorrelated sinusoids

In Signals as sums of sinusoids, each arrow of a Fourier series is measured by multiplying the signal by one sinusoid and averaging. Two different harmonics multiplied and averaged over a period give 0: they are uncorrelated in the sense of this page, with the average over a period in place of the mean. That is why each one can be measured alone.

Reference card

QuantityFormulaNotes
Sum of rectanglesSxy=∑k(xk−xˉ)(yk−yˉ)S_{xy}=\sum_k(x_k-\bar x)(y_k-\bar y)positive: the numbers agree
CovarianceCov{X,Y}=E{(X−μX)(Y−μY)}\mathrm{Cov}\{X,Y\}=\mathbb{E}\{(X-\mu_X)(Y-\mu_Y)\}=E{XY}−μXμY=\mathbb{E}\{XY\}-\mu_X\mu_Y
Correlation, datar=Sxy/SxxSyyr=S_{xy}/\sqrt{S_{xx}S_{yy}}no units
CorrelationρXY=Cov{X,Y}/(σXσY)\rho_{XY}=\mathrm{Cov}\{X,Y\}/(\sigma_X\sigma_Y)between −1-1 and 11
As an angler=cos⁡θr=\cos\theta of the deviation lists0 means a right angle
Slope of the best liner sy/sxr\,s_y/s_xnot rr itself
Independentcovariance 0but 0 does not mean independent

Keep reading with a free account.

By creating an account you accept the terms of use.

Sign in

Already have an account? Use the service you signed up with.

Or keep reading: get new lessons by email

Are you 13 or over?

Accounts and email updates are for people aged 13 or over. We keep only your answer, never your date of birth.

End of the lesson

Where to go next.

Phasorium

Account

Sign inKeep your place on any device

Subjects

Site

Look