# What is the covariance?

Computes the sample and population covariance of paired x and y values, with the means, the standard deviations and the correlation coefficient.

- Page: https://www.acalculator.org/statistics/covariance-calculator
- JSON spec: https://www.acalculator.org/statistics/covariance-calculator.json
- Version: a0ab5eeca270

## Default answer

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.

## Inputs

| Key | Label | Description |
| --- | --- | --- |
| x | x values | The first variable: one number per pair, in order. |
| y | y values | The second variable: one number per pair, in the same order as x. |

## Outputs

| Key | Label | Description |
| --- | --- | --- |
| sample | Sample covariance | Σ(x − x̄)(y − ȳ) ÷ (n − 1): use it when the pairs are a sample from a larger group. |
| population | Population covariance | Σ(x − x̄)(y − ȳ) ÷ n: use it when the pairs are the whole group. |
| direction | Direction | Positive when y tends to rise with x, negative when it falls, none when the covariance is 0. |
| n | Pairs (n) | How many (x, y) pairs there are. |
| meanX | Mean of x (x̄) | The sum of the x values divided by n. |
| meanY | Mean of y (ȳ) | The sum of the y values divided by n. |
| sdX | Sample SD of x | The sample standard deviation of x: √(Σ(x − x̄)² ÷ (n − 1)). |
| sdY | Sample SD of y | The sample standard deviation of y: √(Σ(y − ȳ)² ÷ (n − 1)). |
| r | Correlation r | The covariance divided by both standard deviations, from −1 to 1; left out when x or y never varies. |

## Method

sample cov = Σ(xᵢ − x̄)(yᵢ − ȳ) ÷ (n − 1); population cov = Σ(xᵢ − x̄)(yᵢ − ȳ) ÷ n; r = cov ÷ (sₓ s_y).

## Assumptions

- 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

1. x = 4 or 4.2, y = 2 or 2.1 gives sample = 0.0075, population = 0.006, n = 5, meanX = 4.1, meanY = 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.
2. x = 4 or 4.2, y = 0.6 or 0.59 gives sample = 0.00175, population = 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.
3. x = 2 or 2.1, y = 0.6 or 0.59 gives sample = 0.00135, population = 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.
4. x = 1 or 2, y = 6 or 4 gives sample = -2, population = -1.333333, 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.

## FAQ

### 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.

## Sources

- NIST/SEMATECH e-Handbook of Statistical Methods, §6.5.4.1 Mean Vector and Covariance Matrix (COV = Σ(Xᵢ − x̄)(Yᵢ − ȳ) ÷ (n − 1); five samples of three variables give covariances 0.0075, 0.00175 and 0.00135). https://www.itl.nist.gov/div898/handbook/pmc/section5/pmc541.htm (retrieved 2026-10-02)
