What is the covariance?
Type or paste two lists of numbers that pair up in order. The covariance calculator gives the sample covariance (divide by n − 1) and the population covariance (divide by n), with the means and the correlation.
- Sample covariance
- 0.0075
The sample covariance of the 5 pairs is 0.0075; the population covariance is 0.006.
- Population covariance
- 0.006
- Direction
- positive
- Pairs (n)
- 5
- Mean of x (x̄)
- 4.1
- Mean of y (ȳ)
- 2.08
- Sample SD of x
- 0.158113883
- Sample SD of y
- 0.08366600265
- Correlation r
- 0.566947
Sample covariance: 0.0075. The sample covariance of the 5 pairs is 0.0075; the population covariance is 0.006.
How to calculate
Computes the sample and population covariance of paired x and y values, with the means, the standard deviations and the correlation coefficient.
Example with the default inputs (x values [4, 4.2, 3.9, 4.3, 4.1], y values [2, 2.1, 2, 2.1, 2.2]): The sample covariance of the 5 pairs is 0.0075; the population covariance is 0.006.
Method: sample cov = Σ(xᵢ − x̄)(yᵢ − ȳ) ÷ (n − 1); population cov = Σ(xᵢ − x̄)(yᵢ − ȳ) ÷ n; r = cov ÷ (sₓ s_y).
- The x and y lists pair up in order: the first x with the first y, and so on.
- The sums are exact on the typed decimals, and each result is rounded once at the end.
- 2 to 10,000 pairs. With n = 2 the sample covariance divides by 1.
Worked examples
Each example is checked against the calculator on every build.
- x values 4, 4.2, 3.9, 4.3, 4.1, y values 2, 2.1, 2, 2.1, 2.2 gives Sample covariance 0.0075, Population covariance 0.006, Pairs (n) 5, Mean of x (x̄) 4.1, Mean of y (ȳ) 2.08, Direction positive.Source: NIST/SEMATECH e-Handbook of Statistical Methods, §6.5.4.1 Mean Vector and Covariance Matrix (COV = Σ(Xᵢ − x̄)(Yᵢ − ȳ) ÷ (n − 1); five samples with covariances 0.0075, 0.00175 and 0.00135). https://www.itl.nist.gov/div898/handbook/pmc/section5/pmc541.htm
- x values 4, 4.2, 3.9, 4.3, 4.1, y values 0.6, 0.59, 0.58, 0.62, 0.63 gives Sample covariance 0.00175, Population covariance 0.0014.Source: NIST/SEMATECH e-Handbook of Statistical Methods, §6.5.4.1 Mean Vector and Covariance Matrix (COV = Σ(Xᵢ − x̄)(Yᵢ − ȳ) ÷ (n − 1); five samples with covariances 0.0075, 0.00175 and 0.00135). https://www.itl.nist.gov/div898/handbook/pmc/section5/pmc541.htm
- x values 2, 2.1, 2, 2.1, 2.2, y values 0.6, 0.59, 0.58, 0.62, 0.63 gives Sample covariance 0.00135, Population covariance 0.00108.Source: NIST/SEMATECH e-Handbook of Statistical Methods, §6.5.4.1 Mean Vector and Covariance Matrix (COV = Σ(Xᵢ − x̄)(Yᵢ − ȳ) ÷ (n − 1); five samples with covariances 0.0075, 0.00175 and 0.00135). https://www.itl.nist.gov/div898/handbook/pmc/section5/pmc541.htm
- x values 1, 2, 3, y values 6, 4, 2 gives Sample covariance -2, Population covariance -1.333333, Correlation r -1, Direction negative.Source: NIST/SEMATECH e-Handbook of Statistical Methods, §6.5.4.1 Mean Vector and Covariance Matrix (COV = Σ(Xᵢ − x̄)(Yᵢ − ȳ) ÷ (n − 1); five samples with covariances 0.0075, 0.00175 and 0.00135). https://www.itl.nist.gov/div898/handbook/pmc/section5/pmc541.htm
How it works
For n pairs (x₁, y₁), …, (xₙ, yₙ) with means x̄ = Σx ÷ n and ȳ = Σy ÷ n:
- Sum of products: Sxy = Σ(xᵢ − x̄)(yᵢ − ȳ).
- Sample covariance: Sxy ÷ (n − 1).
- Population covariance: Sxy ÷ n.
- Sample standard deviations: sₓ = √(Sxx ÷ (n − 1)) with Sxx = Σ(xᵢ − x̄)², and s_y the same way.
- Correlation: r = Sxy ÷ √(Sxx × Syy), the same as the sample covariance ÷ (sₓ s_y).
The page reads each number exactly as typed (4.2 is 42/10) and works out Sxy, Sxx and Syy as exact fractions, using Sxy = (nΣxy − ΣxΣy) ÷ n. Each result is rounded to a double-precision number once, at the end. r is the square root of the exact fraction Sxy² ÷ (Sxx × Syy), with the sign of Sxy.
Rules
- 2 to 10,000 numbers in each list, and the same number in both; otherwise there is no answer.
- A sample covariance beyond the largest double-precision number has no answer. Any other value that large is left out.
- r is left out when every x is the same or every y is the same (Sxx or Syy is 0).
- Direction: "positive" when Sxy > 0, "negative" when Sxy < 0, and "none (0)" when Sxy = 0.
Output format. The covariances and standard deviations show 10 significant digits, r shows 6, and the means show up to 6 decimals. Each is a double-precision number.
Worked examples by hand
NIST variables 1 and 2. x = 4.0, 4.2, 3.9, 4.3, 4.1 (x̄ = 4.1) and y = 2.0, 2.1, 2.0, 2.1, 2.2 (ȳ = 2.08). The products of the differences are 0.008, 0.002, 0.016, 0.004 and 0, so Sxy = 0.03. Sample covariance 0.03 ÷ 4 = 0.0075; population covariance 0.03 ÷ 5 = 0.006.
NIST variables 1 and 3. y = 0.60, 0.59, 0.58, 0.62, 0.63 (ȳ = 0.604). Sxy = 0.0004 − 0.0014 + 0.0048 + 0.0032 + 0 = 0.007. Sample covariance 0.00175; population 0.0014.
NIST variables 2 and 3. Sxy = 0.00032 − 0.00028 + 0.00192 + 0.00032 + 0.00312 = 0.0054. Sample covariance 0.00135; population 0.00108.
x = 1, 2, 3 and y = 6, 4, 2. x̄ = 2, ȳ = 4. Sxy = (−1)(2) + (0)(0) + (1)(−2) = −4. Sample covariance −4 ÷ 2 = −2; population −4 ÷ 3 = −1.3333. The points lie on a falling line, so r = −1.
Other questions people ask
What is covariance?
A measure of how two variables move together. It is positive when y tends to be above its mean when x is above its mean, negative when y tends to be below, and near 0 when there is no straight-line link.
How do I calculate the sample covariance?
Subtract the mean from each x and each y, multiply each pair of differences, add the products, and divide by n − 1. For x = 1, 2, 3 and y = 6, 4, 2: (−1)(2) + (0)(0) + (1)(−2) = −4, and −4 ÷ 2 = −2.
When do I divide by n and when by n − 1?
Divide by n − 1 (sample covariance) when your pairs are a sample from a larger group; this is what NIST and most textbooks use. Divide by n (population covariance) when the pairs are the whole group you care about.
What is the difference between covariance and correlation?
Correlation is covariance divided by both standard deviations: r = cov ÷ (sₓ s_y). That removes the units, so r is always from −1 to 1, while the covariance is in x units times y units and has no fixed range.
Can covariance be negative?
Yes. A negative covariance means y tends to fall as x rises. x = 1, 2, 3 with y = 6, 4, 2 has a sample covariance of −2.
What does a covariance of 0 mean?
There is no straight-line link between x and y. They can still be related in another way, such as a curve.