Practice / Probability for ML

Conditional expectation and the law of total variance

Ten problems on conditional expectation: E[Y | X = x] from a joint table, E[Y | X] as a random variable with its own mean and variance, the law of total variance on the same table, the mean and variance of a two-component Gaussian mixture, a random sum of purchases with a Poisson count, E[Y | X] from the joint density x + y, E[Y | X] as the best mean-squared-error predictor with a residual uncorrelated with every function of X, the conditional mean and variance of a bivariate Gaussian, Rao–Blackwell variance reduction for a Monte Carlo estimate, and the noise that dropout adds split from the spread of the data, with worked solutions and the mistakes of averaging the conditional variances only, evaluating E[Y | X] at the mean of X, inverting a regression line, taking E[N]σ² as the variance of a random sum and averaging conditional means without their weights.

Before you start

Conditional expectation answers the question "what is the average of YY once I know XX?". For each value xx it is a number, the mean of YY in the slice X=xX = x; taken over all values of XX at once it is a random variable, a function of XX. That second view is what makes it useful. Averaging it gives back E[Y]E[Y], its spread and the spread left inside each slice add up to Var⁡(Y)\operatorname{Var}(Y), and it is the best possible prediction of YY from XX under squared error, which is why a regression network trained on mean-squared error is learning E[Y∣X]E[Y \mid X]. The same two laws compute the moments of mixtures and random sums, give the regression line of a Gaussian, and explain why replacing a noisy sample by its conditional mean, as Rao–Blackwellization and many gradient estimators do, can only lower the variance. These ten problems work each of them by hand.

  • For discrete variables, E[Y∣X=x]=∑yyPr⁡(Y=y∣X=x)E[Y \mid X = x] = \sum_y y\Pr(Y = y \mid X = x), with Pr⁡(Y=y∣X=x)=Pr⁡(X=x,Y=y)/Pr⁡(X=x)\Pr(Y = y \mid X = x) = \Pr(X = x, Y = y)/\Pr(X = x). For densities, E[Y∣X=x]=∫y f(x,y) dy / fX(x)E[Y \mid X = x] = \int y\,f(x, y)\,dy \,/\, f_X(x).
  • Writing g(x)=E[Y∣X=x]g(x) = E[Y \mid X = x], the conditional expectation E[Y∣X]E[Y \mid X] is the random variable g(X)g(X): it is not a number until a value of XX is plugged in.
  • Tower law. E[E[Y∣X]]=E[Y]E\big[E[Y \mid X]\big] = E[Y], and more generally E[h(X) Y]=E[h(X) E[Y∣X]]E[h(X)\,Y] = E\big[h(X)\,E[Y \mid X]\big] for any function hh.
  • Law of total variance. Var⁡(Y)=E[Var⁡(Y∣X)]+Var⁡(E[Y∣X])\operatorname{Var}(Y) = E\big[\operatorname{Var}(Y \mid X)\big] + \operatorname{Var}\big(E[Y \mid X]\big): the average spread within the slices plus the spread of the slice means.
  • Best predictor. Among all functions gg, E[(Y−g(X))2]E[(Y - g(X))^2] is smallest for g(X)=E[Y∣X]g(X) = E[Y \mid X], and the residual Y−E[Y∣X]Y - E[Y \mid X] has mean 00 given XX, so it is uncorrelated with every h(X)h(X).
  • Random sums. If S=X1+⋯+XNS = X_1 + \dots + X_N with the XiX_i independent of NN and of each other, each with mean μ\mu and variance σ2\sigma^2, then E[S]=E[N]μE[S] = E[N]\mu and Var⁡(S)=E[N]σ2+Var⁡(N)μ2\operatorname{Var}(S) = E[N]\sigma^2 + \operatorname{Var}(N)\mu^2.
  • Bivariate Gaussian. E[Y∣X=x]=μY+ρσYσX(x−μX)E[Y \mid X = x] = \mu_Y + \rho\frac{\sigma_Y}{\sigma_X}(x - \mu_X) and Var⁡(Y∣X)=σY2(1−ρ2)\operatorname{Var}(Y \mid X) = \sigma_Y^2(1 - \rho^2), the same for every xx.

Builds on: Discrete random variables: expectation, variance and the binomial, geometric and Poisson, Continuous random variables: densities, CDFs, expectation and the uniform, exponential and normal, Variance, covariance and correlation

