Skip to content
We use only essential cookies to keep you signed in and remember your preferences. See our Cookie Policy.
TestStop
MocksPracticeResourcesIndiaBlogPricing
Sign in
Resources › Derivations › Covariance
Derivation · Statistics

Covariance: what it is, where the formula comes from, and what it means

Covariance answers one question: when X is above its average, is Y usually above its average too? Everything else is bookkeeping. This page builds the formula from that question, one step at a time, draws it as areas you can click, proves the shortcut, and settles why zero covariance is weaker than independence. 5 past-paper questions at the end.

A question to hold on to
Three random variables are linked by y = 2w + 4u. The variance of w is 5 and the covariance of w and u is 1. What is the covariance of w and y: 14, 9, cannot be computed from the information given, or none of these?
JNU 2018Solved at the end ↓
New here?What is Ashanagar?The invented district behind this page's examples: 60 households, with incomes you can inspect.

Ashanagar is an invented district. The statistics lessons use it for every worked example, so you meet the same households again as the ideas build: first as a list of numbers, then as a spread, then as a population to sample from.

It has 60 households, each with a monthly income in rupees. Unless a page says otherwise, those 60 are the whole population, and a “sample” means a few of them drawn at random. Where a page needs two variables, it uses a smaller set of 12 households with the head’s years of schooling beside income.

The figures are illustrative, made up once and fixed. They are not official statistics.

Households
60
Mean income
₹28,783 a month
Median income
₹25,000 a month
Standard deviation
₹13,366
Lowest and highest
₹10,500 and ₹72,000

Meet the full data set →

SymbolsReading the maths on this pageΣ, x̄, E[ ] and friends: how to say them and what they tell you to do.

Tap a symbol.

In this guide
Derivation · about 40 minutes
  1. The question covariance answers
  2. The formula, derived
  3. The shortcut
  4. Shifting, scaling and adding
  5. Covariance inside a variance
  6. Independent against uncorrelated
  7. From covariance to correlation
  8. The mistakes examiners are fishing for
  9. The opening question, solved
  10. The page on one screen
  11. Check yourself
  12. Past papers, by level
  13. Quick answers
01
The idea

The question covariance answers

Two variables can be unrelated, can rise together, or can pull in opposite directions. Covariance is a number for which of the three it is, and by how much. It does not ask whether y is big, only whether y is bigger than usual when x is bigger than usual.

That wording hides a design. To say “bigger than usual” you must first fix what usual is, which is the average, and then measure every value by how far it sits from it. That distance is the deviation. Every step that follows is a decision about what to do with the deviations of x and of y.

Pause and predict
Households in Ashanagar with more years of schooling than average tend to earn more than the average income. Their covariance of schooling and income should be
02
Derivation

The formula, derived

The derivation below builds the definition in six steps. Open them one at a time. The fourth has a lab: each household’s deviations form a rectangle, and the covariance is the average of their signed areas.

Derive it

Where the covariance formula comes from

