Preview

A lesson from Essential Probability & Statistics for ML

£29/month

£29/month£29/month or £249/year · 30-day money-back guarantee

Get full access

Our content is best on a larger screen

Covariance

What covariance measures

Explanation

The variance of XX is its expected squared deviation from its mean. With two random variables, we often want to know how they vary together: when XX is above its mean, is YY usually above its mean too, or below it?

To measure this, we multiply the two deviations:

Covariance

Definition

For random variables XX and YY with means μX=E[X]\mu_X = \mathbb{E}[X] and μY=E[Y]\mu_Y = \mathbb{E}[Y], the covariance of XX and YY is the expected value of the product of their deviations,

Cov⁡(X,Y)=E[(X−μX)(Y−μY)].\operatorname{Cov}(X, Y) = \mathbb{E}\big[(X - \mu_X)(Y - \mu_Y)\big].

This definition applies to any random variables. When XX and YY are discrete, with joint PMF p(x,y)p(x, y), the law of the unconscious statistician for two random variables makes it a sum: each pair of values adds its product of deviations, weighted by its probability:

Cov⁡(X,Y)=∑(x,y)(x−μX)(y−μY) p(x,y)\operatorname{Cov}(X, Y) = \sum_{(x, y)} (x - \mu_X)(y - \mu_Y)\,p(x, y)

A term is positive when xx and yy lie on the same side of their means, and negative when they lie on opposite sides. The board below works through an example.

0
1
2
3
0
1
2
3
x
y
Cov(X,Y)= ∑(x,y) (x−μX)(y−μY) p(x,y)

Step 1: The joint PMF

Over a three-day weekend, X is the number of hot days and Y is the number of days a family goes to the beach.

Each dot is a pair of values (x,y), with area proportional to its probability p(x,y).

Cov(X,Y)= ∑(x,y) (x−μX)(y−μY) p(x,y)

Step 2: The means

The dashed lines mark the means, μX=1.65 and μY=1.65. Each term of the sum measures a pair from these lines.

Cov(X,Y)= ∑(x,y) (x−μX)(y−μY) p(x,y)

Step 3: A positive term

The pair (3,3) lies to the right of μX and above μY, so both deviations are positive:

(3−1.65)(3−1.65)(0.25)=(1.35)(1.35)(0.25)≈0.46

A positive times a positive is positive, so this term adds to the covariance.

Cov(X,Y)= ∑(x,y) (x−μX)(y−μY) p(x,y)

Step 4: A negative term

The pair (1,2) lies to the left of μX but above μY, so its deviations have opposite signs:

(1−1.65)(2−1.65)(0.05)=(−0.65)(0.35)(0.05)≈−0.01

A negative times a positive is negative, so this term takes away from the covariance.

Cov(X,Y)= ∑(x,y) (x−μX)(y−μY) p(x,y)

Step 5: All the terms

Every pair to the right of and above both means, or to the left of and below both, gives a positive term. The other two regions give negative terms.

Weekends with more hot days tend to have more beach days, so the large terms are positive. The positive terms add to about 1.10 and the negative terms to about −0.02, so Cov(X,Y)≈1.08>0.

Cov(X,Y)= ∑(x,y) (x−μX)(y−μY) p(x,y)

Step 6: A negative covariance

Now let Y be the number of days the family stays indoors instead. Each day they do one or the other, so every dot moves from y to 3−y, and μY=1.35.

Each deviation y−μY changes sign, so every term keeps its size and changes sign. The negative terms now add to about −1.10 and the positive terms to about 0.02, so Cov(X,Y)≈−1.08<0.

Practice questions

4 questions

A student revises for XX hours before a test, and YY is the number of errors they make in it. What sign would we expect Cov⁡(X,Y)\operatorname{Cov}(X, Y) to have?

Select the correct answer:

+ 3 more questions · unlocked with a subscription

Computing covariance from a joint table

Explanation

The variance can be written using expected values alone, as Var⁡(X)=E[X2]−(E[X])2\operatorname{Var}(X) = \mathbb{E}[X^2] - \big(\mathbb{E}[X]\big)^2, with no deviations from the mean. The covariance has a formula of the same form:

