Skip to content
LeetQuant
F4 Random Variables and Expectation

F4 · Random Variables and Expectation · Lesson 2 of 8

Conditional Expectation

The tower property, conditioning on the first step, and Wald's identity.

25 min

Conditioning on a random variable

E[X∣Y]\mathbb{E}[X \mid Y] is the best guess of XX once you know YY. It is itself a random variable: a function of YY. If YY is a die roll, E[X∣Y]\mathbb{E}[X \mid Y] takes six values, one for each face.

Three rules do almost all the work.

  • Tower property: E[E[X∣Y]]=E[X]\mathbb{E}\big[\mathbb{E}[X \mid Y]\big] = \mathbb{E}[X]. Average the conditional answers, weighted by how likely each condition is.
  • Taking out what is known: E[g(Y) X∣Y]=g(Y) E[X∣Y]\mathbb{E}[g(Y)\,X \mid Y] = g(Y)\,\mathbb{E}[X \mid Y].
  • Law of total variance: Var⁡(X)=E[Var⁡(X∣Y)]+Var⁡(E[X∣Y]).\operatorname{Var}(X) = \mathbb{E}\big[\operatorname{Var}(X \mid Y)\big] + \operatorname{Var}\big(\mathbb{E}[X \mid Y]\big). Total uncertainty is the noise left after learning YY, plus the uncertainty about what you will learn.

Worked example

roll a die, then flip that many coins

Roll a fair die and call the result NN. Then flip NN fair coins and let HH be the number of heads. Find E[H]\mathbb{E}[H] and Var⁡(H)\operatorname{Var}(H).

Show solution

Given NN, HH is binomial: E[H∣N]=N/2\mathbb{E}[H \mid N] = N/2 and Var⁡(H∣N)=N/4\operatorname{Var}(H \mid N) = N/4.

By the tower property, E[H]=E[N]/2=3.5/2=1.75\mathbb{E}[H] = \mathbb{E}[N]/2 = 3.5/2 = 1.75.

By total variance, with Var⁡(N)=35/12\operatorname{Var}(N) = 35/12,

Var⁡(H)=E[N4]+Var⁡(N2)=3.54+35/124=0.875+0.729≈1.604.\operatorname{Var}(H) = \mathbb{E}\Big[\frac N4\Big] + \operatorname{Var}\Big(\frac N2\Big) = \frac{3.5}{4} + \frac{35/12}{4} = 0.875 + 0.729 \approx 1.604 .

Conditioning on the first step

In a game or a process, condition on what happens next. If V(s)V(s) is the value from state ss,

V(s)=E[ reward now+V(next state)  ∣  s ].V(s) = \mathbb{E}\big[\,\text{reward now} + V(\text{next state}) \;\big|\; s\,\big].

For optimal stopping games (stop or continue), compare the value of stopping now with the value of continuing, and take the larger. Solve from the end backwards: the last decision is easy, and each earlier one uses the value of the one after it.

Random sums and Wald's identity

Let X1,X2,…X_1, X_2, \dots be i.i.d. with mean μ\mu, and let NN be a stopping time: whether you stop after step nn depends only on X1,…,XnX_1, \dots, X_n. If E[N]<∞\mathbb{E}[N] < \infty, then

E[∑i=1NXi]=E[N] μ.\mathbb{E}\Big[\sum_{i=1}^{N} X_i\Big] = \mathbb{E}[N]\,\mu .

The surprising part is that NN may depend on the XiX_i already seen. It may not peek into the future: "stop just before the first 6" is not a stopping time.

Gambler's ruin

A walk moves +1+1 with probability pp and −1-1 with probability q=1−pq = 1 - p, starting at kk and stopping at 00 or NN. Let PkP_k be the probability of reaching NN. First-step analysis gives Pk=p Pk+1+q Pk−1P_k = p\,P_{k+1} + q\,P_{k-1} with P0=0P_0 = 0, PN=1P_N = 1. The solution is

Pk={kN,p=12,1−(q/p)k1−(q/p)N,p≠12.P_k = \begin{cases} \dfrac{k}{N}, & p = \tfrac12, \\[2ex] \dfrac{1 - (q/p)^k}{1 - (q/p)^N}, & p \neq \tfrac12. \end{cases}

A faster route uses martingales. For the fair walk, SnS_n is a martingale, so E[ST]=k\mathbb{E}[S_T] = k, giving N Pk=kN\,P_k = k. And Sn2−nS_n^2 - n is a martingale, so E[T]=E[ST2]−k2=N2⋅kN−k2=k(N−k)\mathbb{E}[T] = \mathbb{E}[S_T^2] - k^2 = N^2 \cdot \tfrac kN - k^2 = k(N - k). For the biased walk, (q/p)Sn(q/p)^{S_n} is a martingale and gives the second formula directly.

Worked example

a favourable walk

You start with $2 and bet $1 at a time, winning each bet with probability 23\tfrac23. You stop at $5 or at $0. What is the probability you reach $5?

Show solution

Here q/p=1/32/3=12q/p = \tfrac{1/3}{2/3} = \tfrac12, k=2k = 2, N=5N = 5:

P2=1−(1/2)21−(1/2)5=3/431/32=2431≈0.774.P_2 = \frac{1 - (1/2)^2}{1 - (1/2)^5} = \frac{3/4}{31/32} = \frac{24}{31} \approx 0.774 .

Check the limits: as N→∞N \to \infty, Pk→1−(q/p)k=34P_k \to 1 - (q/p)^k = \tfrac34, the probability of never going broke.

Knowledge check

You roll a die until the first 6 appears, and add up every roll including the 6. What is the expected total?

Knowledge check

A fair ±1 random walk starts at 3 and stops when it hits 0 or 10. What is the expected number of steps?

Knowledge check

XX and YY are i.i.d. continuous random variables. What is E[X∣X+Y=s]\mathbb{E}[X \mid X + Y = s]?

Practice problems

Finished reading?

Answer the 3 knowledge checks to complete this lesson (0 done).