What Covariance Measures

Covariance is a number that tells you whether two variables move together in the same direction or opposite directions. If one variable tends to increase when the other increases, the covariance is positive. If one tends to increase when the other decreases, the covariance is negative. If they move independently, the covariance is close to zero.

Covariance is the foundation for correlation and regression analysis. It answers a straightforward question: do these two things track each other? For example, does study time and test score move together? Does temperature and ice cream sales move together? The covariance number itself is hard to interpret on its own—it depends on the units of your variables—but it tells you the direction and strength of the relationship before you move to more refined measures.

Key Takeaways

  • Covariance measures whether two variables move together; positive means they increase together, negative means one increases while the other decreases.
  • The formula is the average of the products of deviations: divide the sum of (each x value minus the mean of x) times (each y value minus the mean of y) by the number of data points.
  • Use n (the sample size) in the denominator for a sample, or n minus 1 for a sample covariance that estimates the population.
  • Covariance depends on the units of your variables, so a covariance of 50 in one unit system may be 5000 in another—use correlation if you need a standardized comparison.

The Covariance Formula and What Each Part Means

The formula for covariance is:

Cov(X, Y) = Σ[(xi − x̄)(yi − ȳ)] / n

Here is what each symbol means: xi is each individual x value, x̄ is the mean (average) of all x values, yi is each individual y value, ȳ is the mean of all y values, Σ means "add them all up", and n is the number of data points. The numerator—the part you add up—is the sum of each point's deviation from its mean, multiplied across the two variables. The denominator is how many points you have.

If you are working with a sample and want to estimate the population covariance, use n − 1 in the denominator instead of n. This is called Bessel's correction and gives you a less biased estimate. Most statistical software uses n − 1 by default for this reason.

Step-by-Step Calculation by Hand

Suppose you have five data points: x values are 2, 4, 6, 8, 10 and y values are 1, 3, 5, 7, 9. Follow these steps.

Step 1: Find the mean of x and the mean of y. The mean of x is (2 + 4 + 6 + 8 + 10) / 5 = 6. The mean of y is (1 + 3 + 5 + 7 + 9) / 5 = 5.

Step 2: For each data point, subtract the mean from the x value and the y value. For the first point: x₁ − x̄ = 2 − 6 = −4 and y₁ − ȳ = 1 − 5 = −4. For the second: x₂ − x̄ = 4 − 6 = −2 and y₂ − ȳ = 3 − 5 = −2. Continue for all five points.

Step 3: Multiply the deviations for each point. For point 1: (−4) × (−4) = 16. For point 2: (−2) × (−2) = 4. For point 3: (0) × (0) = 0. For point 4: (2) × (2) = 4. For point 5: (4) × (4) = 16.

Step 4: Add all the products. 16 + 4 + 0 + 4 + 16 = 40.

Step 5: Divide by n (or n − 1 for a sample). Using n = 5: Cov(X, Y) = 40 / 5 = 8. Using n − 1 = 4: Cov(X, Y) = 40 / 4 = 10. In this example, the covariance is positive and fairly large, which makes sense because both variables increase together in a linear pattern.

Using a Spreadsheet to Calculate Covariance

Most spreadsheet programs have built-in functions that do this work for you. In Microsoft Excel, use =COVARIANCE.S(array1, array2) for a sample or =COVARIANCE.P(array1, array2) for a population. In Google Sheets, use =COVARIANCE(array1, array2), which calculates the sample covariance by default.

To use these functions, enter your x values in one column and your y values in another. Then type the formula in an empty cell. For example, if your x values are in cells A2 through A10 and your y values are in B2 through B10, type =COVARIANCE.S(A2:A10, B2:B10) and press Enter. The spreadsheet will calculate the covariance when ready.

The .S version (sample) uses n − 1 in the denominator, while the .P version (population) uses n. Use .S unless you have data for an entire population, which is rare in practice.

Interpreting the Covariance Result

A positive covariance means the variables tend to move in the same direction. If study hours and test scores have a covariance of 12, students who study more tend to score higher. A negative covariance means they move in opposite directions. If temperature and heating costs have a covariance of −450, higher temperatures are associated with lower heating costs.

A covariance close to zero means the variables do not move together in any consistent pattern. This does not mean they are unrelated—it means they do not have a linear relationship.

The size of the covariance number is hard to interpret on its own because it depends on the units of your variables. A covariance of 100 might be weak if your variables are measured in thousands, or strong if they are measured in single digits. To compare relationships across different datasets or units, use correlation instead, which scales covariance to a standard range of −1 to 1.

Common Mistakes to Avoid

The most common error is forgetting to subtract the mean before multiplying. You must find the deviation (the difference from the mean) for each variable at each point, then multiply those deviations together. Multiplying the raw values will give you the wrong answer.

Another mistake is using n when you should use n − 1. If you are working with a sample of data and want to estimate the population covariance, always use n − 1. Using n will underestimate the true population covariance.

A third mistake is confusing covariance with correlation. Covariance tells you the direction and rough strength of a relationship, but its magnitude depends on the units. Correlation is a standardized version of covariance that always falls between −1 and 1, making it easier to compare across different datasets. If you need to compare the strength of relationships, calculate correlation instead.

When to Use Covariance in Real Work

Covariance is useful as a first step in exploring whether two variables are related. Before you build a regression model or run a statistical test, calculate the covariance to see if the variables move together. A covariance near zero suggests they may not have a linear relationship worth modeling.

Covariance also appears in the formulas for linear regression and correlation. When you fit a line to data or calculate a correlation coefficient, covariance is doing the work behind the scenes. Understanding how to calculate it helps you understand what those other tools are measuring.

In finance and portfolio analysis, covariance between asset returns tells you whether they move together or offset each other. Assets with negative covariance can balance a portfolio because when one loses value, the other tends to gain.

Frequently Asked Questions

What is the difference between covariance and correlation?

Covariance measures whether two variables move together, but its size depends on the units of the variables. Correlation is covariance divided by the standard deviations of both variables, which scales it to a standard range of −1 to 1. Use correlation when you want to compare the strength of relationships across different datasets or units.

Should I use n or n minus 1 in the denominator?

Use n − 1 if you are working with a sample of data and want to estimate the population covariance. Use n only if you have data for the entire population. In most real-world work, you have a sample, so n − 1 is the right choice.

Can covariance be negative?

Yes. A negative covariance means the variables move in opposite directions. When one increases, the other tends to decrease. For example, price and quantity demanded often have negative covariance—as price goes up, quantity demanded goes down.

What does a covariance of zero mean?

A covariance close to zero means the variables do not have a linear relationship. They may still be related in a non-linear way, or they may be independent. Covariance only measures linear association, so zero does not mean the variables are unrelated.

Can I calculate covariance with just two data points?

Technically yes, but it is not useful. With only two points, any two variables will have a perfect linear relationship, and the covariance will be large. You need at least 10 to 20 data points to get a meaningful sense of whether two variables actually move together or whether the pattern is just random noise.