We want to show
The number that measures whether two variables move together is the average of the products of their deviations: Cov(X,Y)=n1​∑i=1n​(xi​−xˉ)(yi​−yˉ​).
  1. 1
    Start with the question, then ask what a number would have to do.

    Take 12 Ashanagar households and, for each, the head’s years of schooling (x) and the monthly income in thousands of rupees (y). We want one number that is positive when households with more schooling than average tend to have more income than average, negative when they tend to have less, and near zero when there is no such tendency. It should also get bigger as the tendency gets stronger.

  2. 2
    Measure every household from the middle.

    The averages are x̄ = 10.42 years and ȳ = ₹23 thousand. Subtract them to get each household’s deviations, how far it sits from the average on each variable:

    dx​=xi​−xˉ,dy​=yi​−yˉ​

    Household 1 has 5 years of schooling and an income of ₹15 thousand, so dx = 5 − 10.42 = −5.42 and dy = 15 − 23 = −8. It is below average on both.

  3. 3
    Adding deviations tells you nothing, so combine them in pairs.

    Add all the dx and you always get zero, because Σ(xi − x̄) = Σxi − n x̄ = 0. The deviations cancel by construction, so a sum cannot measure anything. What matters is whether the two deviations of the same household point the same way. Multiply them:

    dxdyProductMeaning
    +++above average on both
    −−+below average on both
    +−−they pull apart
    −+−they pull apart

    The sign of the product is exactly the “move together or apart” test, and a bigger deviation on either side makes the product bigger.

  4. 4
    See each product as a rectangle.

    Mark the point of means on the scatter. For each household draw the rectangle from that point to the household: its width is dx, its height dy, and its area is the product. Blue rectangles lie in the two quadrants where both deviations share a sign, red ones in the other two. Click through the households, then try the other two scatters.

  5. 5
    Average the rectangles.

    The total signed area is the sum of all the products, and dividing by n turns a total into an average, so the number does not grow just because there are more households. That gives the formula. For the 12 households the products add to 262, and

    Cov(X,Y)=12262​=21.83

    The units are years of schooling times thousands of rupees, so the size of the number depends on the units chosen. That is the reason the covariance is usually divided by both standard deviations to give the correlation.

    Say it in words
    “For each household, take how far its x is from the average x and how far its y is from the average y, multiply the two, and then average those products over all n households.”
  6. 6
    Two checks that the formula is the right one.
    • A variable with itself. Put y = x. Each rectangle becomes a square of area (xi − x̄)², and the average of those is the variance. So Cov(X, X) = Var(X): the variance is the covariance of a variable with itself.
    • Population or sample. Dividing by n gives the population form, 21.83 here. Dividing by n − 1 gives the sample form, 262 ÷ 11 = 23.82, which is the one to use when the 12 households are a sample from a bigger population. The correlation is unchanged, since the same divisor appears in the top and in both standard deviations.
Step 1 of 6
So
Covariance is the average signed area of the deviation rectangles. It is positive when the variables tend to sit on the same side of their averages, negative when on opposite sides, and zero when the blue and red areas cancel.
Covariance
Cov(X,Y)=n1​i=1∑n​(xi​−xˉ)(yi​−yˉ​)
Population form, dividing by n. Divide by n − 1 for a sample. Units: units of x times units of y.

For the twelve households the covariance is 21.83. Its sign says the pattern is positive. Its size, on its own, says very little, since it depends on the units. Dividing by the two standard deviations (3.33 years and ₹7.55 thousand) gives the correlation, r = 0.87, which can be judged against 1.

03
Derivation

The shortcut

The definition needs a deviation for every observation. When you are given a table of probabilities, or a handful of sums, there is a quicker route that gives the same number. It comes from opening the bracket.

Derive it

The shortcut: the mean of the products less the product of the means

We want to show
Cov(X,Y)=E[XY]−E[X]E[Y]
  1. 1
    Open the bracket.

    Write μX = E[X] and μY = E[Y]. The definition is Cov = E[(X − μX)(Y − μY)]. Multiply out the bracket, as in (a − b)(c − d) = ac − ad − bc + bd:

    (X−μX​)(Y−μY​)=XY−μY​X−μX​Y+μX​μY​
  2. 2
    Take the average of each piece.

    The average of a sum is the sum of the averages, and a constant comes out in front of an average. Here μX and μY are constants, since they are fixed numbers. So

    E[XY]−μY​E[X]−μX​E[Y]+μX​μY​=E[XY]−μY​μX​−μX​μY​+μX​μY​
  3. 3
    Cancel.

    Two of the three last terms are the same product with a minus sign, and the third is the same product with a plus. They leave one copy with a minus:

    Cov(X,Y)=E[XY]−μX​μY​
    Say it in words
    “The covariance is the average of X times Y, minus the average of X times the average of Y.”
  4. 4
    Check it on a past-paper table.

    JNU 2021 gives a joint table: X takes 1 or 3, Y takes −3, 2 or 4. The row totals give E[X] = 0.5 × 1 + 0.5 × 3 = 2, the column totals give E[Y] = (−3)(0.4) + (2)(0.3) + (4)(0.3) = 0.6, and multiplying each cell by its x and y gives E[XY] = 0. Then

    Cov=0−(2)(0.6)=−1.2

    That is the key, option D. The shortcut needs only one pass for E[XY] and the two means, instead of computing a deviation for every cell.

