Statistics
How To Calculate The Covariance
Covariance measures whether two variables move together, and in which direction. A positive value means they tend to rise and fall in step; a negative value means one tends to rise as the other falls. This page works through the population form using five fixed pairs of observations, giving a result you can reproduce with nothing more than a pencil.
Last updated
Quick Answer
Cov(X,Y) = Sum((xi - mean x)(yi - mean y)) / n
- Cov(X,Y)
- The covariance between variables X and Y
- xi, yi
- The two values in the i-th paired observation
- mean x, mean y
- The arithmetic average of each variable separately
- n
- The number of paired observations, which is five here
Find the mean of each variable, subtract each mean from its own observations, multiply the two deviations for every pair, add those products and divide by the number of pairs. For the five pairs (1,2), (2,4), (3,5), (4,4) and (5,5) the means are 3 and 4, the cross-products total 6, and the population covariance is 1.2. Dividing by n minus one instead gives the sample covariance of 1.5.
What Is The Covariance?
Covariance is a measure of joint movement. It asks a simple question of two variables: when one is above its own average, is the other usually above its average too? The answer is a single number whose sign carries the direction of the relationship. A positive covariance means the two tend to rise together and fall together; a negative one means they tend to move in opposite directions. A value of zero says there is no consistent linear tendency in either direction, although it does not prove the variables are unrelated in every possible way. Because it is computed from paired observations, covariance only makes sense when each x value is naturally matched with one y value, such as a height and a weight measured on the same person.
The sign is the first thing to read, and it is easy to reason about once you picture the deviations. Subtract the mean of x from each x and the mean of y from each y, then look at the signs of the two deviations for each pair. A pair sitting above both means produces a positive product. A pair sitting below both means also produces a positive product, because two negatives multiply to a positive. Pairs where one variable is above its mean and the other below give a negative product. The covariance is essentially the average of those products, so a preponderance of same-side pairs drives it positive and a preponderance of opposite-side pairs drives it negative.
That construction explains why the means come first. Multiplying raw x and y values would mostly measure how far the variables sit from zero, which is an artefact of the chosen units rather than a statement about co-movement. Centring each variable on its own mean removes that artefact: what remains is purely about how the two fluctuate around their typical levels. The sum of centred products is often called the cross-product sum, and it plays the same role here that the sum of squared deviations plays in variance. Indeed, if you set y equal to x, every cross-product becomes a square and covariance becomes variance. This family resemblance is why the two ideas are usually taught together.
The five pairs on this page make the arithmetic unusually clean. The x values are 1, 2, 3, 4 and 5, so their mean is 3; the y values are 2, 4, 5, 4 and 5, so their mean is 4. Subtracting those means gives x deviations of minus 2, minus 1, zero, 1 and 2, and y deviations of minus 2, zero, 1, zero and 1. Multiplying pair by pair gives 4, 0, 0, 0 and 2, which sum to 6. Divide that 6 by five pairs and the population covariance is 1.2. The negative and positive contributions here are not perfectly balanced, so the result is positive, which agrees with the visual impression that y generally climbs as x climbs.
There are two conventions for the divisor, and the difference matters at small counts. Dividing by n describes the data you actually have, treating the five pairs as the entire population of interest. Dividing by n minus one estimates the covariance of a larger population from which those pairs were sampled, using the same bias correction that appears in sample variance. For these numbers that means dividing 6 by 5 to get 1.2, or by 4 to get 1.5. The sample answer is the larger one, because the smaller divisor inflates the estimate. The gap is substantial with only five observations and shrinks steadily as the sample grows, but you still need to state which convention you used.
A serious limitation is that covariance is scale dependent. If you multiply every x by ten, every deviation in x is multiplied by ten and so is the covariance; if you switch from metres to centimetres, the number changes by a factor of a hundred even though nothing about the relationship has changed. Adding a constant, by contrast, changes nothing: shifting every value up by one moves both means up by one but leaves every deviation untouched. It is this second property, translation invariance, that makes the shifted example on this page return exactly the same 1.2. Because the units of covariance are the product of the two variables' units, a single covariance figure is awkward to compare across datasets measured in different ways.
Scale dependence is precisely why the correlation coefficient exists. Dividing the covariance by the product of the two standard deviations removes the units and squeezes the answer into the range from minus one to one. That standardised number measures the strength of the linear relationship, while covariance retains the original scale and is therefore the quantity that feeds directly into further algebra. In portfolio mathematics, for instance, the variance of a weighted combination of assets depends on the covariances between every pair of assets, not on their correlations alone. In signal processing and error analysis, covariance matrices collect the joint variability of many channels and are manipulated as whole objects.
Changing units scales a covariance by the product of the two scaling factors, which is worth stating explicitly because it is easy to get the direction wrong. Doubling x doubles the covariance; doubling x and y together quadruples it. Multiplying either variable by a negative number flips the sign, so a coding choice such as measuring a temperature as a deficit rather than an excess can reverse the apparent direction. None of this indicates a change in the underlying relationship. It is simply a consequence of the units, and it is the main reason a covariance should never be quoted without saying what the variables were measured in.
Finally, covariance describes linear co-movement only, and it is sensitive to unusual observations because a single extreme pair can contribute a very large cross-product. It also says nothing about causation: two quantities can co-vary because both respond to a third factor, or because of pure coincidence over a short run. Used carefully, it is an honest summary of joint variation and an essential building block for variance of sums, correlation and regression. Used carelessly, it invites confident readings of strength that it cannot support. Read the sign, check the units, inspect the pairs, and standardise the figure before making comparisons.
Formula
Cov(X,Y) = Sum((xi - meanX)(yi - meanY)) / N
Formula page →Use when the paired observations are the entire group of interest. Centre each variable on its own mean, multiply the deviations pair by pair, sum them and divide by the total count.
| Symbol | Meaning | Unit | Notes |
|---|---|---|---|
| Cov(X,Y) | Population covariance | x units times y units | Measured in the product of the two variables' units; 1.2 for the five pairs used here. |
| muX, muY | The two population means | same as each variable | Computed separately for each variable; 3 and 4 in the worked data. |
| N | Number of paired observations | count | Five here, so the cross-product sum of 6 is divided by 5. |
sXY = Sum((xi - x-bar)(yi - y-bar)) / (n - 1)
Formula page →Use when the pairs are a sample from a larger population. Dividing by n minus one corrects the tendency of deviations measured around a fitted sample mean to be too small.
| Symbol | Meaning | Unit | Notes |
|---|---|---|---|
| sXY | Sample covariance | x units times y units | An unbiased estimate of the population covariance; 1.5 for the worked pairs. |
| x-bar, y-bar | The two sample means | same as each variable | Averages calculated from the sampled pairs themselves. |
| n | Sample size | count | With five pairs the divisor becomes four, giving a larger answer than the population form. |
r = Cov(X,Y) / (sX sY)
Formula page →Dividing by both standard deviations removes the units and produces a dimensionless measure of linear association between minus one and one.
| Symbol | Meaning | Unit | Notes |
|---|---|---|---|
| r | Correlation coefficient | dimensionless | Standardised covariance, comparable across datasets with different units. |
| Cov(X,Y) | Covariance of the two variables | x units times y units | The unstandardised joint variation in the numerator. |
| sX, sY | Standard deviations of each variable | same as each variable | Their product rescales covariance back to a pure number. |
How To Calculate The Covariance
- 1
Pair the observations and count them
Write the data as ordered pairs so it is clear which y belongs to which x. Here there are five pairs: (1,2), (2,4), (3,5), (4,4) and (5,5). Both lists must have the same length, and reordering one list without the other will silently change the answer.
- 2
Find the mean of each variable
Add the x values and divide by five: (1 + 2 + 3 + 4 + 5) / 5 = 3. Do the same for y: (2 + 4 + 5 + 4 + 5) / 5 = 4. Each variable gets its own mean, and the two are computed independently rather than from the pairs as a group.
- 3
Centre each value and multiply within each pair
Subtract 3 from every x and 4 from every y, giving x deviations of -2, -1, 0, 1, 2 and y deviations of -2, 0, 1, 0, 1. Multiply the two deviations for each pair: 4, 0, 0, 0 and 2. Same-side pairs contribute positively and opposite-side pairs negatively.
- 4
Add the cross-products
Sum the five products: 4 + 0 + 0 + 0 + 2 = 6. This cross-product sum is the numerator shared by both the population and the sample formula. A quick sanity check is that it should be positive when most pairs sit on the same side of their respective means, as they do here.
- 5
Divide by n or by n minus one
For the population divide 6 by 5 to obtain a covariance of 1.2. For a sample divide by 4 instead and obtain 1.5, which is larger because the divisor is smaller. State which convention you used, and remember that the result carries the multiplied units of the two original variables.
Examples
Example 1: The five default pairs as a population
- x1
- 1
- y1
- 2
- x2
- 2
- y2
- 4
- x3
- 3
- y3
- 5
- x4
- 4
- y4
- 4
- x5
- 5
- y5
- 5
| Step | Calculation | Result |
|---|---|---|
| Means | (1+2+3+4+5)/5 and (2+4+5+4+5)/5 | 3 and 4 |
| Deviations | x: -2,-1,0,1,2; y: -2,0,1,0,1 | sum zero for each variable |
| Cross-products | (-2)(-2), (-1)(0), (0)(1), (1)(0), (2)(1) | 4, 0, 0, 0, 2 |
| Cross-product sum | 4 + 0 + 0 + 0 + 2 | 6 |
| Population covariance | 6 / 5 | 1.2 |
Result: The population covariance of the five default pairs is 1.2, a positive figure indicating that the two variables tend to rise together.
Example 2: The same pairs treated as a sample
- x1
- 1
- y1
- 2
- x2
- 2
- y2
- 4
- x3
- 3
- y3
- 5
- x4
- 4
- y4
- 4
- x5
- 5
- y5
- 5
| Step | Calculation | Result |
|---|---|---|
| Sample means | 15 / 5 and 20 / 5 | 3 and 4 |
| Cross-product sum | 4 + 0 + 0 + 0 + 2 | 6 |
| Sample divisor | n - 1 = 5 - 1 | 4 |
| Sample covariance | 6 / 4 | 1.5 |
Result: Dividing by n minus one gives a sample covariance of 1.5, which is larger than the population value of 1.2 because four is a smaller divisor than five.
Example 3: Every pair shifted up by one
- x1
- 2
- y1
- 3
- x2
- 3
- y2
- 5
- x3
- 4
- y3
- 6
- x4
- 5
- y4
- 5
- x5
- 6
- y5
- 6
| Step | Calculation | Result |
|---|---|---|
| New means | (2+3+4+5+6)/5 and (3+5+6+5+6)/5 | 4 and 5 |
| Deviations unchanged | x: -2,-1,0,1,2; y: -2,0,1,0,1 | same as before |
| Cross-product sum | 4 + 0 + 0 + 0 + 2 | 6 |
| Population covariance | 6 / 5 | 1.2 |
Result: Adding one to every value moves both means up by one but leaves the population covariance at 1.2, which demonstrates translation invariance.
Calculator
Population covariance
1.2
- Sample covariance
- 1.5
- Mean of X
- 3
- Mean of Y
- 4
Values update as you type. This calculator covers the single scenario its formula assumes — see Common Mistakes for what it leaves out.
Prefer a full-width tool? Open the The Covariance calculator page.
Common Mistakes
Using the wrong divisor
Dividing by n when the pairs are a sample understates the population covariance, while dividing by n minus one for a complete population inflates it for no reason. With five pairs the two answers are 1.2 and 1.5, far apart enough that the choice must be made deliberately.
Multiplying raw values instead of deviations
Covariance compares each value with its own variable's mean. Multiplying x and y directly and averaging the products measures something quite different and depends heavily on where zero sits. Always subtract the two means before forming the cross-products.
Reading the size as a strength without checking units
A covariance of 1.2 is not weak or strong in any absolute sense, because the number scales with the units of both variables. Rescaling one variable changes the figure even though the relationship has not changed. Standardise to a correlation if you need a comparable strength.
Assuming the sign proves causation
A positive covariance only says the pairs tend to sit on the same side of their means. Both variables may be driven by a third factor, or the pattern may be a short-run coincidence. Nothing in the arithmetic establishes that one variable produces changes in the other.
Reordering one list but not the other
Covariance depends on which y is paired with which x. Sorting one column and leaving the other in its original order destroys the pairing and can easily flip the sign. Keep the pairs together as units from the moment you collect the data.
Forgetting that nonlinear relationships can give zero
A covariance near zero rules out a consistent linear tendency, not all relationships. A symmetric curve such as a parabola can produce a covariance of zero while the variables are perfectly related in a nonlinear way. Plot the pairs before concluding there is no connection.
FAQ
What does a positive covariance tell me?
It tells you that above-average values of one variable tend to occur alongside above-average values of the other, and below-average values alongside below-average ones. In the five pairs here the covariance is 1.2, so y generally rises as x rises. It says nothing about how strong the relationship is in a standardised sense.
Can covariance be negative or zero?
Yes on both counts. A negative value means the variables tend to move in opposite directions, with one above its mean when the other is below. A zero value means the positive and negative cross-products cancel, so there is no consistent linear tendency in either direction.
Should I divide by n or by n minus one?
Divide by n when your pairs are the whole population you care about, giving 1.2 here. Divide by n minus one when they are a sample from something larger, giving 1.5. The second convention corrects a systematic underestimate and matters most for small samples.
Why is covariance hard to interpret on its own?
Its units are the product of the two variables' units, such as metres times seconds, so the magnitude has no natural scale. Changing units changes the number without changing the relationship. Correlation solves this by dividing covariance by both standard deviations.
Does adding a constant to every value change the covariance?
No. Shifting all x values or all y values by the same amount shifts the corresponding mean by the same amount, leaving every deviation unchanged. Adding one to both variables in the worked example keeps the covariance at exactly 1.2.
How is covariance used in practice?
It appears wherever joint variation matters: computing the variance of a portfolio of assets, building covariance matrices in signal processing, and deriving the slope in simple regression. In each case the unstandardised quantity is needed for the algebra, even when a correlation is what gets reported.
References
- [1], Covariance — https://en.wikipedia.org/wiki/Covariance
- [2], Variance — https://en.wikipedia.org/wiki/Variance
- [3], Pearson correlation coefficient — https://en.wikipedia.org/wiki/Pearson_correlation_coefficient
