Unit 7, lesson 48

Covariance, Correlation and Sums

How covariance and correlation measure the way two random variables move together, why zero covariance does not imply independence, how covariance enters the variance of a sum, and why the size of a correlation is at most 1.

  • Video 48
  • Independence and Covariance

Two sensors measure the same quantity, and the error of each sensor has variance 11. The total error has variance 22 when the two errors are independent, 44 when the second error always equals the first, and 00 when it always equals the first with the sign reversed. The two separate variances cannot tell these cases apart, because the answer depends on how the errors move together. This lesson defines covariance, the summary that fills this gap, and correlation, its unit-free version, shows what each of them detects and what it misses, and derives the variance of sums and linear combinations.

Key terms

Covariance

Definition Let XX and YY be random variables on the same probability model with finite second moments, E[X2]<∞E[X^2] < \infty and E[Y2]<∞E[Y^2] < \infty. Then the means μX=E[X]\mu_X = E[X] and μY=E[Y]\mu_Y = E[Y] are finite, and so is E[XY]E[XY], because ∣xy∣≤12(x2+y2)\lvert xy \rvert \le \tfrac12 (x^2 + y^2) for all real xx and yy. The covariance of XX and YY is the expected product of their deviations from their means,

Cov⁡(X,Y)=E[(X−μX)(Y−μY)]=E[XY]−μXμY.\begin{aligned} &\operatorname{Cov}(X, Y) \\[0.4em] &= E[(X - \mu_X)(Y - \mu_Y)] \\[0.4em] &= E[XY] - \mu_X \mu_Y . \end{aligned}

The second form comes from expanding the product and using linearity of expectation. Its subtracted term is the product of the two means, not the mean of the product. Covariance is symmetric, Cov⁡(X,X)=Var⁡(X)\operatorname{Cov}(X, X) = \operatorname{Var}(X), and its unit is the product of the units of XX and YY. It can be positive, negative or zero. Positive covariance says that the centered products are positive on average, so the products from deviations with the same sign outweigh those from deviations with opposite signs.

In plain words On each outcome, multiply the deviation of XX from its mean by the deviation of YY from its mean, counting a deviation as negative when the value is below the mean, then average these products with the outcome probabilities. Deviations with the same sign push the average up, and deviations with opposite signs push it down.

Example In a constructed parcel model, X=1X = 1 marks an express parcel and Y=1Y = 1 an inspected one, and the pairs (0,0)(0, 0), (0,1)(0, 1), (1,0)(1, 0) and (1,1)(1, 1) have probabilities 0.30.3, 0.20.2, 0.10.1 and 0.40.4. Then μX=0.5\mu_X = 0.5, μY=0.6\mu_Y = 0.6 and E[XY]=P(X=1,Y=1)=0.4E[XY] = P(X = 1, Y = 1) = 0.4, so Cov⁡(X,Y)=0.4−0.5⋅0.6=0.1\operatorname{Cov}(X, Y) = 0.4 - 0.5 \cdot 0.6 = 0.1. The centered products in the four cells are 0.30.3, −0.2-0.2, −0.3-0.3 and 0.20.2, and their weighted average 0.3⋅0.3−0.2⋅0.2−0.3⋅0.1+0.2⋅0.4=0.10.3 \cdot 0.3 - 0.2 \cdot 0.2 - 0.3 \cdot 0.1 + 0.2 \cdot 0.4 = 0.1 agrees.

Uncorrelated variables

Definition Random variables XX and YY with finite second moments are uncorrelated if Cov⁡(X,Y)=0\operatorname{Cov}(X, Y) = 0, or equivalently E[XY]=E[X] E[Y]E[XY] = E[X]\,E[Y]. Independent variables with finite second moments are uncorrelated. For discrete variables the product law turns the double sum for E[XY]E[XY] into the product of the sums for E[X]E[X] and E[Y]E[Y]. The converse is false. Uncorrelated variables can be dependent, even when one of them is a function of the other.

In plain words Zero covariance says that the positive and negative centered products cancel on average. It does not say that knowing one variable tells you nothing about the other. To show independence, check the joint distribution itself.

