Skip to main content
v2026.11,858 entries · CC-BY 4.0

Covariance Formula: What Is Covariance, with Examples

Covariance measures how two variables vary together. This guide gives the sample and population formulas, a worked example, interpretation guidance and Excel, R and Python code.

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 cov function: “The denominator n – 1 is used which gives an unbiased estimator of the (co)variance for i.i.d. observations.” NumPy’s numpy.cov also defaults to n – 1 and divides by N only when asked (bias=True or ddof=0).

Worked example with the arithmetic shown

Take three pairs: x = 2, 4, 8 and y = 5, 11, 12.

  1. Means: x_bar = 14/3 = 4.6667 and y_bar = 28/3 = 9.3333.
  2. Deviations from the mean: for x, -2.6667, -0.6667, 3.3333; for y, -4.3333, 1.6667, 2.6667.
  3. Products of paired deviations: 11.5556, -1.1111, 8.8889.
  4. Sum of products: 19.3333.
  5. Sample covariance: 19.3333 / (3 – 1) = 9.6667.
  6. 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’s cov); 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); pass rowvar=False if your variables are columns.
  • Missing values. In R, cov uses use = "everything" by default, so missing values propagate; choose another use option 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)

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Covariance Formula: What Is Covariance, with Examples

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →

Ask CASRAI · Regulatory Radar

Research-admin question? Get an answer that links its sources.

An AI assistant specialized in research administration. Every answer links its sources to check before you act. 2 questions free, no account. $29/month after.

  • Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.
  • Every answer numbers its sources and links each one, so you can check the source yourself.