Step 1 of 4
So
The shortcut is the definition with the brackets opened. It is the covariance twin of Var(X) = E[X²] − (E[X])², and in fact reduces to it when Y = X.
The shortcut
Cov(X,Y)=E[XY]−E[X]E[Y]
The covariance twin of Var(X) = E[X²] − (E[X])².
04
Derivation

Shifting, scaling and adding

Most covariance questions in papers are not about computing from data. They give a few covariances and variances and ask for another one. Three rules, each easy to see from the rectangles, are enough.

Derive it

Shifting, scaling and adding: the rules of covariance

We want to show
Cov(aX+b,cY+d)=acCov(X,Y) and Cov(X+Z,Y)=Cov(X,Y)+Cov(Z,Y)
  1. 1
    A shift changes nothing.

    Adding a constant b to every x moves the average by b too, so xi + b − (x̄ + b) = xi − x̄. The deviations, and so every rectangle, are unchanged. Measuring income in rupees from zero or from some other base makes no difference.

  2. 2
    A scale multiplies.

    Multiplying every x by a multiplies every deviation by a, and so every rectangle’s width, so every area, by a. The same holds for c on the y side, which is why Cov(aX, cY) = ac Cov(X, Y). Change income from rupees to thousands of rupees, a factor of 1000, and the covariance changes by 1000 too.

  3. 3
    Covariance is linear in each argument.

    The deviation of a sum is the sum of the deviations: (x + z) − (x̄ + z̄) = (x − x̄) + (z − z̄). Multiply by (y − ȳ) and average, and the covariance splits in two:

    Cov(X+Z,Y)=Cov(X,Y)+Cov(Z,Y)

    The same holds in the second argument. A covariance of a variable with a constant is zero, since a constant has no deviations.

  4. 4
    Use the rules on a past-paper question.

    JNU 2018: y = 2w + 4u, var(w) = 5 and cov(w, u) = 1. What is cov(w, y)? Split it, then pull out the constants, then use Cov(w, w) = Var(w):

    Cov(w,2w+4u)=2Cov(w,w)+4Cov(w,u)=2(5)+4(1)=14

    The same splitting, applied to Cov(X + Y, X + Y), is how the variance of a sum, Var(X) + Var(Y) + 2 Cov(X, Y), is derived in the correlation lesson.

Step 1 of 4
So
A constant added to a variable is invisible to its covariances. A constant multiplied scales them. And a covariance of sums is the sum of the covariances, which makes every question about combinations of variables a matter of expanding brackets.
05
Where it matters most

Covariance inside a variance

The rules give the most-used result in the module, the variance of a combination. Expanding (aU + bV)² and averaging each term yields a cross term, and the cross term is a covariance. The derivation below, from the correlation lesson, also draws it as four areas.

Derive it

The variance of a combination of two variables

We want to show
Var(aX+bY)=a2σX2​+b2σY2​+2abCov(X,Y)
  1. 1
    Centre the combination.

    Write U = X − μX and V = Y − μY for the deviations from the means. The mean of aX + bY is aμX + bμY, so the deviation of the combination from its own mean is aU + bV.

  2. 2
    Square it.
    (aU+bV)2=a2U2+2abUV+b2V2

    This is the ordinary expansion of (p + q)². Nothing about statistics has happened yet.

  3. 3
    Average each term.

    The average of U² is σX² and of V² is σY², by the definition of variance, and the average of UV is Cov(X, Y), by the definition of covariance. That gives the formula. The cross term is there because the squared bracket has a middle term, and it carries a factor of 2 for the same reason.

  4. 4
    See it as areas.

    Draw a square of side aσX and another of side bσY, and put the two rectangles between them. Their areas are a²σX², b²σY² and two copies of abσXσY. The covariance is ρσXσY, so each rectangle is filled in only to the fraction ρ. Move ρ to 1 and the pieces make one big square, (aσX + bσY)². Move it to 0 and the rectangles vanish.

  5. 5
    Read off the special cases.
    • Independent variables have Cov = 0, so the variances just add: Var(X + Y) = σX² + σY².
    • A difference has b = −1, so the cross term changes sign but the variances still add: Var(X − Y) = σX² + σY² − 2 Cov. Never subtract the variances themselves.
    • Example (JNU 2013). With Var X = 2, Var Y = 4 and Cov = 2, Var(2X + 3Y) = 4 × 2 + 9 × 4 + 2 × 2 × 3 × 2 = 8 + 36 + 24 = 68.
