Unit 7, lesson 49

Conditional Expectation and the Tower Rule

What conditional expectation is, how the conditional mean function becomes a random variable, why the law of total expectation recovers the overall mean, and why the conditional mean minimizes expected squared error.

  • Video 49
  • Conditional Expectation and Total Variance

A parcel service sends half of its parcels by standard routing and half by express, and a parcel is inspected with probability 0.60.6 overall. Once the route of a parcel is known, 0.60.6 is no longer the right average to use, because express parcels are inspected more often than standard ones. Conditional expectation is the average recomputed with the probabilities that apply after the information is known. This lesson computes it from a joint table, treats it as a random variable before the information arrives, shows that averaging it with the right weights recovers the overall mean, and explains why it is the best prediction under squared error.

Key terms

Conditional expectation

Definition Let XX and YY be discrete random variables on the same probability model, and let xx be a value with pX(x)>0p_X(x) > 0. The conditional expectation of YY given X=xX = x is the mean of the conditional PMF pY∣X(y∣x)=pX,Y(x,y)/pX(x)p_{Y \mid X}(y \mid x) = p_{X,Y}(x, y)/p_X(x),

E[Y∣X=x]=∑yy pY∣X(y∣x),\begin{aligned} &E[Y \mid X = x] \\[0.4em] &= \sum_{y} y \, p_{Y \mid X}(y \mid x), \end{aligned}

provided the sum converges absolutely, which is the case when E[∣Y∣]<∞E[\lvert Y \rvert] < \infty, in particular when YY has finitely many values. It is defined only for values with pX(x)>0p_X(x) > 0, because the conditional PMF divides by pX(x)p_X(x). In the same way, for an event BB with P(B)>0P(B) > 0, the conditional expectation E[Y∣B]E[Y \mid B] is the mean of YY computed with the conditional probabilities P(Y=y∣B)P(Y = y \mid B).

In plain words First update the probabilities with the information X=xX = x, by keeping one row of the joint table and rescaling it so that it adds to 11. Then take an ordinary weighted average of the values of YY with the updated weights. The result is a mean, so it need not be a value that YY can take.

Example In a constructed parcel model, X=0X = 0 for standard routing and X=1X = 1 for express, and Y=1Y = 1 when the parcel is inspected and Y=0Y = 0 otherwise.

y=0y = 0y=1y = 1pX(x)p_X(x)
x=0x = 00.30.30.20.20.50.5
x=1x = 10.10.10.40.40.50.5

Given standard routing, dividing the first row by 0.50.5 gives the conditional probabilities 0.60.6 and 0.40.4, so E[Y∣X=0]=0⋅0.6+1⋅0.4=0.4E[Y \mid X = 0] = 0 \cdot 0.6 + 1 \cdot 0.4 = 0.4. Given express routing, the second row becomes 0.20.2 and 0.80.8, and E[Y∣X=1]=0.8E[Y \mid X = 1] = 0.8. Neither 0.40.4 nor 0.80.8 is a value that the indicator YY can take.

Conditional mean function

Definition The conditional mean function of YY given XX is

m(x)=E[Y∣X=x],m(x) = E[Y \mid X = x],

defined for every xx with pX(x)>0p_X(x) > 0 at which this conditional expectation exists, which is every such xx when E[∣Y∣]<∞E[\lvert Y \rvert] < \infty, in particular when YY has finitely many values. Each m(x)m(x) is a fixed number. When YY is the indicator of an event, m(x)=P(Y=1∣X=x)m(x) = P(Y = 1 \mid X = x) is the conditional probability of that event. When YY is measured in a unit, such as minutes, m(x)m(x) is measured in the same unit. A value xx with pX(x)=0p_X(x) = 0 gets no conditional mean from the table, since its row and its row total are both 00.

In plain words The conditional mean function is a lookup table with one average for each value of XX that can actually occur. It answers the question of what YY is on average among the outcomes with that value of XX.

Example In the parcel model, m(0)=0.4m(0) = 0.4 and m(1)=0.8m(1) = 0.8, the inspection probabilities of standard and express parcels. In a second constructed model, a job belongs to group A or group B and its duration TT is measured in minutes. If its conditional mean duration is 22 minutes in group A and 66 minutes in group B, the conditional mean function of TT given the group takes the values 22 and 66 minutes.