Example Let XX equal −1-1, 00 or 11, each with probability 13\tfrac13, and let Y=X2Y = X^2, so the pairs (X,Y)(X, Y) are (−1,1)(-1, 1), (0,0)(0, 0) and (1,1)(1, 1). Then E[X]=0E[X] = 0, E[XY]=E[X3]=0E[XY] = E[X^3] = 0 and E[Y]=23E[Y] = \tfrac23, so Cov⁡(X,Y)=0−0⋅23=0\operatorname{Cov}(X, Y) = 0 - 0 \cdot \tfrac23 = 0. The centered products are −13-\tfrac13 at X=−1X = -1, 00 at X=0X = 0 and 13\tfrac13 at X=1X = 1, and they cancel. Yet YY is determined by XX, and the product law fails, since P(X=0,Y=1)=0P(X = 0, Y = 1) = 0 while P(X=0) P(Y=1)=13⋅23=29P(X = 0)\,P(Y = 1) = \tfrac13 \cdot \tfrac23 = \tfrac29.

Variance of a sum

Definition If XX and YY have finite second moments, then

Var⁡(X+Y)=Var⁡(X)+Var⁡(Y)+2Cov⁡(X,Y),\begin{aligned} &\operatorname{Var}(X + Y) \\[0.4em] &= \operatorname{Var}(X) + \operatorname{Var}(Y) \\[0.4em] &\quad + 2\operatorname{Cov}(X, Y), \end{aligned}

with or without independence. The formula comes from expanding ((X−μX)+(Y−μY))2\bigl((X - \mu_X) + (Y - \mu_Y)\bigr)^2 and taking expectations. For constants aa and bb the same expansion gives

Var⁡(aX+bY)=a2Var⁡(X)+b2Var⁡(Y)+2abCov⁡(X,Y),\begin{aligned} &\operatorname{Var}(aX + bY) \\[0.4em] &= a^2 \operatorname{Var}(X) + b^2 \operatorname{Var}(Y) \\[0.4em] &\quad + 2ab \operatorname{Cov}(X, Y), \end{aligned}

so in particular Var⁡(X−Y)=Var⁡(X)+Var⁡(Y)−2Cov⁡(X,Y)\operatorname{Var}(X - Y) = \operatorname{Var}(X) + \operatorname{Var}(Y) - 2\operatorname{Cov}(X, Y). For nn variables with finite second moments,

Var⁡(∑i=1nXi)=∑i=1nVar⁡(Xi)+2∑i<jCov⁡(Xi,Xj).\begin{aligned} &\operatorname{Var}\Bigl(\sum_{i=1}^{n} X_i\Bigr) \\[0.4em] &= \sum_{i=1}^{n} \operatorname{Var}(X_i) \\[0.4em] &\quad + 2 \sum_{i < j} \operatorname{Cov}(X_i, X_j). \end{aligned}

The variances add exactly when the covariance terms add to 00. This holds when the variables are pairwise uncorrelated, and in particular when every two of them are independent.

In plain words The variance of a total is the sum of the separate variances plus a correction for how the parts move together. When the covariance terms add to a positive number, the total spreads more than the separate variances suggest, and when they add to a negative number, it spreads less.

Example For XX equal to −1-1, 00 or 11 with probability 13\tfrac13 each and Y=X2Y = X^2, the variances are Var⁡(X)=23\operatorname{Var}(X) = \tfrac23 and Var⁡(Y)=23−(23)2=29\operatorname{Var}(Y) = \tfrac23 - \bigl(\tfrac23\bigr)^2 = \tfrac29, and the covariance is 00, so Var⁡(X+Y)=23+29=89\operatorname{Var}(X + Y) = \tfrac23 + \tfrac29 = \tfrac89. Directly, X+YX + Y equals 00 when XX is −1-1 or 00, and 22 when X=1X = 1, so it is 00 with probability 23\tfrac23 and 22 with probability 13\tfrac13. Its mean is 23\tfrac23, its second moment is 43\tfrac43 and its variance is 43−49=89\tfrac43 - \tfrac49 = \tfrac89. The variances add here because the covariance is 00, even though the variables are dependent. In the parcel model Var⁡(X)=0.25\operatorname{Var}(X) = 0.25, Var⁡(Y)=0.24\operatorname{Var}(Y) = 0.24 and the covariance is 0.10.1, so Var⁡(X+Y)=0.25+0.24+2⋅0.1=0.69\operatorname{Var}(X + Y) = 0.25 + 0.24 + 2 \cdot 0.1 = 0.69.

