Skip to content
BIASED

Conditional expectation

Conditional expectation as a function and as a random variable, the tower property, the best predictor under squared error and its irreducible floor, the law of total variance, and the targets of other losses.

The mean of a slice

The topic The multivariate Gaussian ended with a slice. Take (X1,X2)∼N(0,Σ)(X_1, X_2) \sim \mathcal N(0, \Sigma) with Σ11=2\Sigma_{11} = 2, Σ12=1.2\Sigma_{12} = 1.2 and Σ22=1\Sigma_{22} = 1. Fixing X1=x1X_1 = x_1 leaves a normal distribution for X2X_2, namely N(0.6 x1, 0.28)\mathcal N(0.6\, x_1,\ 0.28). Its mean slides along a straight line as x1x_1 changes. This topic asks the same question for any pair of variables: once we know XX, what is the best guess for YY, and how much error is left?

That is supervised learning in one sentence: features XX, a target YY. Before any model is trained, probability theory already names the best possible guess, provided the joint distribution is known.

The conditional expectation is the expectation of the conditional distribution from the topic Joint distributions and independence. For discrete and continuous YY it reads

E[Y∣X=x]=∑yy pY∣X(y∣x)orE[Y∣X=x]=∫y pY∣X(y∣x) dy.\mathbb E[Y \mid X = x] = \sum_y y\, p_{Y \mid X}(y \mid x) \qquad \text{or} \qquad \mathbb E[Y \mid X = x] = \int y\, p_{Y \mid X}(y \mid x)\, dy .

Back to the two dice. XX is the first die and YY is the larger of the two. Given X=3X = 3, the larger value is 3 whenever the second die shows 1, 2 or 3, with probability 3/63/6. Otherwise it is the second die itself: 4, 5 or 6, each with probability 1/61/6. So

E[Y∣X=3]=3⋅3+4+5+66=4.\mathbb E[Y \mid X = 3] = \frac{3 \cdot 3 + 4 + 5 + 6}{6} = 4 .

Without any information the mean of YY is 161/36≈4.47161/36 \approx 4.47. A low first die pulls the guess down.

A function and a random variable

For every xx the conditional expectation is a number. Collected over all xx it is a function, the regression function:

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

For the dice the six values are

m(1),…,m(6)=72, 113, 4, 92, 316, 6.m(1), \dots, m(6) = \tfrac72,\ \tfrac{11}3,\ 4,\ \tfrac92,\ \tfrac{31}6,\ 6 .

For the Gaussian vector m(x1)=0.6 x1m(x_1) = 0.6\, x_1. In general mm can be any function. It is linear for jointly Gaussian variables, and the dice already show that it need not be: the steps between neighbouring values grow from 1/61/6 to 5/65/6.

Now plug the random XX into mm. The result

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

is a random variable. Before the first die is rolled, the best prediction of YY is itself unknown. For the dice it takes the six values above, each with probability 1/61/6. It has its own distribution, mean and variance, and the rest of the topic uses exactly that.

The same reading works for the spread. Var⁡(Y∣X=x)\operatorname{Var}(Y \mid X = x) is the variance of the conditional distribution, a number. Var⁡(Y∣X)\operatorname{Var}(Y \mid X) is that function applied to XX, again a random variable. For the dice Var⁡(Y∣X=6)=0\operatorname{Var}(Y \mid X = 6) = 0: a six on the first die makes the larger value certain.

Condition, then average

Average the conditional means over the distribution of XX, and the unconditional mean comes back:

E[E[Y∣X]]=E[Y].\mathbb E\big[\mathbb E[Y \mid X]\big] = \mathbb E[Y] .

This is the tower property, also called the law of total expectation. For a discrete XX it reads E[Y]=∑xm(x) P(X=x)\mathbb E[Y] = \sum_x m(x)\, P(X = x), the law of total probability from the topic Conditional probability and Bayes' formula, applied to means. For the dice,

16(72+113+4+92+316+6)=16136,\tfrac16 \left( \tfrac72 + \tfrac{11}3 + 4 + \tfrac92 + \tfrac{31}6 + 6 \right) = \tfrac{161}{36},

the same mean that the joint table gave.

The working rule: to compute a mean, condition on whatever makes the problem easy, then average.

A random number of terms. A shop has N∼Pois⁡(20)N \sim \operatorname{Pois}(20) customers in an hour. Each spends a random amount with mean 30, independent of the other customers and of how many there are. A sum with a random number of terms looks hard. Conditioning on NN makes it easy: given N=nN = n the expected revenue SS is 30n30n, so E[S∣N]=30N\mathbb E[S \mid N] = 30N and

E[S]=E[30N]=30⋅20=600.\mathbb E[S] = \mathbb E[30N] = 30 \cdot 20 = 600 .

The same step gives the expected cost of a random number of retries, or the expected number of tokens a language model writes before a stop token.

The best predictor

The topic Expectation and variance found the best constant guess under squared loss. It is E[Y]\mathbb E[Y], and its error is Var⁡(Y)\operatorname{Var}(Y). Knowing XX, we may pick any function gg of it instead.

The conditional mean m(X)m(X) has the smallest mean squared error among all functions of XX. To see it, add and subtract m(X)m(X) inside the square. For any gg,

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

The cross term 2 E[(Y−m(X))(m(X)−g(X))]2\,\mathbb E\big[(Y - m(X))(m(X) - g(X))\big] vanishes. Given XX, the factor m(X)−g(X)m(X) - g(X) is a constant, and E[Y−m(X)∣X]=0\mathbb E[Y - m(X) \mid X] = 0. The tower property then averages that zero.

The first term does not involve gg. The second is zero only for g=mg = m. So mm is the best predictor, and any other gg pays its mean squared distance from mm on top.

