Day 6. Conditional Expectation, the LIE & Total Variance
The conditional expectation function, the law of iterated expectations and the adjustment formula.
- ●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.
Theory
The conditional expectation function
is the mean of among units with . As a function of it is the conditional expectation function (CEF), . Plugging in the random gives , itself a random variable.
Three workhorse results
1. Law of iterated expectations (LIE).
The overall mean is the average of the group means, weighted by group size.
2. Taking out what is known. For any function , .
3. CEF decomposition. Write with . Then , so is uncorrelated with every function of . Moreover, is the best predictor of : it minimises over all functions . If the CEF happens to be linear, , then , the regression slope.
Law of total variance
The share of variance "explained" by is the between part divided by the total: the population of the CEF.
The adjustment formula
Suppose treatment is as good as random within levels of : ("unconfoundedness"). Then the LIE gives
and similarly for . Weighting by 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, thousand.
Exercise 2 · Checking the LIE
Using Day 5's table, , and . Verify that the LIE gives .
Show solution
. ✓
Exercise 3 · Conditional variances
In the same table, compute and .
Show solution
is Bernoulli: and .
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 via the law of total variance.
Show solution
Step 1. Within-group part, the share-weighted average of group variances:
Step 2. Between-group part, the variance of the group means. (Exercise 1), so
(Shortcut for two groups: .)
Step 3. Total: .
Answer: 436 (sd ≈ $20.9k).
Exercise 5 · Independence
Show that if and are independent, .
Show solution
Independence means , so for every . The CEF is flat.
Part 2 · Proofs
Exercise 6 · Proving the LIE
Prove for discrete variables.
Show solution
Exercise 7 · The CEF error is orthogonal to functions of X
With , show that and for any .
Show solution
. Then, by the LIE and "taking out what is known",
Exercise 8 · The CEF is the best predictor
Prove that for any function .
Show solution
Step 1. Write .
Step 2. Expanding,
Step 3. The middle term is zero by Exercise 7 (it is times a function of ), and the last is non-negative.
Step 4. So the error is smallest when .
Exercise 9 · Proving total variance
Prove .
Show solution
Write .
Step 1. Conditional variance shortcut: .
Step 2. Take expectations and use the LIE on the first term:
Step 3. Variance of the CEF, using (LIE):
Step 4. Add steps 2 and 3; the terms cancel:
Exercise 10 · A linear CEF is the regression line
Suppose . Prove .
Show solution
By the LIE, and . Then
Part 3 · Applied
A hospital's records give mean recovery rates by treatment and severity . In the population, 70% of cases are mild.
| Mild | Severe | |
|---|---|---|
| Treated () | 0.90 | 0.60 |
| Untreated () | 0.80 | 0.40 |
Assume treatment is as good as random within severity level.
Exercise 11 · The adjustment formula
Compute , and the ATE.
Show solution
Step 1. Unconfoundedness lets us use the treated patients' recovery rate in each severity group as . Average over the population case mix (70% mild):
Step 2. Same for the untreated:
Step 3. ATE .
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 .
Show solution
Step 1. Treated patients' case mix: 40% mild, 60% severe. Their average recovery:
Step 2. Untreated case mix: 90% mild, 10% severe:
Step 3. Naive difference: .
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, . Why does it differ from the ATE?
Show solution
Step 1. Effect within each stratum: mild ; severe .
Step 2. ATT averages these over the treated case mix (40% mild, 60% severe):
Step 3. ATE uses the population mix (70% mild): .
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: for winners. Let be a child's test score with a voucher. Show that if winning is independent of , then the winners' average score identifies , i.e. .
Show solution
Step 1. Among winners we observe the voucher outcome: when , so .
Step 2. The lottery makes independent of , so conditioning on does not change its mean (Exercise 5): .
Step 3. The same argument for losers gives .
Answer: the winners' mean minus the losers' mean equals , 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 .
Answer: education group explains about half the variance in wages (the population 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 .
- 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.
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.