Bilinearity of covariance

Definition For random variables with finite second moments and constants aa, bb, cc and dd,

Cov⁡(aX+b,cY+d)=acCov⁡(X,Y),\begin{aligned} &\operatorname{Cov}(aX + b, cY + d) \\[0.4em] &= ac \operatorname{Cov}(X, Y), \end{aligned}

and covariance distributes over sums in each argument, as in Cov⁡(X+Y,Z)=Cov⁡(X,Z)+Cov⁡(Y,Z)\operatorname{Cov}(X + Y, Z) = \operatorname{Cov}(X, Z) + \operatorname{Cov}(Y, Z). Together with the symmetry Cov⁡(X,Y)=Cov⁡(Y,X)\operatorname{Cov}(X, Y) = \operatorname{Cov}(Y, X), these rules say that covariance is linear in each argument when the other one is held fixed, which is what bilinear means. Added constants drop out because centering removes them, and Cov⁡(X,c)=0\operatorname{Cov}(X, c) = 0 for every constant cc. With Y=XY = X the rules give Var⁡(aX+b)=a2Var⁡(X)\operatorname{Var}(aX + b) = a^2 \operatorname{Var}(X), and expanding Cov⁡(X+Y,X+Y)\operatorname{Cov}(X + Y, X + Y) term by term gives the variance of a sum again.

In plain words Shifting a variable does not change how it moves with another one, rescaling it rescales the covariance by the same factor, and the covariance of a total with another variable is the sum of the covariances of its parts. Changing one variable from meters to centimeters multiplies its covariance with any other variable by 100100.

Example For XX equal to −1-1, 00 or 11 with probability 13\tfrac13 each, let Z=2X+3Z = 2X + 3. Bilinearity gives Cov⁡(X,Z)=2Cov⁡(X,X)=2⋅23=43\operatorname{Cov}(X, Z) = 2\operatorname{Cov}(X, X) = 2 \cdot \tfrac23 = \tfrac43 and Var⁡(Z)=Cov⁡(2X+3,2X+3)=4⋅23=83\operatorname{Var}(Z) = \operatorname{Cov}(2X + 3, 2X + 3) = 4 \cdot \tfrac23 = \tfrac83. Directly, ZZ takes the values 11, 33 and 55 with probability 13\tfrac13 each, so its mean is 33 and its variance is 4+0+43=83\tfrac{4 + 0 + 4}{3} = \tfrac83.

Correlation coefficient

Definition Let XX and YY have finite second moments and positive variances. Their correlation coefficient is

ρX,Y=Cov⁡(X,Y)SD⁡(X) SD⁡(Y).\rho_{X,Y} = \frac{\operatorname{Cov}(X, Y)}{\operatorname{SD}(X)\,\operatorname{SD}(Y)} .

It has no unit. For constants a≠0a \ne 0, c≠0c \ne 0, bb and dd, the correlation of aX+baX + b and cY+dcY + d equals ρX,Y\rho_{X,Y} when ac>0ac > 0 and −ρX,Y-\rho_{X,Y} when ac<0ac < 0, so a change of units with positive scale factors leaves it unchanged. If one of the variances is 00, that variable equals a constant with probability 11, its covariance with every variable is 00, and the correlation is undefined, because the formula divides by 00. Such a case is reported as undefined, not as zero correlation. Uncorrelated variables with positive variances have correlation 00.

In plain words Correlation is covariance measured in units of the two standard deviations, which makes the strength of a linear association comparable across problems with different scales. The next card shows that it always lies between −1-1 and 11.

Example In the parcel model SD⁡(X)=0.5\operatorname{SD}(X) = 0.5 and SD⁡(Y)=0.24\operatorname{SD}(Y) = \sqrt{0.24}, so

ρX,Y=0.10.50.24=16≈0.408.\begin{aligned} \rho_{X,Y} &= \frac{0.1}{0.5\sqrt{0.24}} \\[0.4em] &= \frac{1}{\sqrt{6}} \approx 0.408 . \end{aligned}

