Post · 8 min read· pp. 358–363

5.3.1 Covariance and Correlation


Consider two random variables XX and YY. Here, we define the covariance between XX and YY, written Cov(X,Y)\textrm{Cov}(X,Y). The covariance gives some information about how XX and YY are statistically related. Let us provide the definition, then discuss the properties and applications of covariance.

The covariance between XX and YY is defined as \begin{align}%\label{} \nonumber \textrm{Cov}(X,Y)&=E\big[(X-EX)(Y-EY)\big]=E[XY]-(EX)(EY). \end{align}

Note that \begin{align}%\label{} \nonumber E\big[(X-EX)(Y-EY)\big]&=E\big[XY-X(EY)-(EX)Y+(EX)(EY)\big]\ \nonumber &=E[XY]-(EX)(EY)-(EX)(EY)+(EX)(EY)\ \nonumber &=E[XY]-(EX)(EY). \end{align} Intuitively, the covariance between XX and YY indicates how the values of XX and YY move relative to each other. If large values of XX tend to happen with large values of YY, then (X−EX)(Y−EY)(X-EX)(Y-EY) is positive on average. In this case, the covariance is positive and we say XX and YY are positively correlated. On the other hand, if XX tends to be small when YY is large, then (X−EX)(Y−EY)(X-EX)(Y-EY) is negative on average. In this case, the covariance is negative and we say XX and YY are negatively correlated.


Example Suppose X∼Uniform(1,2)X \sim Uniform(1,2), and given X=xX=x, YY is exponential with parameter λ=x\lambda=x. Find Cov(X,Y)\textrm{Cov}(X,Y).

  • Solution
    • We can use Cov(X,Y)=EXY−EXEY(X,Y)=EXY-EXEY. We have EX=32EX=\frac{3}{2} and \begin{align}%\label{} \nonumber EY &=E[E[Y|X]] &\big(\textrm{law of iterated expectations (Equation 5.17)}\big)\ \nonumber &=E\left[\frac{1}{X}\right] &\big(\textrm{since }Y|X \sim Exponential(X)\big)\ \nonumber &=\int_{1}^{2} \frac{1}{x} dx\ \nonumber &=\ln 2. \end{align} We also have \begin{align}%\label{} \nonumber EXY &=E[E[XY|X]] &\big(\textrm{law of iterated expectations}\big)\ \nonumber EXY &=E[XE[Y|X]] &\big(\textrm{since} E[X|X=x]=x\big)\ \nonumber &=E\left[X\frac{1}{X}\right] &\big(\textrm{since }Y|X \sim Exponential(X)\big)\ \nonumber &=1. \end{align} Thus, \begin{align}%\label{} \nonumber \textrm{Cov}(X,Y)=E[XY]-(EX)(EY)=1-\frac{3}{2} \ln 2. \end{align}

Now we discuss the properties of covariance.

Lemma The covariance has the following properties:

  1. Cov(X,X)=Var(X)\textrm{Cov}(X,X)=\textrm{Var}(X);
  2. if XX and YY are independent then Cov(X,Y)=0\textrm{Cov}(X,Y)=0;
  3. Cov(X,Y)=Cov(Y,X)\textrm{Cov}(X,Y)=\textrm{Cov}(Y,X);
  4. Cov(aX,Y)=aCov(X,Y)\textrm{Cov}(aX,Y)=a\textrm{Cov}(X,Y);
  5. Cov(X+c,Y)=Cov(X,Y)\textrm{Cov}(X+c,Y)=\textrm{Cov}(X,Y);
  6. Cov(X+Y,Z)=Cov(X,Z)+Cov(Y,Z)\textrm{Cov}(X+Y,Z)=\textrm{Cov}(X,Z)+\textrm{Cov}(Y,Z);
  7. more generally,

\begin{align}%\label{} \nonumber \textrm{Cov}\left(\sum_{i=1}^{m}a_iX_i, \sum_{j=1}^{n}b_jY_j\right)=\sum_{i=1}^{m} \sum_{j=1}^{n} a_ib_j \textrm{Cov}(X_i,Y_j). \end{align}

All of the above results can be proven directly from the definition of covariance. For example, if XX and YY are independent, then as we have seen before E[XY]=EXEYE[XY]=EX EY, so \begin{align}%\label{} \nonumber \textrm{Cov}(X,Y)=E[XY]-EX EY=0. \end{align} Note that the converse is not necessarily true. That is, if Cov(X,Y)=0\textrm{Cov}(X,Y)=0, XX and YY may or may not be independent. Let us prove Item 6 in Lemma 5.3, Cov(X+Y,Z)=Cov(X,Z)+Cov(Y,Z)\textrm{Cov}(X+Y,Z)=\textrm{Cov}(X,Z)+\textrm{Cov}(Y,Z). We have \begin{align}%\label{} \nonumber \textrm{Cov}(X+Y,Z)&=E[(X+Y)Z]-E(X+Y)EZ\ \nonumber &=E[XZ+YZ]-(EX+EY)EZ\ \nonumber &=EXZ-EXEZ+EYZ-EYEZ\ \nonumber &=\textrm{Cov}(X,Z)+\textrm{Cov}(Y,Z). \end{align} You can prove the rest of the items in Lemma 5.3 similarly.


