A two-stage experiment first draws X, then draws Y from a distribution that depends on the value of X. The conditional mean E(Y | X = x) is a function of x (the line in the left panel); evaluated at the random X it becomes a random variable, E(Y | X), whose average is E(Y) (Theorem 3.24). The spread of Y has two sources: the spread around the line at a fixed x, averaged over x, and the movement of the line itself as x varies. Theorem 3.27 says the two simply add: V(Y) = E V(Y | X) + V E(Y | X).
The simulated terms use the exact conditional mean and variance at each simulated first stage: E V is the average of V(Y | X = xr) and V E the variance of E(Y | X = xr) over the R repetitions. Their sum is compared with the plain variance of the simulated Ys.
code/ch03_expectation.py (two_stage_study, two_stage_theory).