Problems

  1. ·

    The joint distribution of XX and YY is

    Y=1Y=2Y=3X=00.20.10.1X=10.10.20.3\begin{array}{c|ccc} & Y = 1 & Y = 2 & Y = 3 \\ \hline X = 0 & 0.2 & 0.1 & 0.1 \\ X = 1 & 0.1 & 0.2 & 0.3 \end{array}

    Find E[Y∣X=0]E[Y \mid X = 0], E[Y∣X=1]E[Y \mid X = 1] and E[Y]E[Y], and check that the conditional means average to E[Y]E[Y].

  2. ·

    For the table of Problem 1, write E[Y∣X]E[Y \mid X] as a function of XX. Find its mean and variance, and use the tower law to get E[XY]E[XY] and Cov⁡(X,Y)\operatorname{Cov}(X, Y).

  3. ··

    For the table of Problem 1, find Var⁡(Y∣X=0)\operatorname{Var}(Y \mid X = 0) and Var⁡(Y∣X=1)\operatorname{Var}(Y \mid X = 1), and check the law of total variance against Var⁡(Y)\operatorname{Var}(Y) computed directly.

  4. ··

    A model draws each output from a two-component mixture: with probability 0.30.3 from N(0,1)N(0, 1), and with probability 0.70.7 from N(4,22)N(4, 2^2). Find the mean and variance of the output.

  5. ··

    The number of customers in an hour is Poisson with mean 44, and each customer spends an amount with mean 20 dollars and standard deviation 5 dollars, independently of the others and of how many come. Find the mean and standard deviation of the hour's revenue SS.

  6. ··

    XX and YY have joint density f(x,y)=x+yf(x, y) = x + y on the unit square 0≤x,y≤10 \le x, y \le 1. Find fXf_X, the conditional mean E[Y∣X=x]E[Y \mid X = x] and its values at x=0x = 0 and x=1x = 1, and check the tower law.

  7. ···

    For the table of Problem 1, compare three predictors of YY from XX by mean-squared error: the constant E[Y]E[Y], the conditional mean E[Y∣X]E[Y \mid X], and g(X)=2+0.5Xg(X) = 2 + 0.5X. Show that the residual R=Y−E[Y∣X]R = Y - E[Y \mid X] is uncorrelated with XX.

  8. ··

    Heights XX (cm) and weights YY (kg) are bivariate Gaussian with μX=170\mu_X = 170, σX=10\sigma_X = 10, μY=70\mu_Y = 70, σY=12\sigma_Y = 12 and correlation ρ=0.5\rho = 0.5. Find the mean and standard deviation of the weight of a person 185185 cm tall, and check the law of total variance.

  9. ···

    To estimate E[Y]E[Y] where X∼Uniform(0,1)X \sim \text{Uniform}(0, 1) and Y∣X∼N(X,1)Y \mid X \sim N(X, 1), one can average n=100n = 100 samples of YY, or average E[Y∣Xi]=XiE[Y \mid X_i] = X_i over 100100 samples of XX. Find the variance of one term of each estimator, their standard errors, and how many samples of YY match the second estimator's accuracy.

  10. ···

    A unit computes s=∑imiwixi/0.8s = \sum_i m_iw_ix_i/0.8 with weights w=(1,−2,3)w = (1, -2, 3) and independent dropout masks mi∼Bernoulli(0.8)m_i \sim \text{Bernoulli}(0.8). The input is xa=(2,1,1)x_a = (2, 1, 1) or xb=(1,1,0)x_b = (1, 1, 0) with probability 12\tfrac12 each. Find E[s∣x]E[s \mid x] and Var⁡(s∣x)\operatorname{Var}(s \mid x) for each input, then E[s]E[s] and Var⁡(s)\operatorname{Var}(s), and the share of Var⁡(s)\operatorname{Var}(s) that dropout causes.

Worked solutions

Problem 1

The joint distribution of XX and YY is

Y=1Y=2Y=3X=00.20.10.1X=10.10.20.3\begin{array}{c|ccc} & Y = 1 & Y = 2 & Y = 3 \\ \hline X = 0 & 0.2 & 0.1 & 0.1 \\ X = 1 & 0.1 & 0.2 & 0.3 \end{array}

