COVARIANCE CALCULATOR

Covariance calculator

Two lists in, sample and population covariance out.

What is the covariance formula?

Sample covariance = Σ(xᵢ − x̄)(yᵢ − ȳ) ÷ (n − 1). Population covariance divides the same sum by n instead. Use the sample version when your pairs are a sample from a bigger group — the usual case.

Example

For x = 2, 4, 8 and y = 5, 11, 12: x̄ = 14/3 and ȳ = 28/3, the products of deviations add up to 58/3, and 58/3 ÷ 2 = 9.667 — the same answer as Excel’s COVARIANCE.S. For x = 3, 2, 4, 5, 6 and y = 9, 7, 12, 15, 17, the population covariance is 5.2 (Excel’s COVARIANCE.P).

What does the sign of the covariance mean?

Positive: the two variables tend to be above (or below) their means together. Negative: when one is above its mean, the other tends to be below. Near zero: no linear relationship — though there can still be a curved one.

How is covariance related to correlation?

Correlation is covariance scaled by both standard deviations: r = cov(x, y) ÷ (sₓ × s_y). Covariance carries the units of x times the units of y, so its size is hard to judge; r is always between −1 and 1.

Where do the formulas come from?

The sample covariance (divisor n − 1) is from the NIST/SEMATECH e-Handbook of Statistical Methods, section 6.5.4.1, whose covariance matrix example this calculator reproduces. The examples above are from Microsoft’s documentation for COVARIANCE.S and COVARIANCE.P.

Why does the covariance change when I change units?

Because it carries the units of x times the units of y. If the same scores are multiplied by 10, the sample covariance goes from 21.60 to 216.00, although the relationship has not changed. The correlation stays at 0.981 in both cases, which is why r is used to compare relationships.

Do I need the same number of x and y values?

Yes. Each x must be paired with the y measured on the same item, in the same order. If the lists have different lengths the pairs cannot be formed and the calculator reports an error.

Reading the intermediate values

Covariance starts from the deviations xᵢ − x̄ and yᵢ − ȳ. Each pair’s product is positive when both are on the same side of their means and negative when they are on opposite sides. The sum of the products divided by n − 1 (a sample) or n (a whole population) is the covariance. Because the sum can be dominated by a few large deviations, a single unusual pair can change the sign or size of the result, so it is worth checking the products column for outliers.

Worked example: hours of study and test score

Take six students with study hours 1, 2, 3, 4, 5, 6 and scores 52, 60, 61, 70, 74, 85. The means are 3.50 hours and 67.00 points. The products of the deviations add up to 108.00, so the sample covariance is 108.00 ÷ 5 = 21.60 hour-points and the population covariance is 18.00. The sample standard deviations are 1.871 hours and 11.764 points, so the correlation is r = 0.981. If a seventh student studied 7 hours and scored 30, the sample covariance drops to -0.50: a single unusual pair turns a strongly positive covariance negative.

Common mistakes

Using the population divisor n on a sample understates the covariance by a factor of (n − 1) ÷ n, a large difference for small samples; use n only when the data are the whole population. Reading the size of a covariance as the strength of a relationship is the other classic error, since the number depends on the units; compare r instead. And a covariance near zero does not mean the variables are unrelated: a symmetric U-shaped relationship can have a covariance of zero. Finally, covariance, like correlation, does not show that one variable causes the other.

Sources: NIST/SEMATECH e-Handbook of Statistical Methods, section 6.5.4.1 (covariance matrix; sample covariance with n − 1); Microsoft documentation for COVARIANCE.S and COVARIANCE.P for the sample and population definitions. Related: correlation coefficient calculator, linear regression calculator, standard deviation calculator and Spearman correlation calculator.