Unit 7, lesson 50

The Law of Total Variance

How the law of total variance writes a variance exactly as within-group variation, the expected conditional variance, plus between-group variation, the variance of the conditional means.

  • Video 50
  • Conditional Expectation and Total Variance

Parcels go out by standard or express routing, and some of them are pulled aside for an extra inspection. Whether a parcel is inspected varies for two separate reasons. Inspections are random inside each route, and the two routes inspect at different rates. This lesson defines the conditional variance and proves the law of total variance, which splits the variance of a quantity exactly into a part from the spread inside the groups and a part from the differences between the group means.

Key terms

Conditional variance

Definition Let XX be a discrete random variable and let YY be a random variable on the same sample space with a finite second moment, E[Y2]<∞E[Y^2] < \infty. For each value xx with pX(x)>0p_X(x) > 0, write m(x)=E[Y∣X=x]m(x) = E[Y \mid X = x] for the conditional mean. The conditional variance of YY given X=xX = x is the variance of YY under its conditional distribution given X=xX = x,

Var⁡(Y∣X=x)=E[(Y−m(x))2∣X=x]=E[Y2∣X=x]−m(x)2.\begin{aligned} &\operatorname{Var}(Y \mid X = x) \\[0.4em] &= E[(Y - m(x))^2 \mid X = x] \\[0.4em] &= E[Y^2 \mid X = x] - m(x)^2 . \end{aligned}

For a discrete YY this is ∑y(y−m(x))2 pY∣X(y∣x)\sum_y (y - m(x))^2 \, p_{Y \mid X}(y \mid x). It is finite, because E[Y2∣X=x]≤E[Y2]/pX(x)E[Y^2 \mid X = x] \le E[Y^2] / p_X(x). Writing v(x)=Var⁡(Y∣X=x)v(x) = \operatorname{Var}(Y \mid X = x), the random conditional variance Var⁡(Y∣X)\operatorname{Var}(Y \mid X) is the random variable v(X)v(X), in the same way that E[Y∣X]=m(X)E[Y \mid X] = m(X). No conditional distribution is defined at a value xx with pX(x)=0p_X(x) = 0, but XX takes such values with probability 00, so they never affect an expectation.

In plain words A conditional variance measures the spread of YY inside one group, around that group’s own mean rather than the overall mean. It carries the squared units of YY. A group with a larger conditional mean need not have a larger conditional variance.

Example In a constructed parcel model, X=0X = 0 for standard routing and X=1X = 1 for express routing, and YY is the indicator of an extra inspection. The joint PMF is in the table.

y=0y = 0y=1y = 1
x=0x = 00.30.30.20.2
x=1x = 10.10.10.40.4

Each row adds to 0.50.5. Dividing the first row by 0.50.5 gives the conditional probabilities 0.60.6 and 0.40.4 for y=0y = 0 and y=1y = 1, so m(0)=0.4m(0) = 0.4. An indicator with success probability pp has variance p(1−p)p(1 - p), so Var⁡(Y∣X=0)=0.4⋅0.6=0.24\operatorname{Var}(Y \mid X = 0) = 0.4 \cdot 0.6 = 0.24. The second row gives 0.20.2 and 0.80.8, so m(1)=0.8m(1) = 0.8 and Var⁡(Y∣X=1)=0.8⋅0.2=0.16\operatorname{Var}(Y \mid X = 1) = 0.8 \cdot 0.2 = 0.16. Express parcels are inspected more often, yet their indicator is closer to the constant 11 and therefore less spread out.

Within-group variation

Definition Under the same assumptions, the within-group variation is the expected conditional variance

E[Var⁡(Y∣X)]=∑xpX(x) v(x),\begin{aligned} &E[\operatorname{Var}(Y \mid X)] \\[0.4em] &= \sum_x p_X(x)\, v(x), \end{aligned}

where the sum runs over the values xx with pX(x)>0p_X(x) > 0. It is the weighted average of the conditional variances, with the group probabilities as weights, and it equals E[(Y−m(X))2]E\bigl[(Y - m(X))^2\bigr], the mean squared distance of YY from its own group mean. It is nonnegative, it carries the squared units of YY, and it equals 00 exactly when YY is constant within every group of positive probability, in other words when P(Y=m(X))=1P(Y = m(X)) = 1.

