Course contents 11 / 22

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 00, 22 or 66, 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 XX denote the information we observe and YY the response. Start with this stipulated finite law; each row after the group probability is a conditional distribution of YY:

Group xxPr⁡(X=x)\Pr(X=x)Y=0Y=0Y=2Y=2Y=6Y=6
A1/41/41/21/21/41/41/41/4
B1/21/21/41/41/21/21/41/4
C1/41/4001/41/43/43/4

The rows are assumed probabilities, not estimates from a dataset. Multiplying a row by its group probability gives joint probabilities. For example, the event X=B,Y=6X=\mathrm B,Y=6 has probability 1/81/8.

The conditional mean function, also called the regression function, averages within an observed group:

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

For discrete YY and a group with positive probability,

m(x)=∑yyPr⁡(Y=y∣X=x).m(x)=\sum_y y\Pr(Y=y\mid X=x).

For the three rows, direct weighted sums give

m(A)=0⋅12+2⋅14+6⋅14,=2,m(B)=0⋅14+2⋅12+6⋅14,=52,m(C)=0⋅0+2⋅14+6⋅34,=5.\begin{aligned} m(\mathrm A)&=0\cdot\frac12+2\cdot\frac14+6\cdot\frac14,\\ &=2,\\ m(\mathrm B)&=0\cdot\frac14+2\cdot\frac12+6\cdot\frac14,\\ &=\frac52,\\ m(\mathrm C)&=0\cdot0+2\cdot\frac14+6\cdot\frac34,\\ &=5. \end{aligned}

The value m(x)m(x) is a number after we observe xx. Before that observation, m(X)m(X) is a random variable: it takes 22, 5/25/2 or 55 according to the group probabilities. This distinction explains the notation E[Y∣X]=m(X)\mathbb E[Y\mid X]=m(X).

If the conditional law instead has a density, the definition is an integral:

m(x)=∫RyfY∣X(y∣x) dy.m(x)=\int_{\mathbb R} y f_{Y\mid X}(y\mid x)\,dy.

We assume E[∣Y∣]<∞\mathbb E[|Y|]<\infty 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:

E[m(X)]=∑x∑yyPr⁡(Y=y∣X=x)Pr⁡(X=x),=∑x∑yyPr⁡(X=x,Y=y),=E[Y].\begin{aligned} \mathbb E[m(X)] &=\sum_x\sum_y y\Pr(Y=y\mid X=x)\Pr(X=x),\\ &=\sum_x\sum_y y\Pr(X=x,Y=y),\\ &=\mathbb E[Y]. \end{aligned}

Integrability justifies exchanging the sums. In our table, the weighted average is

E[Y]=14⋅2+12⋅52+14⋅5,=3.\begin{aligned} \mathbb E[Y]&=\frac14\cdot2+\frac12\cdot\frac52+\frac14\cdot5,\\ &=3. \end{aligned}

This is the simplest tower property: E[E[Y∣X]]=E[Y]\mathbb E[\mathbb E[Y\mid X]]=\mathbb E[Y]. It does not say that all conditional means equal 33; their weighted average does.

The nested form compares two amounts of information. Let ZZ reveal only whether XX is in the merged group AB or in C. Then ZZ is a function of XX, and

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

Inside AB, the conditional group weights are 1/31/3 for A and 2/32/3 for B. The coarse mean is therefore

E[Y∣Z=AB]=13⋅2+23⋅52,=73.\begin{aligned} \mathbb E[Y\mid Z=\mathrm{AB}] &=\frac13\cdot2+\frac23\cdot\frac52,\\ &=\frac73. \end{aligned}

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 E[Y2]<∞\mathbb E[Y^2]<\infty and consider predictors a(X)a(X) with finite second moment. At a fixed observation xx, write the conditional variance as v(x)v(x). Expanding around the conditional mean gives

E[(Y−a(x))2∣X=x]=v(x)+(m(x)−a(x))2.\begin{aligned} \mathbb E[(Y-a(x))^2\mid X=x] &=v(x)+(m(x)-a(x))^2. \end{aligned}

The cross term vanishes because the conditional mean of Y−m(x)Y-m(x) is zero. The remaining second term is nonnegative and reaches zero exactly at a(x)=m(x)a(x)=m(x).

For our groups, direct sums of squared deviations give

GroupMean m(x)m(x)Variance v(x)v(x)
A2266
B5/25/219/419/4
C5533

In group B, for example, the conditional squared risk at prediction aa is

rB(a)=194+(a−52)2.r_{\mathrm B}(a)=\frac{19}{4}+\left(a-\frac52\right)^2.

The minimum is 19/419/4, reached at a=5/2a=5/2. Predicting the most probable response 22 instead raises that squared risk to 55. A likely individual outcome need not minimize average squared error.

Figure

Squared risk within each group

Three conditional-risk parabolasThe solid blue A curve has minimum (2, 6), dashed red B has (2.5, 4.75), and dotted green C has (5, 3). Each risk rises by the squared distance of the prediction from its conditional mean.081624320123456Squared riska
  • 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.

