Visual Tools
Calculators
Tables
Mathematical Keyboard
Converters
Other Tools

How to Find Covariance and Correlation

Covariance measures whether two random variables move together. Find the mean of the product and subtract the product of the means; divide by both standard deviations to get the correlation, a unit-free number between −1-1 and 11.

Recognize the task

Two random variables on the same experiment: a joint probability table, E[XY]E[XY] with the two means, or a question about Var⁡(X+Y)\operatorname{Var}(X + Y) when XX and YY are not independent. Words: "move together", "linear relationship", Cov⁡(X,Y)\operatorname{Cov}(X, Y), ρ\rho. Test: is there a joint distribution, or at least E[XY]E[XY]? Then use the shortcut formula.
Covariance is introduced in the covariance lesson.

Two ways to do it

Mean of the product minus product of the meansusual
Cov⁡(X,Y)=E[XY]−E[X] E[Y]\operatorname{Cov}(X, Y) = E[XY] - E[X]\,E[Y]
The computational formula: one extra weighted sum over the joint table.
Average product of deviationsby definition
Cov⁡(X,Y)=E[(X−μX)(Y−μY)]\operatorname{Cov}(X, Y) = E\big[(X - \mu_X)(Y - \mu_Y)\big]
The definition; shows the sign directly, but needs more arithmetic.

Steps

StepOn X,Y∈{0,1}X, Y \in \{0, 1\} with p(0,0)=0.4, p(0,1)=0.1, p(1,0)=0.1, p(1,1)=0.4p(0,0) = 0.4,\ p(0,1) = 0.1,\ p(1,0) = 0.1,\ p(1,1) = 0.4
1Find both means from the margins. Add across rows and down columns to get the marginal pmfs, then take each weighted sum.E[X]=0.5, E[Y]=0.5E[X] = 0.5,\ E[Y] = 0.5
2Find the mean of the product. Multiply x⋅yx \cdot y in every cell by that cell's probability and add.E[XY]=1⋅1⋅0.4=0.4E[XY] = 1 \cdot 1 \cdot 0.4 = 0.4
3Subtract the product of the means. Positive: the variables tend to be large together. Negative: one large, the other small.Cov⁡(X,Y)=0.4−0.25=0.15\operatorname{Cov}(X, Y) = 0.4 - 0.25 = 0.15
4Find both standard deviations. Use each margin as on the standard deviation page.Var⁡(X)=Var⁡(Y)=0.5−0.25=0.25\operatorname{Var}(X) = \operatorname{Var}(Y) = 0.5 - 0.25 = 0.25, so σX=σY=0.5\sigma_X = \sigma_Y = 0.5
5Divide to get the correlation and check its range. The correlation always lies in [−1,1][-1, 1]; outside means an error.ρ=0.150.5⋅0.5=0.6\rho = \frac{0.15}{0.5 \cdot 0.5} = 0.6

Worked examples

Example 1 · from summary numbers

shortcut
E[XY]=10, E[X]=2, E[Y]=3E[XY] = 10,\ E[X] = 2,\ E[Y] = 3
Cov⁡(X,Y)=10−2⋅3\operatorname{Cov}(X, Y) = 10 - 2 \cdot 3
mean of product minus product of means
Cov⁡(X,Y)=4\operatorname{Cov}(X, Y) = 4
positive: they move together

Example 2 · from a joint table

2 × 2
p(0,0)=0.4, p(0,1)=0.1, p(1,0)=0.1, p(1,1)=0.4p(0,0) = 0.4,\ p(0,1) = 0.1,\ p(1,0) = 0.1,\ p(1,1) = 0.4
mass on the diagonal
Cov⁡=0.4−0.5⋅0.5=0.15\operatorname{Cov} = 0.4 - 0.5 \cdot 0.5 = 0.15
only cell (1,1)(1,1) adds to E[XY]E[XY]
ρ=0.150.5⋅0.5=0.6\rho = \frac{0.15}{0.5 \cdot 0.5} = 0.6
divide by both deviations

Example 3 · variance of a sum with covariance

dependent
Var⁡(X)=4, Var⁡(Y)=9, Cov⁡(X,Y)=−2\operatorname{Var}(X) = 4,\ \operatorname{Var}(Y) = 9,\ \operatorname{Cov}(X, Y) = -2
Var⁡(X+Y)=4+9+2(−2)=9\operatorname{Var}(X + Y) = 4 + 9 + 2(-2) = 9
add twice the covariance
ρ=−22⋅3=−13\rho = \frac{-2}{2 \cdot 3} = -\frac{1}{3}
σX=2, σY=3\sigma_X = 2,\ \sigma_Y = 3
Build and read joint tables in the contingency table tool.

See it happen

Think of each cell of the joint table as a point (x,y)(x, y) with a weight. Draw a cross through the point of means (μX,μY)(\mu_X, \mu_Y). Points to the upper right and lower left give positive products of deviations; the other two corners give negative ones. Covariance is the weighted average of those products, so it is positive when most weight lies on the rising diagonal. Dividing by σXσY\sigma_X \sigma_Y removes the units and scales the answer to [−1,1][-1, 1].

Common mistakes

Reading zero covariance as independence

✗ XX uniform on {−1,0,1}\{-1, 0, 1\}, Y=X2Y = X^2: Cov⁡=0\operatorname{Cov} = 0, so independent
✓ YY is a function of XX; zero covariance only rules out a linear trend
Independence implies zero covariance, not the other way round (covariance and independence).

Dropping the covariance term in a sum

✗ Var⁡(X+Y)=4+9=13\operatorname{Var}(X + Y) = 4 + 9 = 13 when Cov⁡=−2\operatorname{Cov} = -2
✓ Var⁡(X+Y)=4+9+2(−2)=9\operatorname{Var}(X + Y) = 4 + 9 + 2(-2) = 9

Not scaling covariance with the variables

✗ Cov⁡(2X,3Y)=Cov⁡(X,Y)=4\operatorname{Cov}(2X, 3Y) = \operatorname{Cov}(X, Y) = 4
✓ Cov⁡(2X,3Y)=2⋅3⋅4=24\operatorname{Cov}(2X, 3Y) = 2 \cdot 3 \cdot 4 = 24
Constants come out of each slot (bilinearity); the correlation stays the same.

A correlation outside the range

✗ ρ=0.150.25⋅0.25=2.4\rho = \frac{0.15}{0.25 \cdot 0.25} = 2.4
✓ divide by standard deviations, not variances: ρ=0.150.5⋅0.5=0.6\rho = \frac{0.15}{0.5 \cdot 0.5} = 0.6