Random conditional mean

Definition Putting the random variable XX into the conditional mean function gives the random variable

E[Y∣X]=m(X).E[Y \mid X] = m(X).

On an outcome where XX takes the value xx, it takes the value m(x)m(x). When m(x)m(x) exists at every xx with pX(x)>0p_X(x) > 0, for example when E[∣Y∣]<∞E[\lvert Y \rvert] < \infty, it is defined with probability 11, because a discrete XX takes such a value with probability 11. It is a function of XX, so its value is known as soon as XX is observed, and its distribution comes from that of XX. For each number vv, the probability P(m(X)=v)P(m(X) = v) is the sum of pX(x)p_X(x) over the values xx with m(x)=vm(x) = v.

In plain words Before the route of a parcel is known, the inspection probability that will apply to it is itself uncertain. The expression m(0)m(0) is a number, while m(X)m(X) is a random quantity whose value is revealed together with XX.

Example In the parcel model, E[Y∣X]E[Y \mid X] equals 0.40.4 when the parcel is standard and 0.80.8 when it is express, so it takes each of these values with probability 0.50.5. Its mean is 0.5⋅0.4+0.5⋅0.8=0.60.5 \cdot 0.4 + 0.5 \cdot 0.8 = 0.6, which is exactly E[Y]E[Y], the inspection probability of a parcel whose route is unknown. The next card shows that this is no coincidence. The random variable E[Y∣X]E[Y \mid X] is not YY itself, since YY is always 00 or 11.

Law of total expectation

Definition Let XX and YY be discrete random variables on the same probability model with E[∣Y∣]<∞E[\lvert Y \rvert] < \infty. Then

E[E[Y∣X]]=E[Y],E\bigl[E[Y \mid X]\bigr] = E[Y],

or, written out,

E[Y]=∑xpX(x) m(x),E[Y] = \sum_{x} p_X(x)\, m(x),

where m(x)=E[Y∣X=x]m(x) = E[Y \mid X = x] and the sum runs over the values xx with pX(x)>0p_X(x) > 0. This is also called the tower rule. For a finite table the proof is a regrouping of one double sum. Each conditional probability is a cell divided by its row total, so the row total cancels,

∑xpX(x)∑yy pX,Y(x,y)pX(x)=∑x∑yy pX,Y(x,y)=E[Y].\begin{aligned} &\sum_{x} p_X(x) \sum_{y} y \, \frac{p_{X,Y}(x, y)}{p_X(x)} \\[0.4em] &\quad = \sum_{x} \sum_{y} y \, p_{X,Y}(x, y) \\[0.4em] &\quad = E[Y]. \end{aligned}

Every cell contributes its value of yy times its probability exactly once, whether the sum is taken row by row or column by column. The same rule holds for groups A1,…,AkA_1, \dots, A_k that partition the sample space and have positive probabilities, as E[Y]=∑iP(Ai) E[Y∣Ai]E[Y] = \sum_{i} P(A_i)\, E[Y \mid A_i].

In plain words The overall mean is the average of the group means, weighted by how likely each group is. Equal weights give the overall mean when the groups are equally likely, and in general they do not.

Example In the parcel model, 0.5⋅0.4+0.5⋅0.8=0.6=E[Y]0.5 \cdot 0.4 + 0.5 \cdot 0.8 = 0.6 = E[Y]. For the jobs, let group A have probability 0.30.3 and group B probability 0.70.7, with conditional mean durations of 22 and 66 minutes. Then

E[T]=0.3⋅2+0.7⋅6=0.6+4.2=4.8\begin{aligned} E[T] &= 0.3 \cdot 2 + 0.7 \cdot 6 \\[0.4em] &= 0.6 + 4.2 = 4.8 \end{aligned}

minutes, not the midpoint 44 of the two group means. If the two groups were equally likely, the same group means would give 44 minutes.

Integrability