Conditional risks for the stipulated groups A, B and C. Horizontal position is prediction a; height is mean squared error within the group. Minima occur at conditional means 2, 2.5 and 5; each minimum height equals the conditional variance.

Averaging over the observations gives the full risk decomposition:

E[(Y−a(X))2]=E[v(X)]+E[(m(X)−a(X))2].\begin{aligned} \mathbb E[(Y-a(X))^2] &=\mathbb E[v(X)]\\ &\quad+\mathbb E[(m(X)-a(X))^2]. \end{aligned}

The first term is the error left when the joint law is known and only XX is observed. The second is the penalty for choosing a different predictor. Equality with the minimum requires a(X)=m(X)a(X)=m(X) 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 (1,3,4)(1,3,4) for groups A, B and C. Their extra squared error is

E[(m(X)−a(X))2]=14⋅1+12⋅14+14⋅1,=58.\begin{aligned} \mathbb E[(m(X)-a(X))^2] &=\frac14\cdot1+\frac12\cdot\frac14+\frac14\cdot1,\\ &=\frac58. \end{aligned}

The minimum risk is 37/837/8, so the alternative risk is 21/421/4. 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 a(X)=E[Y]a(X)=\mathbb E[Y]. Its squared error is Var⁡(Y)\operatorname{Var}(Y), and the tower property gives E[m(X)]=E[Y]\mathbb E[m(X)]=\mathbb E[Y]. Therefore

Var⁡(Y)=E[v(X)]+Var⁡(m(X)).\operatorname{Var}(Y) =\mathbb E[v(X)]+\operatorname{Var}(m(X)).

This is the law of total variance: within-group variation plus variation between group means. For our finite law,

E[v(X)]=14⋅6+12⋅194+14⋅3,=378,Var⁡(m(X))=14(2−3)2+12(52−3)2+14(5−3)2,=118.\begin{aligned} \mathbb E[v(X)] &=\frac14\cdot6+\frac12\cdot\frac{19}{4}+\frac14\cdot3,\\ &=\frac{37}{8},\\ \operatorname{Var}(m(X)) &=\frac14(2-3)^2+\frac12\left(\frac52-3\right)^2\\ &\quad+\frac14(5-3)^2,\\ &=\frac{11}{8}. \end{aligned}

Their sum is 66. As a separate check, the marginal law of YY has probabilities 1/41/4, 3/83/8, 3/83/8 at 00, 22, 66. It gives E[Y2]=15\mathbb E[Y^2]=15, so subtracting the squared mean gives the same variance 66.

Figure

Variation left after an observation

Variance split into two partsThe blue bar segment has length 4.625 variance units and the amber segment has length 1.375. Together they give 6. After observing the group, minimum squared risk equals the blue part.Response variance0246Within groups: 37/8Between means: 11/8
InformationMin. risk
Constant only6
AB or C14/3
A, B or C37/8

The remaining error depends on the available features. An additional measurement can reduce it.

Total variance 6 is the sum of average within-group variance 37/8 and variance of group means 11/8. The table gives minimum squared risks for three amounts of information. Distinguishing A from B reduces risk by only 1/24.

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 ZZ, the AB group has mean 7/37/3 and variance 47/947/9, while C stays unchanged. Its optimal risk is

E[Var⁡(Y∣Z)]=34⋅479+14⋅3,=143.\begin{aligned} \mathbb E[\operatorname{Var}(Y\mid Z)] &=\frac34\cdot\frac{47}{9}+\frac14\cdot3,\\ &=\frac{14}{3}. \end{aligned}

The best constant prediction has risk 66, the coarse prediction has risk 14/314/3, and the fine prediction has risk 37/837/8. Distinguishing A from B improves on the coarse prediction by only 1/241/24. Resolving groups with nearly equal means can offer little gain even when it adds information.

The residual Y−m(X)Y-m(X) has conditional mean zero, but need not be independent of XX. In this example its conditional variance still depends on the group: 66, 19/419/4 or 33. 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 xx, 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

ddaE[∣Y−a∣∣X=x]=2FY∣X(a∣x)−1.\frac{d}{da}\mathbb E[|Y-a|\mid X=x] =2F_{Y\mid X}(a\mid x)-1.

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 [0,2][0,2] is a median. Groups B and C have medians 22 and 66. Choosing (0,2,6)(0,2,6) gives overall absolute risk 3/23/2, whereas the conditional means give 7/47/4. 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 00, 22, 66 and the loss is 00 for a correct label and 11 otherwise, the conditional risk of a label aa is

Pr⁡(Y≠a∣X=x)=1−Pr⁡(Y=a∣X=x).\Pr(Y\ne a\mid X=x)=1-\Pr(Y=a\mid X=x).

We minimize it by choosing a conditional mode. Here the modes are (0,2,6)(0,2,6) and the overall error probability is 7/167/16. 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.