Conditional Expectation
E[X|Y] as a random variable, the tower property, and conditional variance decomposition.
Assumes you know
Conditional Expectation
Intuition first
An average taken within a group. The average height of everyone is one number; the average height of everyone aged 12 is another. That second quantity is a conditional expectation.
The step that makes this subtle is treating the age itself as random. Before you know someone's age, "the average height of people their age" is not yet a number — it is a value that will be determined once the age is revealed. So is a random variable, a function of , and it has its own mean and its own variance.
Two results follow, and both are workhorses. Averaging those group averages, weighted by group size, recovers the overall average — the tower property. And the total variance splits cleanly into variation within groups plus variation between group means, which is the identity behind ANOVA and behind the bias–variance decomposition.
Two objects, carefully distinguished
| Symbol | Meaning | Read aloud |
|---|---|---|
| E[X | Y = y] | A number: the mean of X within the group Y = y | E of X given Y equals y |
| E[X | Y] | A random variable: that mean, as a function of the random Y | E of X given Y |
| Var(X | Y) | Conditional variance, also a random variable | variance of X given Y |
For discrete variables:
The tower property
Also called the law of total expectation or the law of iterated expectations. Averaging the group averages, weighted by how likely each group is, gives the overall average.
Proof of the tower propertyAdvanced
Work discretely. The outer expectation averages over the possible values of :
Expand the inner conditional expectation by its definition:
Move inside the inner sum and use the multiplication rule :
Swap the order of summation and marginalise over :
The whole proof is the multiplication rule followed by marginalisation. It is the law of total probability with values attached.
Why conditioning is a prediction
E[X | Y] minimises expected squared errorAdvanced
Among all functions , which minimises ?
Write and decompose:
Square and take expectations:
The cross term vanishes. Condition on first — inside that conditioning, is a constant, so it factors out:
By the tower property . Therefore
The first term does not involve ; the second is non-negative and is zero exactly when . So is the minimiser.
This is the theoretical target of regression. A squared-error regression model is estimating , which is why it predicts conditional means and why it is pulled around by outliers — the mean is.
The variance decomposition
Sometimes called Eve's law, or the law of total variance.
Solved problem 1 · Both laws on a two-machine factory
Machine A produces 70% of items with mean weight 50 g and variance 4. Machine B produces 30% with mean weight 55 g and variance 9. An item is selected at random.
Find the overall mean and variance of its weight.
Step 1 — set up the conditioning variable
Let be the machine, the weight.
Step 2 — overall mean by the tower property
Step 3 — within-group variance component
This is the average spread inside a machine's output.
Step 4 — between-group variance component
is a random variable taking value 50 with probability 0.7 and 55 with probability 0.3. Its variance:
Step 5 — combine
So g.
Step 6 — interpret the split
Within-group variation is 5.5 and between-group is 5.25 — almost equal. Roughly half the total variability in item weight comes from the machines differing in their averages, and half from scatter within each machine.
That is actionable: calibrating the two machines to a common mean would remove about 49% of total variance, while tightening each machine's own tolerance would address the other half.
Answer
g and , decomposing as within-machine plus between-machine.
Solved problem 2 · Conditioning to avoid a hard calculation
A hen lays eggs, where . Each egg hatches independently with probability . Let be the number that hatch. Find and .
Step 1 — why direct calculation is unpleasant
has a compound distribution — a binomial whose number of trials is itself random. Writing its PMF requires summing over all possible :
Doable, but conditioning is far easier.
Step 2 — condition on N
Given , the number hatching is binomial:
Step 3 — mean by the tower property
Step 4 — within-group variance term
Step 5 — between-group variance term
For Poisson, :
Step 6 — total
Step 7 — recognise the result
. Equal mean and variance is the signature of a Poisson distribution, and indeed .
This is Poisson thinning: independently keeping each event of a Poisson process with probability yields a Poisson process with rate . Conditioning gave us the mean and variance in six lines; the general theorem is worth remembering separately.
Answer
and , with .
import numpy as np
rng = np.random.default_rng(0)
n = 2_000_000
# Factory example.
machine = rng.random(n) < 0.7 # True = machine A
weight = np.where(machine,
rng.normal(50, 2, n), # var 4
rng.normal(55, 3, n)) # var 9
print(f"E[W] {weight.mean():.4f} theory 51.5")
print(f"Var(W) {weight.var():.4f} theory 10.75")
print(f" within {4*0.7 + 9*0.3:.4f}")
print(f" between {50**2*0.7 + 55**2*0.3 - 51.5**2:.4f}")
# Poisson thinning.
N = rng.poisson(6, n)
H = rng.binomial(N, 0.8)
print(f"\nE[H] {H.mean():.4f} Var(H) {H.var():.4f} both theory 4.8")Exercise 1
and has mean 5 and variance 3. Find .
Show solutionHide solution
By the tower property and then linearity:
The variance of is not needed for the mean — it would be needed for , which also requires , information not supplied here. Without it you can compute only the between-group component, , which is a lower bound on .
Exercise 2
A coin has unknown bias , uniform on . Given , you flip it 10 times. Find the expected number of heads.
Show solutionHide solution
Let be the number of heads. Given , , so
By the tower property:
For , , giving
The same answer as a fair coin — but the distribution is completely different. For a fair coin, . Here, applying the variance decomposition:
Four times the variance of a fair coin, because the unknown bias adds substantial between-group spread. In fact is uniform on here — a genuinely surprising result, and the Bayesian reason a uniform prior on is sometimes called uninformative about the count.