Math for Causal Inference

Day 6. Conditional Expectation, the LIE & Total Variance

The conditional expectation function, the law of iterated expectations and the adjustment formula.

Video coming soon
Learning outcomes
  • ●Compute conditional expectations and use the law of iterated expectations (LIE).
  • ●Prove that the conditional expectation function (CEF) is the best predictor of an outcome.
  • ●Decompose a variance into within-group and between-group parts.
  • ●Use the adjustment formula to compute ATE and ATT from subgroup means.
2-HOUR SESSION PLAN
Theory 25′Warm-up (1–5) 20′Proofs (6–10) 35′Applied (11–15) 25′Python & Lab 15′
Colab notebook (.ipynb) Jump to the simulator

Theory

The conditional expectation function

E[Y∣X=x]E[Y \mid X = x] is the mean of YY among units with X=xX = x. As a function of xx it is the conditional expectation function (CEF), m(x)m(x). Plugging in the random XX gives E[Y∣X]=m(X)E[Y \mid X] = m(X), itself a random variable.

Three workhorse results

1. Law of iterated expectations (LIE).

E[Y]=E[E[Y∣X]]=∑xE[Y∣X=x] P(X=x).E[Y] = E\big[E[Y \mid X]\big] = \sum_x E[Y \mid X = x]\,P(X = x).

The overall mean is the average of the group means, weighted by group size.

2. Taking out what is known. For any function gg, E[g(X) Y∣X]=g(X) E[Y∣X]E[g(X)\,Y \mid X] = g(X)\,E[Y \mid X].

3. CEF decomposition. Write Y=m(X)+eY = m(X) + e with e=Y−m(X)e = Y - m(X). Then E[e∣X]=0E[e \mid X] = 0, so ee is uncorrelated with every function of XX. Moreover, m(X)m(X) is the best predictor of YY: it minimises E[(Y−g(X))2]E[(Y - g(X))^2] over all functions gg. If the CEF happens to be linear, m(x)=a+bxm(x) = a + bx, then b=Cov⁡(X,Y)/Var⁡(X)b = \operatorname{Cov}(X, Y)/\operatorname{Var}(X), the regression slope.

Law of total variance

Var⁡(Y)=E[Var⁡(Y∣X)]⏟within groups+Var⁡(E[Y∣X])⏟between groups.\operatorname{Var}(Y) = \underbrace{E\big[\operatorname{Var}(Y \mid X)\big]}_{\text{within groups}} + \underbrace{\operatorname{Var}\big(E[Y \mid X]\big)}_{\text{between groups}}.

The share of variance "explained" by XX is the between part divided by the total: the population R2R^2 of the CEF.

The adjustment formula

Suppose treatment is as good as random within levels of XX: (Y(1),Y(0))⊥T∣X\big(Y(1), Y(0)\big) \perp T \mid X ("unconfoundedness"). Then the LIE gives

E[Y(1)]=∑xE[Y∣T=1,X=x] P(X=x),E[Y(1)] = \sum_x E[Y \mid T = 1, X = x]\,P(X = x),

and similarly for Y(0)Y(0). Weighting by P(X=x∣T=1)P(X = x \mid T = 1) instead gives the effect on the treated (ATT). This adjustment formula (also called standardisation or the g-formula) is the backbone of matching, regression adjustment and inverse probability weighting.

Part 1 · Warm-up

Exercise 1 · Average wage by education

60% of workers have high school (mean wage $40k) and 40% college (mean wage $70k). Find the average wage.

Show solution

By the LIE, E[W]=0.6(40)+0.4(70)=24+28=52E[W] = 0.6(40) + 0.4(70) = 24 + 28 = 52 thousand.

Exercise 2 · Checking the LIE

Using Day 5's table, E[Y∣X=1]=0.75E[Y \mid X = 1] = 0.75, E[Y∣X=0]=0.50E[Y \mid X = 0] = 0.50 and P(X=1)=0.4P(X = 1) = 0.4. Verify that the LIE gives E[Y]=0.6E[Y] = 0.6.

Show solution

E[Y]=0.4(0.75)+0.6(0.50)=0.30+0.30=0.60E[Y] = 0.4(0.75) + 0.6(0.50) = 0.30 + 0.30 = 0.60. ✓

Exercise 3 · Conditional variances

In the same table, compute Var⁡(Y∣X=1)\operatorname{Var}(Y \mid X = 1) and Var⁡(Y∣X=0)\operatorname{Var}(Y \mid X = 0).

Show solution

