Mehmet Emre Tiryaki
Back to Notes

Expectation and Covariance

Expectation

The expectation of a random variable XX is

E[X]=∫x p(x) dx,in discrete E[X]=∑x∈Sx P(X=x)E[X] = \int x \, p(x) \, dx, \qquad \text{in discrete } E[X] = \sum_{x \in S} x \, P(X = x)

E.g. X∼Exp(λ)X \sim \mathrm{Exp}(\lambda),

E[X]=∫0∞x⋅λe−λx dx=λ⋅1λ2=1λE[X] = \int_0^\infty x \cdot \lambda e^{-\lambda x} \, dx = \lambda \cdot \frac{1}{\lambda^2} = \frac{1}{\lambda}

Expectation is linear,

E[aX+bY]=aE[X]+bE[Y]E[aX + bY] = aE[X] + bE[Y]

LOTUS (law of the unconscious statistician) states

E[g(X)]=∫g(x) p(x) dxE[g(X)] = \int g(x) \, p(x) \, dx

e.g. E[X3]=∫x3p(x) dxE[X^3] = \int x^3 p(x) \, dx.

Variance

Variance is the measure of spread of an RV with a distribution,

Var(X)=E[(X−E[X])2]=E[X2]−E[X]2\mathrm{Var}(X) = E\big[(X - E[X])^2\big] = E[X^2] - E[X]^2

Expectation with Respect to a Distribution

Ep(x)[f(X,…)]E_{p(x)}[f(X, \ldots)] means take the expectation with respect to the distribution of XX, that is p(x)p(x). An equivalent form is EX∼p(x)[f(X,…)]E_{X \sim p(x)}[f(X, \ldots)].

E.g. let X∼Unif(0,1)X \sim \mathrm{Unif}(0,1), Y∼Unif(0,2)Y \sim \mathrm{Unif}(0,2), with X,YX, Y independent. Then

E[XY]=EX,Y[XY]=Ep(x,y)[XY]=∫02∫01xy⋅12⋅11 dx dy=12E[XY] = E_{X,Y}[XY] = E_{p(x,y)}[XY] = \int_0^2 \int_0^1 xy \cdot \frac{1}{2} \cdot \frac{1}{1} \, dx \, dy = \frac{1}{2}

But

EX[XY]=∫01xY⋅11 dx=Y∫01x dx=Y2E_X[XY] = \int_0^1 xY \cdot \frac{1}{1} \, dx = Y \int_0^1 x \, dx = \frac{Y}{2}

which is still a random variable.

Conditional Expectation

A conditional expectation E[X∣Y]E[X | Y] is itself a random variable, a function of YY, with

E[X∣Y=y]=∫x p(x∣y) dxE[X | Y = y] = \int x \, p(x | y) \, dx

E.g. for the counting heads example (from here),

E[X∣Y=0]=0⋅12+1⋅12=12E[X∣Y=1]=1⋅12+2⋅12=32soE[X∣Y]={1/2if Y=0, with probability 1/23/2if Y=1, with probability 1/2\begin{aligned} E[X | Y = 0] &= 0 \cdot \tfrac{1}{2} + 1 \cdot \tfrac{1}{2} = \tfrac{1}{2} \\ E[X | Y = 1] &= 1 \cdot \tfrac{1}{2} + 2 \cdot \tfrac{1}{2} = \tfrac{3}{2} \end{aligned} \qquad \text{so} \qquad E[X | Y] = \begin{cases} 1/2 & \text{if } Y = 0, \text{ with probability } 1/2 \\ 3/2 & \text{if } Y = 1, \text{ with probability } 1/2 \end{cases}

Tower Law states

E[X]=EY[EX∣Y[X∣Y]]E[X] = E_Y\big[E_{X|Y}[X | Y]\big]

i.e. first take the expectation given YY, which is an RV as a function of YY, then take the expectation over YY to get E[X]E[X].

E.g. let X∼Unif(0,1)X \sim \mathrm{Unif}(0,1) and Y∣X∼N(X,1)Y | X \sim \mathcal{N}(X, 1). Then

E[Y]=EX[E[Y∣X]]=EX[X]=12E[Y] = E_X\big[E[Y | X]\big] = E_X[X] = \tfrac{1}{2}

Covariance and Correlation

A measure of relation or dependence between random variables is covariance,

Cov(X,Y)=E[(X−E[X])(Y−E[Y])]=E[XY]−E[X]E[Y]\mathrm{Cov}(X, Y) = E\big[(X - E[X])(Y - E[Y])\big] = E[XY] - E[X]E[Y]

and Cov(X,X)=Var(X)\mathrm{Cov}(X, X) = \mathrm{Var}(X).

Correlation is a measure of linear relation between two RVs,

ρX,Y=Cov(X,Y)Var(X) Var(Y)∈[−1,1]\rho_{X,Y} = \frac{\mathrm{Cov}(X, Y)}{\sqrt{\mathrm{Var}(X)\, \mathrm{Var}(Y)}} \in [-1, 1]

−1-1 or 11 means the RVs are linear transformations of each other, e.g. Y=3X+2Y = 3X + 2.

If X,YX, Y are independent,

E[XY]=E[X]E[Y]  ⟹  Cov(X,Y)=0  ⟹  ρX,Y=0E[XY] = E[X]E[Y] \implies \mathrm{Cov}(X, Y) = 0 \implies \rho_{X,Y} = 0

But ρX,Y=0\rho_{X,Y} = 0 does not mean X,YX, Y are independent.

Random Vectors

Instead of 1D RVs, we can define random vectors with each entry being an RV. Then

X=[X1⋮Xn],E[X]=[E[X1]⋮E[Xn]],Cov(X)=[Var(X1)⋯Cov(X1,Xn)⋮⋱⋮Cov(Xn,X1)⋯Var(Xn)]∈Rn×nX = \begin{bmatrix} X_1 \\ \vdots \\ X_n \end{bmatrix}, \qquad E[X] = \begin{bmatrix} E[X_1] \\ \vdots \\ E[X_n] \end{bmatrix}, \qquad \mathrm{Cov}(X) = \begin{bmatrix} \mathrm{Var}(X_1) & \cdots & \mathrm{Cov}(X_1, X_n) \\ \vdots & \ddots & \vdots \\ \mathrm{Cov}(X_n, X_1) & \cdots & \mathrm{Var}(X_n) \end{bmatrix} \in \mathbb{R}^{n \times n}

The covariance matrix Σ=Cov(X)\Sigma = \mathrm{Cov}(X) is a symmetric, positive semidefinite matrix of the pairwise covariances.

Multivariate Normal

A multivariate normal is the distribution of a random vector where every linear combination of its entries is normally distributed. In particular each entry is normal, but normal entries alone are not enough.

It is specified as X∼N(μ,Σ)X \sim \mathcal{N}(\mu, \Sigma), where μ\mu is the vector of means and Σ\Sigma is the covariance matrix. If Σ\Sigma is invertible, for nn dimensional XX

fX(x)=1(2π)n/2∣Σ∣1/2exp⁡(−12(x−μ)TΣ−1(x−μ))f_X(x) = \frac{1}{(2\pi)^{n/2} |\Sigma|^{1/2}} \exp\Big(-\frac{1}{2}(x - \mu)^T \Sigma^{-1} (x - \mu)\Big)

where ∣Σ∣|\Sigma| is the determinant. Also,

AX+b∼N(Aμ+b, AΣAT)AX + b \sim \mathcal{N}(A\mu + b,\ A\Sigma A^T)