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 Y once I know ?X?". For each value x it is a number, the mean of Y in the slice ;X=x; taken over all values of X at once it is a random variable, a function of .X. That second view is what makes it useful. Averaging it gives back ,E[Y], its spread and the spread left inside each slice add up to ,Var(Y), and it is the best possible prediction of Y from X under squared error, which is why a regression network trained on mean-squared error is learning .E[Y∣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), with .Pr(Y=y∣X=x)=Pr(X=x,Y=y)/Pr(X=x). For densities, .E[Y∣X=x]=∫yf(x,y)dy/fX(x).
Writing ,g(x)=E[Y∣X=x], the conditional expectation E[Y∣X] is the random variable :g(X): it is not a number until a value of X is plugged in.
Tower law.,E[E[Y∣X]]=E[Y], and more generally E[h(X)Y]=E[h(X)E[Y∣X]] for any function .h.
Law of total variance.:Var(Y)=E[Var(Y∣X)]+Var(E[Y∣X]): the average spread within the slices plus the spread of the slice means.
Best predictor. Among all functions ,g,E[(Y−g(X))2] is smallest for ,g(X)=E[Y∣X], and the residual Y−E[Y∣X] has mean 0 given ,X, so it is uncorrelated with every .h(X).
Random sums. If S=X1+⋯+XN with the Xi independent of N and of each other, each with mean μ and variance ,σ2, then E[S]=E[N]μ and .Var(S)=E[N]σ2+Var(N)μ2.
Bivariate Gaussian.E[Y∣X=x]=μY+ρσXσY(x−μX) and ,Var(Y∣X)=σY2(1−ρ2), the same for every .x.
Find ,E[Y∣X=0],E[Y∣X=1] and ,E[Y], and check that the conditional means average to .E[Y].
·
For the table of Problem 1, write E[Y∣X] as a function of .X. Find its mean and variance, and use the tower law to get E[XY] and .Cov(X,Y).
··
For the table of Problem 1, find Var(Y∣X=0) and ,Var(Y∣X=1), and check the law of total variance against Var(Y) computed directly.
··
A model draws each output from a two-component mixture: with probability 0.3 from ,N(0,1), and with probability 0.7 from .N(4,22). Find the mean and variance of the output.
··
The number of customers in an hour is Poisson with mean ,4, 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 .S.
··
X and Y have joint density f(x,y)=x+y on the unit square .0≤x,y≤1. Find ,fX, the conditional mean E[Y∣X=x] and its values at x=0 and ,x=1, and check the tower law.
···
For the table of Problem 1, compare three predictors of Y from X by mean-squared error: the constant ,E[Y], the conditional mean ,E[Y∣X], and .g(X)=2+0.5X. Show that the residual R=Y−E[Y∣X] is uncorrelated with .X.
··
Heights X (cm) and weights Y (kg) are bivariate Gaussian with ,μX=170,,σX=10,,μY=70,σY=12 and correlation .ρ=0.5. Find the mean and standard deviation of the weight of a person 185 cm tall, and check the law of total variance.
···
To estimate E[Y] where X∼Uniform(0,1) and ,Y∣X∼N(X,1), one can average n=100 samples of ,Y, or average E[Y∣Xi]=Xi over 100 samples of .X. Find the variance of one term of each estimator, their standard errors, and how many samples of Y match the second estimator's accuracy.
···
A unit computes s=∑imiwixi/0.8 with weights w=(1,−2,3) and independent dropout masks .mi∼Bernoulli(0.8). The input is xa=(2,1,1) or xb=(1,1,0) with probability 21 each. Find E[s∣x] and Var(s∣x) for each input, then E[s] and ,Var(s), and the share of Var(s) that dropout causes.
,E[Y∣X]=47+127X, with mean 2.1 and variance ;60049;E[XY]=1.4 and Cov(X,Y)=1.4−0.6⋅2.1=0.14
,Var(Y∣X=0)=1611,,Var(Y∣X=1)=95, and 0.6083+0.0817=0.69=Var(Y)
E[Y]=2.8 and ,Var(Y)=3.1+3.36=6.46, a standard deviation of about 2.54
E[S]=80 dollars and ,Var(S)=100+1600=1700, so the standard deviation is about 41.23 dollars
,fX(x)=x+21,,E[Y∣X=x]=6x+33x+2, and E[E[Y∣X]]=∫01(2x+31)dx=127=E[Y]
The errors are ,0.69,12073≈0.608 and ;0.65;,E[RX]=E[XE[R∣X]]=0, and ,E[R]=0, so Cov(R,X)=0
Mean 79 kg, standard deviation 108≈10.39 kg
Variances 1213 against ,121, standard errors 0.104 against ;0.029; matching it takes 13⋅100=1300 samples of Y
E[s]=1 and ;Var(s)=2.75+4=6.75; dropout causes 6.752.75=2711≈41% of it
Worked solutions
Problem 1
The joint distribution of X and Y is
X=0X=1Y=10.20.1Y=20.10.2Y=30.10.3
Find ,E[Y∣X=0],E[Y∣X=1] and ,E[Y], and check that the conditional means average to .E[Y].
,Pr(X=0)=0.4,Pr(X=1)=0.6Row sums of the table.
E[Y∣X=0]=0.41⋅0.2+2⋅0.1+3⋅0.1=0.40.7=1.75Divide the row by its total to get the conditional distribution, then take its mean.
E[Y∣X=1]=0.61⋅0.1+2⋅0.2+3⋅0.3=0.61.4=37The same in the second row.
E[Y]=0.7+1.4=2.1The column totals are ,0.3,,0.3,,0.4, and ;1⋅0.3+2⋅0.3+3⋅0.4=2.1; the two numerators of steps 2 and 3 add to the same thing.
,E[Y∣X=0]=1.75,,E[Y∣X=1]=37≈2.333,E[Y]=0.4⋅1.75+0.6⋅37=2.1The tower law: each conditional mean is weighted by how likely its slice is. The plain average of 1.75 and 37 is 2449≈2.042 (Mistake 5).
Problem 2
For the table of Problem 1, write E[Y∣X] as a function of .X. Find its mean and variance, and use the tower law to get E[XY] and .Cov(X,Y).
E[Y∣X]=47+127XX takes only the values 0 and ,1, so a line through (0,1.75) and (1,37) matches both: .37−47=127.
E[E[Y∣X]]=47+127⋅0.6=2.1;E[X]=0.6; this is E[Y] again.
Var(E[Y∣X])=(127)2Var(X)=14449⋅0.24=60049≈0.0817Var(X)=0.6⋅0.4 for a 0/1 variable; the constant 47 does not change the variance.
E[XY]=E[XE[Y∣X]]=0.6⋅37=1.4XE[Y∣X] is 0 when X=0 and 37 when .X=1. Directly: .1⋅(0.1⋅1+0.2⋅2+0.3⋅3)=1.4.
,E[Y∣X]=47+127X, with mean 2.1 and variance ;60049;E[XY]=1.4 and Cov(X,Y)=1.4−0.6⋅2.1=0.14The slope is ,Cov(X,Y)/Var(X)=0.14/0.24=127, 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) and ,Var(Y∣X=1), and check the law of total variance against Var(Y) computed directly.
,E[Y2∣X=0]=0.40.2+0.4+0.9=3.75, so Var(Y∣X=0)=3.75−1.752=1611,12⋅0.2+22⋅0.1+32⋅0.1=1.5, divided by .0.4.
,E[Y2∣X=1]=0.60.1+0.8+2.7=6, so Var(Y∣X=1)=6−949=95The second row: .3.6/0.6.
E[Var(Y∣X)]=0.4⋅1611+0.6⋅95=0.275+31=12073≈0.6083The within-slice spread, weighted by the slice probabilities.
E[Var(Y∣X)]+Var(E[Y∣X])=12073+60049=600414=0.69The between-slice spread from Problem 2.
,Var(Y∣X=0)=1611,,Var(Y∣X=1)=95, and 0.6083+0.0817=0.69=Var(Y)Directly, E[Y2]=1.5+3.6=5.1 and .5.1−2.12=0.69. Stopping at step 3 gives ,0.6083, which is too small (Mistake 1).
Problem 4
A model draws each output from a two-component mixture: with probability 0.3 from ,N(0,1), and with probability 0.7 from .N(4,22). Find the mean and variance of the output.
Let Z be the component, so E[Y∣Z]∈{0,4} and Var(Y∣Z)∈{1,4}Condition on the hidden label; given it, Y is a single Gaussian.
E[Y]=0.3⋅0+0.7⋅4=2.8The tower law over .Z.
E[Var(Y∣Z)]=0.3⋅1+0.7⋅4=3.1The average within-component variance. Note :22=4: the second component has standard deviation .2.
Var(E[Y∣Z])=0.3⋅0.7⋅(4−0)2=3.36A two-valued variable taking values a and b with probabilities p and 1−p has variance .p(1−p)(b−a)2.
E[Y]=2.8 and ,Var(Y)=3.1+3.36=6.46, a standard deviation of about 2.54Check with second moments: ,E[Y2]=0.3⋅1+0.7⋅(4+16)=14.3, and .14.3−2.82=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 ,4, 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 .S.
E[S∣N]=20N and Var(S∣N)=25NGiven ,N=n,S is a sum of n independent amounts: means add and variances add.
E[S]=E[20N]=20⋅4=80The tower law.
E[Var(S∣N)]=25E[N]=100The spread of the amounts, for the average number of customers.
Var(E[S∣N])=202Var(N)=400⋅4=1600A Poisson count has variance equal to its mean; this term is the uncertainty in how many customers come.
E[S]=80 dollars and ,Var(S)=100+1600=1700, so the standard deviation is about 41.23 dollarsMost of the risk is in the count, not the amounts. Treating the count as fixed at 4 gives a standard deviation of 10 dollars (Mistake 4).
Problem 6
X and Y have joint density f(x,y)=x+y on the unit square .0≤x,y≤1. Find ,fX, the conditional mean E[Y∣X=x] and its values at x=0 and ,x=1, and check the tower law.
fX(x)=∫01(x+y)dy=x+21Integrate out .y.
∫01y(x+y)dy=2x+31The numerator of the conditional mean.
E[Y∣X=x]=x+1/2x/2+1/3=6x+33x+2Multiply top and bottom by .6.
E[Y∣X=0]=32 and E[Y∣X=1]=95≈0.556At larger x the density x+y is flatter in ,y, so the conditional mean moves toward .21.
,fX(x)=x+21,,E[Y∣X=x]=6x+33x+2, and E[E[Y∣X]]=∫01(2x+31)dx=127=E[Y]In the tower law the fX(x) in the denominator cancels the weight .fX(x)dx. Directly, .E[Y]=∫01y(y+21)dy=31+41=127. Plugging E[X]=127 into the curve gives 2615 instead (Mistake 2).
Problem 7
For the table of Problem 1, compare three predictors of Y from X by mean-squared error: the constant ,E[Y], the conditional mean ,E[Y∣X], and .g(X)=2+0.5X. Show that the residual R=Y−E[Y∣X] is uncorrelated with .X.
E[(Y−2.1)2]=Var(Y)=0.69Problem 3.
E[(Y−E[Y∣X])2]=E[Var(Y∣X)]=12073≈0.6083Inside each slice the squared error around the slice mean is the slice variance.
E[(Y−g(X))2]=0.4⋅0.75+0.6⋅127=0.65Given :X=0:.(0.2⋅1+0.1⋅0+0.1⋅1)/0.4=0.75. Given :X=1:.(0.1⋅2.25+0.2⋅0.25+0.3⋅0.25)/0.6=127.
0.65=12073+E[(E[Y∣X]−g(X))2]=12073+241E[Y∣X]−g(X) is −0.25 at X=0 and −61 at :X=1:.0.4⋅0.0625+0.6⋅361=241. The cross term vanishes because .E[R∣X]=0.
The errors are ,0.69,12073≈0.608 and ;0.65;,E[RX]=E[XE[R∣X]]=0, and ,E[R]=0, so Cov(R,X)=0Every other predictor pays the extra .E[(E[Y∣X]−g(X))2]. The share of Var(Y) that X explains is .0.6949/600=41449≈0.118.
Problem 8
Heights X (cm) and weights Y (kg) are bivariate Gaussian with ,μX=170,,σX=10,,μY=70,σY=12 and correlation .ρ=0.5. Find the mean and standard deviation of the weight of a person 185 cm tall, and check the law of total variance.
E[Y∣X=x]=70+0.5⋅1012(x−170)=70+0.6(x−170)The slope is .ρσY/σX=Cov(X,Y)/Var(X).
E[Y∣X=185]=70+0.6⋅15=791.5 standard deviations above the mean in height, but only 0.75 above in weight.
Var(Y∣X)=144(1−0.25)=108It does not depend on x for a Gaussian.
,Var(E[Y∣X])=0.62⋅100=36, and 108+36=144=σY2The regression line explains ρ2=25% of the variance.
Mean 79 kg, standard deviation 108≈10.39 kgKnowing the height narrows the spread from 12 kg only to 10.39 kg at .ρ=0.5. The reverse question, the expected height of a 79 kg person, is not 185 cm (Mistake 3).
Problem 9
To estimate E[Y] where X∼Uniform(0,1) and ,Y∣X∼N(X,1), one can average n=100 samples of ,Y, or average E[Y∣Xi]=Xi over 100 samples of .X. Find the variance of one term of each estimator, their standard errors, and how many samples of Y match the second estimator's accuracy.
E[Y]=E[X]=21Both estimators are unbiased, by the tower law.
Var(Y)=E[1]+Var(X)=1+121=1213≈1.083Total variance: the noise around X plus the spread of .X.
Var(E[Y∣X])=Var(X)=121≈0.0833Conditioning removes the within-slice term entirely.
10013/12≈0.1041 and 1001/12≈0.0289The standard error of an average of n independent terms is .Var/n.
Variances 1213 against ,121, standard errors 0.104 against ;0.029; matching it takes 13⋅100=1300 samples of YThis 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)]. Baselines and marginalized gradient estimators use the same idea.
Problem 10
A unit computes s=∑imiwixi/0.8 with weights w=(1,−2,3) and independent dropout masks .mi∼Bernoulli(0.8). The input is xa=(2,1,1) or xb=(1,1,0) with probability 21 each. Find E[s∣x] and Var(s∣x) for each input, then E[s] and ,Var(s), and the share of Var(s) that dropout causes.
E[s∣x]=w⊤x,E[mi/0.8]=1, so dropout with this rescaling leaves the mean unchanged.
Var(s∣x)=0.80.2∑iwi2xi2,Var(mi/0.8)=0.8⋅0.2/0.82=0.25, and the masks are independent.
:xa:,E=2−2+3=3,;Var=0.25(4+4+9)=4.25;:xb:,E=1−2=−1,Var=0.25(1+4)=1.25Plug in each input.
E[Var(s∣x)]=21(4.25+1.25)=2.75 and Var(E[s∣x])=41(3−(−1))2=4Equal weights: a two-valued variable with p=21 has variance .41(b−a)2.
E[s]=1 and ;Var(s)=2.75+4=6.75; dropout causes 6.752.75=2711≈41% of itThe other 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 X".
Var(Y∣X=0)=1611 and Var(Y∣X=1)=95Right so far: Problem 3.
“Var(Y) is the average of the conditional variances.”That is only the within-slice spread. The slice means 1.75 and 37 also differ, and that spread counts too.
Var(Y)=0.4⋅1611+0.6⋅95≈0.6083The missing term is ,Var(E[Y∣X])=60049≈0.0817, and the right total is .0.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] is a function of ,X, and the tower law averages it over ;X; plugging in a single value of X looks like a shortcut.
E[Y∣X=x]=6x+33x+2 and E[X]=127Right so far: Problem 6, and E[X]=E[Y] by the symmetry of .x+y.
“.E[Y]=E[Y∣X=E[X]].”That treats the random variable E[Y∣X] as if it were a fixed number. Averaging a function and evaluating it at the average agree only for straight lines.
E[Y]=6⋅127+33⋅127+2=2615≈0.577The curve is not a line, so the answer is off: .E[Y]=∫01(2x+31)dx=127≈0.583. In Problem 2 the shortcut happens to work, because there E[Y∣X] is linear in .X.
3. Inverting the regression line
The line E[Y∣X=x]=70+0.6(x−170) predicts weight from height, and solving it for x looks like it predicts height from weight.
E[Y∣X=185]=79Right so far: Problem 8.
“So a person who weighs 79 kg is expected to be 185 cm tall.”E[X∣Y] is a different regression, with slope ,ρσX/σY, not the reciprocal .1/0.6.
E[X∣Y=79]=185E[X∣Y=79]=170+0.5⋅1210(79−70)=173.75 cm. Both regressions pull toward the mean; that is regression to the mean, and only at ∣ρ∣=1 are the two lines the same.
4. The variance of a random sum taken as E[N]σ²
Variances of independent terms add, so n terms of variance σ2 give ,nσ2, and E[N] seems the natural .n.
Var(S∣N)=25NRight so far: Problem 5.
“Replace N by its mean: .Var(S)=25E[N].”That is only .E[Var(S∣N)]. The count itself is random, and Var(20N)=1600 is missing.
,Var(S)=25⋅4=100, a standard deviation of 10 dollarsThe right variance is ,1700, a standard deviation of about 41.23 dollars, more than four times as wide. The 10-dollar answer is correct only if exactly 4 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.
E[Y∣X=0]=1.75 and E[Y∣X=1]=37Right so far: Problem 1.
“E[Y] is the average of the two conditional means.”The tower law weights each slice by its probability, 0.4 and ,0.6, not 21 each.
E[Y]=21(1.75+37)=2449≈2.042The right value is ,0.4⋅1.75+0.6⋅37=2.1, which the column totals confirm. The unweighted average is right only when the slices are equally likely, as in Problem 10.