Y∣XY \mid X is Bernoulli: Var⁡(Y∣X=1)=0.75×0.25=0.1875\operatorname{Var}(Y \mid X = 1) = 0.75 \times 0.25 = 0.1875 and Var⁡(Y∣X=0)=0.5×0.5=0.25\operatorname{Var}(Y \mid X = 0) = 0.5 \times 0.5 = 0.25.

Exercise 4 · Total variance

High-school wages have mean 40 and variance 100; college wages have mean 70 and variance 400; the shares are 0.6 and 0.4. Find Var⁡(W)\operatorname{Var}(W) via the law of total variance.

Show solution

Step 1. Within-group part, the share-weighted average of group variances:

E[Var⁡(W∣X)]=0.6(100)+0.4(400)=60+160=220.E[\operatorname{Var}(W \mid X)] = 0.6(100) + 0.4(400) = 60 + 160 = 220.

Step 2. Between-group part, the variance of the group means. E[W]=52E[W] = 52 (Exercise 1), so

Var⁡(E[W∣X])=0.6(40−52)2+0.4(70−52)2=0.6(144)+0.4(324)=86.4+129.6=216.\operatorname{Var}(E[W \mid X]) = 0.6(40 - 52)^2 + 0.4(70 - 52)^2 = 0.6(144) + 0.4(324) = 86.4 + 129.6 = 216.

(Shortcut for two groups: 0.6×0.4×302=2160.6 \times 0.4 \times 30^2 = 216.)

Step 3. Total: Var⁡(W)=220+216=436\operatorname{Var}(W) = 220 + 216 = 436.

Answer: 436 (sd ≈ $20.9k).

Exercise 5 · Independence

Show that if XX and YY are independent, E[Y∣X]=E[Y]E[Y \mid X] = E[Y].

Show solution

Independence means p(y∣x)=pY(y)p(y \mid x) = p_Y(y), so E[Y∣X=x]=∑yy pY(y)=E[Y]E[Y \mid X = x] = \sum_y y\,p_Y(y) = E[Y] for every xx. The CEF is flat.

Part 2 · Proofs

Exercise 6 · Proving the LIE

Prove E[E[Y∣X]]=E[Y]E[E[Y \mid X]] = E[Y] for discrete variables.

Show solution E[E[Y∣X]]=∑x(∑yy p(x,y)pX(x))pX(x)=∑x∑yy p(x,y)=∑yy pY(y)=E[Y].E\big[E[Y \mid X]\big] = \sum_x\Big(\sum_y y\,\frac{p(x, y)}{p_X(x)}\Big)p_X(x) = \sum_x\sum_y y\,p(x, y) = \sum_y y\,p_Y(y) = E[Y].

Exercise 7 · The CEF error is orthogonal to functions of X

With e=Y−E[Y∣X]e = Y - E[Y \mid X], show that E[e∣X]=0E[e \mid X] = 0 and E[g(X) e]=0E[g(X)\,e] = 0 for any gg.

Show solution

E[e∣X]=E[Y∣X]−E[Y∣X]=0E[e \mid X] = E[Y \mid X] - E[Y \mid X] = 0. Then, by the LIE and "taking out what is known",

E[g(X)e]=E[E[g(X)e∣X]]=E[g(X) E[e∣X]]=0.E[g(X)e] = E\big[E[g(X)e \mid X]\big] = E\big[g(X)\,E[e \mid X]\big] = 0.

Exercise 8 · The CEF is the best predictor

Prove that E[(Y−g(X))2]≥E[(Y−m(X))2]E[(Y - g(X))^2] \ge E[(Y - m(X))^2] for any function gg.

Show solution

Step 1. Write Y−g(X)=e+(m(X)−g(X))Y - g(X) = e + \big(m(X) - g(X)\big).

Step 2. Expanding,

E[(Y−g(X))2]=E[e2]+2E[e (m(X)−g(X))]+E[(m(X)−g(X))2].E[(Y - g(X))^2] = E[e^2] + 2E\big[e\,(m(X) - g(X))\big] + E\big[(m(X) - g(X))^2\big].

Step 3. The middle term is zero by Exercise 7 (it is ee times a function of XX), and the last is non-negative.

Step 4. So the error is smallest when g=mg = m.

Exercise 9 · Proving total variance

Prove Var⁡(Y)=E[Var⁡(Y∣X)]+Var⁡(E[Y∣X])\operatorname{Var}(Y) = E[\operatorname{Var}(Y \mid X)] + \operatorname{Var}(E[Y \mid X]).

Show solution

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

