Covariance measures how two variables move together

Covariance is a number that tells you whether two sets of data tend to increase or decrease together. If one variable goes up when the other goes up, covariance is positive. If one goes up when the other goes down, covariance is negative. If they move independently, covariance is close to zero.

The calculation itself is straightforward: you find how far each data point is from its average, multiply those distances for each pair, then average all those products. The result is a single number that describes the relationship between your two variables.

Covariance is used in finance to understand how stocks move relative to each other, in science to analyze relationships between measurements, and in machine learning to understand feature relationships. The formula is the same across all these fields.

Key Takeaways

  • Covariance is calculated by finding how far each data point is from its mean, multiplying paired distances, and averaging the results.
  • The formula differs slightly depending on whether you are working with a full population or a sample: divide by n for population covariance, by n−1 for sample covariance.
  • A positive covariance means variables move in the same direction; negative means they move opposite; near-zero means little relationship.
  • Covariance values are hard to compare across different data sets because the number depends on the scale of your variables.

The covariance formula and what each part means

The formula for sample covariance (the most common version) is:

Cov(X, Y) = Σ[(Xi − X̄)(Yi − Ȳ)] / (n − 1)

Here is what each symbol represents: X and Y are your two variables. Xi and Yi are individual data points. and Ȳ are the averages (means) of each variable. Σ means you add up all the products. n is the total number of data points. The n − 1 in the denominator is used when you are working with a sample rather than an entire population.

If you are working with a complete population rather than a sample, use n instead of n − 1 in the denominator. In practice, most real-world calculations use n − 1 because you are almost always working with a sample.

Step-by-step calculation with a real example

Suppose you have two variables: hours studied and exam score. Your data is:

StudentHours Studied (X)Exam Score (Y)
1265
2372
3478
4585

Step 1: Calculate the mean of each variable. For hours studied: (2 + 3 + 4 + 5) / 4 = 3.5. For exam scores: (65 + 72 + 78 + 85) / 4 = 75.

Step 2: Find the difference between each data point and its mean. For hours studied: 2 − 3.5 = −1.5; 3 − 3.5 = −0.5; 4 − 3.5 = 0.5; 5 − 3.5 = 1.5. For exam scores: 65 − 75 = −10; 72 − 75 = −3; 78 − 75 = 3; 85 − 75 = 10.

Step 3: Multiply each pair of differences. (−1.5)(−10) = 15; (−0.5)(−3) = 1.5; (0.5)(3) = 1.5; (1.5)(10) = 15. Sum: 15 + 1.5 + 1.5 + 15 = 33.

Step 4: Divide by n − 1. 33 / (4 − 1) = 33 / 3 = 11. The covariance is 11, which is positive, indicating that hours studied and exam scores move together.

Population covariance versus sample covariance

The difference between these two comes down to what data you have. Population covariance is used when you have data for every single member of the group you care about. Sample covariance is used when you have data for only some members and want to estimate the relationship for the whole group.

Population covariance divides by n. Sample covariance divides by n − 1. The n − 1 adjustment (called Bessel's correction) makes the sample estimate slightly larger to account for the fact that a sample tends to underestimate the true spread. In practice, unless you are working with a complete census, use n − 1.

Why covariance alone is not always useful

Covariance tells you the direction of a relationship, but the size of the number depends on the scale of your variables. If you measure temperature in Celsius versus Fahrenheit, the covariance changes even though the relationship is identical. This makes it hard to compare covariance values across different data sets.

For this reason, statisticians often use correlation instead, which is covariance divided by the standard deviations of both variables. Correlation always falls between −1 and 1, making it easier to interpret and compare. But covariance is still the foundation: correlation is built from it.

Computing covariance in spreadsheets and software

Most tools have built-in functions so you do not have to calculate by hand. In Microsoft Excel, use COVARIANCE.S() for sample covariance or COVARIANCE.P() for population covariance. In Google Sheets, the function is COVARIANCE() for sample covariance. In Python with NumPy, use numpy.cov(). In R, use cov().

To use these functions, you typically pass in two columns or arrays of numbers. The software handles the mean calculations, differences, products, and division automatically. This is much faster than hand calculation and eliminates arithmetic errors, especially with large data sets.

Common mistakes when calculating covariance

The most frequent error is using n instead of n − 1 when working with a sample. This produces a covariance that is slightly too small. Another common mistake is forgetting to subtract the mean before multiplying the differences. If you multiply the raw data points instead of their deviations from the mean, you get a meaningless number.

A third mistake is confusing covariance with correlation. Covariance can be any number; correlation is always between −1 and 1. If your covariance is 500, that does not mean the relationship is strong — it just means your variables are measured on a large scale. Check the correlation or the standard deviations of your variables to understand whether the relationship is actually strong.

Frequently Asked Questions

What does a covariance of zero mean?

A covariance near zero means the two variables have little to no linear relationship. When one goes up, the other does not consistently go up or down. This does not mean they are unrelated — they could have a curved or non-linear relationship — but they do not move together in a straight-line pattern.

Can covariance be negative?

Yes. Negative covariance means when one variable increases, the other tends to decrease. For example, the covariance between exercise frequency and resting heart rate is negative because more exercise usually leads to a lower resting heart rate. The sign tells you the direction; the size tells you the strength relative to your data's scale.

Why do you divide by n − 1 instead of n?

Dividing by n − 1 corrects for the fact that a sample tends to underestimate the true spread in a population. This adjustment is called Bessel's correction. Use n − 1 when your data is a sample from a larger group; use n only when you have the complete population.

Is covariance the same as correlation?

No. Covariance measures whether two variables move together, but its size depends on the scale of the variables. Correlation is covariance divided by the standard deviations of both variables, which standardizes it to a scale of −1 to 1. Correlation is easier to interpret and compare across different data sets.

What if I have more than two variables?

You can calculate covariance for any pair of variables. When you have many variables, you often create a covariance matrix, which shows the covariance between every pair. Each row and column represents one variable, and each cell shows the covariance between that pair. Spreadsheet software and statistical packages can build these automatically.