Skip to main content

Expectation, Variance, and Covariance

A distribution contains everything there is to know about a random variable, but we often want a few numbers that summarize it. The expectation summarizes where a distribution is centered, the variance how spread out it is, and the covariance how two random variables move together. The conditional expectation then asks what we expect once we know something about the outcome, and it is the quantity that value functions in reinforcement learning are built on.

info

This chapter covers

  • the expectation of a random variable and of a function of it,
  • linearity of expectation,
  • variance and standard deviation,
  • covariance, correlation, the variance of a sum, and the covariance matrix, and
  • conditional expectation and the law of total expectation.

Expectation​

The expectation (or expected value) of a random variable is the average of its values, each weighted by its probability,

E[X]={∑xx p(x)if X is discrete,∫−∞∞x f(x) dxif X is continuous.\mathbb{E}[X] = \begin{cases} \displaystyle \sum_{x} x \, p(x) & \text{if } X \text{ is discrete,} \\[2ex] \displaystyle \int_{-\infty}^{\infty} x \, f(x) \, dx & \text{if } X \text{ is continuous.} \end{cases}

It is also called the mean of XX and written μ\mu. If you repeat the experiment many times and average the results, the average approaches E[X]\mathbb{E}[X], which chapter 5 makes precise.

For a fair d20, every face has probability 1/201/20, so

E[X]=1+2+⋯+2020=10.5.\mathbb{E}[X] = \frac{1 + 2 + \cdots + 20}{20} = 10.5.

The expected value is a value the die can never show. It describes the long-run average, not a typical roll.

For the crit indicator C∼Bernoulli(0.05)C \sim \text{Bernoulli}(0.05), E[C]=0×0.95+1×0.05=0.05\mathbb{E}[C] = 0 \times 0.95 + 1 \times 0.05 = 0.05. In general, the expectation of the indicator of an event is the probability of that event.

Expectation of a Function​

To find the expectation of g(X)g(X), weight g(x)g(x) by the probabilities of XX. There's no need to work out the distribution of g(X)g(X) first,

E[g(X)]=∑xg(x) p(x)orE[g(X)]=∫−∞∞g(x) f(x) dx.\mathbb{E}[g(X)] = \sum_{x} g(x) \, p(x) \qquad \text{or} \qquad \mathbb{E}[g(X)] = \int_{-\infty}^{\infty} g(x) \, f(x) \, dx.

For the d20, E[X2]=(12+22+⋯+202)/20=143.5\mathbb{E}[X^2] = (1^2 + 2^2 + \cdots + 20^2)/20 = 143.5. This is not (E[X])2=110.25(\mathbb{E}[X])^2 = 110.25. In general E[g(X)]≠g(E[X])\mathbb{E}[g(X)] \neq g(\mathbb{E}[X]) unless gg is linear.

Linearity​

Expectation is linear. For any random variables XX and YY and constants aa, bb and cc,

E[aX+bY+c]=a E[X]+b E[Y]+c.\mathbb{E}[aX + bY + c] = a \, \mathbb{E}[X] + b \, \mathbb{E}[Y] + c.

This holds whether or not XX and YY are related, which makes it one of the most useful facts in probability. A damage roll of 2d6+3 is D=D1+D2+3D = D_1 + D_2 + 3, where D1D_1 and D2D_2 are the two six-sided dice. Each die has mean 3.53.5, so E[D]=3.5+3.5+3=10\mathbb{E}[D] = 3.5 + 3.5 + 3 = 10 without ever writing out the distribution of DD.

In RL, the return satisfies Gt=Rt+1+γGt+1G_t = R_{t+1} + \gamma G_{t+1}, and linearity gives E[Gt]=E[Rt+1]+γ E[Gt+1]\mathbb{E}[G_t] = \mathbb{E}[R_{t+1}] + \gamma \, \mathbb{E}[G_{t+1}], the first step toward the Bellman equations.

Variance and Standard Deviation​

The variance measures how far a random variable typically falls from its mean. It is the expected squared distance from the mean,

Var⁡(X)=E[(X−μ)2]={∑x(x−μ)2 p(x)if X is discrete,∫−∞∞(x−μ)2f(x) dxif X is continuous.\operatorname{Var}(X) = \mathbb{E}\big[(X - \mu)^2\big] = \begin{cases} \displaystyle \sum_{x} (x - \mu)^2 \, p(x) & \text{if } X \text{ is discrete,} \\[2ex] \displaystyle \int_{-\infty}^{\infty} (x - \mu)^2 f(x) \, dx & \text{if } X \text{ is continuous.} \end{cases}

Expanding the square and using linearity gives a shortcut that is usually easier to compute,

Var⁡(X)=E[X2]−(E[X])2.\operatorname{Var}(X) = \mathbb{E}[X^2] - (\mathbb{E}[X])^2.

For the d20, Var⁡(X)=143.5−110.25=33.25\operatorname{Var}(X) = 143.5 - 110.25 = 33.25. Because the variance is in squared units, we often report the standard deviation σ=Var⁡(X)\sigma = \sqrt{\operatorname{Var}(X)} instead, which has the same units as XX. A d20 roll has a standard deviation of about 5.775.77.

Shifting a random variable doesn't change its spread, and scaling it scales the standard deviation by the same factor,

Var⁡(aX+b)=a2Var⁡(X).\operatorname{Var}(aX + b) = a^2 \operatorname{Var}(X).

Covariance and Correlation​

The covariance of two random variables measures whether they tend to be above their means at the same time,

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

It is positive when large values of XX come with large values of YY, negative when large values of one come with small values of the other, and Cov⁡(X,X)=Var⁡(X)\operatorname{Cov}(X, X) = \operatorname{Var}(X). Its size depends on the units of XX and YY, so the correlation rescales it to lie between −1-1 and 11,

ρ(X,Y)=Cov⁡(X,Y)σX σY.\rho(X, Y) = \frac{\operatorname{Cov}(X, Y)}{\sigma_X \, \sigma_Y}.

A correlation of ±1\pm 1 means YY is an exact linear function of XX.

Independent random variables, whose values don't affect each other's probabilities, have zero covariance. The converse is false, because covariance only detects linear relationships. If XX is equally likely to be −1-1, 00 or 11 and Y=X2Y = X^2, then Cov⁡(X,Y)=E[X3]−E[X] E[X2]=0\operatorname{Cov}(X, Y) = \mathbb{E}[X^3] - \mathbb{E}[X]\,\mathbb{E}[X^2] = 0, even though YY is completely determined by XX.

Variance of a Sum​

Covariance is what makes variances of sums more complicated than expectations of sums,

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).

When XX and YY are independent the covariance term vanishes and the variances simply add. The two dice in a 2d6+3 damage roll are independent and each has variance 35/1235/12, so Var⁡(D)=35/12+35/12=35/6≈5.83\operatorname{Var}(D) = 35/12 + 35/12 = 35/6 \approx 5.83. The same rule for nn independent random variables, Var⁡(X1+⋯+Xn)=Var⁡(X1)+⋯+Var⁡(Xn)\operatorname{Var}(X_1 + \cdots + X_n) = \operatorname{Var}(X_1) + \cdots + \operatorname{Var}(X_n), is the reason averages become less noisy as nn grows, which is the subject of chapter 5.

Covariance Matrix​

For a random vector X=(X1,…,Xd)\mathbf{X} = (X_1, \dots, X_d), the mean vector μ=E[X]\boldsymbol{\mu} = \mathbb{E}[\mathbf{X}] collects the means of the coordinates, and the covariance matrix collects all their pairwise covariances,

Σ=E[(X−μ)(X−μ)⊤],Σij=Cov⁡(Xi,Xj).\boldsymbol{\Sigma} = \mathbb{E}\big[(\mathbf{X} - \boldsymbol{\mu})(\mathbf{X} - \boldsymbol{\mu})^\top\big], \qquad \Sigma_{ij} = \operatorname{Cov}(X_i, X_j).

The diagonal holds the variances of the coordinates. A covariance matrix is always symmetric and positive semidefinite. Linear transformations act on it in a simple way,

E[AX+b]=Aμ+b,Cov⁡(AX+b)=AΣA⊤.\mathbb{E}[\mathbf{A}\mathbf{X} + \mathbf{b}] = \mathbf{A}\boldsymbol{\mu} + \mathbf{b}, \qquad \operatorname{Cov}(\mathbf{A}\mathbf{X} + \mathbf{b}) = \mathbf{A}\boldsymbol{\Sigma}\mathbf{A}^\top.

The covariance matrix is the second parameter of the multivariate normal distribution.

Conditional Expectation​

Often we learn something about an outcome and want to update what we expect. The conditional expectation of XX given Y=yY = y is the expectation computed with the conditional distribution of XX once Y=yY = y is known,

E[X∣Y=y]=∑xx Pr⁡(X=x∣Y=y),Pr⁡(X=x∣Y=y)=Pr⁡(X=x,Y=y)Pr⁡(Y=y).\mathbb{E}[X \mid Y = y] = \sum_{x} x \, \Pr(X = x \mid Y = y), \qquad \Pr(X = x \mid Y = y) = \frac{\Pr(X = x, Y = y)}{\Pr(Y = y)}.

Back to the dragon fight. The paladin hits armor class 15 with probability 0.30.3, deals 2d6+3 damage on a hit and nothing on a miss (ignoring the extra dice on a critical hit). Let DD be the damage and HH indicate a hit. Then E[D∣H=1]=10\mathbb{E}[D \mid H = 1] = 10 and E[D∣H=0]=0\mathbb{E}[D \mid H = 0] = 0.

Before YY is observed, E[X∣Y]\mathbb{E}[X \mid Y] is itself a random variable, a function of YY that takes the value E[X∣Y=y]\mathbb{E}[X \mid Y = y] when Y=yY = y.

Law of Total Expectation​

Averaging the conditional expectations over the possible values of YY recovers the overall expectation,

E[X]=E[E[X∣Y]]=∑yE[X∣Y=y] Pr⁡(Y=y).\mathbb{E}[X] = \mathbb{E}\big[\mathbb{E}[X \mid Y]\big] = \sum_{y} \mathbb{E}[X \mid Y = y] \, \Pr(Y = y).

This is the law of total expectation. For the paladin, the expected damage per attack is E[D]=10×0.3+0×0.7=3\mathbb{E}[D] = 10 \times 0.3 + 0 \times 0.7 = 3.

Where this shows up

The value of a state in RL is a conditional expectation, vπ(s)=Eπ[Gt∣St=s]v_\pi(s) = \mathbb{E}_\pi[G_t \mid S_t = s]. The Bellman equation for vπv_\pi comes from applying the law of total expectation twice, first conditioning on the action and then on the next state and reward, together with linearity and the Markov property (see Markov Decision Processes). In supervised learning, the prediction of YY from X=xX = x that minimizes the expected squared error is E[Y∣X=x]\mathbb{E}[Y \mid X = x], which is what a regression model tries to approximate.

Recap​

  • The expectation is the probability-weighted average of a random variable, and E[g(X)]\mathbb{E}[g(X)] is computed by weighting g(x)g(x) with the probabilities of XX.
  • Expectation is linear for any random variables, dependent or not.
  • The variance is the expected squared distance from the mean, and Var⁡(X)=E[X2]−(E[X])2\operatorname{Var}(X) = \mathbb{E}[X^2] - (\mathbb{E}[X])^2.
  • Covariance measures linear co-movement, independence implies zero covariance but not the other way around, and variances of independent random variables add.
  • The covariance matrix collects the pairwise covariances of a random vector.
  • The conditional expectation E[X∣Y=y]\mathbb{E}[X \mid Y = y] updates the expectation once YY is known, and averaging it over YY gives back E[X]\mathbb{E}[X].

1. What is the expected damage of a 2d6+3 roll?

2. If Var⁡(X)=2\operatorname{Var}(X) = 2, what is Var⁡(3X+5)\operatorname{Var}(3X + 5)?

3. Two random variables have Cov⁡(X,Y)=0\operatorname{Cov}(X, Y) = 0. What can you conclude?

What's Next​

So far we have computed expectations from known distributions. In practice the distribution is unknown and we only have samples. Sampling Distributions and the Central Limit Theorem studies how well the average of a sample estimates the expectation.