Step 1 of 5
So
The cross term is what correlation adds to the picture. This one formula gives the standard error of a mean (the next module), the variance of a portfolio and the variance of a difference of two sample means.
06
Derivation

Independent against uncorrelated

Two words, “independent” and “uncorrelated”, are often used as if they meant the same thing. They do not, and papers ask about the gap. Uncorrelated means the covariance is zero. Independent means the whole joint distribution factors into the two separate ones.

Derive it

Independent means zero covariance, and zero covariance does not mean independent

We want to show
If X and Y are independent then Cov(X, Y) = 0. The reverse is false.
  1. 1
    Independence lets the probabilities factor.

    For independent discrete variables, P(X = x, Y = y) = P(X = x) P(Y = y) for every pair. The average of the product is the double sum over all pairs:

    E[XY]​=x∑​y∑​xyP(x)P(y)=(x∑​xP(x))(y∑​yP(y))=E[X]E[Y]​

    The double sum splits into a product because each x multiplies the whole sum over y. By the shortcut, Cov = E[XY] − E[X]E[Y] = 0.

  2. 2
    Now try to run it backwards, and fail.

    Let X be −1, 0 or 1, each with probability 1/3, and let Y = X². Y is completely decided by X, so they are as dependent as two variables can be. But E[X] = 0, E[XY] = E[X³] = (−1 + 0 + 1) ÷ 3 = 0, and so Cov = 0 − 0 × E[Y] = 0.

    The rectangles explain it: the points lie on a U shape, and for every point on the right arm there is a mirror point on the left arm with the opposite sign of dx but the same dy, so the areas cancel. Covariance sees only the straight-line part of a relationship.

  3. 3
    The same example, in a past paper.

    DSE 2012: X is uniform on [−1, 1]. Find Cov(X, X²). The same argument gives E[X] = 0 and E[X³] = 0, since X³ is an odd function and the interval is symmetric, so the covariance is 0 even though X² is a function of X.

Step 1 of 3
So
Independence is the stronger property. It rules out every kind of dependence, and zero covariance rules out only a linear one. The exception is the jointly normal pair, for which uncorrelated does imply independent.
The one-way street

Independent implies uncorrelated. Uncorrelated does not imply independent, except for a jointly normal pair.

07
Derivation

From covariance to correlation

A covariance of 21.83 is hard to read, because multiplying income by 1000 would make it 21,830. Dividing by both standard deviations cancels the units. That leaves a number that always lies between −1 and 1, and the next derivation shows why.

Derive it

Why r can never leave −1 to 1

We want to show
For any data, −1≤r≤1, and r is exactly 1 or −1 only when every point lies on a straight line.
  1. 1
    Put both variables on the same ruler.

    Standardise each variable: zx​=σX​x−xˉ​ and zy​=σY​y−yˉ​​. Two facts follow at once. By the definition of the standard deviation the average of zx² is 1, and so is the average of zy². And the correlation is the average product of the two z scores:

    r=σX​σY​Cov(X,Y)​=n1​∑zx​zy​
  2. 2
    Squares are never negative, so look at a gap.

    For each household the gap zx − zy squared is at least 0, so its average is too. Expand it:

    0≤(zx​−zy​)2​=zx2​​−2zx​zy​​+zy2​​=1−2r+1=2−2r

    So 2 − 2r ≥ 0, which says r ≤ 1. For the twelve households r = 0.869 and the average squared gap is 2 − 2 × 0.869 = 0.262.

  3. 3
    Do the same with the sum.
    0≤(zx​+zy​)2​=1+2r+1=2+2r

    So 2 + 2r ≥ 0, which says r ≥ −1. Both bounds come from nothing more than the fact that a square cannot be negative. The picture shows what the two averages measure: how far the standardised points stray from the line y = x and from the line y = −x.

  4. 4
    When is the bound reached?

    An average of squares is 0 only if every square is 0. So r = 1 means zx = zy for every household, which is the straight line y = ȳ + (σY ÷ σX)(x − x̄) with a positive slope. In the same way r = −1 forces zx = −zy, a straight line sloping down. Any scatter that is not exactly a straight line leaves some gap, and then r is strictly inside the interval.