Step 1. Conditional variance shortcut: Var⁡(Y∣X)=E[Y2∣X]−m(X)2\operatorname{Var}(Y \mid X) = E[Y^2 \mid X] - m(X)^2.

Step 2. Take expectations and use the LIE on the first term:

E[Var⁡(Y∣X)]=E[Y2]−E[m(X)2].E[\operatorname{Var}(Y \mid X)] = E[Y^2] - E[m(X)^2].

Step 3. Variance of the CEF, using E[m(X)]=E[Y]E[m(X)] = E[Y] (LIE):

Var⁡(m(X))=E[m(X)2]−(E[Y])2.\operatorname{Var}(m(X)) = E[m(X)^2] - (E[Y])^2.

Step 4. Add steps 2 and 3; the E[m(X)2]E[m(X)^2] terms cancel:

E[Var⁡(Y∣X)]+Var⁡(m(X))=E[Y2]−(E[Y])2=Var⁡(Y).■E[\operatorname{Var}(Y \mid X)] + \operatorname{Var}(m(X)) = E[Y^2] - (E[Y])^2 = \operatorname{Var}(Y). \qquad\blacksquare

Exercise 10 · A linear CEF is the regression line

Suppose E[Y∣X]=a+bXE[Y \mid X] = a + bX. Prove b=Cov⁡(X,Y)/Var⁡(X)b = \operatorname{Cov}(X, Y)/\operatorname{Var}(X).

Show solution

By the LIE, E[XY]=E[X E[Y∣X]]=E[X(a+bX)]=aE[X]+bE[X2]E[XY] = E[X\,E[Y \mid X]] = E[X(a + bX)] = aE[X] + bE[X^2] and E[Y]=a+bE[X]E[Y] = a + bE[X]. Then

Cov⁡(X,Y)=aE[X]+bE[X2]−E[X](a+bE[X])=bVar⁡(X).\operatorname{Cov}(X, Y) = aE[X] + bE[X^2] - E[X](a + bE[X]) = b\operatorname{Var}(X).

Part 3 · Applied

A hospital's records give mean recovery rates E[Y∣T,S]E[Y \mid T, S] by treatment TT and severity SS. In the population, 70% of cases are mild.

MildSevere
Treated (T=1T = 1)0.900.60
Untreated (T=0T = 0)0.800.40

Assume treatment is as good as random within severity level.

Exercise 11 · The adjustment formula

Compute E[Y(1)]E[Y(1)], E[Y(0)]E[Y(0)] and the ATE.

Show solution

Step 1. Unconfoundedness lets us use the treated patients' recovery rate in each severity group as E[Y(1)∣S]E[Y(1) \mid S]. Average over the population case mix (70% mild):

E[Y(1)]=0.7(0.90)+0.3(0.60)=0.63+0.18=0.81.E[Y(1)] = 0.7(0.90) + 0.3(0.60) = 0.63 + 0.18 = 0.81.

Step 2. Same for the untreated:

E[Y(0)]=0.7(0.80)+0.3(0.40)=0.56+0.12=0.68.E[Y(0)] = 0.7(0.80) + 0.3(0.40) = 0.56 + 0.12 = 0.68.

Step 3. ATE =0.81−0.68=0.13= 0.81 - 0.68 = 0.13.

Answer: treatment raises the recovery rate by 13 percentage points on average.

Exercise 12 · What the naive comparison says

Doctors treat the sickest: 60% of treated patients are severe, but only 10% of untreated ones. Compute the naive difference E[Y∣T=1]−E[Y∣T=0]E[Y \mid T = 1] - E[Y \mid T = 0].

Show solution

Step 1. Treated patients' case mix: 40% mild, 60% severe. Their average recovery:

E[Y∣T=1]=0.4(0.90)+0.6(0.60)=0.36+0.36=0.72.E[Y \mid T = 1] = 0.4(0.90) + 0.6(0.60) = 0.36 + 0.36 = 0.72.

Step 2. Untreated case mix: 90% mild, 10% severe:

E[Y∣T=0]=0.9(0.80)+0.1(0.40)=0.72+0.04=0.76.E[Y \mid T = 0] = 0.9(0.80) + 0.1(0.40) = 0.72 + 0.04 = 0.76.

Step 3. Naive difference: 0.72−0.76=−0.040.72 - 0.76 = -0.04.

Answer: the raw comparison says treatment lowers recovery by 4 points, although it raises it by 13. Each group is averaged over its own case mix, and the treated are much sicker.

Exercise 13 · ATT versus ATE

Compute the effect on the treated, E[Y(1)−Y(0)∣T=1]E[Y(1) - Y(0) \mid T = 1]. Why does it differ from the ATE?

