Variance tells you how much one variable spreads out. Covariance tells you whether two variables spread out together: when one is above its average, does the other tend to be above its own average too? It is the raw ingredient behind both correlation and the regression slope, so understanding it makes those later ideas much easier to see through.
- How covariance is calculated from deviations around each mean
- What its sign means, and why its size is hard to interpret on its own
- How it turns into correlation and into the regression slope
- Why zero covariance does not mean the variables are unrelated
The Idea and the Formula
For each observation, measure how far x is from its mean and how far y is from its own mean, then multiply the two deviations. If both are above average, or both below, the product is positive. If one is above and the other below, the product is negative. Covariance is the average of these products. For a sample of n pairs, the sample covariance divides by n − 1 for the same reason the sample variance does (see unbiased estimators):
A useful special case: the covariance of a variable with itself is its variance, since the product of a deviation with itself is the squared deviation.
Worked Example
Problem: An ice-cream stand records the daily high temperature (°C) and the number of ice creams sold on five days. This is the same dataset used in simple linear regression.
| Temperature x | 18 | 22 | 25 | 28 | 30 |
|---|---|---|---|---|---|
| Sales y | 45 | 58 | 70 | 82 | 95 |
Step 1 — Means. x̄ = 123 ÷ 5 = 24.6 and ȳ = 350 ÷ 5 = 70.
Step 2 — Deviations and their products.
| x | y | x − x̄ | y − ȳ | Product |
|---|---|---|---|---|
| 18 | 45 | −6.6 | −25 | 165.0 |
| 22 | 58 | −2.6 | −12 | 31.2 |
| 25 | 70 | 0.4 | 0 | 0.0 |
| 28 | 82 | 3.4 | 12 | 40.8 |
| 30 | 95 | 5.4 | 25 | 135.0 |
Step 3 — Sum and divide. The products add to 372.0, so the sample covariance is 372.0 ÷ 4 = 93.0. (Dividing by n = 5 instead would give the population covariance, 74.4.) Every product is zero or positive, so the covariance is positive: hot days go with high sales.
Reading the Number
The sign is easy to read. Positive covariance means the variables tend to rise together, negative means one tends to rise as the other falls, and near zero means no consistent linear pattern. The size is much harder. A covariance of 93.0 is in units of degrees times ice creams, which has no intuitive meaning, and the value changes whenever the units do. Suppose temperature is recorded in Fahrenheit instead (F = 1.8C + 32). Every temperature deviation is multiplied by 1.8, so the covariance becomes 1.8 × 93.0 = 167.4, even though the relationship is identical. Because its size depends on units and on how spread out each variable is, covariance cannot tell you whether a relationship is strong.
From Covariance to Correlation
The fix is to rescale by the two standard deviations. Here var(x) = 91.2 ÷ 4 = 22.8, so sx ≈ 4.775, and var(y) = 1538 ÷ 4 = 384.5, so sy ≈ 19.609. Dividing:
This is the Pearson correlation: covariance with the units divided out, fixed between −1 and 1. In Fahrenheit, both the covariance (167.4) and the standard deviation of x are scaled by 1.8, so the ratio is unchanged and r is still 0.993. Correlation is covariance made unit-free, which is why it, not covariance, is used to describe strength.
From Covariance to the Regression Slope
Covariance also gives the least-squares slope directly: slope = cov(x, y) ÷ var(x) = 93.0 ÷ 22.8 ≈ 4.079 ice creams per degree. That is exactly the slope found in the regression article, since the (n − 1) factors cancel and the calculation reduces to Sxy ÷ Sxx. The slope answers "how much does y change per unit of x," a question covariance alone cannot.
Covariance and the Variance of a Sum
Covariance is what makes variances of combined quantities differ from simple addition:
Using the dataset, adding each day's temperature and sales gives a variance of 22.8 + 384.5 + 2(93.0) = 593.3, which matches a direct calculation on the summed values. Subtracting instead gives 22.8 + 384.5 − 186 = 221.3. When two quantities move together, their sum is more variable than independent quantities would be, and their difference is less variable. This is the mathematics behind diversification in investing: combining two assets whose returns have negative covariance shrinks the variance of the total, and combining assets that rise and fall together does not.
The Covariance Matrix
With more than two variables, the covariances of every pair are arranged in a covariance matrix, with the variances on the diagonal. For the two variables here it is:
| x | y | |
|---|---|---|
| x | 22.8 | 93.0 |
| y | 93.0 | 384.5 |
This matrix is the input to many multivariate methods, from multiple regression to dimension-reduction techniques used in statistics in machine learning.
Zero Covariance Is Not Independence
Independent variables always have zero covariance, but the reverse fails. Take x = −2, −1, 0, 1, 2 and y = x², so y is perfectly determined by x. The mean of x is 0, the mean of y is 2, and the products (x − 0)(y − 2) are (−2)(2), (−1)(−1), 0, (1)(−1), (2)(2) = −4, 1, 0, −1, 4, which sum to zero. The covariance is exactly 0 despite a perfect, deterministic relationship, because covariance detects only linear association. This is why a scatterplot should accompany any covariance or correlation, as stressed in exploratory data analysis.
Real-World Uses
Finance: portfolio risk depends on the covariances between asset returns, which is why two risky assets can together be safer than either alone.
Science and engineering: covariance matrices summarize how multiple measurements vary jointly, and they underlie techniques for combining noisy sensors.
Data analysis: nearly every correlation and every regression slope reported in practice is computed from covariances first.
Common Mistakes
Judging strength from the size of the covariance. A covariance of 500 is not necessarily a stronger relationship than one of 5, since the units and spreads differ. Use correlation for strength.
Mixing sample and population formulas. Dividing by n rather than n − 1 changes the value (74.4 vs. 93.0 here). Software packages differ in their defaults, so check which is used.
Assuming zero covariance means no relationship. It only rules out a linear one; curved relationships can hide behind a covariance of zero.
Ignoring outliers. A single extreme pair contributes a huge product to the sum and can dominate the result, in the same way influential points affect regression, as discussed in influential points.
What is the difference between covariance and correlation?
Can covariance be negative?
Does covariance work for ranked or non-linear data?
Is there a calculator for this?
Share this article
Written by
Statistics Fundamentals Editorial Team
Written and reviewed by our team of statistics educators. Content is aligned with GCSE, A-Level, AP Statistics, and undergraduate curricula.
About Statistics Fundamentals →