Find E[Y∣X=0]E[Y \mid X = 0], E[Y∣X=1]E[Y \mid X = 1] and E[Y]E[Y], and check that the conditional means average to E[Y]E[Y].

  1. Pr⁡(X=0)=0.4\Pr(X = 0) = 0.4, Pr⁡(X=1)=0.6\Pr(X = 1) = 0.6Row sums of the table.
  2. E[Y∣X=0]=1⋅0.2+2⋅0.1+3⋅0.10.4=0.70.4=1.75E[Y \mid X = 0] = \dfrac{1\cdot0.2 + 2\cdot0.1 + 3\cdot0.1}{0.4} = \dfrac{0.7}{0.4} = 1.75Divide the row by its total to get the conditional distribution, then take its mean.
  3. E[Y∣X=1]=1⋅0.1+2⋅0.2+3⋅0.30.6=1.40.6=73E[Y \mid X = 1] = \dfrac{1\cdot0.1 + 2\cdot0.2 + 3\cdot0.3}{0.6} = \dfrac{1.4}{0.6} = \dfrac73The same in the second row.
  4. E[Y]=0.7+1.4=2.1E[Y] = 0.7 + 1.4 = 2.1The column totals are 0.30.3, 0.30.3, 0.40.4, and 1⋅0.3+2⋅0.3+3⋅0.4=2.11\cdot0.3 + 2\cdot0.3 + 3\cdot0.4 = 2.1; the two numerators of steps 2 and 3 add to the same thing.
  5. E[Y∣X=0]=1.75E[Y \mid X = 0] = 1.75, E[Y∣X=1]=73≈2.333E[Y \mid X = 1] = \tfrac73 \approx 2.333, E[Y]=0.4⋅1.75+0.6⋅73=2.1E[Y] = 0.4\cdot1.75 + 0.6\cdot\tfrac73 = 2.1The tower law: each conditional mean is weighted by how likely its slice is. The plain average of 1.751.75 and 73\tfrac73 is 4924≈2.042\tfrac{49}{24} \approx 2.042 (Mistake 5).

Problem 2

For the table of Problem 1, write E[Y∣X]E[Y \mid X] as a function of XX. Find its mean and variance, and use the tower law to get E[XY]E[XY] and Cov⁡(X,Y)\operatorname{Cov}(X, Y).

  1. E[Y∣X]=74+712XE[Y \mid X] = \tfrac74 + \tfrac{7}{12}XXX takes only the values 00 and 11, so a line through (0,1.75)(0, 1.75) and (1,73)(1, \tfrac73) matches both: 73−74=712\tfrac73 - \tfrac74 = \tfrac{7}{12}.
  2. E[E[Y∣X]]=74+712⋅0.6=2.1E\big[E[Y \mid X]\big] = \tfrac74 + \tfrac{7}{12}\cdot0.6 = 2.1E[X]=0.6E[X] = 0.6; this is E[Y]E[Y] again.
  3. Var⁡(E[Y∣X])=(712)2Var⁡(X)=49144⋅0.24=49600≈0.0817\operatorname{Var}\big(E[Y \mid X]\big) = \big(\tfrac{7}{12}\big)^2\operatorname{Var}(X) = \tfrac{49}{144}\cdot0.24 = \tfrac{49}{600} \approx 0.0817Var⁡(X)=0.6⋅0.4\operatorname{Var}(X) = 0.6\cdot0.4 for a 0/10/1 variable; the constant 74\tfrac74 does not change the variance.
  4. E[XY]=E[X E[Y∣X]]=0.6⋅73=1.4E[XY] = E\big[X\,E[Y \mid X]\big] = 0.6\cdot\tfrac73 = 1.4X E[Y∣X]X\,E[Y \mid X] is 00 when X=0X = 0 and 73\tfrac73 when X=1X = 1. Directly: 1⋅(0.1⋅1+0.2⋅2+0.3⋅3)=1.41\cdot(0.1\cdot1 + 0.2\cdot2 + 0.3\cdot3) = 1.4.
  5. E[Y∣X]=74+712XE[Y \mid X] = \tfrac74 + \tfrac{7}{12}X, with mean 2.12.1 and variance 49600\tfrac{49}{600}; E[XY]=1.4E[XY] = 1.4 and Cov⁡(X,Y)=1.4−0.6⋅2.1=0.14\operatorname{Cov}(X, Y) = 1.4 - 0.6\cdot2.1 = 0.14The slope is Cov⁡(X,Y)/Var⁡(X)=0.14/0.24=712\operatorname{Cov}(X, Y)/\operatorname{Var}(X) = 0.14/0.24 = \tfrac{7}{12}, the regression slope, as it must be when the conditional mean is a straight line.

Problem 3