Step 1 of 4
So
The average squared gap to y = x is 2 − 2r and to y = −x is 2 + 2r, so r is trapped between −1 and 1. The same fact is the Cauchy-Schwarz inequality, written for data.
Correlation
r=σX​σY​Cov(X,Y)​
The units cancel, so r is the same in rupees or in thousands.

One more link. The slope of the least-squares line of y on x is b = Cov(X, Y) ÷ Var(X), the covariance measured per unit of x’s variance. The regression primer derives it. The correlation lesson uses this page’s results in full, and the joint distributions lesson applies them to tables.

Say it in words
“The correlation is the covariance divided by the standard deviation of X times the standard deviation of Y.”
08
Exam focus

The mistakes examiners are fishing for

Watch out for

The mistakes examiners are fishing for

  • Believing zero covariance means independence. Only the reverse is true. Y = X² is the standard counterexample (DSE 2012).
  • Forgetting the 2 in Var(X + Y). The cross term is 2 Cov(X, Y), because the expanded square has a middle term that appears twice.
  • Subtracting variances for a difference. Var(X − Y) = Var(X) + Var(Y) − 2 Cov(X, Y). The variances still add.
  • Reading the size of a covariance as strength. It carries units. Only the correlation can be compared with 1.
  • Thinking a shift changes the covariance. Adding a constant changes nothing. Multiplying scales it.
  • Mixing n and n − 1 within one question. Population form for a population, sample form for a sample. The correlation does not care, but the covariance does.
09
Back to the puzzle

The opening question, solved

JNU 2018 · moderate
y = 2w + 4u, var(w) = 5 and cov(w, u) = 1. Find cov(w, y).
  1. 1
    Substitute for y
    cov(w, y) = cov(w, 2w + 4u).
  2. 2
    Split by linearity and pull the constants out
    = 2 cov(w, w) + 4 cov(w, u).
  3. 3
    Use cov(w, w) = var(w)
    = 2 × 5 + 4 × 1 = 14.
Answer
14. The tempting “cannot be computed” forgets that cov(w, w) is a variance, which is given.
DSE 2007 · moderate
E[X] = 3, E[Y] = 0, V[X] = 4, V[Y] = 25, E[XY] = 5. Find Cov, ρ and V[X + Y].
  1. 1
    Cov by the shortcut
    5 − 3 × 0 = 5.
  2. 2
    Correlation
    5 ÷ (2 × 5) = 0.5.
  3. 3
    Variance of the sum
    4 + 25 + 2 × 5 = 39.
Answer
Cov = 5, ρ = 0.5, V[X + Y] = 39.
DSE 2015 · moderate
Two balls are drawn without replacement from 1, 2, 3. X is the smaller and Y the larger. Find Cov(X, Y).
  1. 1
    List the three equally likely outcomes
    (X, Y) = (1, 2), (1, 3), (2, 3), each with probability 1/3.
  2. 2
    The means and E[XY]
    E[X] = 4/3, E[Y] = 8/3, E[XY] = (2 + 3 + 6) ÷ 3 = 11/3.
  3. 3
    The shortcut
    11/3 − (4/3)(8/3) = 33/9 − 32/9 = 1/9.
Answer
1/9.
10
Revision

The page on one screen

The lesson on one screen
What it measures
Whether two variables sit on the same side of their averages together. Positive, negative, or near zero.
Definition
The average of (x − x̄)(y − ȳ): the average signed area of the deviation rectangles.
Shortcut
E[XY] − E[X]E[Y], found by opening the bracket.
Rules
A shift changes nothing, a scale multiplies, and a sum splits. Cov(X, X) = Var(X).
Variance of a sum
Var(X + Y) = Var X + Var Y + 2 Cov. A difference has −2 Cov.
Independence
Independent implies Cov = 0. The reverse fails (Y = X²), except for a jointly normal pair.
Correlation
Cov ÷ (σXσY). Unit-free, between −1 and 1.
11
Practice

Check yourself

