Course contents 11 / 22

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 XX, guess the target YY. 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 E[Y∣X]E[Y \mid X]. 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 YY into the part XX explains and the part it cannot, and shows which other losses lead to which other targets.

Notation. m(x)=E[Y∣X=x]m(x) = E[Y \mid X = x] is a number for each xx, the regression function. E[Y∣X]=m(X)E[Y \mid X] = m(X) is the same function applied to the random XX, so it is a random variable. Var⁡(Y∣X=x)\operatorname{Var}(Y \mid X = x) is the variance of the conditional distribution.

Conditional expectation as a function#

The conditional expectation is the expectation of the conditional distribution:

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

With two dice, XX the first and YY the larger of the two, the conditional distribution given X=3X = 3 puts 3/63/6 on 3 and 1/61/6 on each of 4, 5, 6, so E[Y∣X=3]=(9+4+5+6)/6=4E[Y \mid X = 3] = (9 + 4 + 5 + 6)/6 = 4. Over all six values,

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 of the previous note, 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 in general need not be.

Reading E[Y∣X]E[Y \mid X] as a random variable, m(X)m(X), is what makes the concept work. Before XX is observed, the best prediction of YY 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 XX gives back the unconditional mean:

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

For the dice, 16(72+113+4+92+316+6)=16136\frac16 (\frac72 + \frac{11}3 + 4 + \frac92 + \frac{31}6 + 6) = \frac{161}{36}, 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 N∼Poisson(20)N \sim \mathrm{Poisson}(20) customers in an hour, and each spends an amount with mean 30, independent of the other customers and of their number. Given N=nN = n the expected revenue is 30n30n, so the expected revenue is E[30N]=30⋅20=600E[30N] = 30 \cdot 20 = 600. 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 gg, add and subtract m(X)m(X):

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

The cross term 2E[(Y−m(X))(m(X)−g(X))]2E[(Y - m(X))(m(X) - g(X))] vanishes: conditioning on XX turns m(X)−g(X)m(X) - g(X) into a constant and E[Y−m(X)∣X]=0E[Y - m(X) \mid X] = 0, and the tower property averages the zero. The first term does not depend on gg; the second is zero only for g=mg = m. So mm is the best predictor, and every other predictor pays the extra squared distance from mm.

Figure

The conditional mean is the best predictor

Point cloud, conditional mean and best linePoints around a sine curve; the red sine passes through the middle of the cloud, the amber line crosses it diagonally.-2-1012E[Y | X = x] = sin 1.5x: error 0.090best line 0.52x: error 0.255Point cloud, conditional mean and best linePoints around a sine curve; the red sine passes through the middle of the cloud, the amber line crosses it diagonally.-2-1012E[Y | X = x] = sin 1.5x: error 0.090best line 0.52x: error 0.255
260 points: X uniform on [−2, 2], Y = sin 1.5X plus noise with standard deviation 0.3. The conditional mean E[Y | X] = sin 1.5X (red curve) has mean squared error 0.3² = 0.090, the noise that no predictor can remove. The best straight line 0.519x (amber) adds the error of its shape and gets 0.255.

The decomposition separates the error of any predictor into two parts. The first term, the noise in YY that XX does not determine, is irreducible: 0.0900.090 in the figure, whatever the model. The second is the squared distance of gg from mm. For the best predictor g∗g^* inside a model class it is the approximation error of that class: the best straight line cannot follow a sine and pays 0.255−0.090=0.1650.255 - 0.090 = 0.165 on top. A model fitted to a finite training set lands at some other gg in the class, further from mm than g∗g^*; 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 YY splits in the same way:

Var⁡(Y)=E[Var⁡(Y∣X)]⏟unexplained+Var⁡(E[Y∣X])⏟explained.\operatorname{Var}(Y) = \underbrace{E\big[\operatorname{Var}(Y \mid X)\big]}_{\text{unexplained}} + \underbrace{\operatorname{Var}\big(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 section; the second is how much the prediction itself varies with XX. For the dice:

Figure

The law of total variance on two dice

Conditional distributions by column and the split of the varianceSix columns of circles; the means rise from 3.5 to 6; the variance bar splits into 1.20 and 0.77.123456123456X: the first dieY: the largerVar(Y) = 1.97E[Var(Y | X)] = 1.20Var(E[Y | X]) = 0.77Conditional distributions by column and the split of the varianceSix columns of circles; the means rise from 3.5 to 6; the variance bar splits into 1.20 and 0.77.123456123456X: the first dieY: the largerVar(Y) = 1.97E[Var(Y | X)] = 1.20Var(E[Y | X]) = 0.77
For each value of the first die X, the conditional distribution of the larger value Y (circle area proportional to probability) and its mean E[Y | X = x] (diamonds): 3.5, 3.67, 4, 4.5, 5.17, 6. The variance of Y, 1.97, splits into the average variance within the columns, 1.20, and the variance of the means themselves, 0.77: knowing X explains 39% of the spread of Y.

The ratio Var⁡(E[Y∣X])/Var⁡(Y)\operatorname{Var}(E[Y \mid X]) / \operatorname{Var}(Y) is the largest R2R^2 any model can reach using XX, here 0.390.39. For the Gaussian of the previous note it checks out with the formulas found there: Var⁡(X2)=1=0.28+0.62⋅2\operatorname{Var}(X_2) = 1 = 0.28 + 0.6^2 \cdot 2, 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 E[Var⁡(⋅∣X)]E[\operatorname{Var}(\cdot \mid X)] 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:

LossBest prediction given X=xX = xIn practice
squared error (y−g)2(y - g)^2conditional mean E[Y∣X=x]E[Y \mid X = x]regression with MSE
absolute error ∣y−g∣\lvert y - g \rvertconditional medianrobust regression, MAE
pinball loss at level qq, (y−g)(q−1[y<g])(y - g)(q - \mathbb 1[y < g])conditional qq-quantileprediction intervals, quantile regression
0–1 loss for classesmost 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 conditional 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. 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 XXWith XX
Best prediction of YYthe constant E[Y]E[Y]the function m(X)=E[Y∣X]m(X) = E[Y \mid X]
Its mean squared errorVar⁡(Y)\operatorname{Var}(Y)E[Var⁡(Y∣X)]E[\operatorname{Var}(Y \mid X)]
Computing a meandirectlytower property: condition, then average
Variance of YYone numberexplained + unexplained
Error of a trained modelone numberirreducible + squared distance from mm (approximation, estimation)

Limitations#

  • mm 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 mm.
  • 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. E[Y∣X=x]E[Y \mid X = x] describes what YY looks like among cases where XX happens to be xx, not what YY would become if XX were set to xx.

What you should remember#

  • E[Y∣X=x]E[Y \mid X = x] is the mean of the conditional distribution; E[Y∣X]=m(X)E[Y \mid X] = m(X) is a random variable.
  • Tower property: E[E[Y∣X]]=E[Y]E[E[Y \mid X]] = E[Y].
  • m(X)m(X) minimizes E[(Y−g(X))2]E[(Y - g(X))^2]; E[(Y−g(X))2]=E[(Y−m(X))2]+E[(m(X)−g(X))2]E[(Y - g(X))^2] = E[(Y - m(X))^2] + E[(m(X) - g(X))^2].
  • Var⁡(Y)=E[Var⁡(Y∣X)]+Var⁡(E[Y∣X])\operatorname{Var}(Y) = E[\operatorname{Var}(Y \mid X)] + \operatorname{Var}(E[Y \mid X]): 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.