For the table of Problem 1, find Var⁡(Y∣X=0)\operatorname{Var}(Y \mid X = 0) and Var⁡(Y∣X=1)\operatorname{Var}(Y \mid X = 1), and check the law of total variance against Var⁡(Y)\operatorname{Var}(Y) computed directly.

  1. E[Y2∣X=0]=0.2+0.4+0.90.4=3.75E[Y^2 \mid X = 0] = \dfrac{0.2 + 0.4 + 0.9}{0.4} = 3.75, so Var⁡(Y∣X=0)=3.75−1.752=1116\operatorname{Var}(Y \mid X = 0) = 3.75 - 1.75^2 = \tfrac{11}{16}12⋅0.2+22⋅0.1+32⋅0.1=1.51^2\cdot0.2 + 2^2\cdot0.1 + 3^2\cdot0.1 = 1.5, divided by 0.40.4.
  2. E[Y2∣X=1]=0.1+0.8+2.70.6=6E[Y^2 \mid X = 1] = \dfrac{0.1 + 0.8 + 2.7}{0.6} = 6, so Var⁡(Y∣X=1)=6−499=59\operatorname{Var}(Y \mid X = 1) = 6 - \tfrac{49}{9} = \tfrac59The second row: 3.6/0.63.6/0.6.
  3. E[Var⁡(Y∣X)]=0.4⋅1116+0.6⋅59=0.275+13=73120≈0.6083E\big[\operatorname{Var}(Y \mid X)\big] = 0.4\cdot\tfrac{11}{16} + 0.6\cdot\tfrac59 = 0.275 + \tfrac13 = \tfrac{73}{120} \approx 0.6083The within-slice spread, weighted by the slice probabilities.
  4. E[Var⁡(Y∣X)]+Var⁡(E[Y∣X])=73120+49600=414600=0.69E\big[\operatorname{Var}(Y \mid X)\big] + \operatorname{Var}\big(E[Y \mid X]\big) = \tfrac{73}{120} + \tfrac{49}{600} = \tfrac{414}{600} = 0.69The between-slice spread from Problem 2.
  5. Var⁡(Y∣X=0)=1116\operatorname{Var}(Y \mid X = 0) = \tfrac{11}{16}, Var⁡(Y∣X=1)=59\operatorname{Var}(Y \mid X = 1) = \tfrac59, and 0.6083+0.0817=0.69=Var⁡(Y)0.6083 + 0.0817 = 0.69 = \operatorname{Var}(Y)Directly, E[Y2]=1.5+3.6=5.1E[Y^2] = 1.5 + 3.6 = 5.1 and 5.1−2.12=0.695.1 - 2.1^2 = 0.69. Stopping at step 3 gives 0.60830.6083, which is too small (Mistake 1).

Problem 4

A model draws each output from a two-component mixture: with probability 0.30.3 from N(0,1)N(0, 1), and with probability 0.70.7 from N(4,22)N(4, 2^2). Find the mean and variance of the output.

  1. Let ZZ be the component, so E[Y∣Z]∈{0,4}E[Y \mid Z] \in \{0, 4\} and Var⁡(Y∣Z)∈{1,4}\operatorname{Var}(Y \mid Z) \in \{1, 4\}Condition on the hidden label; given it, YY is a single Gaussian.
  2. E[Y]=0.3⋅0+0.7⋅4=2.8E[Y] = 0.3\cdot0 + 0.7\cdot4 = 2.8The tower law over ZZ.
  3. E[Var⁡(Y∣Z)]=0.3⋅1+0.7⋅4=3.1E\big[\operatorname{Var}(Y \mid Z)\big] = 0.3\cdot1 + 0.7\cdot4 = 3.1The average within-component variance. Note 22=42^2 = 4: the second component has standard deviation 22.
  4. Var⁡(E[Y∣Z])=0.3⋅0.7⋅(4−0)2=3.36\operatorname{Var}\big(E[Y \mid Z]\big) = 0.3\cdot0.7\cdot(4 - 0)^2 = 3.36A two-valued variable taking values aa and bb with probabilities pp and 1−p1 - p has variance p(1−p)(b−a)2p(1 - p)(b - a)^2.
  5. E[Y]=2.8E[Y] = 2.8 and Var⁡(Y)=3.1+3.36=6.46\operatorname{Var}(Y) = 3.1 + 3.36 = 6.46, a standard deviation of about 2.542.54Check with second moments: E[Y2]=0.3⋅1+0.7⋅(4+16)=14.3E[Y^2] = 0.3\cdot1 + 0.7\cdot(4 + 16) = 14.3, and 14.3−2.82=6.4614.3 - 2.8^2 = 6.46. The mixture is wider than either component because its centers are far apart.

Problem 5