Covariance from E[XY]\mathbb{E}[XY], E[X]\mathbb{E}[X] and E[Y]\mathbb{E}[Y]

Theorem

For any random variables XX and YY,

Cov⁡(X,Y)=E[XY]−E[X] E[Y]\operatorname{Cov}(X, Y) = \mathbb{E}[XY] - \mathbb{E}[X]\,\mathbb{E}[Y]

When XX and YY are discrete, with their joint PMF p(x,y)p(x, y) set out as a table, we find E[XY]\mathbb{E}[XY] by weighting each cell's product xyxy by its probability,

E[XY]=∑(x,y)xy p(x,y)\mathbb{E}[XY] = \sum_{(x, y)} xy\,p(x, y)

and E[X]\mathbb{E}[X] and E[Y]\mathbb{E}[Y] from the marginal PMFs, the row and column sums of the table.

Why does this formula give the covariance?

Details

Example

A household is chosen at random. XX is the number of cars it owns, and Y=1Y = 1 if someone in it cycles to work, and Y=0Y = 0 otherwise. XX and YY have the following joint PMF:

y=0y = 0y=1y = 1
x=0x = 00.050.050.150.15
x=1x = 10.350.350.150.15
x=2x = 20.250.250.050.05

Find Cov⁡(X,Y)\operatorname{Cov}(X, Y).

Solution

The formula needs E[XY]\mathbb{E}[XY], E[X]\mathbb{E}[X] and E[Y]\mathbb{E}[Y]. We start with E[XY]\mathbb{E}[XY], weighting each cell's product xyxy by its probability. The product is 00 whenever x=0x = 0 or y=0y = 0, so only the cells (1,1)(1, 1) and (2,1)(2, 1) add to the sum:

E[XY]=∑(x,y)xy p(x,y)=1×1×0.15+2×1×0.05=0.25\begin{align*} \mathbb{E}[XY] &= \sum_{(x, y)} xy\,p(x, y) \\[6pt] &= 1 \times 1 \times 0.15 + 2 \times 1 \times 0.05 \\[6pt] &= 0.25 \end{align*}

Next we find the marginal PMFs by summing out. The row sums give pXp_X, and the column sums give pYp_Y:

xx001122
pX(x)p_X(x)0.20.20.50.50.30.3
yy0011
pY(y)p_Y(y)0.650.650.350.35

From the marginal PMFs, each mean weights the values by their probabilities:

E[X]=0×0.2+1×0.5+2×0.3=1.1E[Y]=0×0.65+1×0.35=0.35\begin{align*} \mathbb{E}[X] &= 0 \times 0.2 + 1 \times 0.5 + 2 \times 0.3 = 1.1 \\[6pt] \mathbb{E}[Y] &= 0 \times 0.65 + 1 \times 0.35 = 0.35 \end{align*}

We subtract the product of the means from the mean of the product:

Cov⁡(X,Y)=E[XY]−E[X] E[Y]=0.25−1.1×0.35=0.25−0.385=−0.135\begin{align*} \operatorname{Cov}(X, Y) &= \mathbb{E}[XY] - \mathbb{E}[X]\,\mathbb{E}[Y] \\[6pt] &= 0.25 - 1.1 \times 0.35 \\[6pt] &= 0.25 - 0.385 \\[6pt] &= -0.135 \end{align*}

The covariance is negative: in this table, households with more cars are less likely to have someone who cycles to work.

Practice questions

4 questions

Random variables XX and YY have the following joint PMF:

y=0y = 0y=1y = 1
x=0x = 00.40.40.10.1
x=1x = 10.20.20.30.3

What is Cov⁡(X,Y)\operatorname{Cov}(X, Y)?

Select the correct answer:

+ 3 more questions · unlocked with a subscription

Memory is built by recalling, not by reading.

Every time you pull an answer from memory, the memory gets stronger. Reading doesn't do that. Nodeledge asks you questions on every step of every lesson, then asks again after days, weeks and months.

Get full access

£29/month or £249/year · 30-day money-back guarantee

See the learning path