In plain words Average the spread inside the groups, counting each group as often as it occurs. Each conditional variance already averages squared deviations, so the average is not squared again. Since every function gg satisfies E[(Y−g(X))2]≥E[(Y−m(X))2]E\bigl[(Y - g(X))^2\bigr] \ge E\bigl[(Y - m(X))^2\bigr], as shown in Conditional Expectation and the Tower Rule, the within-group variation is the mean squared error that remains when YY is predicted from XX in the best possible way.

Example In the parcel model both routes have probability 0.50.5, so

E[Var⁡(Y∣X)]=0.5⋅0.24+0.5⋅0.16=0.12+0.08=0.2.\begin{aligned} &E[\operatorname{Var}(Y \mid X)] \\[0.4em] &= 0.5 \cdot 0.24 + 0.5 \cdot 0.16 \\[0.4em] &= 0.12 + 0.08 = 0.2 . \end{aligned}

Here the plain average of 0.240.24 and 0.160.16 gives the same number, because the two routes are equally likely. With unequal group probabilities the weights must be those probabilities.

Between-group variation

Definition Under the same assumptions, let μ=E[Y]\mu = E[Y]. The between-group variation is the variance of the conditional mean E[Y∣X]=m(X)E[Y \mid X] = m(X),

Var⁡(E[Y∣X])=∑xpX(x) (m(x)−μ)2,\begin{aligned} &\operatorname{Var}(E[Y \mid X]) \\[0.4em] &= \sum_x p_X(x)\,\bigl(m(x) - \mu\bigr)^2 , \end{aligned}

summed over the values xx with pX(x)>0p_X(x) > 0. The sum is centered at μ\mu because the tower rule gives E[m(X)]=E[Y]=μE[m(X)] = E[Y] = \mu. The between-group variation carries the squared units of YY, and it equals 00 exactly when every group of positive probability has the same conditional mean.

In plain words Replace every outcome by the mean of its group, and measure how much these group means vary around the overall mean. Even if YY were constant inside each group, different constants in different groups would still make YY vary. If XX and YY are independent, every conditional mean equals μ\mu and the between-group variation is 00. The converse is false, since groups with equal means can still have different distributions.

Example In the parcel model the conditional mean m(X)m(X) equals 0.40.4 or 0.80.8 with probability 0.50.5 each. Its mean is 0.60.6, which agrees with E[Y]=P(Y=1)=0.2+0.4E[Y] = P(Y = 1) = 0.2 + 0.4. Both group means lie 0.20.2 away from 0.60.6, so

Var⁡(E[Y∣X])=0.5 (0.4−0.6)2+0.5 (0.8−0.6)2=0.02+0.02=0.04.\begin{aligned} &\operatorname{Var}(E[Y \mid X]) \\[0.4em] &= 0.5\,(0.4 - 0.6)^2 \\[0.4em] &\quad + 0.5\,(0.8 - 0.6)^2 \\[0.4em] &= 0.02 + 0.02 = 0.04 . \end{aligned}

Law of total variance

Definition Let XX be a discrete random variable and let YY be a random variable on the same sample space with E[Y2]<∞E[Y^2] < \infty. Then

Var⁡(Y)=E[Var⁡(Y∣X)]+Var⁡(E[Y∣X]).\begin{aligned} &\operatorname{Var}(Y) \\[0.4em] &= E[\operatorname{Var}(Y \mid X)] \\[0.4em] &\quad + \operatorname{Var}(E[Y \mid X]) . \end{aligned}

In words, the total variance is the within-group variation plus the between-group variation, and all three are in the squared units of YY. Both terms on the right are finite and nonnegative, so each of them is at most Var⁡(Y)\operatorname{Var}(Y). The examples on this page use finite tables, and the proof in the next card works for any discrete XX.

In plain words The overall spread of YY has two sources, the spread of outcomes around their own group mean and the spread of the group means around the overall mean. The law says that these two parts add up to the total exactly. The two terms are easy to confuse. The first is an average of variances, and the second is a variance of averages. The lesson Random Sums with Independent Counts uses this law with the number of terms as the group label, to find the variance of a total with a random number of terms.

Example In the parcel model the two parts give 0.2+0.04=0.240.2 + 0.04 = 0.24. Directly, YY is an indicator with P(Y=1)=0.6P(Y = 1) = 0.6, so Var⁡(Y)=0.6⋅0.4=0.24\operatorname{Var}(Y) = 0.6 \cdot 0.4 = 0.24 as well.

For unequal groups, take a constructed device model in which the mode MM is A with probability 34\frac34 and B with probability 14\frac14, and the duration TT is 22 or 66 minutes. In mode A these durations have probabilities 34\frac34 and 14\frac14, and in mode B the probabilities are reversed.