The number of customers in an hour is Poisson with mean 44, and each customer spends an amount with mean 20 dollars and standard deviation 5 dollars, independently of the others and of how many come. Find the mean and standard deviation of the hour's revenue SS.

  1. E[S∣N]=20NE[S \mid N] = 20N and Var⁡(S∣N)=25N\operatorname{Var}(S \mid N) = 25NGiven N=nN = n, SS is a sum of nn independent amounts: means add and variances add.
  2. E[S]=E[20N]=20⋅4=80E[S] = E[20N] = 20\cdot4 = 80The tower law.
  3. E[Var⁡(S∣N)]=25 E[N]=100E\big[\operatorname{Var}(S \mid N)\big] = 25\,E[N] = 100The spread of the amounts, for the average number of customers.
  4. Var⁡(E[S∣N])=202Var⁡(N)=400⋅4=1600\operatorname{Var}\big(E[S \mid N]\big) = 20^2\operatorname{Var}(N) = 400\cdot4 = 1600A Poisson count has variance equal to its mean; this term is the uncertainty in how many customers come.
  5. E[S]=80E[S] = 80 dollars and Var⁡(S)=100+1600=1700\operatorname{Var}(S) = 100 + 1600 = 1700, so the standard deviation is about 41.2341.23 dollarsMost of the risk is in the count, not the amounts. Treating the count as fixed at 44 gives a standard deviation of 1010 dollars (Mistake 4).

Problem 6

XX and YY have joint density f(x,y)=x+yf(x, y) = x + y on the unit square 0≤x,y≤10 \le x, y \le 1. Find fXf_X, the conditional mean E[Y∣X=x]E[Y \mid X = x] and its values at x=0x = 0 and x=1x = 1, and check the tower law.

  1. fX(x)=∫01(x+y) dy=x+12f_X(x) = \int_0^1 (x + y)\,dy = x + \tfrac12Integrate out yy.
  2. ∫01y(x+y) dy=x2+13\int_0^1 y(x + y)\,dy = \tfrac{x}{2} + \tfrac13The numerator of the conditional mean.
  3. E[Y∣X=x]=x/2+1/3x+1/2=3x+26x+3E[Y \mid X = x] = \dfrac{x/2 + 1/3}{x + 1/2} = \dfrac{3x + 2}{6x + 3}Multiply top and bottom by 66.
  4. E[Y∣X=0]=23E[Y \mid X = 0] = \tfrac23 and E[Y∣X=1]=59≈0.556E[Y \mid X = 1] = \tfrac59 \approx 0.556At larger xx the density x+yx + y is flatter in yy, so the conditional mean moves toward 12\tfrac12.
  5. fX(x)=x+12f_X(x) = x + \tfrac12, E[Y∣X=x]=3x+26x+3E[Y \mid X = x] = \tfrac{3x + 2}{6x + 3}, and E[E[Y∣X]]=∫01(x2+13)dx=712=E[Y]E\big[E[Y \mid X]\big] = \int_0^1 \big(\tfrac x2 + \tfrac13\big)dx = \tfrac{7}{12} = E[Y]In the tower law the fX(x)f_X(x) in the denominator cancels the weight fX(x) dxf_X(x)\,dx. Directly, E[Y]=∫01y(y+12) dy=13+14=712E[Y] = \int_0^1 y(y + \tfrac12)\,dy = \tfrac13 + \tfrac14 = \tfrac{7}{12}. Plugging E[X]=712E[X] = \tfrac{7}{12} into the curve gives 1526\tfrac{15}{26} instead (Mistake 2).

Problem 7

