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.
- students
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
For random variables and (described together by their joint distribution) with means and , the covariance is
Here is the expected value, the probability-weighted average. The term is how far sits from its mean, and likewise for . 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:
- .
- Take expectations. The terms and each equal , and the last term is also .
- Four terms, two cancel: .
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. .
- Variance as a special case. .
- Scaling and shifting. For constants , . 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 gives
If and 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 and are independent, then , so . The reverse is false. Zero covariance only rules out a linear relationship. Take equally likely to be , or , and let . Then and , so . Yet is completely determined by .
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 instead of .
Let and each take the values 0 or 1, with joint probabilities , , and .
- Marginal means: , so . By symmetry .
- Compute . Only the outcome has , so .
- Apply the shortcut: .
- Check with the definition. The four products are , , and , weighted by , , and . The sum is .
Interpretation. The positive value says the two variables agree more often than they disagree. Both are 1 together more often than chance would give.
Let and be independent, each with variance 1 and mean 0. Define .
- .
- , since and are independent.
- Check with the sum rule. , so . The formula gives . The two agree.
Interpretation. The positive covariance comes entirely from the term that borrows from . The independent noise adds variance but no covariance.
Use the five students from the plot. Hours and scores .
- Means: and .
- Deviations from the means: and .
- Products of deviations: . Their sum is 56.
- Sample covariance, dividing by : .
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 and , is often reported instead.
Tradeoffs and failure modes
| Situation | Problem | Better choice |
|---|---|---|
| Comparing association across variables with different units | Covariance depends on the units, so 14 hours-points means nothing on its own | Correlation coefficient, which divides out the spreads |
| Relationship is curved or non-monotone | Covariance can be zero even when the variables are strongly linked | [[mutual-information |
| A few extreme points | A single outlier can dominate the sum of products | Rank-based measures such as Spearman's correlation, or robust estimates |
| Small sample | The sample covariance varies a lot from sample to sample | Report 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 and with , and , what is ?Show answer
. The covariance term is added twice because both orders of the product appear when the square is expanded.
QuestionIf and are independent, what is ? Explain why the converse does not hold.Show answer
It is zero, because independence gives . The converse fails because covariance only measures linear association. In the example with equally likely to be , or , the covariance is zero but depends on 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
- Christopher Bishop, Pattern Recognition and Machine Learning (2006), the treatment of covariance in multivariate distributions.
- Covariance, Wikipedia.
- Pearson correlation coefficient, Wikipedia.