For XX equal to −1-1, 00 or 11 with probability 13\tfrac13 each and Y=X2Y = X^2, both variances are positive and the covariance is 00, so the correlation is 00 although YY is a function of XX. If W=5W = 5 on every outcome, then Cov⁡(X,W)=0\operatorname{Cov}(X, W) = 0, but the correlation of XX and WW is undefined.

Cauchy-Schwarz inequality

Definition For random variables UU and VV with finite second moments, the Cauchy-Schwarz inequality states

(E[UV])2≤E[U2] E[V2].\bigl(E[UV]\bigr)^2 \le E[U^2]\,E[V^2] .

Applied to the centered variables X−μXX - \mu_X and Y−μYY - \mu_Y, it gives Cov⁡(X,Y)2≤Var⁡(X)Var⁡(Y)\operatorname{Cov}(X, Y)^2 \le \operatorname{Var}(X)\operatorname{Var}(Y), so −1≤ρX,Y≤1-1 \le \rho_{X,Y} \le 1 whenever the correlation is defined. A direct argument standardizes the variables. With ρ=ρX,Y\rho = \rho_{X,Y}, the variables U=(X−μX)/SD⁡(X)U = (X - \mu_X)/\operatorname{SD}(X) and V=(Y−μY)/SD⁡(Y)V = (Y - \mu_Y)/\operatorname{SD}(Y) have mean 00, variance 11 and E[UV]=ρE[UV] = \rho, so

E[(U−ρV)2]=1−2ρ2+ρ2=1−ρ2≥0.\begin{aligned} &E\bigl[(U - \rho V)^2\bigr] \\[0.4em] &= 1 - 2\rho^2 + \rho^2 \\[0.4em] &= 1 - \rho^2 \ge 0 . \end{aligned}

Perfect correlation means ρX,Y=1\rho_{X,Y} = 1 or ρX,Y=−1\rho_{X,Y} = -1. For XX and YY with positive variances, it happens exactly when Y=aX+bY = aX + b with probability 11 for constants a≠0a \ne 0 and bb, with ρX,Y=1\rho_{X,Y} = 1 when a>0a > 0 and ρX,Y=−1\rho_{X,Y} = -1 when a<0a < 0. For example, ρX,Y=1\rho_{X,Y} = 1 forces E[(U−V)2]=2−2ρX,Y=0E[(U - V)^2] = 2 - 2\rho_{X,Y} = 0, so U=VU = V with probability 11.

In plain words Correlation never exceeds 11 in size, and it equals 11 in size only when all of the probability lies on one straight line that is neither horizontal nor vertical. Perfect correlation is about a straight line. A curved exact relationship, such as Y=X2Y = X^2 in the three-point model, can have correlation 00.

Example For XX equal to −1-1, 00 or 11 with probability 13\tfrac13 each and Z=2X+3Z = 2X + 3, the previous cards give Cov⁡(X,Z)=43\operatorname{Cov}(X, Z) = \tfrac43, Var⁡(X)=23\operatorname{Var}(X) = \tfrac23 and Var⁡(Z)=83\operatorname{Var}(Z) = \tfrac83. So SD⁡(Z)=2SD⁡(X)\operatorname{SD}(Z) = 2\operatorname{SD}(X) and

ρX,Z=4/32⋅23=1.\rho_{X,Z} = \frac{4/3}{2 \cdot \tfrac23} = 1 .

For W=−2X+3W = -2X + 3 the covariance is −43-\tfrac43 and the correlation is −1-1. In the parcel model the inequality is strict, since Cov⁡(X,Y)2=0.01\operatorname{Cov}(X, Y)^2 = 0.01 while Var⁡(X)Var⁡(Y)=0.25⋅0.24=0.06\operatorname{Var}(X)\operatorname{Var}(Y) = 0.25 \cdot 0.24 = 0.06.

Check yourself

  1. A random variable XX has variance 44. Let A=XA = X and B=3XB = 3X. Find Cov⁡(A,B)\operatorname{Cov}(A, B), the correlation of AA and BB, and Var⁡(A+B)\operatorname{Var}(A + B). Why is adding the two variances wrong here?