For the table of Problem 1, compare three predictors of YY from XX by mean-squared error: the constant E[Y]E[Y], the conditional mean E[Y∣X]E[Y \mid X], and g(X)=2+0.5Xg(X) = 2 + 0.5X. Show that the residual R=Y−E[Y∣X]R = Y - E[Y \mid X] is uncorrelated with XX.

  1. E[(Y−2.1)2]=Var⁡(Y)=0.69E[(Y - 2.1)^2] = \operatorname{Var}(Y) = 0.69Problem 3.
  2. E[(Y−E[Y∣X])2]=E[Var⁡(Y∣X)]=73120≈0.6083E\big[(Y - E[Y \mid X])^2\big] = E\big[\operatorname{Var}(Y \mid X)\big] = \tfrac{73}{120} \approx 0.6083Inside each slice the squared error around the slice mean is the slice variance.
  3. E[(Y−g(X))2]=0.4⋅0.75+0.6⋅712=0.65E[(Y - g(X))^2] = 0.4\cdot0.75 + 0.6\cdot\tfrac{7}{12} = 0.65Given X=0X = 0: (0.2⋅1+0.1⋅0+0.1⋅1)/0.4=0.75(0.2\cdot1 + 0.1\cdot0 + 0.1\cdot1)/0.4 = 0.75. Given X=1X = 1: (0.1⋅2.25+0.2⋅0.25+0.3⋅0.25)/0.6=712(0.1\cdot2.25 + 0.2\cdot0.25 + 0.3\cdot0.25)/0.6 = \tfrac{7}{12}.
  4. 0.65=73120+E[(E[Y∣X]−g(X))2]=73120+1240.65 = \tfrac{73}{120} + E\big[(E[Y \mid X] - g(X))^2\big] = \tfrac{73}{120} + \tfrac{1}{24}E[Y∣X]−g(X)E[Y \mid X] - g(X) is −0.25-0.25 at X=0X = 0 and −16-\tfrac16 at X=1X = 1: 0.4⋅0.0625+0.6⋅136=1240.4\cdot0.0625 + 0.6\cdot\tfrac{1}{36} = \tfrac{1}{24}. The cross term vanishes because E[R∣X]=0E[R \mid X] = 0.
  5. The errors are 0.690.69, 73120≈0.608\tfrac{73}{120} \approx 0.608 and 0.650.65; E[RX]=E[X E[R∣X]]=0E[RX] = E\big[X\,E[R \mid X]\big] = 0, and E[R]=0E[R] = 0, so Cov⁡(R,X)=0\operatorname{Cov}(R, X) = 0Every other predictor pays the extra E[(E[Y∣X]−g(X))2]E[(E[Y \mid X] - g(X))^2]. The share of Var⁡(Y)\operatorname{Var}(Y) that XX explains is 49/6000.69=49414≈0.118\tfrac{49/600}{0.69} = \tfrac{49}{414} \approx 0.118.

Problem 8

Heights XX (cm) and weights YY (kg) are bivariate Gaussian with μX=170\mu_X = 170, σX=10\sigma_X = 10, μY=70\mu_Y = 70, σY=12\sigma_Y = 12 and correlation ρ=0.5\rho = 0.5. Find the mean and standard deviation of the weight of a person 185185 cm tall, and check the law of total variance.

  1. E[Y∣X=x]=70+0.5⋅1210(x−170)=70+0.6(x−170)E[Y \mid X = x] = 70 + 0.5\cdot\tfrac{12}{10}(x - 170) = 70 + 0.6(x - 170)The slope is ρσY/σX=Cov⁡(X,Y)/Var⁡(X)\rho\sigma_Y/\sigma_X = \operatorname{Cov}(X, Y)/\operatorname{Var}(X).
  2. E[Y∣X=185]=70+0.6⋅15=79E[Y \mid X = 185] = 70 + 0.6\cdot15 = 791.51.5 standard deviations above the mean in height, but only 0.750.75 above in weight.
  3. Var⁡(Y∣X)=144(1−0.25)=108\operatorname{Var}(Y \mid X) = 144(1 - 0.25) = 108It does not depend on xx for a Gaussian.
  4. Var⁡(E[Y∣X])=0.62⋅100=36\operatorname{Var}\big(E[Y \mid X]\big) = 0.6^2\cdot100 = 36, and 108+36=144=σY2108 + 36 = 144 = \sigma_Y^2The regression line explains ρ2=25%\rho^2 = 25\% of the variance.
  5. Mean 7979 kg, standard deviation 108≈10.39\sqrt{108} \approx 10.39 kgKnowing the height narrows the spread from 1212 kg only to 10.3910.39 kg at ρ=0.5\rho = 0.5. The reverse question, the expected height of a 7979 kg person, is not 185185 cm (Mistake 3).

Problem 9

To estimate E[Y]E[Y] where X∼Uniform(0,1)X \sim \text{Uniform}(0, 1) and Y∣X∼N(X,1)Y \mid X \sim N(X, 1), one can average n=100n = 100 samples of YY, or average E[Y∣Xi]=XiE[Y \mid X_i] = X_i over 100100 samples of XX. Find the variance of one term of each estimator, their standard errors, and how many samples of YY match the second estimator's accuracy.

  1. E[Y]=E[X]=12E[Y] = E[X] = \tfrac12Both estimators are unbiased, by the tower law.
  2. Var⁡(Y)=E[1]+Var⁡(X)=1+112=1312≈1.083\operatorname{Var}(Y) = E[1] + \operatorname{Var}(X) = 1 + \tfrac{1}{12} = \tfrac{13}{12} \approx 1.083Total variance: the noise around XX plus the spread of XX.
  3. Var⁡(E[Y∣X])=Var⁡(X)=112≈0.0833\operatorname{Var}\big(E[Y \mid X]\big) = \operatorname{Var}(X) = \tfrac{1}{12} \approx 0.0833Conditioning removes the within-slice term entirely.
  4. 13/12100≈0.1041\sqrt{\tfrac{13/12}{100}} \approx 0.1041 and 1/12100≈0.0289\sqrt{\tfrac{1/12}{100}} \approx 0.0289The standard error of an average of nn independent terms is Var⁡/n\sqrt{\operatorname{Var}/n}.
  5. Variances 1312\tfrac{13}{12} against 112\tfrac{1}{12}, standard errors 0.1040.104 against 0.0290.029; matching it takes 13⋅100=130013\cdot100 = 1300 samples of YYThis is Rao–Blackwellization: replacing a sample by its conditional mean keeps the mean and can only lower the variance, by exactly E[Var⁡(Y∣X)]E[\operatorname{Var}(Y \mid X)]. Baselines and marginalized gradient estimators use the same idea.

