11 / 22 · Several Random Variables
Conditional expectation
Use the information available at prediction time to choose a mean, quantify the remaining squared error and separate within-group from between-group variation. Different losses select different conditional targets.
On this page 6
A single prediction may ignore information that is already available. Suppose we must predict a response that can be , or , and before predicting we learn which of three groups it belongs to. The useful prediction can change with the group, even though the target values stay the same.
The Gaussian conditional law gave a linear conditional mean. A general joint distribution can give a nonlinear function, a table or a constant instead.
Conditional expectation as a function#
Let denote the information we observe and the response. Start with this stipulated finite law; each row after the group probability is a conditional distribution of :
| Group | ||||
|---|---|---|---|---|
| A | ||||
| B | ||||
| C |
The rows are assumed probabilities, not estimates from a dataset. Multiplying a row by its group probability gives joint probabilities. For example, the event has probability .
The conditional mean function, also called the regression function, averages within an observed group:
For discrete and a group with positive probability,
For the three rows, direct weighted sums give
The value is a number after we observe . Before that observation, is a random variable: it takes , or according to the group probabilities. This distinction explains the notation .
If the conditional law instead has a density, the definition is an integral:
We assume so the conditional expectation exists as an integrable random variable. Values at observations with zero probability or outside the support may need a choice of version; the defining random variable is unique almost surely. With a joint density, the ratio defining a conditional density is used where the marginal density is positive. These definitions and conditions are developed in Siegrist’s Conditional Expected Value and its measure-theoretic continuation.
The tower property#
Averaging the within-group means must recover the overall mean. For a discrete joint law, expand the two expectations:
Integrability justifies exchanging the sums. In our table, the weighted average is
This is the simplest tower property: . It does not say that all conditional means equal ; their weighted average does.
The nested form compares two amounts of information. Let reveal only whether is in the merged group AB or in C. Then is a function of , and
Inside AB, the conditional group weights are for A and for B. The coarse mean is therefore
Computing directly from the merged conditional response probabilities gives the same result. The fine mean averages back to the coarse mean because the coarse information is contained in the fine information. Without this nesting, replacing a double conditional expectation by either one is generally invalid. See the Consistency property in Siegrist’s conditional-expectation treatment.
The best predictor#
Why choose a mean rather than another summary of each group? The answer depends on the loss. For squared loss, assume and consider predictors with finite second moment. At a fixed observation , write the conditional variance as . Expanding around the conditional mean gives
The cross term vanishes because the conditional mean of is zero. The remaining second term is nonnegative and reaches zero exactly at .
For our groups, direct sums of squared deviations give
| Group | Mean | Variance |
|---|---|---|
| A | ||
| B | ||
| C |
In group B, for example, the conditional squared risk at prediction is
The minimum is , reached at . Predicting the most probable response instead raises that squared risk to . A likely individual outcome need not minimize average squared error.
Squared risk within each group
- A: mean 2, variance 6
- B: mean 2.5, variance 4.75
- C: mean 5, variance 3
Curve heights compare errors within different groups. Overall risk additionally weights them by group probabilities.
Averaging over the observations gives the full risk decomposition:
The first term is the error left when the joint law is known and only is observed. The second is the penalty for choosing a different predictor. Equality with the minimum requires almost surely, so choices on null events cannot change the risk. This best-predictor statement is proved in Siegrist’s section The Best Predictor.
Take the alternative predictions for groups A, B and C. Their extra squared error is
The minimum risk is , so the alternative risk is . The term “best” concerns a specified joint distribution, available information and loss. It does not promise that fitting a regression model to finite data will recover the exact conditional mean.
The law of total variance#
The same decomposition explains where the variance comes from. Take the constant prediction . Its squared error is , and the tower property gives . Therefore
This is the law of total variance: within-group variation plus variation between group means. For our finite law,
Their sum is . As a separate check, the marginal law of has probabilities , , at , , . It gives , so subtracting the squared mean gives the same variance .
Variation left after an observation
| Information | Min. risk |
|---|---|
| Constant only | 6 |
| AB or C | 14/3 |
| A, B or C | 37/8 |
The remaining error depends on the available features. An additional measurement can reduce it.
More information can reduce the optimal squared risk, but the size of the reduction depends on how much the conditional means change. For the coarser observation , the AB group has mean and variance , while C stays unchanged. Its optimal risk is
The best constant prediction has risk , the coarse prediction has risk , and the fine prediction has risk . Distinguishing A from B improves on the coarse prediction by only . Resolving groups with nearly equal means can offer little gain even when it adds information.
The residual has conditional mean zero, but need not be independent of . In this example its conditional variance still depends on the group: , or . Zero conditional mean does not make the remaining distribution identical across groups.
Other losses, other targets#
Under absolute loss, a conditional median minimizes the conditional risk. For a fixed , moving a prediction slightly to the right increases distances to observations below it and decreases distances to those above it. Away from atoms the derivative of that risk is
At an atom there are left and right derivatives. A minimum occurs precisely when at least half the conditional probability is on each side, counting the prediction in both closed half-lines. This is the median condition; see Siegrist, Mean Absolute Error.
In group A every value in is a median. Groups B and C have medians and . Choosing gives overall absolute risk , whereas the conditional means give . Conversely, median predictions need not minimize squared risk. Loss functions express different costs for mistakes.
If the target is one of the finite class labels , , and the loss is for a correct label and otherwise, the conditional risk of a label is
We minimize it by choosing a conditional mode. Here the modes are and the overall error probability is . A mean can even lie between available class labels, so the prediction rule must follow the loss and allowed outputs.
What changed#
A predictor can now be defined from the information available to it. Coarse and fine observations need not lead to the same target, and the risk decomposition quantifies the cost of using the wrong one. In the finite example, nearly all the improvement over a constant comes from distinguishing C from the other groups; separating A and B adds only a small further reduction.
Limitations#
The ordinary conditional mean needs integrability, and the finite squared-risk argument needs a finite second moment. For a heavy-tailed response with no mean, a conditional mean is not rescued by calling it a predictor. Values of a conditional expectation on null observations do not identify a unique pointwise function without additional regularity or a specified version.
All risks above use the stipulated population law. Estimating that law adds sampling error, and changing the deployment distribution can change the optimal rule. The within-group term is irreducible only relative to the chosen information and loss; an additional useful measurement can reduce it. It is not evidence that the physical outcome is intrinsically unpredictable.
What you should remember#
Conditioning averages over the response distribution left after observing information. The result is a function of that information and hence a random variable before observation. Averaging it recovers the original mean; nested information gives the tower property. Under squared loss, its risk is the expected conditional variance, and every other predictor adds a nonnegative squared discrepancy. Absolute loss selects medians; zero-one loss selects modes.
Code#
The finite table can be checked exactly with rational arithmetic:
from fractions import Fraction as F
values = [0, 2, 6]
weights = [F(1, 4), F(1, 2), F(1, 4)]
rows = [[F(1, 2), F(1, 4), F(1, 4)],
[F(1, 4), F(1, 2), F(1, 4)],
[F(0), F(1, 4), F(3, 4)]]
means = [sum(y * p for y, p in zip(values, row)) for row in rows]
variances = [sum(p * (y - m) ** 2 for y, p in zip(values, row))
for row, m in zip(rows, means)]
mean = sum(w * m for w, m in zip(weights, means))
within = sum(w * v for w, v in zip(weights, variances))
between = sum(w * (m - mean) ** 2 for w, m in zip(weights, means))
print("means:", " ".join(map(str, means)))
print(f"mean={mean} within={within} between={between} total={within + between}")
means: 2 5/2 5
mean=3 within=37/8 between=11/8 total=6
The repository script scripts/notes/conditioning-concentration-cases.py also checks the merged groups, each prediction loss and every plotted risk value. The example contains no sampled or external data.
Next#
Knowing an expected error does not yet give the chance of a large error. Inequalities and concentration turns assumptions about nonnegativity, variance, boundedness and independence into bounds on such probabilities.
References#
- Kyle Siegrist, Conditional Expected Value, Definitions, Consistency, The Best Predictor and Conditional Variance: conditional means, nested averaging, squared-risk projection and total variance.
- Kyle Siegrist, Conditional Expected Value Revisited, Definitions and Basic Properties: integrability, uniqueness up to null events and conditioning on information.
- Kyle Siegrist, Vector Spaces of Random Variables, Mean Absolute Error: medians as minimizers, including a nonunique median interval.