Definition A random variable YY is integrable if E[∣Y∣]<∞E[\lvert Y \rvert] < \infty. For a discrete YY this means ∑y∣y∣ pY(y)<∞\sum_y \lvert y \rvert \, p_Y(y) < \infty, and every variable with finitely many values is integrable. If YY is integrable, then every conditional expectation E[Y∣X=x]E[Y \mid X = x] with pX(x)>0p_X(x) > 0 is finite, because E[∣Y∣∣X=x]≤E[∣Y∣]/pX(x)E[\lvert Y \rvert \mid X = x] \le E[\lvert Y \rvert]/p_X(x), and the double sum in the tower rule converges absolutely, so it may be regrouped by rows or by columns. Integrability is a sufficient condition for the law of total expectation. Without it, the conditional means can all exist while E[Y]E[Y] does not.

In plain words Before regrouping an infinite sum of positive and negative terms, check that the sizes of the terms add to a finite total. Then summing by rows first or by columns first gives the same answer.

Example Let YY take the values nn and −n-n, each with probability 12n(n+1)\frac{1}{2n(n+1)}, for n=1,2,3,…n = 1, 2, 3, \dots, a distribution also studied in Expected Value, and let X=∣Y∣X = \lvert Y \rvert. Given X=nX = n, the variable YY equals nn or −n-n with probability 12\tfrac12 each, so every conditional mean is 00 and their weighted average is 00. Yet

E[∣Y∣]=∑n=1∞1n+1=∞,E[\lvert Y \rvert] = \sum_{n=1}^{\infty} \frac{1}{n+1} = \infty,

and E[Y]E[Y] is undefined, because its positive part and its negative part both add to infinity. The tower rule does not apply, and the 00 it would suggest is not a mean of YY.

Squared-error prediction

Definition Suppose E[Y2]<∞E[Y^2] < \infty, and let xx be a value with pX(x)>0p_X(x) > 0. For every real number aa,

E[(Y−a)2∣X=x]=Var⁡(Y∣X=x)+(m(x)−a)2,\begin{aligned} &E\bigl[(Y - a)^2 \mid X = x\bigr] \\[0.4em] &= \operatorname{Var}(Y \mid X = x) \\[0.4em] &\quad + \bigl(m(x) - a\bigr)^2, \end{aligned}

where Var⁡(Y∣X=x)=E[(Y−m(x))2∣X=x]\operatorname{Var}(Y \mid X = x) = E\bigl[(Y - m(x))^2 \mid X = x\bigr] is the variance of the conditional distribution. The identity follows by writing Y−aY - a as (Y−m(x))+(m(x)−a)\bigl(Y - m(x)\bigr) + \bigl(m(x) - a\bigr) and expanding, and the cross term vanishes because E[Y−m(x)∣X=x]=0E[Y - m(x) \mid X = x] = 0. So a=m(x)a = m(x) is the unique number that minimizes the conditional expected squared error. Averaging over xx shows that for every function gg defined at the values xx with pX(x)>0p_X(x) > 0,

E[(Y−g(X))2]≥E[(Y−m(X))2],\begin{aligned} &E\bigl[(Y - g(X))^2\bigr] \\[0.4em] &\quad \ge E\bigl[(Y - m(X))^2\bigr], \end{aligned}

with equality exactly when g(x)=m(x)g(x) = m(x) for every such xx. The finite second moment makes the conditional variances and the expected squared error of m(X)m(X) finite. The result is about squared error only. The conditional mean need not be the most likely value, and other losses have other best predictions, for example a median for absolute error.

In plain words If you must announce one number for YY after seeing XX and the penalty is the square of your miss, announce the conditional mean for that value of XX to make the average penalty as small as possible. Any other number adds the squared distance from the conditional mean to the spread that no prediction can remove.

Example For a standard parcel, predicting the number aa for the inspection indicator gives the expected squared error

0.6a2+0.4(1−a)2=0.24+(0.4−a)2.\begin{aligned} &0.6a^2 + 0.4(1 - a)^2 \\[0.4em] &= 0.24 + (0.4 - a)^2 . \end{aligned}

The best prediction is a=0.4a = 0.4, with error 0.240.24, while predicting 00, the more likely value, gives 0.40.4. For an express parcel the conditional variance is 0.8⋅0.2=0.160.8 \cdot 0.2 = 0.16. Using m(X)m(X) for every parcel gives the mean squared error 0.5⋅0.24+0.5⋅0.16=0.200.5 \cdot 0.24 + 0.5 \cdot 0.16 = 0.20, below the error Var⁡(Y)=0.24\operatorname{Var}(Y) = 0.24 of the best single number 0.60.6 chosen without seeing the route.