Show solution

Step 1. Effect within each stratum: mild 0.90−0.80=0.100.90 - 0.80 = 0.10; severe 0.60−0.40=0.200.60 - 0.40 = 0.20.

Step 2. ATT averages these over the treated case mix (40% mild, 60% severe):

ATT=0.4(0.10)+0.6(0.20)=0.04+0.12=0.16.\text{ATT} = 0.4(0.10) + 0.6(0.20) = 0.04 + 0.12 = 0.16.

Step 3. ATE uses the population mix (70% mild): 0.7(0.10)+0.3(0.20)=0.130.7(0.10) + 0.3(0.20) = 0.13.

Answer: ATT = 0.16 > ATE = 0.13, because the effect is larger for severe cases and the treated are disproportionately severe.

Exercise 14 · Why a voucher lottery works

A city allocates private-school vouchers by lottery: T=1T = 1 for winners. Let Y(1)Y(1) be a child's test score with a voucher. Show that if winning is independent of Y(1)Y(1), then the winners' average score identifies E[Y(1)]E[Y(1)], i.e. E[Y∣T=1]=E[Y(1)]E[Y \mid T = 1] = E[Y(1)].

Show solution

Step 1. Among winners we observe the voucher outcome: Y=Y(1)Y = Y(1) when T=1T = 1, so E[Y∣T=1]=E[Y(1)∣T=1]E[Y \mid T = 1] = E[Y(1) \mid T = 1].

Step 2. The lottery makes TT independent of Y(1)Y(1), so conditioning on TT does not change its mean (Exercise 5): E[Y(1)∣T=1]=E[Y(1)]E[Y(1) \mid T = 1] = E[Y(1)].

Step 3. The same argument for losers gives E[Y∣T=0]=E[Y(0)]E[Y \mid T = 0] = E[Y(0)].

Answer: the winners' mean minus the losers' mean equals E[Y(1)]−E[Y(0)]E[Y(1)] - E[Y(0)], the ATE of a voucher. That is why lotteries are prized in education research.

Exercise 15 · How much does education explain?

Using Exercise 4, what share of wage variance is explained by education group?

Show solution

Step 1. From Exercise 4: between-group variance 216, total 436.

Step 2. Share explained =216/436≈0.495= 216/436 \approx 0.495.

Answer: education group explains about half the variance in wages (the population R2R^2 of the CEF); the other half is variation among workers with the same education.

Verify in Python

import numpy as np

mild = 0.7
cef = {(1, "mild"): 0.90, (1, "severe"): 0.60, (0, "mild"): 0.80, (0, "severe"): 0.40}
w_pop = {"mild": mild, "severe": 1 - mild}
w_treated = {"mild": 0.4, "severe": 0.6}
w_untreated = {"mild": 0.9, "severe": 0.1}

def mean(t, w):
    return sum(cef[(t, s)] * w[s] for s in w)

print("ATE  :", round(mean(1, w_pop) - mean(0, w_pop), 3))
print("ATT  :", round(mean(1, w_treated) - mean(0, w_treated), 3))
print("naive:", round(mean(1, w_treated) - mean(0, w_untreated), 3))

# Law of total variance by simulation (Exercise 4)
rng = np.random.default_rng(3)
n = 1_000_000
college = rng.random(n) < 0.4
W = np.where(college, rng.normal(70, 20, n), rng.normal(40, 10, n))
within = 0.6 * W[~college].var() + 0.4 * W[college].var()
between = 0.6 * 0.4 * (W[college].mean() - W[~college].mean())**2
print(f"Var(W) = {W.var():.1f}; within + between = {within + between:.1f}")

Key takeaways

  • The overall mean is a weighted average of conditional means (LIE).
  • The CEF is the best possible predictor; its error is uncorrelated with every function of XX.
  • With unconfoundedness, the adjustment formula turns subgroup comparisons into ATE and ATT.

Simulation Lab: Conditional Expectation & Total Variance

Open full screen →

Two groups, their means and spreads: split total variance into within and between parts.

00.010.020.030255075100125wage ($ thousands)densityE[W|HS]E[W|Col]E[W]
high school (weighted by share)college (weighted by share)all workers
Var(W) = E[Var(W|X)] + Var(E[W|X])
within 220
between 216
E[W] (LIE)
52.0
Var(W)
436
Share explained (R²)
0.50
between ÷ total
Within-group share
0.50

The overall distribution (grey) is a mixture of the two groups. Its mean is the share-weighted average of group means (LIE); its variance is within + between.

Day 5Day 7