Example Let XX and YY be two independent N(0,1)N(0,1) random variables and \begin{align}%\label{} \nonumber &Z=1+X+XY^2, \ \nonumber &W=1+X. \end{align} Find Cov(Z,W)(Z,W).

  • Solution
    • \begin{align}%\label{} \nonumber \textrm{Cov}(Z,W)&=\textrm{Cov}(1+X+XY^2,1+X) \ \nonumber &=\textrm{Cov}(X+XY^2,X) \hspace{80pt}(\textrm{by part 5 of Lemma 5.3}) \ \nonumber &=\textrm{Cov}(X,X)+\textrm{Cov}(XY^2,X) \hspace{44pt}(\textrm{by part 6 of Lemma 5.3}) \ \nonumber &=\textrm{Var}(X)+E[X^2Y^2]-E[XY^2]EX \hspace{12pt}(\textrm{by part 1 of Lemma 5.3 &\& definition of Cov}) \ \nonumber &=1+E[X^2]E[Y^2]-E[X]^2E[Y^2] \hspace{24pt}(\textrm{since XX and YY are independent})\ \nonumber &=1+1-0=2. \end{align}

Variance of a sum:

One of the applications of covariance is finding the variance of a sum of several random variables. In particular, if Z=X+YZ=X+Y, then \begin{align}%\label{} \nonumber \textrm{Var}(Z)&=\textrm{Cov}(Z,Z)\ \nonumber &=\textrm{Cov}(X+Y,X+Y)\ \nonumber &=\textrm{Cov}(X,X)+\textrm{Cov}(X,Y)+ \textrm{Cov}(Y,X)+\textrm{Cov}(Y,Y)\ \nonumber &=\textrm{Var}(X)+\textrm{Var}(Y)+2 \textrm{Cov}(X,Y). \end{align} More generally, for a,b∈Ra,b \in \mathbb{R}, we conclude:

\begin{align}\label{eq:var-aX+bY} \textrm{Var}(aX+bY)=a^2\textrm{Var}(X)+b^2\textrm{Var}(Y)+2ab \textrm{Cov}(X,Y) \hspace{20pt} (5.21) \end{align}

Correlation Coefficient:

The correlation coefficient, denoted by ρXY\rho_{XY} or ρ(X,Y)\rho(X,Y), is obtained by normalizing the covariance. In particular, we define the correlation coefficient of two random variables XX and YY as the covariance of the standardized versions of XX and YY. Define the standardized versions of XX and YY as \begin{align}\label{eq:normalize} U=\frac{X-EX}{\sigma_X}, \hspace{10pt} V=\frac{Y-EY}{\sigma_Y} \hspace{20pt} (5.22) \end{align} Then, \begin{align}%\label{} \nonumber \rho_{XY}=\textrm{Cov}(U,V)&=\textrm{Cov}\left(\frac{X-EX}{\sigma_X},\frac{Y-EY}{\sigma_Y}\right)\ \nonumber &=\textrm{Cov}\left(\frac{X}{\sigma_X},\frac{Y}{\sigma_Y}\right) &(\textrm{by Item 5 of Lemma 5.3})\ \nonumber &=\frac{\textrm{Cov}(X,Y)}{\sigma_X \sigma_Y}. \end{align}

\begin{align}%\label{} \nonumber \rho_{XY}=\rho(X,Y)=\frac{\textrm{Cov}(X,Y)}{\sqrt{\textrm{Var(X) Var(Y)}}}=\frac{\textrm{Cov}(X,Y)}{\sigma_X \sigma_Y} \end{align}

A nice thing about the correlation coefficient is that it is always between −1-1 and $1$. This is an immediate result of Cauchy-Schwarz inequality that is discussed in Section 6.2.4. One way to prove that −1≤ρ≤1-1 \leq \rho \leq 1 is to use the following inequality: \begin{align}%\label{} \alpha \beta \leq \frac{\alpha^2+\beta^2}{2}, \textrm{for }\alpha,\beta \in \mathbb{R}. \end{align} This is because (α−β)2≥0(\alpha-\beta)^2 \geq 0. The equality holds only if α=β\alpha=\beta. From this, we can conclude that for any two random variables UU and VV, \begin{align}%\label{} E[UV] \leq \frac{EU^2+EV^2}{2}, \end{align} with equality only if U=VU=V with probability one. Now, let $ U$ and VV be the standardized versions of XX and YY as defined in Equation 5.22. Then, by definition ρXY=Cov(U,V)=EUV\rho_{XY}=\textrm{Cov}(U,V)=EUV. But since EU2=EV2=1EU^2=EV^2=1, we conclude \begin{align}%\label{} \rho_{XY}=E[UV] & \leq \frac{EU^2+EV^2}{2}=1, \end{align} with equality only if U=VU=V. That is, \begin{align}%\label{} \frac{Y-EY}{\sigma_Y}=\frac{X-EX}{\sigma_X}, \end{align} which implies \begin{align}%\label{} Y&=\frac{\sigma_Y}{\sigma_X} X+ \left(EY-\frac{\sigma_Y}{\sigma_X} EX\right)\ &=aX+b, \hspace{3pt} \textrm{where aa and bb are constants.} \end{align} Replacing XX by −X-X, we conclude that \begin{align}%\label{} \nonumber \rho(-X,Y) \leq 1. \end{align} But ρ(−X,Y)=−ρ(X,Y)\rho(-X,Y)=-\rho(X,Y), thus we conclude ρ(X,Y)≥−1\rho(X,Y) \geq -1. Thus, we can summarize some properties of the correlation coefficient as follows.