ModeProbabilityE[T∣M]E[T \mid M]Var⁡(T∣M)\operatorname{Var}(T \mid M)
A34\frac343333
B14\frac145533

In mode A the mean is 2⋅34+6⋅14=32 \cdot \frac34 + 6 \cdot \frac14 = 3 minutes, and the squared deviations 11 and 99 average to 34⋅1+14⋅9=3\frac34 \cdot 1 + \frac14 \cdot 9 = 3 square minutes. In mode B the mean is 55 minutes, and the squared deviations 99 and 11 average to 14⋅9+34⋅1=3\frac14 \cdot 9 + \frac34 \cdot 1 = 3 square minutes. The within-group variation is 34⋅3+14⋅3=3\frac34 \cdot 3 + \frac14 \cdot 3 = 3 square minutes. The overall mean is 34⋅3+14⋅5=72\frac34 \cdot 3 + \frac14 \cdot 5 = \frac72 minutes, so

Var⁡(E[T∣M])=34(3−72)2+14(5−72)2=316+916=34\begin{aligned} &\operatorname{Var}(E[T \mid M]) \\[0.4em] &= \tfrac34\bigl(3 - \tfrac72\bigr)^2 + \tfrac14\bigl(5 - \tfrac72\bigr)^2 \\[0.4em] &= \tfrac{3}{16} + \tfrac{9}{16} = \tfrac34 \end{aligned}

square minutes, and Var⁡(T)=3+34=154\operatorname{Var}(T) = 3 + \frac34 = \frac{15}{4} square minutes. As an independent check, P(T=2)=916+116=58P(T = 2) = \frac{9}{16} + \frac{1}{16} = \frac58 and P(T=6)=38P(T = 6) = \frac38. Then E[T]=108+188=72E[T] = \frac{10}{8} + \frac{18}{8} = \frac72 and E[T2]=4⋅58+36⋅38=16E[T^2] = 4 \cdot \frac58 + 36 \cdot \frac38 = 16, so Var⁡(T)=16−494=154\operatorname{Var}(T) = 16 - \frac{49}{4} = \frac{15}{4}.

Vanishing cross term

Definition Under the assumptions of the law, write μ=E[Y]\mu = E[Y], let D=Y−m(X)D = Y - m(X) be the deviation of YY from its group mean, and let G=m(X)−μG = m(X) - \mu be the deviation of the group mean from the overall mean. Then Y−μ=D+GY - \mu = D + G exactly, and

Var⁡(Y)=E[(D+G)2]=E[D2]+E[G2]+2E[DG].\begin{aligned} \operatorname{Var}(Y) &= E\bigl[(D + G)^2\bigr] \\[0.4em] &= E[D^2] + E[G^2] \\[0.4em] &\quad + 2E[DG] . \end{aligned}

The first term is E[Var⁡(Y∣X)]E[\operatorname{Var}(Y \mid X)], and the second is Var⁡(m(X))\operatorname{Var}(m(X)) because E[m(X)]=μE[m(X)] = \mu. For the cross term, fix a value xx with pX(x)>0p_X(x) > 0. Given X=xX = x, the factor G=m(x)−μG = m(x) - \mu is a constant and D=Y−m(x)D = Y - m(x) has conditional mean 00, so E[DG∣X=x]=(m(x)−μ)⋅0=0E[DG \mid X = x] = (m(x) - \mu) \cdot 0 = 0. Averaging over the groups gives E[DG]=0E[DG] = 0. Every expectation here is finite, because m(x)2≤E[Y2∣X=x]m(x)^2 \le E[Y^2 \mid X = x] gives E[m(X)2]≤E[Y2]<∞E[m(X)^2] \le E[Y^2] < \infty, so the expectation of the sum may be split term by term.

In plain words Inside a fixed group, the deviations from the group mean average to zero, while the group’s offset from the overall mean is a fixed number, so their product also averages to zero. That is why the two parts add with no correction term. The split has to use the group mean. Spread inside a group measured around the overall mean already contains that group’s offset.

Example In the device model, mode A has group mean 33 and the overall mean is 72\frac72. Measured around 72\frac72, the durations 22 and 66 in mode A give

E[(T−72)2∣M=A]=34⋅94+14⋅254=134,\begin{aligned} &E\bigl[(T - \tfrac72)^2 \mid M = \text{A}\bigr] \\[0.4em] &= \tfrac34 \cdot \tfrac94 + \tfrac14 \cdot \tfrac{25}{4} = \tfrac{13}{4}, \end{aligned}

