Foundations to Frontier
Tests
A. Mathematical Foundations › A4 Probability Theory › Basics

Law of Total Probability and Total Expectation

Hard questions become easy once you split them into cases. Condition on whatever makes the problem simple, solve each case, and average with the case probabilities. This covers total probability, total expectation E[X] = E[E[X|Y]], total variance (within + between), first-step analysis for expected waiting and hitting times, and Wald's identity for random sums. It's the same recursion as Bellman's equation in reinforcement learning.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Split into cases

Will you be late today? It depends on the weather. If it rains (30% chance), you’re late 80% of the time. If not, only 10%. Work out each case and weigh it by how likely it is: 0.3×0.8+0.7×0.1=0.310.3\times0.8 + 0.7\times0.1 = 0.31. That’s the law of total probability.

Averages work the same way. Two classes of 20 and 30 students average 70 and 80 marks. The overall average is 2050⋅70+3050⋅80=76\frac{20}{50}\cdot70 + \frac{30}{50}\cdot80 = 76. That’s the law of total expectation: the overall average is the average of the case averages.

Split into cases, then add them up: total probability and total expectation
0.50Adefect 0.010ok 0.9900.30Bdefect 0.020ok 0.9800.20Cdefect 0.050ok 0.950
P(defect) = Σ P(defect | source) P(source) = 0.0050 + 0.0060 + 0.0100 = 0.0210
given a defect, which source? A: 0.238 B: 0.286 C: 0.476
Total expectation and variance (a part's resistance)
E[X] = Σ E[X | source] P(source) = 103.20
Var X = within 9.70 + between 14.56 = 24.26 ← mixing the sources dominates
Branch thickness ∝ probability. Multiply along a path, add the paths that end in "defect". Variance has two parts: the average spread inside each source, plus how much the sources' means differ.
Why engineers care

A factory has three machines with different defect rates. A rover’s battery drain depends on which terrain it crosses. A robot’s time to reach its charger depends on where it starts. Each answer is “condition on the case, then average”, and it’s the same idea behind how reinforcement-learning agents compute the value of a state.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Rain 30% (late 80%) vs dry 70% (late 10%). P(late) = ?
  2. 2. 20 students average 70 and 30 average 80. Overall average?
  3. 3. The cases must…

Level 1: Beginner

★★☆☆☆ Basic⏱ 15 minSecondary-school mathematics: arithmetic and elementary algebra.

The laws

For a partition B1,…,BkB_1,\dots,B_k (or any random variable YY you condition on):

  • Total probability: P(A)=∑iP(A∣Bi)P(Bi)P(A) = \sum_iP(A\mid B_i)P(B_i).
  • Total expectation: E[X]=∑iE[X∣Bi]P(Bi)\mathbb E[X] = \sum_i\mathbb E[X\mid B_i]P(B_i), or compactly E[X]=E[E[X∣Y]]\mathbb E[X] = \mathbb E[\mathbb E[X\mid Y]].

Here E[X∣Y]\mathbb E[X\mid Y] is itself a random variable, a function of YY giving the average of XX in each case.

A factory: machines A, B and C make 50%, 30% and 20% of parts, with defect rates 1%, 2% and 5%. Then P(defect)=0.005+0.006+0.010=0.021P(\text{defect}) = 0.005 + 0.006 + 0.010 = 0.021. Bayes then gives P(C∣defect)=0.010/0.021≈0.48P(C\mid\text{defect}) = 0.010/0.021\approx0.48.

Roll then flip: roll a die to get NN, then flip NN coins. E[heads∣N]=N/2\mathbb E[\text{heads}\mid N] = N/2, so E[heads]=E[N]/2=1.75\mathbb E[\text{heads}] = \mathbb E[N]/2 = 1.75, with no need for the full distribution.

📝 Quick check (Level 1)
0/3 answered
  1. 1. Factory defect rate in the example?
  2. 2. E[heads] in roll-then-flip?
  3. 3. E[X | Y] is…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.

Total variance: within + between

Var⁡X=E[Var⁡(X∣Y)]⏟average spread within cases+Var⁡(E[X∣Y])⏟spread of the case means\operatorname{Var}X = \underbrace{\mathbb E[\operatorname{Var}(X\mid Y)]}_{\text{average spread within cases}} + \underbrace{\operatorname{Var}(\mathbb E[X\mid Y])}_{\text{spread of the case means}}

Here’s an example. Resistors come from two machines with means 100 Ω and 110 Ω and small spreads. Most of the batch’s variance comes from mixing the machines, not from either machine’s own noise (Practice Code 3). This is also the decomposition behind ANOVA, and behind the “aleatoric vs epistemic” uncertainty split in Bayesian deep learning.

First-step analysis

To find an expected waiting time, condition on the first step.

  • Rolls until a 6: E=1+56EE = 1 + \tfrac56E, so E=6E = 6.
  • Markov chains: expected hitting times satisfy hi=1+∑jPijhjh_i = 1 + \sum_jP_{ij}h_j for non-target states, a linear system (Exercise 2).
  • Hitting probabilities: the same trick, as in gambler’s ruin.

Random sums (Wald’s identity)

Let S=X1+⋯+XNS = X_1 + \dots + X_N, with NN independent of the i.i.d. XiX_i. Conditioning on NN gives

  • ES=EN EX\mathbb E S = \mathbb E N\,\mathbb E X;
  • Var⁡S=ENVar⁡X+Var⁡N(EX)2\operatorname{Var}S = \mathbb E N\operatorname{Var}X + \operatorname{Var}N(\mathbb E X)^2.

Examples: packets per second × packet size, insurance claims × claim size, number of steps × cost per step.

🐍 Exercise 1: roll a die, flip that many coins
Compute the exact distribution of the number of heads by total probability over N. Check that its mean equals E[E[X | N]] and its variance equals E[Var(X | N)] + Var(E[X | N]).
📝 Quick check (Level 2)
0/3 answered
  1. 1. Expected rolls until the first 6?
  2. 2. Var X = E[Var(X|Y)] + …
  3. 3. Wald: E[X₁ + … + X_N] = …

Level 3: Advanced

★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.

Proofs

  • Total expectation (discrete). E[X]=∑x∑yx p(x,y)=∑ypY(y)∑xx p(x∣y)=∑yE[X∣Y=y]pY(y)\mathbb E[X] = \sum_x\sum_yx\,p(x,y) = \sum_yp_Y(y)\sum_xx\,p(x\mid y) = \sum_y\mathbb E[X\mid Y = y]p_Y(y). ∎
  • Total variance. E[X2]=E[E[X2∣Y]]=E[Var⁡(X∣Y)+E[X∣Y]2]\mathbb E[X^2] = \mathbb E[\mathbb E[X^2\mid Y]] = \mathbb E[\operatorname{Var}(X\mid Y) + \mathbb E[X\mid Y]^2]. Subtract (EX)2=(E[E[X∣Y]])2(\mathbb E X)^2 = (\mathbb E[\mathbb E[X\mid Y]])^2, and the second part becomes Var⁡(E[X∣Y])\operatorname{Var}(\mathbb E[X\mid Y]). ∎
  • Wald. E[S∣N]=NEX\mathbb E[S\mid N] = N\mathbb E X and Var⁡(S∣N)=NVar⁡X\operatorname{Var}(S\mid N) = N\operatorname{Var}X. Then apply total expectation and total variance. ∎

Conditional expectation as projection

E[X∣Y]\mathbb E[X\mid Y] is the function of YY that minimizes E[(X−g(Y))2]\mathbb E[(X - g(Y))^2]. In the Hilbert space L2L^2 it’s the orthogonal projection of XX onto the functions of YY (A3.5-5). The tower property is “projecting onto a smaller space after a bigger one”. This is why least-squares regression, and any network trained with MSE, estimates E[Y∣X]\mathbb E[Y\mid X].

Gambler’s ruin by first-step analysis

A fair walk on {0,…,N}\{0,\dots,N\} starting at kk. Let pkp_k be P(hit NN before 0). Then pk=12pk+1+12pk−1p_k = \tfrac12p_{k+1} + \tfrac12p_{k-1} with p0=0p_0 = 0 and pN=1p_N = 1, so pk=k/Np_k = k/N. The expected duration is k(N−k)k(N - k).

Exercise 3.1: Expected number of tosses until two heads in a row (HH)

Let E0E_0 be the expectation from the start and E1E_1 just after an H. Then E0=1+12E1+12E0E_0 = 1 + \tfrac12E_1 + \tfrac12E_0 and E1=1+12⋅0+12E0E_1 = 1 + \tfrac12\cdot0 + \tfrac12E_0. Solving gives E0=6E_0 = 6.

📝 Quick check (Level 3)
0/3 answered
  1. 1. E[X | Y] minimizes…
  2. 2. Fair gambler's ruin from 3 with target 10: P(win) = ?
  3. 3. Expected tosses until HH?

Level 4: Expert

★★★★★ Expert⏱ 45 minResearchers: original papers and open problems.

Expected hitting times and Bellman’s equation

A robot moves along a corridor toward its charger, with random slips. Its expected time to arrive from each cell satisfies h=1+Pnontargeth\mathbf h = \mathbf 1 + P_{\text{nontarget}}\mathbf h, one application of total expectation per state. That’s a linear system, solved exactly. Replace “1 step” with “reward r(s)r(s)” and add a discount, and you get Bellman’s equation for a policy’s value, V=r+γPVV = r + \gamma PV (Bellman, 1957; Sutton and Barto, 2018). Policy evaluation, TD learning and the Neumann series of A3.5-6 are all this law of total expectation, iterated.

🐍 Exercise 2 (research): expected time to reach the charger
Model a robot heading for a charger with forward/stay/slip-back probabilities as a Markov chain. Solve the first-step equations h = 1 + Ph for expected hitting times, and verify by simulation.

Conditioning as a design principle

  • Mixture and scenario models. Mission-risk analyses condition on scenarios (nominal, sensor failure, high winds) and combine them by total probability (NASA PRA event trees; Stamatelatos et al., 2011).
  • Aleatoric vs epistemic uncertainty. For a Bayesian model, Var⁡(y)=Eθ[Var⁡(y∣θ)]+Var⁡θ(E[y∣θ])\operatorname{Var}(y) = \mathbb E_\theta[\operatorname{Var}(y\mid\theta)] + \operatorname{Var}_\theta(\mathbb E[y\mid\theta]), which is noise plus model uncertainty. Ensembles and MC dropout estimate the second term to flag out-of-distribution inputs (Kendall and Gal, 2017).
  • Rao–Blackwellization. Replacing an estimator by its conditional expectation never increases variance, by the total-variance law. It’s used in Rao–Blackwellized particle filters such as FastSLAM, where the map is computed analytically given the trajectory (Doucet et al., 2000; Montemerlo et al., 2002).
  • Queueing and networking. Wald’s identity and conditioning give mean delays and buffer sizes for robot fleets’ communication links and onboard telemetry.

References

  1. D. Bertsekas, J. Tsitsiklis (2008). Introduction to Probability, 2nd ed., Ch. 4, 7. Athena Scientific.
  2. S. Ross (2014). Introduction to Probability Models, 11th ed. Academic Press. (First-step analysis.)
  3. R. Bellman (1957). Dynamic Programming. Princeton.
  4. R. Sutton, A. Barto (2018). Reinforcement Learning: An Introduction, 2nd ed. MIT Press.
  5. A. Wald (1944). “On cumulative sums of random variables.” Annals of Mathematical Statistics, 15(3).
  6. A. Kendall, Y. Gal (2017). “What uncertainties do we need in Bayesian deep learning for computer vision?” NeurIPS.
  7. A. Doucet, N. de Freitas, K. Murphy, S. Russell (2000). “Rao-Blackwellised particle filtering for dynamic Bayesian networks.” UAI.
  8. M. Montemerlo, S. Thrun, D. Koller, B. Wegbreit (2002). “FastSLAM: a factored solution to the simultaneous localization and mapping problem.” AAAI.
  9. M. Stamatelatos et al. (2011). NASA PRA Procedures Guide. NASA/SP-2011-3421.