Check yourself

  1. A school has two sections. Section 1 has 1010 students with mean test score 7070, and section 2 has 3030 students with mean test score 9090. A student is chosen uniformly at random from all 4040. What is the expected score of the chosen student, and why is 8080 wrong?
Show the answer

The chosen student is in section 1 with probability 1040=14\tfrac{10}{40} = \tfrac14 and in section 2 with probability 34\tfrac34. The law of total expectation gives

14⋅70+34⋅90=17.5+67.5=85.\begin{aligned} &\tfrac14 \cdot 70 + \tfrac34 \cdot 90 \\[0.4em] &= 17.5 + 67.5 = 85 . \end{aligned}

The value 8080 averages the two section means with equal weights, which would be right only if the sections were equally likely to be chosen. Here section 2 is three times as likely.

  1. A device runs in mode A with probability 34\tfrac34 and in mode B with probability 14\tfrac14. In mode A a task takes 22 minutes with probability 34\tfrac34 and 66 minutes with probability 14\tfrac14. In mode B these two probabilities are reversed. Find the conditional mean duration in each mode and the overall mean duration.
Show the answer

In mode A the conditional mean is 2⋅34+6⋅14=32 \cdot \tfrac34 + 6 \cdot \tfrac14 = 3 minutes, and in mode B it is 2⋅14+6⋅34=52 \cdot \tfrac14 + 6 \cdot \tfrac34 = 5 minutes. The tower rule weights them by the mode probabilities,

E[T]=34⋅3+14⋅5=72,E[T] = \tfrac34 \cdot 3 + \tfrac14 \cdot 5 = \tfrac72 ,

so the overall mean is 3.53.5 minutes. As a check, P(T=2)=34⋅34+14⋅14=58P(T = 2) = \tfrac34 \cdot \tfrac34 + \tfrac14 \cdot \tfrac14 = \tfrac58 and P(T=6)=38P(T = 6) = \tfrac38, so E[T]=108+188=72E[T] = \tfrac{10}{8} + \tfrac{18}{8} = \tfrac72. Equal weights would give 44 minutes, but mode A occurs three times as often as mode B.

  1. The joint table of XX and YY has the row 0.20.2, 0.20.2, 00 for x=0x = 0 and the row 00, 0.30.3, 0.30.3 for x=1x = 1, in the columns y=0y = 0, 11 and 22. Find m(0)m(0), m(1)m(1) and the distribution of E[Y∣X]E[Y \mid X], and check the tower rule. Which prediction of YY minimizes the expected squared error when X=1X = 1, and what is that error?
Show the answer

The row totals are pX(0)=0.4p_X(0) = 0.4 and pX(1)=0.6p_X(1) = 0.6. Then m(0)=(1⋅0.2)/0.4=0.5m(0) = (1 \cdot 0.2)/0.4 = 0.5 and m(1)=(1⋅0.3+2⋅0.3)/0.6=1.5m(1) = (1 \cdot 0.3 + 2 \cdot 0.3)/0.6 = 1.5, so E[Y∣X]E[Y \mid X] equals 0.50.5 with probability 0.40.4 and 1.51.5 with probability 0.60.6. Its mean is 0.4⋅0.5+0.6⋅1.5=1.10.4 \cdot 0.5 + 0.6 \cdot 1.5 = 1.1, and directly E[Y]=1⋅0.5+2⋅0.3=1.1E[Y] = 1 \cdot 0.5 + 2 \cdot 0.3 = 1.1, as the tower rule requires. The equal-weight average 12(0.5+1.5)=1\tfrac12(0.5 + 1.5) = 1 is wrong because the rows have different probabilities. When X=1X = 1, the value YY is 11 or 22 with probability 12\tfrac12 each, so the best prediction is m(1)=1.5m(1) = 1.5, even though YY never equals 1.51.5. Its expected squared error is the conditional variance 12(0.5)2+12(0.5)2=0.25\tfrac12 (0.5)^2 + \tfrac12 (0.5)^2 = 0.25, while predicting 11 or 22 gives 0.25+0.25=0.50.25 + 0.25 = 0.5.

How this lesson connects