Covariance Calculator

Covariance Calculator

Find out whether two variables move together. Enter paired X and Y values to get the sample or population covariance, plus the correlation that puts it on a −1 to 1 scale.

cov(X, Y) = Σ(x − x̄)(y − ȳ) ÷ (n − 1)

One value per observation.

In the same order as X, so each pair lines up.

Enter paired X and Y values to see their covariance

What is Covariance?

Covariance measures whether two variables tend to move in the same direction. It's positive when above-average X values go with above-average Y values, negative when they go with below-average Y values, and close to zero when there's no straight-line pattern.

The sign is easy to read; the size isn't. Covariance comes out in the product of the two units (hours × marks, say), so the same relationship gives a covariance 60 times bigger if you record time in minutes instead of hours. To judge strength, divide by both standard deviations to get the correlation coefficient, which always lies between −1 and 1.

Covariance still matters in its own right. Portfolio risk in finance is built from the covariances between assets, error propagation uses it when measurements are linked, and it's the raw ingredient of both correlation and regression slopes.

Covariance Formula

Definitions as given in the NIST/SEMATECH e-Handbook of Statistical Methods. See Sources.

1. Find each variable's deviations from its mean

(x − x̄) and (y − ȳ) for every pair

2. Multiply the pairs and add the products

A pair where both are above (or both below) their means gives a positive product. A pair split either side gives a negative one.

3. Divide by n − 1 (sample) or n (population)

Sample: s_xy = Σ(x − x̄)(y − ȳ) ÷ (n − 1)
Population: σ_xy = Σ(x − μx)(y − μy) ÷ N
Correlation: r = s_xy ÷ (s_x × s_y)

The covariance of a variable with itself is its variance: cov(X, X) = var(X).

Worked Example: Study Hours and Quiz Scores

Given:
Five students' study hours 1, 2, 3, 4, 5 and quiz scores 2, 4, 5, 4, 5.
Step 1:
Means and deviations:
x̄ = 3, ȳ = 4 → x − x̄: −2, −1, 0, 1, 2 · y − ȳ: −2, 0, 1, 0, 1
Step 2:
Multiply and add:
4 + 0 + 0 + 0 + 2 = 6
Step 3:
Divide, then standardise:
6 ÷ 4 = 1.5 · r = 1.5 ÷ (1.581 × 1.225) = 0.775
Result:
Sample covariance 1.5

Positive, so more study goes with higher scores. The 1.5 on its own says nothing about strength; the correlation of 0.77 says the link is fairly strong. The population covariance would be 6 ÷ 5 = 1.2.

How Units Change the Covariance

The worked example with study time recorded in different units. The covariance moves every time; the correlation never does.

Covariance and correlation after rescaling X
Study time inCovarianceCorrelation
Hours1.50.7746
Minutes900.7746
Days0.06250.7746

Common Covariance Mistakes

  • Judging strength from the size. A covariance of 90 isn't necessarily stronger than one of 0.9; it may just be in bigger units. Use the correlation for strength.
  • Misaligning the pairs. Each X must sit next to its own Y. Sorting one list but not the other destroys the relationship.
  • Reading zero as "unrelated". Covariance only detects straight-line patterns. Y = X² for X from −3 to 3 has zero covariance but a perfect curved relationship.
  • Mixing sample and population formulas. Divide by n − 1 for a sample (Excel COVARIANCE.S); COVARIANCE.P divides by n. The older COVAR function is the population version.

Covariance FAQs

What’s the difference between covariance and correlation?

Correlation is covariance divided by both standard deviations. That removes the units and puts the result between −1 and 1, so strengths can be compared.

Does positive covariance mean X causes Y?

No. It only says they move together. Both might be driven by something else, or the pattern might be chance.

Can covariance be negative?

Yes. Negative covariance means high values of one tend to go with low values of the other, like outdoor temperature and heating bills.

Is there a maximum covariance?

Its size can’t exceed √(var X × var Y). That limit is exactly what keeps correlation between −1 and 1.

Should I use the sample or population version?

Use the sample version (n − 1) when your pairs are a sample from something larger, which is almost always. Use n only when you have every member of the population.

Where is covariance used in finance?

Portfolio variance is built from the covariances between assets. Assets with negative covariance offset each other, which is the basis of diversification.

What is a covariance matrix?

A table of covariances between every pair of several variables, with the variances down the diagonal. It’s the starting point for principal component analysis and many multivariate methods.

How do I calculate covariance in Excel?

=COVARIANCE.S(x_range, y_range) for a sample or =COVARIANCE.P(x_range, y_range) for a population. Both ranges must be the same length.

Sources

Related Tools