4.2 Computing expectations by conditioning

As noted above, \(E(X \mid Y)\) is a random variable, a function of \(Y\) whose value at \(Y = y\) is \(E(X \mid Y = y)\). Its expectation is the expectation of \(X\).

Result 4.2.1. For random variables \(X\) and \(Y\), \[ E(X) = E\bigl (E(X \mid Y)\bigr ) . \] That is, \[ E(X) = \begin {cases} \displaystyle \int E(X \mid Y = y)f_Y(y)\,dy , & \text {if } Y \text { is continuous} , \\[2ex] \displaystyle \sum _yE(X \mid Y = y)P(Y = y) , & \text {if } Y \text { is discrete.} \end {cases} \]

Proof. We give the proof for discrete \(Y\) (and discrete \(X\)); the continuous case replaces sums by integrals. \begin {align*} \sum _yE(X \mid Y = y)P(Y = y) &= \sum _y\sum _xx\,f_{X\mid Y}(x \mid y)f_Y(y) \\ &= \sum _y\sum _xx\,f_{X,Y}(x, y) = \sum _xx\sum _yf_{X,Y}(x, y) \\ &= \sum _xx\,f_X(x) = E(X) . \end {align*} □

Example 4.2.2. A miner is trapped in a mine with three doors. The first door leads to a tunnel that takes him to safety after \(3\) hours of walking. The second door leads to a tunnel that returns him to the mine after \(5\) hours, and the third to a tunnel that returns him to the mine after \(7\) hours. If the miner is at all times equally likely to choose any of the doors, what is the expected time until he reaches safety?

Solution. Let \(X\) be the time until he reaches safety and \(Y\) the door he chooses first. If he chooses door \(2\) or \(3\), he is back where he started after \(5\) or \(7\) hours, so \(E(X \mid Y = 2) = 5 + E(X)\) and \(E(X \mid Y = 3) = 7 + E(X)\). By Result 4.2.1, \begin {align*} E(X) &= E\bigl (E(X \mid Y)\bigr ) \\ &= E(X \mid Y = 1)P(Y = 1) + E(X \mid Y = 2)P(Y = 2) \\ &\qquad + E(X \mid Y = 3)P(Y = 3) \\ &= \tfrac 13\bigl (3 + 5 + E(X) + 7 + E(X)\bigr ) , \end {align*}

so \(3E(X) = 15 + 2E(X)\), and \(E(X) = 15\) hours.

Example 4.2.3. An urn contains \(n\) white balls and \(m\) black balls. Balls are drawn at random, one at a time and without replacement, until the first white ball is drawn. Find the expected number of black balls drawn.

Solution. Let \(X\) be the number of black balls drawn, and write \(N(n, m) = E(X)\). Condition on the first draw: let \(Y = 1\) if it is white and \(Y = 0\) if it is black, so \[ P(Y = 1) = \frac {n}{n + m} , \qquad P(Y = 0) = \frac {m}{n + m} . \] If the first ball is white, no black balls are drawn: \(E(X \mid Y = 1) = 0\). If it is black, one black ball has been drawn and the urn now holds \(n\) white and \(m - 1\) black balls: \(E(X \mid Y = 0) = 1 + N(n, m - 1)\). Hence \begin {align*} N(n, m) &= 0 \cdot \frac {n}{n + m} + \bigl (1 + N(n, m - 1)\bigr )\frac {m}{n + m} \\ &= \bigl (1 + N(n, m - 1)\bigr )\frac {m}{n + m} . \end {align*}

With \(N(n, 0) = 0\), \begin {align*} N(n, 1) &= \bigl (1 + 0\bigr )\frac {1}{n + 1} = \frac {1}{n + 1} , \\ N(n, 2) &= \Bigl (1 + \frac {1}{n + 1}\Bigr )\frac {2}{n + 2} = \frac {n + 2}{n + 1}\cdot \frac {2}{n + 2} = \frac {2}{n + 1} , \\ N(n, 3) &= \Bigl (1 + \frac {2}{n + 1}\Bigr )\frac {3}{n + 3} = \frac {n + 3}{n + 1}\cdot \frac {3}{n + 3} = \frac {3}{n + 1} . \end {align*}

The pattern is \(N(n, m) = m/(n + 1)\), and it holds in general by induction: if \(N(n, m - 1) = (m - 1)/(n + 1)\), then \(N(n, m) = \frac {n + m}{n + 1}\cdot \frac {m}{n + m} = \frac {m}{n + 1}\). So \(E(X) = m/(n + 1)\).

Example 4.2.4. Let \(X\) be the number of independent trials needed to get the first success, when each trial succeeds with probability \(p\), so that \(X\) has the geometric distribution \[ P(X = x) = p(1 - p)^{x-1} , \qquad x = 1, 2, \dots , \] with \(E(X) = 1/p\). Find \(\Var (X)\) by conditioning on the first trial.

Solution. Let \(Y = 1\) if the first trial is a success and \(Y = 0\) otherwise, so \(P(Y = 1) = p\) and \(P(Y = 0) = 1 - p\). If the first trial succeeds, \(X = 1\). If it fails, the process starts afresh after one wasted trial, so given \(Y = 0\), \(X\) has the distribution of \(1 + X\). Hence \begin {align*} E(X^2) &= E(X^2 \mid Y = 0)P(Y = 0) + E(X^2 \mid Y = 1)P(Y = 1) \\ &= E\bigl ((1 + X)^2\bigr )(1 - p) + 1^2 \cdot p \\ &= \bigl (1 + 2E(X) + E(X^2)\bigr )(1 - p) + p \\ &= \Bigl (1 + \frac 2p + E(X^2)\Bigr )(1 - p) + p . \end {align*}

Collecting the \(E(X^2)\) terms, \(p\,E(X^2) = (1 - p) + \frac {2(1 - p)}{p} + p = \frac {2 - p}{p}\), so \[ E(X^2) = \frac {2 - p}{p^2} , \] and \[ \Var (X) = E(X^2) - \bigl (E(X)\bigr )^2 = \frac {2 - p}{p^2} - \frac {1}{p^2} = \frac {1 - p}{p^2} . \]

Directly, by differentiating a series

Writing \(x^2 = x(x - 1) + x\), \begin {align*} E(X^2) &= \sum _{x=1}^\infty x^2p(1 - p)^{x-1} \\ &= p(1 - p)\sum _{x=1}^\infty x(x - 1)(1 - p)^{x-2} + \sum _{x=1}^\infty xp(1 - p)^{x-1} \\ &= p(1 - p)\sum _{x=1}^\infty \frac {d^2}{dp^2}(1 - p)^x - p\sum _{x=1}^\infty \frac {d}{dp}(1 - p)^x \\ &= p(1 - p)\frac {d^2}{dp^2}\sum _{x=1}^\infty (1 - p)^x - p\frac {d}{dp}\sum _{x=1}^\infty (1 - p)^x . \end {align*}

Since \(\sum _{x=1}^\infty (1 - p)^x = \frac {1}{1 - (1 - p)} - 1 = \frac 1p - 1\), \[ E(X^2) = p(1 - p)\cdot \frac {2}{p^3} - p\Bigl (-\frac {1}{p^2}\Bigr ) = \frac {2(1 - p)}{p^2} + \frac 1p = \frac {2 - p}{p^2} , \] as before.

Questions on this section

Stuck on something here? Ask below and it stays attached to this topic.