Problem 10

A unit computes s=∑imiwixi/0.8s = \sum_i m_iw_ix_i/0.8 with weights w=(1,−2,3)w = (1, -2, 3) and independent dropout masks mi∼Bernoulli(0.8)m_i \sim \text{Bernoulli}(0.8). The input is xa=(2,1,1)x_a = (2, 1, 1) or xb=(1,1,0)x_b = (1, 1, 0) with probability 12\tfrac12 each. Find E[s∣x]E[s \mid x] and Var⁡(s∣x)\operatorname{Var}(s \mid x) for each input, then E[s]E[s] and Var⁡(s)\operatorname{Var}(s), and the share of Var⁡(s)\operatorname{Var}(s) that dropout causes.

  1. E[s∣x]=w⊤xE[s \mid x] = w^\top xE[mi/0.8]=1E[m_i/0.8] = 1, so dropout with this rescaling leaves the mean unchanged.
  2. Var⁡(s∣x)=0.20.8∑iwi2xi2\operatorname{Var}(s \mid x) = \dfrac{0.2}{0.8}\sum_i w_i^2x_i^2Var⁡(mi/0.8)=0.8⋅0.2/0.82=0.25\operatorname{Var}(m_i/0.8) = 0.8\cdot0.2/0.8^2 = 0.25, and the masks are independent.
  3. xax_a: E=2−2+3=3E = 2 - 2 + 3 = 3, Var⁡=0.25(4+4+9)=4.25\operatorname{Var} = 0.25(4 + 4 + 9) = 4.25; xbx_b: E=1−2=−1E = 1 - 2 = -1, Var⁡=0.25(1+4)=1.25\operatorname{Var} = 0.25(1 + 4) = 1.25Plug in each input.
  4. E[Var⁡(s∣x)]=12(4.25+1.25)=2.75E\big[\operatorname{Var}(s \mid x)\big] = \tfrac12(4.25 + 1.25) = 2.75 and Var⁡(E[s∣x])=14(3−(−1))2=4\operatorname{Var}\big(E[s \mid x]\big) = \tfrac14(3 - (-1))^2 = 4Equal weights: a two-valued variable with p=12p = \tfrac12 has variance 14(b−a)2\tfrac14(b - a)^2.
  5. E[s]=1E[s] = 1 and Var⁡(s)=2.75+4=6.75\operatorname{Var}(s) = 2.75 + 4 = 6.75; dropout causes 2.756.75=1127≈41%\tfrac{2.75}{6.75} = \tfrac{11}{27} \approx 41\% of itThe other 59%59\% is the genuine difference between the inputs. At test time dropout is switched off, which removes exactly the first term.

Where this goes wrong

1. Averaging the conditional variances and stopping

The law of total variance has two terms, and the first one already looks like "the variance, averaged over XX".

  1. Var⁡(Y∣X=0)=1116\operatorname{Var}(Y \mid X = 0) = \tfrac{11}{16} and Var⁡(Y∣X=1)=59\operatorname{Var}(Y \mid X = 1) = \tfrac59Right so far: Problem 3.
  2. “Var⁡(Y)\operatorname{Var}(Y) is the average of the conditional variances.”That is only the within-slice spread. The slice means 1.751.75 and 73\tfrac73 also differ, and that spread counts too.
  3. Var⁡(Y)=0.4⋅1116+0.6⋅59≈0.6083\operatorname{Var}(Y) = 0.4\cdot\tfrac{11}{16} + 0.6\cdot\tfrac59 \approx 0.6083The missing term is Var⁡(E[Y∣X])=49600≈0.0817\operatorname{Var}(E[Y \mid X]) = \tfrac{49}{600} \approx 0.0817, and the right total is 0.690.69. The averaged conditional variance is never larger than the true variance.

