01
Covariance formula
sample: cov(X, Y) = Σ (x − x̄)(y − ȳ) ÷ (n − 1)
population: cov(X, Y) = Σ (x − x̄)(y − ȳ) ÷ n
x̄ and ȳ are the two means. Use the sample version when your pairs are a sample from something larger, which is the usual case.
02
Worked example
X = 2, 4, 6, 8, 10 and Y = 1, 3, 7, 9, 15. The means are 6 and 7.
| x − x̄ | y − ȳ | Product |
|---|---|---|
| −4 | −6 | 24 |
| −2 | −4 | 8 |
| 0 | 0 | 0 |
| 2 | 2 | 4 |
| 4 | 8 | 32 |
sum of products = 68; sample covariance = 68 ÷ 4 = 17
The population covariance is 68 ÷ 5 = 13.6, and r = 0.981.
03
Covariance vs correlation
The sign of the covariance tells you the direction of the relationship, but its size depends on the units: measuring height in centimetres instead of metres multiplies it by 100. Dividing by both standard deviations removes the units and gives the correlation coefficient, which always lies between −1 and 1.
04
Common mistakes with covariance
- Reading its size as strength. Multiplying every X by 100, as when metres become centimetres, turns 17 into 1,700, while r stays 0.981.
- Mixing up n and n − 1. With five pairs the population covariance (13.6) is 20% smaller than the sample covariance (17).
- Pairs out of order. Listing Y the other way round (15, 9, 7, 3, 1) keeps both means but turns the covariance into −17.
Trust
How this page is checked
- The calculation runs on code with automated tests against worked reference values. Every number in the examples on this page was produced by that same code, not typed by hand.
- The formulas and conventions used are stated on the page, so you can reproduce the result yourself or compare it with your own software.
- Last reviewed by the Sciloom team, Zebra Nine Studio Inc. It is a tool for research and teaching, not a substitute for a statistician, a validated lab method or medical advice.
Cite this page
Sciloom. (2026). Covariance calculator. Zebra Nine Studio Inc. https://sciloom.si/covariance-calculator
Frequently asked questions
What does a positive covariance mean?
Above-average values of one variable tend to go with above-average values of the other. A negative covariance means they tend to move in opposite directions.
Why divide by n − 1?
Because the means are estimated from the same data, dividing by n would underestimate the covariance on average. Dividing by n − 1 corrects that for a sample.
Does zero covariance mean the variables are independent?
No. It means there is no linear relationship. A curved relationship, such as a U shape, can give a covariance of zero.
What is the unit of covariance?
The product of the two units, for example cm × kg. That is why it is hard to interpret on its own and correlation is usually reported instead.