Covariance, Explained
After reading this you can compute the covariance between two paired variables by hand, tell sample from population covariance, and convert covariance into a correlation you can actually interpret.
What covariance measures
Covariance answers one question: when one variable is above its mean, does the other tend to be above its mean too? If yes, the covariance is positive. If one tends to be high while the other is low, the covariance is negative. If there is no consistent pattern, the covariance sits near zero.
Take heights and weights of 30 people. Taller people usually weigh more, so the covariance is positive. Take hours of daytime and heating bills across a year. More daylight tends to pair with lower bills, so the covariance is negative.
The sign is the honest part of covariance. The magnitude is harder to read, because it carries the units of both variables. Covariance between height in centimeters and weight in kilograms comes out in cm·kg. Switch height to meters and the number shrinks by a factor of 100, even though nothing about the relationship changed. That scale dependence is why you almost always report the correlation alongside it.
When to use it, and when not to
Use covariance when you have paired numeric measurements and you want a single number for the direction and joint spread of two variables. It is the building block for the correlation coefficient, for linear regression slopes, and for portfolio variance in finance.
Do not use covariance to compare the strength of two different relationships. A covariance of 500 is not stronger than a covariance of 5 unless the variables share the same units and scale. For strength, use the correlation coefficient, which is covariance rescaled to the range from -1 to +1.
Covariance detects only linear co-movement. Two variables can be tightly related and still have covariance near zero. If y = x^2 and x ranges symmetrically from -3 to 3, the covariance is exactly 0 even though y is fully determined by x. Always look at a scatter plot before trusting a single number.
The formula and the intuition
Population covariance uses N, the full count of pairs:
Here x_i and y_i are the paired values, \bar{x} and \bar{y} are the two means, and N is the number of pairs. Each term multiplies two deviations from the mean. When both deviations share a sign, the product is positive; when they differ, it is negative. The average of those products is the covariance.
Sample covariance divides by n - 1 instead of n:
The n - 1 divisor (Bessel's correction) corrects the bias that appears when you estimate a population from a sample. You spend one degree of freedom estimating each mean from the same data. For five pairs, dividing the same sum by 4 instead of 5 makes the sample covariance larger than the population covariance by a factor of 5/4 = 1.25.
Turning covariance into correlation
The Pearson correlation coefficient divides covariance by the product of the two standard deviations. This cancels the units and pins the result between -1 and +1.
Here s_x and s_y are the standard deviations of the two variables. Because both the covariance and the standard deviations use the same divisor, the choice of n or n - 1 cancels out. Sample and population versions give the exact same r. A value of +1 means the points fall on a rising straight line, -1 means a falling straight line, and 0 means no linear trend.
Worked example with the demo data
Use the demo values: x = 1, 2, 3, 4, 5 and y = 2, 1, 4, 3, 5. There are n = 5 pairs.
- Means: \bar{x} = 15/5 = 3 and \bar{y} = 15/5 = 3.
- Deviation products (x_i - 3)(y_i - 3):
(-2)(-1) = 2,(-1)(-2) = 2,(0)(1) = 0,(1)(0) = 0,(2)(2) = 4. - Sum of products:
2 + 2 + 0 + 0 + 4 = 8. - Population covariance:
8 / 5 = 1.6. - Sample covariance:
8 / 4 = 2.0.
Now the correlation. The sum of squared x-deviations is 4 + 1 + 0 + 1 + 4 = 10, and by symmetry the y-deviations give 1 + 4 + 1 + 0 + 4 = 10. Sample standard deviations are s_x = s_y = \sqrt{10/4} = 1.581.
The correlation is 0.8, a strong positive relationship. Notice the covariance changed with the divisor (1.6 versus 2.0) but r did not.
Watch covariance respond to a moved point
Covariance and correlation both react to where the points sit. The widget below lets you drag one point and watch both numbers move.
Reading the results
Read the two numbers together. The covariance gives the direction and the raw scale; the correlation gives the strength on a fixed ruler.
| |r| range | Interpretation | Example |
|---|---|---|
| 0.00 to 0.19 | Negligible | Shoe size vs test score |
| 0.20 to 0.39 | Weak | Age vs reaction time |
| 0.40 to 0.59 | Moderate | Study hours vs grade |
| 0.60 to 0.79 | Strong | Height vs weight |
| 0.80 to 1.00 | Very strong | Celsius vs Fahrenheit |
These bands are conventions, not laws. In physics an r of 0.9 may signal a problem; in psychology an r of 0.4 can be a solid finding. Judge against your field.
Common mistakes
The first mistake is treating a large covariance as a strong relationship. Change units and the number changes. Only correlation is unit free.
The second is confusing correlation with causation. The demo data has r = 0.8, but that says nothing about whether x drives y. A hidden third variable can produce a high correlation between two effects that never touch each other.
The third is misaligned pairs. Covariance requires x and y lists of equal length in matching order. If one column has a missing entry and you delete it from just one variable, every pair after it shifts and the result is meaningless.
A single outlier can dominate covariance because it multiplies two large deviations. If one point in the demo set were (20, 20), it alone would contribute (20 - 6)(20 - 6) = 196 to the sum, swamping the other four. Screen for outliers with the Outlier Detector before you trust the number.
Related tools
Covariance needs the means and standard deviations of both variables. Compute those, plus quartiles and skewness, with the Descriptive Statistics Calculator. To locate specific ranks in either variable, use the Percentile Calculator. For categorical data instead of paired numbers, the Shannon Entropy & Diversity Calculator measures spread across categories, and the Benford's Law Checker tests first-digit patterns in a single column.
Frequently asked questions
Should I use sample or population covariance?
Use sample covariance (divide by n - 1) when your data is a sample drawn from a larger group, which is the usual case. Use population covariance (divide by N) only when your data is the entire population. For the demo data the two are 2.0 and 1.6.
Why is my covariance a huge number?
Because covariance keeps the units of both variables multiplied together. Large-valued variables like incomes or populations produce large covariances. Convert to correlation, which is always between -1 and +1, to judge strength.
Can covariance be negative?
Yes. A negative covariance means that when one variable is above its mean, the other tends to be below its mean. For the demo data, swap y to 5, 4, 2, 1, 3 and the sum of products goes negative.
Does zero covariance mean the variables are independent?
No. Zero covariance rules out a linear relationship, not every relationship. The parabola case y = x^2 over symmetric x gives covariance 0 while y depends entirely on x.
How many data points do I need?
You need at least two pairs to compute sample covariance, and the standard deviations must both be nonzero for correlation to exist. With very small samples the number is unstable: a single point change can flip the sign, as the widget above shows.