Conditional Expectation and Total Variance

Average within each known group, then average across groups; split total variation into within-group and between-group parts.

Draft — not yet reviewed by the site owner.

Prerequisites: Conditional Distributions, Expected Value, Variance of a Random Variable.

A conditional expectation is the average value of a random quantity after some information is known. If we know which group a person belongs to, we can use that group’s average instead of the overall average. Averaging those group averages brings us back to the overall average.

Two stages of averaging

Let XX be the information we learn and YY the quantity we want to predict. For each possible xx, the number E[Y∣X=x]E[Y\mid X=x] is the mean of the conditional distribution of YY given X=xX=x. As xx changes, this number changes, so E[Y∣X]E[Y\mid X] is itself a random variable: a function of XX.

The law of total expectation, also called the tower property, says

E[Y]=E ⁣[E[Y∣X]],E[Y]=E\!\left[E[Y\mid X]\right],

provided E[∣Y∣]E[|Y|] is finite. First find the mean within each XX group. Then average those means, weighting each group by how likely it is. This is the same operation as summing the cells of a probability table in two stages.

A small example

Suppose a fair coin chooses a group. In group A, YY is equally likely to be 0 or 2. In group B, it is equally likely to be 2 or 4. Here XX is the chosen group.

Group XX Possible YY values E[Y∣X]E[Y\mid X] Var⁡(Y∣X)\operatorname{Var}(Y\mid X)
A 0 or 2, equally likely 1 1
B 2 or 4, equally likely 3 1

Each group has probability 1/21/2, so the overall mean is (1+3)/2=2(1+3)/2=2. We could instead list all four equally likely outcomes, 0,2,2,40,2,2,4, and get the same answer. The two-stage calculation becomes more useful when there are many groups or a continuous random parameter.

Group A has equally likely values zero and two with mean one; group B has equally likely values two and four with mean three. The overall mean is two. Each group varies around its own mean, and the group means vary around the overall mean.
Model example, not data. A fair coin chooses group A or B; within a group the two shown values are equally likely. Each group has variance 1; the group means 1 and 3 have variance 1 around the overall mean 2. Total variance is 1 + 1 = 2.

Where the variance goes

The law of total variance separates two reasons YY can vary:

Var⁡(Y)=E ⁣[Var⁡(Y∣X)]+Var⁡ ⁣(E[Y∣X]),\operatorname{Var}(Y) =E\!\left[\operatorname{Var}(Y\mid X)\right] +\operatorname{Var}\!\left(E[Y\mid X]\right),

provided E[Y2]E[Y^2] is finite. The first term is the average variation within the groups. The second is variation between the group means. In the example, the within-group variance is 1 in either group, so its average is 1. The group means are 1 and 3, equally likely, and their variance is also 1. The total is 2.

To see why the identity works, write m(X)=E[Y∣X]m(X)=E[Y\mid X] and μ=E[Y]\mu=E[Y]. Then

Y−μ=(Y−m(X))+(m(X)−μ).Y-\mu=(Y-m(X))+(m(X)-\mu).

Square this expression and take expectations. The cross-term vanishes because E[Y−m(X)∣X]=0E[Y-m(X)\mid X]=0. The two squared terms become the within-group and between-group variances above.

When this helps

If a model first draws a random parameter and then draws an outcome given that parameter, the conditional mean and variance are often easy to find even when the outcome’s full marginal distribution is hard. This is common in mixture models, prediction, and Bayesian calculations.

Do not confuse E[Y∣X=x]E[Y\mid X=x], a number for a fixed xx, with E[Y∣X]E[Y\mid X], a random variable before XX is observed. Nor should the two variance terms be swapped: one averages the conditional variances; the other measures the spread of conditional means.

Further reading