2. E[Y | X] evaluated at the mean of X

E[Y∣X]E[Y \mid X] is a function of XX, and the tower law averages it over XX; plugging in a single value of XX looks like a shortcut.

  1. E[Y∣X=x]=3x+26x+3E[Y \mid X = x] = \dfrac{3x + 2}{6x + 3} and E[X]=712E[X] = \tfrac{7}{12}Right so far: Problem 6, and E[X]=E[Y]E[X] = E[Y] by the symmetry of x+yx + y.
  2. “E[Y]=E[Y∣X=E[X]]E[Y] = E[Y \mid X = E[X]].”That treats the random variable E[Y∣X]E[Y \mid X] as if it were a fixed number. Averaging a function and evaluating it at the average agree only for straight lines.
  3. E[Y]=3⋅712+26⋅712+3=1526≈0.577E[Y] = \dfrac{3\cdot\tfrac{7}{12} + 2}{6\cdot\tfrac{7}{12} + 3} = \tfrac{15}{26} \approx 0.577The curve is not a line, so the answer is off: E[Y]=∫01(x2+13)dx=712≈0.583E[Y] = \int_0^1 \big(\tfrac x2 + \tfrac13\big)dx = \tfrac{7}{12} \approx 0.583. In Problem 2 the shortcut happens to work, because there E[Y∣X]E[Y \mid X] is linear in XX.

3. Inverting the regression line

The line E[Y∣X=x]=70+0.6(x−170)E[Y \mid X = x] = 70 + 0.6(x - 170) predicts weight from height, and solving it for xx looks like it predicts height from weight.

  1. E[Y∣X=185]=79E[Y \mid X = 185] = 79Right so far: Problem 8.
  2. “So a person who weighs 7979 kg is expected to be 185185 cm tall.”E[X∣Y]E[X \mid Y] is a different regression, with slope ρσX/σY\rho\sigma_X/\sigma_Y, not the reciprocal 1/0.61/0.6.
  3. E[X∣Y=79]=185E[X \mid Y = 79] = 185E[X∣Y=79]=170+0.5⋅1012(79−70)=173.75E[X \mid Y = 79] = 170 + 0.5\cdot\tfrac{10}{12}(79 - 70) = 173.75 cm. Both regressions pull toward the mean; that is regression to the mean, and only at ∣ρ∣=1|\rho| = 1 are the two lines the same.

4. The variance of a random sum taken as E[N]σ²

Variances of independent terms add, so nn terms of variance σ2\sigma^2 give nσ2n\sigma^2, and E[N]E[N] seems the natural nn.

  1. Var⁡(S∣N)=25N\operatorname{Var}(S \mid N) = 25NRight so far: Problem 5.
  2. “Replace NN by its mean: Var⁡(S)=25 E[N]\operatorname{Var}(S) = 25\,E[N].”That is only E[Var⁡(S∣N)]E[\operatorname{Var}(S \mid N)]. The count itself is random, and Var⁡(20N)=1600\operatorname{Var}(20N) = 1600 is missing.
  3. Var⁡(S)=25⋅4=100\operatorname{Var}(S) = 25\cdot4 = 100, a standard deviation of 1010 dollarsThe right variance is 17001700, a standard deviation of about 41.2341.23 dollars, more than four times as wide. The 1010-dollar answer is correct only if exactly 44 customers are certain to come.

5. Averaging conditional means without their weights

There are two slices and two conditional means, and the midpoint looks like the overall mean.

  1. E[Y∣X=0]=1.75E[Y \mid X = 0] = 1.75 and E[Y∣X=1]=73E[Y \mid X = 1] = \tfrac73Right so far: Problem 1.
  2. “E[Y]E[Y] is the average of the two conditional means.”The tower law weights each slice by its probability, 0.40.4 and 0.60.6, not 12\tfrac12 each.
  3. E[Y]=12(1.75+73)=4924≈2.042E[Y] = \tfrac12(1.75 + \tfrac73) = \tfrac{49}{24} \approx 2.042The right value is 0.4⋅1.75+0.6⋅73=2.10.4\cdot1.75 + 0.6\cdot\tfrac73 = 2.1, which the column totals confirm. The unweighted average is right only when the slices are equally likely, as in Problem 10.

Print this set: conditional-expectation-and-the-law-of-total-variance.pdf (problems, answers, and worked solutions on separate pages).

Read the guide: Conditional expectation and the law of total variance: average inside each slice, then add the spread between the slices