Show the answer

Bilinearity gives Cov⁡(X,3X)=3Var⁡(X)=12\operatorname{Cov}(X, 3X) = 3\operatorname{Var}(X) = 12. The standard deviations are 22 and 9⋅4=6\sqrt{9 \cdot 4} = 6, so the correlation is 122⋅6=1\tfrac{12}{2 \cdot 6} = 1, as expected for B=3AB = 3A. The sum is 4X4X, whose variance is 16⋅4=6416 \cdot 4 = 64, and the formula agrees, since 4+36+2⋅12=644 + 36 + 2 \cdot 12 = 64. Adding only the variances gives 4040 and leaves out the covariance term 2424. Because B=3AB = 3A, the two variables rise and fall together, and their covariance is 1212, not 00. The mean of XX is not needed for any of this.

  1. Random variables XX and YY have Var⁡(X)=9\operatorname{Var}(X) = 9, Var⁡(Y)=4\operatorname{Var}(Y) = 4 and correlation −0.5-0.5. Find Cov⁡(X,Y)\operatorname{Cov}(X, Y), Var⁡(X+Y)\operatorname{Var}(X + Y), Var⁡(X−Y)\operatorname{Var}(X - Y) and Var⁡(2X−3Y+7)\operatorname{Var}(2X - 3Y + 7).
Show the answer

The covariance is −0.5⋅3⋅2=−3-0.5 \cdot 3 \cdot 2 = -3. Then Var⁡(X+Y)=9+4+2(−3)=7\operatorname{Var}(X + Y) = 9 + 4 + 2(-3) = 7 and Var⁡(X−Y)=9+4−2(−3)=19\operatorname{Var}(X - Y) = 9 + 4 - 2(-3) = 19. The negative covariance makes the sum less spread out and the difference more spread out. For the last one the constant 77 drops out, and with a=2a = 2 and b=−3b = -3,

Var⁡(2X−3Y+7)=4⋅9+9⋅4+2⋅2⋅(−3)⋅(−3)=108.\begin{aligned} &\operatorname{Var}(2X - 3Y + 7) \\[0.4em] &= 4 \cdot 9 + 9 \cdot 4 \\[0.4em] &\quad + 2 \cdot 2 \cdot (-3) \cdot (-3) \\[0.4em] &= 108 . \end{aligned}
  1. Let XX and YY have the joint probabilities P(X=−1,Y=0)=14P(X = -1, Y = 0) = \tfrac14, P(X=−1,Y=2)=14P(X = -1, Y = 2) = \tfrac14 and P(X=1,Y=1)=12P(X = 1, Y = 1) = \tfrac12. Find Cov⁡(X,Y)\operatorname{Cov}(X, Y). Are XX and YY independent?
Show the answer

The mean of XX is −1⋅12+1⋅12=0-1 \cdot \tfrac12 + 1 \cdot \tfrac12 = 0, the mean of YY is 0⋅14+1⋅12+2⋅14=10 \cdot \tfrac14 + 1 \cdot \tfrac12 + 2 \cdot \tfrac14 = 1, and

E[XY]=(−1)(2)⋅14+(1)(1)⋅12=0,\begin{aligned} E[XY] &= (-1)(2) \cdot \tfrac14 \\[0.4em] &\quad + (1)(1) \cdot \tfrac12 \\[0.4em] &= 0, \end{aligned}

so Cov⁡(X,Y)=0−0⋅1=0\operatorname{Cov}(X, Y) = 0 - 0 \cdot 1 = 0. Both variances are positive, Var⁡(X)=1\operatorname{Var}(X) = 1 and Var⁡(Y)=32−1=12\operatorname{Var}(Y) = \tfrac32 - 1 = \tfrac12, so the correlation is 00 as well. The variables are still dependent. For instance P(X=1,Y=0)=0P(X = 1, Y = 0) = 0, while P(X=1) P(Y=0)=12⋅14=18P(X = 1)\,P(Y = 0) = \tfrac12 \cdot \tfrac14 = \tfrac18. Given X=1X = 1 the value of YY is certainly 11, while given X=−1X = -1 it is 00 or 22. Zero covariance does not prove independence.

How this lesson connects