Properties of the correlation coefficient:

  1. −1≤ρ(X,Y)≤1-1 \leq \rho(X,Y) \leq 1;
  2. if ρ(X,Y)=1\rho(X,Y)=1, then Y=aX+bY=aX+b, where a>0a>0;
  3. if ρ(X,Y)=−1\rho(X,Y)=-1, then Y=aX+bY=aX+b, where a<0a<0;
  4. ρ(aX+b,cY+d)=ρ(X,Y)\rho(aX+b,cY+d)=\rho(X,Y) for a,c>0a,c>0.

Definition Consider two random variables XX and YY:

  • If ρ(X,Y)=0\rho(X,Y)=0, we say that XX and YY are uncorrelated.
  • If ρ(X,Y)>0\rho(X,Y)>0, we say that XX and YY are positively correlated.
  • If ρ(X,Y)<0\rho(X,Y)<0, we say that XX and YY are negatively correlated.

Note that as we discussed previously, two independent random variables are always uncorrelated, but the converse is not necessarily true. That is, if XX and YY are uncorrelated, then XX and YY may or may not be independent. Also, note that if XX and YY are uncorrelated from Equation 5.21, we conclude that Var(X+Y)=Var(X)+Var(Y)\textrm{Var}(X+Y)=\textrm{Var}(X)+\textrm{Var}(Y).

If XX and YY are uncorrelated, then \begin{align}%\label{} \nonumber \textrm{Var}(X+Y)=\textrm{Var}(X)+\textrm{Var}(Y). \end{align} More generally, if X1,X2,...,XnX_1,X_2,...,X_n are pairwise uncorrelated, i.e., ρ(Xi,Xj)=0\rho(X_i,X_j)=0 when i≠ji \neq j, then \begin{align}%\label{} \nonumber \textrm{Var}(X_1+X_2+...+X_n)=\textrm{Var}(X_1)+\textrm{Var}(X_2)+...+\textrm{Var}(X_n). \end{align}

Note that if XX and YY are independent, then they are uncorrelated, and so Var(X+Y)=Var(X)+Var(Y)\textrm{Var}(X+Y)=\textrm{Var}(X)+\textrm{Var}(Y). This is a fact that we stated previously in Chapter 3, and now we could easily prove using covariance.


Example Let XX and YY be as in Example 5.24 in Section 5.2.3, i.e., suppose that we choose a point (X,Y)(X,Y) uniformly at random in the unit disc \begin{align}%\label{} \nonumber D={(x,y)|x^2+y^2 \leq 1}. \end{align} Are XX and YY uncorrelated?

  • Solution
    • We need to check whether Cov(X,Y)=0\textrm{Cov}(X,Y)=0. First note that, in Example 5.24 of Section 5.2.3, we found out that XX and YY are not independent and in fact, we found that \begin{align}%\label{} \nonumber X|Y \hspace{5pt} \sim \hspace{5pt} Uniform(-\sqrt{1-Y^2},\sqrt{1-Y^2}). \end{align} Now let's find Cov(X,Y)=EXY−EXEY\textrm{Cov}(X,Y)=EXY-EXEY. We have \begin{align}%\label{} \nonumber EX &=E[E[X|Y]] &\big(\textrm{law of iterated expectations (Equation 5.17)}\big) \ \nonumber &=E[0]=0 &\big(\textrm{since }X|Y \hspace{5pt} \sim \hspace{5pt} Uniform(-\sqrt{1-Y^2},\sqrt{1-Y^2})\big). \end{align} Also, we have \begin{align}%\label{} \nonumber E[XY] &=E[E[XY|Y]] &\big(\textrm{law of iterated expectations (Equation 5.17)}\big)\ \nonumber &=E[YE[X|Y]] &\big( \textrm{Equation 5.6}\big) \ \nonumber &=E[Y \cdot 0]=0. \end{align} Thus, \begin{align}%\label{} \nonumber \textrm{Cov}(X,Y)=E[XY]-EXEY=0. \end{align} Thus, XX and YY are uncorrelated.