Written and maintained by CASRAI Editorial Board
Last updated
Covariance is a measure of how two variables vary together: it is positive when they tend to rise and fall together, negative when one tends to rise as the other falls, and near zero when there is no linear co-movement. The covariance formula averages the products of each pair’s deviations from the two means. This guide gives the sample and population versions, a worked example with the arithmetic shown, how to read the result, and code for Excel, R and Python.
What is covariance?
Covariance describes the joint variability of two variables measured on the same cases, for example height and weight of the same people, or two test scores from the same students. Microsoft’s documentation describes the sample covariance as “the average of the products of deviations for each data point pair in two data sets.” The covariance of a variable with itself is its variance, which is why a covariance matrix has variances on the diagonal and covariances off it. NumPy’s documentation states this directly: element Cij is the covariance of xi and xj, and Cii is the variance of xi.
Covariance formula for sample and population data
For paired observations (x1, y1), …, (xn, yn):
Sample covariance: s_xy = sum( (x_i - x_bar) * (y_i - y_bar) ) / (n - 1)
Population covariance: sigma_xy = sum( (x_i - mu_x) * (y_i - mu_y) ) / N
- xi, yi: the i-th pair of observations.
- x_bar, y_bar: the sample means of x and y; mu_x, mu_y are the population means.
- n: the number of pairs in the sample; N: the number of pairs in the whole population.
- n – 1: the divisor that makes the sample estimate unbiased. The R documentation says of its
covfunction: “The denominator n – 1 is used which gives an unbiased estimator of the (co)variance for i.i.d. observations.” NumPy’snumpy.covalso defaults to n – 1 and divides by N only when asked (bias=Trueorddof=0).
Worked example with the arithmetic shown
Take three pairs: x = 2, 4, 8 and y = 5, 11, 12.
- Means: x_bar = 14/3 = 4.6667 and y_bar = 28/3 = 9.3333.
- Deviations from the mean: for x, -2.6667, -0.6667, 3.3333; for y, -4.3333, 1.6667, 2.6667.
- Products of paired deviations: 11.5556, -1.1111, 8.8889.
- Sum of products: 19.3333.
- Sample covariance: 19.3333 / (3 – 1) = 9.6667.
- Population covariance (divide by 3): 19.3333 / 3 = 6.4444.
The sample value matches the figure Microsoft gives for =COVARIANCE.S({2,4,8},{5,11,12}), which is 9.666666667. The positive sign says that larger x values go with larger y values in these three pairs.
Interpretation: how to read a covariance
| Result | What it indicates |
|---|---|
| Positive | Above-average values of one variable tend to occur with above-average values of the other |
| Negative | Above-average values of one variable tend to occur with below-average values of the other |
| Close to zero | No consistent linear co-movement; this does not by itself show the variables are independent |
Covariance carries the units of x multiplied by the units of y, so its size depends on the scale of measurement and cannot be compared across different variable pairs. There are no universal thresholds for “large” or “small”. To compare strength of association, standardize it: the Pearson correlation is r = s_xy / (s_x * s_y). In the example, s_x = 3.0551, s_y = 3.7859 and r = 9.6667 / (3.0551 * 3.7859) = 0.836. For the SPSS workflow see our guide to Pearson correlation in SPSS.
Assumptions and common mistakes
- Mixing up sample and population divisors. Software defaults to n – 1 (R’s
cov, NumPy’scov); dividing by N gives a smaller number. Check which one a tool uses before comparing results. - Treating covariance as a standardized effect size. Rescaling a variable (metres to centimetres) rescales the covariance; the correlation does not change.
- Reading zero as independence. Covariance captures linear co-movement only.
- Orienting the data wrongly in NumPy. By default each row is a variable and observations are in columns (
rowvar=True); passrowvar=Falseif your variables are columns. - Missing values. In R,
covusesuse = "everything"by default, so missing values propagate; choose anotheruseoption such as"complete.obs"to drop them. In Excel, text, logical values and empty cells are ignored.
Covariance in research
Covariance is the building block of several methods. The correlation coefficient is covariance rescaled by the two standard deviations, covariance matrices underlie correlation matrices and principal component analysis, and the variance partitioning in analysis of variance rests on the same sums of squared and cross-product deviations.
How to calculate covariance in Excel, R or Python
Each snippet uses the worked example above, so each should return 9.6667.
Excel (sample covariance, values in A2:A4 and B2:B4):
=COVARIANCE.S(A2:A4, B2:B4)
Microsoft documents that mismatched array lengths return #N/A, and an empty range or a single data point returns #DIV/0!.
R:
x <- c(2, 4, 8)
y <- c(5, 11, 12)
cov(x, y) # sample covariance, 9.666667
cov(x, y) * (length(x) - 1) / length(x) # population covariance, 6.444444
Python (NumPy):
import numpy as np
x = [2, 4, 8]
y = [5, 11, 12]
np.cov(x, y)[0, 1] # sample covariance, 9.6667
np.cov(x, y, ddof=0)[0, 1] # population covariance, 6.4444
Frequently asked questions
What is covariance?
A statistic that measures how two variables vary together, computed as the average product of their deviations from their means.
What is the covariance formula?
Sample covariance is the sum of (x_i – x_bar)(y_i – y_bar) divided by n – 1; population covariance divides by N.
Why divide by n – 1?
R’s documentation states that n – 1 gives an unbiased estimator of the covariance for i.i.d. observations.
What is the difference between covariance and correlation?
Correlation is covariance divided by the product of the two standard deviations, which removes the units and bounds the value between -1 and 1.
Can covariance be negative?
Yes. A negative value means one variable tends to be above its mean when the other is below its own. NumPy’s documentation example, with x = (0, 1, 2) and y = (2, 1, 0), gives an off-diagonal covariance of -1.
Sources
- NumPy documentation, numpy.cov. https://numpy.org/doc/stable/reference/generated/numpy.cov.html (checked October 28, 2026)
- Microsoft Support, COVARIANCE.S function. https://support.microsoft.com/en-us/office/covariance-s-function-0a539b74-7371-42aa-a18f-1f5320314977 (checked October 28, 2026)
- R Documentation, stats::cor (var, cov and cor). https://stat.ethz.ch/R-manual/R-devel/library/stats/html/cor.html (checked October 28, 2026)