0 of 11 correct so far

Numbers

Use the definitions from this page. The first two use a tiny data set so every deviation can be done by hand.

From the definition
Three households have x = 1, 2, 3 and y = 2, 4, 9. What is the population covariance (dividing by n)?
Sample form
For the same three households, what is the sample covariance (dividing by n − 1)?
The shortcut
E[X] = 3, E[Y] = 4 and E[XY] = 14. What is Cov(X, Y)?
Splitting a covariance
y = 2w + 4u, var(w) = 5 and cov(w, u) = 1. Find cov(w, y).
A variance of a sum
V[X] = 4, V[Y] = 25 and Cov = 5. What is V[X + Y]?
To correlation
A sample has covariance 100, and variances 25 and 400. What is the correlation?

Concepts

1If every x is increased by 10 and every y is left alone, the covariance
2If X is measured in rupees and then converted to thousands of rupees, Cov(X, Y) is
3X and Y are independent. Then Cov(X, Y) is
4X is uniform on [−1, 1]. Cov(X, X²) is
5Cov(X, X) equals
12
Past papers

Past papers, by level

Foundation

Definitions, directions and one-step sums. Most CUET, UGC NET and Hyderabad questions sit here.

2 questionsPractise →

Standard

Classification traps, multi-step numericals and statement questions, up to the hardest JNU, JAM and GATE items.

3 questionsPractise →

Delhi School level

The Delhi School entrance asks this topic harder than any other exam. These questions are kept apart from the ordinary sets.

5 questionsTry them →
All 5 past-paper questions on covariance, easiest first: practise the full set →

✦These are questions from the TestStop bank. A free account answers 10 a day, each with its worked solution; All-Access opens every set with no daily limit.

13
Quick answers

Quick answers

What does covariance measure?

Covariance measures whether two variables tend to be above their averages together and below their averages together. It is the average, over all observations, of the product of each variable's deviation from its mean. It is positive when they move together, negative when they move in opposite directions, and near zero when there is no straight-line tendency.

What is the formula for covariance?

Cov(X, Y) = (1 ÷ n) Σ (xᵢ − x̄)(yᵢ − ȳ) for a population, or the same sum divided by n − 1 for a sample. An equivalent shortcut is Cov(X, Y) = E[XY] − E[X]E[Y], the mean of the products minus the product of the means.

Why do we divide by n − 1 for a sample covariance?

The deviations in a sample are measured from the sample mean, which is itself fitted to the data and uses up one degree of freedom, so dividing by n slightly understates the average. Dividing by n − 1 corrects this, just as for the sample variance. The correlation coefficient is the same either way.

Does zero covariance mean the variables are independent?

No. Independent variables always have zero covariance, but zero covariance only rules out a straight-line relationship. If X is symmetric about zero and Y = X², then Y is completely determined by X, yet the covariance is zero. The exception is a pair of jointly normal variables, for which zero covariance does imply independence.

What is the difference between covariance and correlation?

Correlation is the covariance divided by the product of the two standard deviations. This removes the units, so the correlation always lies between −1 and 1 and does not change when you rescale a variable, while the covariance changes with the units.

How is variance related to covariance?

The variance of a variable is its covariance with itself: Cov(X, X) = Var(X). In general, Var(X + Y) = Var(X) + Var(Y) + 2 Cov(X, Y), which is why variances add only when the covariance is zero.

Read next

All resources →
StatisticsStatistics 6: covariance, correlation and rank correlationScatter plots, covariance as an average of products, Pearson's r and what leaves it unchanged, the variance of sums, Anscombe's quartet, why zero correlation is not independence, Spearman's rank correlation and the probable error.Complete guide · 50 minStatisticsInference 1: joint distributions, marginals, covariance and independenceThe joint table and density, the normalising constant, marginal and conditional distributions, expectation of a function of two variables, covariance, why zero covariance is not independence, and the minimum-variance portfolio.Complete guide · 50 minStatisticsThe binomial and Poisson distributions, derivedThe binomial by counting sequences of independent trials, its mean and variance from indicators, and the Poisson as the limit of the binomial, with a sequence counter, a binomial-to-Poisson lab, and the mean and variance worked out.Derivation · 40 min