which is Var⁡(T∣M=A)+(3−72)2=3+14\operatorname{Var}(T \mid M = \text{A}) + (3 - \frac72)^2 = 3 + \frac14. In mode A the deviation DD is −1-1 or 33 with probabilities 34\frac34 and 14\frac14, so its conditional mean is −34+34=0-\frac34 + \frac34 = 0, and the cross term of this group is 00. Mode B gives 3+94=2143 + \frac94 = \frac{21}{4} in the same way, and 34⋅134+14⋅214=154\frac34 \cdot \frac{13}{4} + \frac14 \cdot \frac{21}{4} = \frac{15}{4} is already the whole variance. Adding the between-group variation on top of these spreads would count it twice.

Check yourself

  1. A group label is A or B with probability 12\frac12 each. In group A the value YY is always 22, and in group B it is always 66. Find the within-group variation, the between-group variation and Var⁡(Y)\operatorname{Var}(Y).
Show the answer

Each group contains a single value, so both conditional variances are 00 and the within-group variation is 00. The conditional means 22 and 66 have overall mean 44, and both lie 22 away from it, so the between-group variation is 12⋅4+12⋅4=4\frac12 \cdot 4 + \frac12 \cdot 4 = 4. The law gives Var⁡(Y)=0+4=4\operatorname{Var}(Y) = 0 + 4 = 4. Directly, YY is 22 or 66 with probability 12\frac12 each, so E[Y2]=4+362=20E[Y^2] = \frac{4 + 36}{2} = 20 and Var⁡(Y)=20−16=4\operatorname{Var}(Y) = 20 - 16 = 4.

  1. Change the model so that groups A and B, still with probability 12\frac12 each, both have conditional mean 44 and conditional variance 33. Find Var⁡(Y)\operatorname{Var}(Y). Does a between-group variation of 00 mean that the group label XX and YY are independent?
Show the answer

The conditional mean equals 44 in both groups, so m(X)m(X) is the constant 44 and the between-group variation is 00. The within-group variation is 12⋅3+12⋅3=3\frac12 \cdot 3 + \frac12 \cdot 3 = 3, so Var⁡(Y)=3+0=3\operatorname{Var}(Y) = 3 + 0 = 3. No further details of the two distributions are needed.

It does not. For example, let YY be 11, 44 or 77 with probabilities 16\frac16, 23\frac23 and 16\frac16 in group A, and 22, 44 or 66 with probabilities 38\frac38, 14\frac14 and 38\frac38 in group B. Both groups have mean 44 and variance 33, since 9⋅16+9⋅16=39 \cdot \frac16 + 9 \cdot \frac16 = 3 and 4⋅38+4⋅38=34 \cdot \frac38 + 4 \cdot \frac38 = 3. Yet P(Y=1∣X=A)=16P(Y = 1 \mid X = \text{A}) = \frac16 while P(Y=1∣X=B)=0P(Y = 1 \mid X = \text{B}) = 0, so the conditional distribution of YY depends on the group and the variables are dependent. A between-group variation of 00 says only that the group means agree.

  1. Group A has probability 0.20.2, conditional mean 1010 and conditional variance 44. Group B has probability 0.80.8, conditional mean 55 and conditional variance 11. Someone reports Var⁡(Y)=4+12=2.5\operatorname{Var}(Y) = \frac{4 + 1}{2} = 2.5. Find the correct variance and name the two mistakes.
Show the answer

The within-group variation is 0.2⋅4+0.8⋅1=0.8+0.8=1.60.2 \cdot 4 + 0.8 \cdot 1 = 0.8 + 0.8 = 1.6. The overall mean is 0.2⋅10+0.8⋅5=60.2 \cdot 10 + 0.8 \cdot 5 = 6, so the between-group variation is

0.2 (10−6)2+0.8 (5−6)2=3.2+0.8=4.\begin{aligned} &0.2\,(10 - 6)^2 + 0.8\,(5 - 6)^2 \\[0.4em] &= 3.2 + 0.8 = 4 . \end{aligned}

The law gives Var⁡(Y)=1.6+4=5.6\operatorname{Var}(Y) = 1.6 + 4 = 5.6. The report weights the two groups equally although B is four times as likely as A, and it leaves out the between-group variation entirely. Even with the correct weights, the average 1.61.6 of the conditional variances is only the within-group part.

How this lesson connects

Where this leads in Statistics