The first term is a floor. Conditioned on XX it is the spread of YY around its conditional mean, so by the tower property

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

For the Gaussian vector Var⁡(X2∣X1)=0.28\operatorname{Var}(X_2 \mid X_1) = 0.28 wherever X1X_1 lands, so no predictor of X2X_2 from X1X_1 gets below 0.280.28. And since mm is linear there, linear regression on jointly Gaussian data is not an approximation: it is the best predictor. In general, every regression model trained with squared loss is an estimate of mm.

Where the error comes from

The decomposition splits the error of any model into named parts. Here is an example where mm is not a line. XX is uniform on [−2,2][-2, 2], and

Y=sin⁡1.5X+ε,ε∼N(0, 0.32) independent of X.Y = \sin 1.5X + \varepsilon, \qquad \varepsilon \sim \mathcal N(0,\ 0.3^2) \text{ independent of } X .

The conditional mean is m(x)=sin⁡1.5xm(x) = \sin 1.5x.

  • Irreducible error. The noise that XX does not determine: E[Var⁡(Y∣X)]=0.32=0.090\mathbb E[\operatorname{Var}(Y \mid X)] = 0.3^2 = 0.090. No model goes below it.
  • Approximation error. Restrict the model to straight lines. The best line is about 0.52 x0.52\, x, and its mean squared error is about 0.2550.255. A line cannot follow a sine, and the shape costs 0.255−0.090=0.1650.255 - 0.090 = 0.165 on top of the floor.
  • Estimation error. A line fitted to a finite training set lands somewhere else in the class, further from mm than the best line. The topic Estimators: bias and variance splits this part into bias and variance.
  • Optimization error. If training stops before reaching the best fit on the training set, that adds a little more.

More data shrinks only the estimation error. A richer class of models shrinks the approximation error. Nothing shrinks the floor except a more informative XX.

Explained and unexplained variance

Apply the decomposition to the best constant, g=E[Y]g = \mathbb E[Y]. The left side becomes Var⁡(Y)\operatorname{Var}(Y). On the right, the tower property gives E[m(X)]=E[Y]\mathbb E[m(X)] = \mathbb E[Y], so the second term is the variance of m(X)m(X). That is the law of total variance:

Var⁡(Y)=E[Var⁡(Y∣X)]⏟unexplained+Var⁡(E[Y∣X])⏟explained.\operatorname{Var}(Y) = \underbrace{\mathbb E\big[\operatorname{Var}(Y \mid X)\big]}_{\text{unexplained}} + \underbrace{\operatorname{Var}\big(\mathbb E[Y \mid X]\big)}_{\text{explained}} .

The first term is the average spread of YY around the prediction, the irreducible error of the previous block. The second is how much the prediction itself moves with XX.

For the dice, the variance of the larger value splits as

1.97=1.20+0.77.1.97 = 1.20 + 0.77 .

The conditional variances run from 2.922.92 at X=1X = 1 down to 00 at X=6X = 6, and they average to 1.201.20. The six conditional means vary with variance 0.770.77. Knowing the first die explains 0.77/1.97≈0.390.77 / 1.97 \approx 0.39 of the spread of YY. That ratio is the largest R2R^2 any model using XX can reach.

One more reading. Both terms are non-negative, so Var⁡(E[Y∣X])≤Var⁡(Y)\operatorname{Var}(\mathbb E[Y \mid X]) \le \operatorname{Var}(Y). Replacing a random quantity by its conditional expectation never increases its variance: averaging out randomness that XX does not explain can only make a quantity steadier.

Other losses, other targets

The conditional mean is the answer for squared loss. Other losses ask for other summaries of the same conditional distribution:

Loss Best prediction given X=xX = x In practice
squared error (y−g)2(y - g)^2 conditional mean regression with MSE
absolute error ∣y−g∣\lvert y - g \rvert conditional median robust regression, MAE
pinball loss at level qq conditional qq-quantile prediction intervals, quantile regression
0–1 loss for classes most probable class arg⁡max⁡kP(Y=k∣X=x)\arg\max_k P(Y = k \mid X = x) the Bayes classifier
log loss (cross-entropy) the whole distribution P(Y∣X=x)P(Y \mid X = x) classifiers, language models

The last row is why classifiers and language models output distributions rather than single answers. A spam filter trained with log loss reports P(spam∣email)P(\text{spam} \mid \text{email}), and the threshold can be chosen later.

When the conditional distribution has several modes, the mean is a poor summary. Train a model with squared error to predict the next frame of a video, and it averages the possible futures into a blur. That is one reason generative models learn the conditional distribution itself and sample from it; the course Generative Models picks this up.

Limits and what comes next

Three limits keep the conditional mean honest.

  • mm is unknown. It is defined by the joint distribution, which we never see. Supervised learning is, in essence, estimating mm from finitely many samples. With many features that takes either a lot of data or strong assumptions about the shape of mm.
  • A mean is not a decision. For a skewed or multimodal conditional distribution the mean can be a value that rarely or never occurs. The loss, not habit, should choose the target.
  • Conditioning is not intervention. E[Y∣X=x]\mathbb E[Y \mid X = x] describes YY among the cases where XX happens to equal xx. It does not say what YY would become if we set XX to xx ourselves.

This closes the part of the course about several random variables. So far every distribution was known: the dice, the Gaussian, the sine with its noise. In practice a distribution is seen only through a sample, and we work with averages: a validation accuracy, a mean loss, a conversion rate. How far can such an average be from the expectation it estimates? The next topic, Inequalities and concentration, gives the first guarantees. They bound the probability of a large deviation using only the mean, the variance or the range.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.