11 / 22 · Several Random Variables
Conditional expectation
The best prediction of one random variable from another: E[Y | X] as a function and as a random variable, the tower property, why the conditional mean minimizes squared error and leaves an irreducible noise floor, the law of total variance (explained and unexplained variance), and which loss leads to which target: the median, a quantile, the most probable class, a whole distribution.
On this page 11
Supervised learning is a prediction problem: given features , guess the target . Before any model is trained, probability theory already says what the best possible guess is, if the joint distribution were known, and how much error would remain even then. The answer is the conditional expectation . The multivariate Gaussian note found it for one family: a linear function. This note defines it in general, proves that it minimizes squared error, splits the variance of into the part explains and the part it cannot, and shows which other losses lead to which other targets.
Notation. is a number for each , the regression function. is the same function applied to the random , so it is a random variable. is the variance of the conditional distribution.
Conditional expectation as a function#
The conditional expectation is the expectation of the conditional distribution:
With two dice, the first and the larger of the two, the conditional distribution given puts on 3 and on each of 4, 5, 6, so . Over all six values,
For the Gaussian vector of the previous note, . In general can be any function; it is linear for jointly Gaussian variables and in general need not be.
Reading as a random variable, , is what makes the concept work. Before is observed, the best prediction of is itself uncertain, and it has a distribution, a mean and a variance of its own.
The tower property#
Averaging the conditional means over the distribution of gives back the unconditional mean:
For the dice, , the mean of the larger value found in the joint distributions note. The tower property, also called the law of total expectation, is the law of total probability for expectations: 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, and each spends an amount with mean 30, independent of the other customers and of their number. Given the expected revenue is , so the expected revenue is . The same step gives the expected cost of a random number of retries, or the expected number of tokens generated before a stop token.
The best predictor#
For any function , add and subtract :
The cross term vanishes: conditioning on turns into a constant and , and the tower property averages the zero. The first term does not depend on ; the second is zero only for . So is the best predictor, and every other predictor pays the extra squared distance from .
The conditional mean is the best predictor
The decomposition separates the error of any predictor into two parts. The first term, the noise in that does not determine, is irreducible: in the figure, whatever the model. The second is the squared distance of from . For the best predictor inside a model class it is the approximation error of that class: the best straight line cannot follow a sine and pays on top. A model fitted to a finite training set lands at some other in the class, further from than ; the extra distance is the estimation error, plus an optimization error if training stops short of the best fit. The estimators note splits the estimation part into bias and variance.
The law of total variance#
The variance of splits in the same way:
The first term is the average spread of around the prediction, the irreducible error of the previous section; the second is how much the prediction itself varies with . For the dice:
The law of total variance on two dice
The ratio is the largest any model can reach using , here . For the Gaussian of the previous note it checks out with the formulas found there: , the conditional variance plus the variance of the linear conditional mean.
The law also says that replacing a random quantity by its conditional expectation never increases the variance, since the dropped term is non-negative. Applied to an estimator, by conditioning it on a sufficient statistic, a summary of the data given which their distribution no longer depends on the unknown parameter (for coin flips, the number of heads), this is the Rao–Blackwell theorem: averaging out irrelevant randomness never makes an estimate worse and usually makes it better.
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 conditional distribution | classifiers, language models |
The last row is why classifiers and language models output distributions rather than single answers. When the conditional distribution has several modes, the mean is a poor summary: a model trained with squared error to predict the next video frame averages the possible futures into a blur, which is one of the reasons generative models learn the conditional distribution and sample from it.
What changed#
| Without | With | |
|---|---|---|
| Best prediction of | the constant | the function |
| Its mean squared error | ||
| Computing a mean | directly | tower property: condition, then average |
| Variance of | one number | explained + unexplained |
| Error of a trained model | one number | irreducible + squared distance from (approximation, estimation) |
Limitations#
- is defined by the unknown joint distribution. Supervised learning is, in essence, the problem of estimating it from finitely many samples, and in many dimensions that needs either many samples or strong assumptions about the shape of .
- A mean is not a decision. When the conditional distribution is skewed or multimodal, the conditional mean can be a value that rarely or never occurs; the loss, not habit, should choose the target.
- Conditioning is not intervention. describes what looks like among cases where happens to be , not what would become if were set to .
What you should remember#
- is the mean of the conditional distribution; is a random variable.
- Tower property: .
- minimizes ; .
- : unexplained plus explained; the irreducible error is the first term.
- Squared loss targets the mean, absolute loss the median, pinball loss a quantile, 0–1 loss the most probable class, log loss the whole conditional distribution.
Code#
The dice in exact fractions, the best predictor and a random sum by simulation:
import numpy as np
from fractions import Fraction
# two dice, X the first and Y the larger: E[Y | X = x], the tower property, the law of total variance
m = {x: Fraction(x * x + sum(range(x + 1, 7)), 6) for x in range(1, 7)}
v = {x: Fraction(x**3 + sum(y * y for y in range(x + 1, 7)), 6) - m[x] ** 2 for x in range(1, 7)}
EY = sum(m.values()) / 6
print([str(m[x]) for x in m], EY) # ['7/2', '11/3', '4', '9/2', '31/6', '6'] 161/36
unexplained, explained = sum(v.values()) / 6, sum((m[x] - EY) ** 2 for x in m) / 6
print(unexplained, explained, unexplained + explained, round(float(explained / (unexplained + explained)), 3))
# 259/216 1001/1296 2555/1296 0.392: 1.20 + 0.77 = 1.97, and X explains 39%
# the best predictor sin(1.5x) against the best straight line, on simulated data
rng = np.random.default_rng(0)
x = rng.uniform(-2, 2, 1_000_000)
y = np.sin(1.5 * x) + 0.3 * rng.normal(size=x.size)
b = np.cov(x, y)[0, 1] / np.var(x, ddof=1)
print(round(float(np.mean((y - np.sin(1.5 * x)) ** 2)), 3), round(float(b), 3), round(float(np.mean((y - b * x) ** 2)), 3))
# 0.09 0.518 0.254
# the tower property for a random sum: N ~ Poisson(20) customers, each spending 30 on average
N = rng.poisson(20, 20_000)
print(round(float(np.mean([rng.exponential(30, n).sum() for n in N])), 1)) # 599.9, against 20 * 30 = 600
Next#
The section on several random variables ends here: joint distributions, the Gaussian, and the best prediction. Everything so far has assumed the distributions are known. In practice they are seen only through samples, and the question becomes how close an average of samples is to the expectation it estimates. The next note gives the first guarantees, inequalities that bound the probability of a large deviation using only the mean, the variance or the range.
References#
- J. K. Blitzstein, J. Hwang. Introduction to Probability, 2nd ed. CRC Press, 2019. Chapter 9 (conditional expectation, Adam’s law, Eve’s law, prediction).
- T. Hastie, R. Tibshirani, J. Friedman. The Elements of Statistical Learning, 2nd ed. Springer, 2009. Section 2.4 (statistical decision theory: the regression function and the Bayes classifier).
- R. Koenker, G. Bassett. Regression Quantiles. Econometrica, 46(1), 1978.