Understanding the covariance calculator — concept and principle
This calculator finds the covariance of two paired data sets, both the sample version (divide by n − 1) and the population version (divide by n), plus the direction of the relationship, the means and the Pearson correlation. Use it in statistics homework, finance (how two returns move together) or any study of paired measurements.
How it is calculated
Sample covariance = Σ(xᵢ − x̄)(yᵢ − ȳ) ÷ (n − 1)
Population covariance = Σ(xᵢ − x̄)(yᵢ − ȳ) ÷ n
Correlation r = covariance ÷ (sₓ × s_y)
- x̄ and ȳ are the means; n is the number of pairs; sₓ and s_y are the standard deviations.
- The two lists must have the same length and be in matching order.
The principle
For each pair, (x − x̄)(y − ȳ) is positive when both values are on the same side of their means and negative when they are on opposite sides. Averaging those products shows whether the variables tend to move together (positive), in opposite directions (negative), or not in a linear way (near zero). Dividing by n − 1 instead of n corrects for the fact that the means were estimated from the same data, making the sample covariance an unbiased estimate. Because covariance carries the units of both variables, its size is hard to judge; the correlation rescales it to between −1 and 1.
Worked example
Default inputs: X = 2, 4, 5, 7, 8, 10 and Y = 62, 68, 71, 80, 83, 91.
- Means: x̄ = 36 ÷ 6 = 6; ȳ = 455 ÷ 6 = 75.83333333; 6 pairs.
- Sum of cross-products Σ(x − x̄)(y − ȳ) = 155.
- Sample covariance = 155 ÷ 5 = 31.
- Population covariance = 155 ÷ 6 = 25.83333333.
- Correlation r = 0.9975532341: positive — X and Y tend to rise together.
How to read the result
The sign is the most useful part of a covariance: here X and Y rise together. The size, 31, is in units of X times units of Y, so it changes if you change units (for example from hours to minutes). To judge strength, look at r: 0.998 is an almost perfect straight-line relationship. A covariance near zero means no linear relationship, though a curved one may still exist.
Common mistakes
- Comparing covariances between data sets measured in different units.
- Mixing up the sample and population versions.
- Pairing values in the wrong order.
- Reading a strong covariance or correlation as proof of cause and effect.
Frequently asked questions
What is the difference between covariance and correlation?
Correlation is covariance divided by both standard deviations, so it has no units and lies between −1 and 1.
When do I use n − 1?
For a sample used to estimate a population; use n when your data are the whole population.
What does a negative covariance mean?
When one variable is above its mean, the other tends to be below its mean.
Sources: NIST/SEMATECH e-Handbook of Statistical Methods, §6.5.4.1 Mean Vector and Covariance Matrix