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 with , and . Fixing leaves a normal distribution for , namely . Its mean slides along a straight line as changes. This topic asks the same question for any pair of variables: once we know , what is the best guess for , and how much error is left?
That is supervised learning in one sentence: features , a target . 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 it reads
Back to the two dice. is the first die and is the larger of the two. Given , the larger value is 3 whenever the second die shows 1, 2 or 3, with probability . Otherwise it is the second die itself: 4, 5 or 6, each with probability . So
Without any information the mean of is . A low first die pulls the guess down.
A function and a random variable
For every the conditional expectation is a number. Collected over all it is a function, the regression function:
For the dice the six values are
For the Gaussian vector . In general 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 to .
Now plug the random into . The result
is a random variable. Before the first die is rolled, the best prediction of is itself unknown. For the dice it takes the six values above, each with probability . It has its own distribution, mean and variance, and the rest of the topic uses exactly that.
The same reading works for the spread. is the variance of the conditional distribution, a number. is that function applied to , again a random variable. For the dice : a six on the first die makes the larger value certain.
Condition, then average
Average the conditional means over the distribution of , and the unconditional mean comes back:
This is the tower property, also called the law of total expectation. For a discrete it reads , the law of total probability from the topic Conditional probability and Bayes' formula, applied to means. For the dice,
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 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 makes it easy: given the expected revenue is , so and
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 , and its error is . Knowing , we may pick any function of it instead.
The conditional mean has the smallest mean squared error among all functions of . To see it, add and subtract inside the square. For any ,
The cross term vanishes. Given , the factor is a constant, and . The tower property then averages that zero.
The first term does not involve . The second is zero only for . So is the best predictor, and any other pays its mean squared distance from on top.
The first term is a floor. Conditioned on it is the spread of around its conditional mean, so by the tower property
For the Gaussian vector wherever lands, so no predictor of from gets below . And since 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 .
Where the error comes from
The decomposition splits the error of any model into named parts. Here is an example where is not a line. is uniform on , and
The conditional mean is .
- Irreducible error. The noise that does not determine: . No model goes below it.
- Approximation error. Restrict the model to straight lines. The best line is about , and its mean squared error is about . A line cannot follow a sine, and the shape costs on top of the floor.
- Estimation error. A line fitted to a finite training set lands somewhere else in the class, further from 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 .
Explained and unexplained variance
Apply the decomposition to the best constant, . The left side becomes . On the right, the tower property gives , so the second term is the variance of . That is the law of total variance:
The first term is the average spread of around the prediction, the irreducible error of the previous block. The second is how much the prediction itself moves with .
For the dice, the variance of the larger value splits as
The conditional variances run from at down to at , and they average to . The six conditional means vary with variance . Knowing the first die explains of the spread of . That ratio is the largest any model using can reach.
One more reading. Both terms are non-negative, so . Replacing a random quantity by its conditional expectation never increases its variance: averaging out randomness that 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 | In practice |
|---|---|---|
| squared error | conditional mean | regression with MSE |
| absolute error | conditional median | robust regression, MAE |
| pinball loss at level | conditional -quantile | prediction intervals, quantile regression |
| 0–1 loss for classes | most probable class | the Bayes classifier |
| log loss (cross-entropy) | the whole distribution | 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 , 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.
- is unknown. It is defined by the joint distribution, which we never see. Supervised learning is, in essence, estimating from finitely many samples. With many features that takes either a lot of data or strong assumptions about the shape of .
- 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. describes among the cases where happens to equal . It does not say what would become if we set to 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.
Answers are saved on the server and tied to this browser until you sign in.