iterated-expectation-and-continuous-random-variables.html

Lecture 5: Iterated Expectation, Conditional Variance, and Continuous Random Variables

EECS 126 (UC Berkeley) Β· Spring 2026 Β· February 3, 2026

Reading: Bertsekas & Tsitsiklis, Β§4.2–4.3; Ch. 3.

These are my personal notes from Professor Kannan Ramchandran’s EECS 126 (Probability and Random Processes) course at UC Berkeley.

We open with a famous game whose expected payout is infinite. We then finish the story of conditional expectation from Lecture 4: the law of iterated expectation (the tower rule), which we use to analyze a random walk, and its companion for variances, the law of total variance. The second half of the lecture moves from discrete to continuous random variables, introducing probability density functions (PDFs) and cumulative distribution functions (CDFs).

The St. Petersburg Paradox

Example 5.1 (St. Petersburg game)

I toss a fair coin until I get a heads. If I toss nn times, I give you $2n\text{\textdollar}2^n. For example,

Hβ€…β€Šβ†¦β€…β€Š$2,THβ€…β€Šβ†¦β€…β€Š$4,TTHβ€…β€Šβ†¦β€…β€Š$8,…H \;\mapsto\; \$2, \qquad TH \;\mapsto\; \$4, \qquad TTH \;\mapsto\; \$8, \qquad \ldots

Question: how much would you pay to play this game?

Let NN be the number of tosses in the game and W=2NW = 2^N the amount of money you win. The game ends on the first heads, so N∼Geometric⁑(12)N \sim \Geom(\tfrac12):

P(N=n)=12β‹…12β‹―12⏟nΒ flips=2βˆ’n,n=1,2,3,…\PP(N = n) = \underbrace{\tfrac12 \cdot \tfrac12 \cdots \tfrac12}_{n \text{ flips}} = 2^{-n}, \qquad n = 1, 2, 3, \ldots

(The first nβˆ’1n - 1 flips are tails and the nn-th is heads; each has probability 12\tfrac12.) By LOTUS,

E[W]=βˆ‘n=1∞2nβ‹…P(N=n)=βˆ‘n=1∞2nβ‹…12n=βˆ‘n=1∞1=∞.\EE[W] = \sum_{n=1}^{\infty} 2^n \cdot \PP(N = n) = \sum_{n=1}^{\infty} 2^n \cdot \frac{1}{2^n} = \sum_{n=1}^{\infty} 1 = \infty.

The expected payout is infinite. This is the St. Petersburg paradox.

Why is it a paradox? If expected value were the whole story, you should be willing to pay any finite price to play. Yet almost nobody would pay more than a few dollars: half the time the game pays $2\text{\textdollar}2, and P(W≀$8)=78\PP(W \le \text{\textdollar}8) = \tfrac78. The infinite mean is driven entirely by astronomically rare, astronomically large payouts (each outcome nn contributes exactly $1\text{\textdollar}1 to the mean). Two standard resolutions:

  1. Diminishing utility. Daniel Bernoulli argued that the value of money grows more slowly than the amount, e.g. logarithmically. The expected log-payout is finite: E[log⁑2W]=E[N]=2\EE[\log_2 W] = \EE[N] = 2 (the geometric mean from Lecture 4), which corresponds to a “fair price” of about 22=$42^2 = \text{\textdollar}4.
  2. Finite bankroll. No casino can pay more than some $2M\text{\textdollar}2^M. If payouts are capped at 2M2^M, then E[W]=βˆ‘n=1M2n2βˆ’n+βˆ‘n=M+1∞2M2βˆ’n=M+2Mβ‹…2βˆ’M=M+1.\EE[W] = \sum_{n=1}^{M} 2^n 2^{-n} + \sum_{n=M+1}^{\infty} 2^M 2^{-n} = M + 2^M \cdot 2^{-M} = M + 1. Even a billion-dollar bankroll (230β‰ˆ1.07Γ—1092^{30} \approx 1.07\times 10^9) gives an expected payout of only $31\text{\textdollar}31.

Conditional Expectation as a Random Variable

Recall from Lecture 4 that for each value yy with P(Y=y)>0\PP(Y = y) > 0,

E[X∣Y=y]=βˆ‘xxβ‹…P(X=x∣Y=y)\EE[X \mid Y = y] = \sum_x x \cdot \PP(X = x \mid Y = y)

is a number. Letting g(y)=E[X∣Y=y]g(y) = \EE[X \mid Y = y], the conditional expectation E[X∣Y]β‰œg(Y)\EE[X \mid Y] \triangleq g(Y) is a function of YY, and therefore E[X∣Y]\EE[X \mid Y] is a random variable.

Example 5.2 (Tossing a fair coin a random number of times)

We toss a fair coin NN times, where NN is itself a random variable taking values in {1,2,3,…}\{1, 2, 3, \ldots\}. Let XX be the total number of heads. Given N=nN = n, the nn tosses are independent fair Bernoulli trials, so X∣N=n∼Binomial⁑(n,12)X \mid N = n \sim \Bin(n, \tfrac12).

N=1N = 1: one toss gives 00 or 11 heads, each with probability 12\tfrac12:

E[X∣N=1]=0β‹…12+1β‹…12=12(aΒ number).\EE[X \mid N = 1] = 0 \cdot \tfrac12 + 1 \cdot \tfrac12 = \tfrac12 \qquad \text{(a number).}

N=2N = 2: two tosses give 00, 11, or 22 heads, with probabilities 14\tfrac14 (TTTT), 12\tfrac12 (HTHT or THTH), and 14\tfrac14 (HHHH):

E[X∣N=2]=0β‹…14+1β‹…12+2β‹…14=1(aΒ number).\EE[X \mid N = 2] = 0 \cdot \tfrac14 + 1 \cdot \tfrac12 + 2 \cdot \tfrac14 = 1 \qquad \text{(a number).}

General N=nN = n: by the binomial mean from Lecture 4,

E[X∣N=n]=nβ‹…12=n2(aΒ number).\EE[X \mid N = n] = n \cdot \tfrac12 = \frac{n}{2} \qquad \text{(a number).}

Sweeping over all values nn and replacing nn by the random variable NN,

E[X∣N]⏟r.v.=N2⏟r.v..(5.9)\underbrace{\EE[X \mid N]}_{\text{r.v.}} = \underbrace{\frac{N}{2}}_{\text{r.v.}}. \tag{5.9}

This is an equality of random variables: for every outcome of the experiment, the two sides take the same value.

The outcomes are not equally likely. For N=2N = 2, averaging the possible head counts as 0+1+23=1\frac{0 + 1 + 2}{3} = 1 happens to give the right answer, but only by coincidence: the three outcomes have probabilities 14,12,14\tfrac14, \tfrac12, \tfrac14, not 13\tfrac13 each. For N=3N = 3 the same shortcut would still give 32\tfrac32 by symmetry, but for a biased coin it fails immediately. Always weight by the conditional PMF.

The Law of Iterated Expectation (Tower Property)

Since E[X∣Y]\EE[X \mid Y] is a random variable, we can take its expectation. The answer is remarkably simple.

Theorem 5.1 (Law of iterated expectation)

For random variables X,YX, Y with E[∣X∣]<∞\EE\bigl[|X|\bigr] < \infty,

E[E[X∣Y]]=E[X].\EE\bigl[\EE[X \mid Y]\bigr] = \EE[X].

The inner expectation is over XX (with YY held fixed); the outer expectation is over YY.

Proof.

Write E[X∣Y]=g(Y)\EE[X \mid Y] = g(Y) with g(y)=E[X∣Y=y]g(y) = \EE[X \mid Y = y]. The outer expectation is the expectation of a function of YY, so by LOTUS (summing over the values yy with P(Y=y)>0\PP(Y = y) > 0):

E[E[X∣Y]]=βˆ‘yE[X∣Y=y]β‹…P(Y=y)(outerΒ expectation,Β LOTUS)=βˆ‘yβˆ‘xxβ‹…P(X=x∣Y=y)β‹…P(Y=y)(definitionΒ ofΒ E[X∣Y=y])=βˆ‘yβˆ‘xxβ‹…P(X=x, Y=y)(definitionΒ ofΒ conditionalΒ probability)=βˆ‘xxβˆ‘yP(X=x, Y=y)(swapΒ theΒ orderΒ ofΒ summation)=βˆ‘xxβ‹…P(X=x)(marginalizeΒ outΒ Y)=E[X].\begin{aligned} \EE\bigl[\EE[X \mid Y]\bigr] &= \sum_y \EE[X \mid Y = y] \cdot \PP(Y = y) && \text{(outer expectation, LOTUS)} \\ &= \sum_y \sum_x x \cdot \PP(X = x \mid Y = y) \cdot \PP(Y = y) && \text{(definition of } \EE[X \mid Y = y]) \\ &= \sum_y \sum_x x \cdot \PP(X = x,\, Y = y) && \text{(definition of conditional probability)} \\ &= \sum_x x \sum_y \PP(X = x,\, Y = y) && \text{(swap the order of summation)} \\ &= \sum_x x \cdot \PP(X = x) && \text{(marginalize out } Y) \\ &= \EE[X]. \end{aligned}

The third line uses P(X=x∣Y=y) P(Y=y)=P(X=x,Y=y)\PP(X = x \mid Y = y)\,\PP(Y = y) = \PP(X = x, Y = y), i.e. pX∣Y(x∣y) pY(y)=pX,Y(x,y)p_{X\mid Y}(x \mid y)\,p_Y(y) = p_{X,Y}(x, y). Swapping the sums is justified by E[∣X∣]<∞\EE[|X|] < \infty (absolute convergence).

Intuition: an average of averages. E[X∣Y=y]\EE[X \mid Y = y] is the average of XX within the “group” where Y=yY = y. The tower rule says: average these group averages, weighting each group by how likely it is, and you recover the overall average. The total expectation theorem from Lecture 4, E[X]=βˆ‘iP(Ai) E[X∣Ai]\EE[X] = \sum_i \PP(A_i)\,\EE[X \mid A_i], is the special case where YY records which event AiA_i of a partition occurred.

A useful companion result lets us pull functions of YY through the inner expectation: once we condition on YY, any function of YY is a known constant.

Theorem 5.2 (Taking out what is known)

For any function ff with E[∣f(Y) X∣]<∞\EE\bigl[|f(Y)\,X|\bigr] < \infty,

E[f(Y) X∣Y]=f(Y) E[X∣Y],\EE\bigl[f(Y)\,X \mid Y\bigr] = f(Y)\,\EE[X \mid Y],

and consequently

E[f(Y) X]=E[f(Y) E[X∣Y]].(5.13)\EE\bigl[f(Y)\,X\bigr] = \EE\bigl[f(Y)\,\EE[X \mid Y]\bigr]. \tag{5.13}
Proof.

Fix yy with P(Y=y)>0\PP(Y = y) > 0. Given Y=yY = y, the random variable f(Y)f(Y) equals the constant f(y)f(y), so it factors out of the conditional expectation:

E[f(Y) X∣Y=y]=βˆ‘xf(y) x P(X=x∣Y=y)=f(y)βˆ‘xx P(X=x∣Y=y)=f(y) E[X∣Y=y].\begin{aligned} \EE\bigl[f(Y)\,X \mid Y = y\bigr] &= \sum_x f(y)\,x\,\PP(X = x \mid Y = y) \\ &= f(y) \sum_x x\,\PP(X = x \mid Y = y) = f(y)\,\EE[X \mid Y = y]. \end{aligned}

Replacing yy by YY gives E[f(Y)X∣Y]=f(Y) E[X∣Y]\EE[f(Y)X \mid Y] = f(Y)\,\EE[X \mid Y]. Now apply the tower rule to the random variable f(Y)Xf(Y)X:

E[f(Y) X]=E[E[f(Y) X∣Y]]=E[f(Y) E[X∣Y]].\EE\bigl[f(Y)\,X\bigr] = \EE\bigl[\EE[f(Y)\,X \mid Y]\bigr] = \EE\bigl[f(Y)\,\EE[X \mid Y]\bigr].

Where the brackets go. In (5.13), f(Y)f(Y) stays inside the outer expectation, multiplied by the random variable E[X∣Y]\EE[X \mid Y]. It is not true in general that E[f(Y)X]=E[f(Y)]β‹…E[X∣Y]\EE[f(Y)X] = \EE[f(Y)]\cdot\EE[X \mid Y]; the right-hand side of that would not even be a number. For instance, with f(Y)=Y2f(Y) = Y^2: E[Y2X]=E[Y2 E[X∣Y]]\EE[Y^2 X] = \EE\bigl[Y^2\,\EE[X \mid Y]\bigr].

Example 5.3 (Random number of tosses, with N∼Geometric⁑(p)N \sim \Geom(p))

Suppose in Example 5.2 that NN is generated by a first coin with heads probability pp, tossed until its first heads, so that N∼Geometric⁑(p)N \sim \Geom(p). We then toss a second, fair coin NN times and let XX be the number of heads. From (5.9), E[X∣N]=N2=12β‹…N\EE[X \mid N] = \frac{N}{2} = \frac12 \cdot N.

Mean. By the tower rule, linearity, and E[N]=1p\EE[N] = \frac1p for N∼Geometric⁑(p)N \sim \Geom(p) (Lecture 4),

E[X]=E[E[X∣N]]=E[N2]=12 E[N]=12β‹…1p=12p.\EE[X] = \EE\bigl[\EE[X \mid N]\bigr] = \EE\Bigl[\frac{N}{2}\Bigr] = \frac12\,\EE[N] = \frac12 \cdot \frac1p = \frac{1}{2p}.

Sanity check: if p=1p = 1, then N=1N = 1 always, and one fair toss gives E[X]=12\EE[X] = \frac12. βœ“

A mixed moment. Taking out what is known with f(N)=Nf(N) = N, and using E[N2]=2βˆ’pp2\EE[N^2] = \frac{2-p}{p^2} from Lecture 4,

E[NX]=E[N E[X∣N]]=E[Nβ‹…N2]=12 E[N2]=2βˆ’p2p2.\EE[NX] = \EE\bigl[N\,\EE[X \mid N]\bigr] = \EE\Bigl[N \cdot \frac{N}{2}\Bigr] = \frac12\,\EE[N^2] = \frac{2 - p}{2p^2}.

The Symmetric Random Walk

Example 5.4 (Random walk on the integers)

A drunk man is walking along the integer line. He starts at the origin, X0=0X_0 = 0. At each step, independently of everything before, he moves one unit right with probability 12\tfrac12 or one unit left with probability 12\tfrac12. Let XnX_n be his location after nn steps.

Number line with the walker at n and arrows to n βˆ’ 1 and n + 1, each labeled probability 1/2

Questions: What is E[Xn]\EE[X_n]? What is E[Xn2]\EE[X_n^2], and how far from the origin is he typically?

Write the (n+1)(n+1)-th step using two indicator random variables,

1+(n+1)={1ifΒ stepΒ n+1Β isΒ +1,0otherwise,1βˆ’(n+1)={1ifΒ stepΒ n+1Β isΒ βˆ’1,0otherwise.\mathbf{1}_{+}^{(n+1)} = \begin{cases} 1 & \text{if step } n+1 \text{ is } +1, \\ 0 & \text{otherwise,} \end{cases} \qquad \mathbf{1}_{-}^{(n+1)} = \begin{cases} 1 & \text{if step } n+1 \text{ is } -1, \\ 0 & \text{otherwise.} \end{cases}

Exactly one of them equals 11, so the step is 1+(n+1)βˆ’1βˆ’(n+1)∈{+1,βˆ’1}\mathbf{1}_{+}^{(n+1)} - \mathbf{1}_{-}^{(n+1)} \in \{+1, -1\}, and

Xn+1=Xn+1+(n+1)βˆ’1βˆ’(n+1).(5.19)X_{n+1} = X_n + \mathbf{1}_{+}^{(n+1)} - \mathbf{1}_{-}^{(n+1)}. \tag{5.19}

Notation. An indicator only takes the values 00 and 11, so a step of βˆ’1-1 is written by subtracting the indicator 1βˆ’(n+1)\mathbf{1}_{-}^{(n+1)}. The superscript (n+1)(n+1) records that each step is a fresh, independent coin flip. Similarly, the location XnX_n and the step taken at time nn are different random variables, so we keep separate names for them.

The mean: 𝔼[Xβ‚™] = 0 for all n

Take expectations in (5.19). By linearity, and since E[1Β±(n+1)]=P(stepΒ isΒ Β±1)=12\EE[\mathbf{1}_{\pm}^{(n+1)}] = \PP(\text{step is } \pm 1) = \tfrac12,

E[Xn+1]=E[Xn]+E[1+(n+1)]βˆ’E[1βˆ’(n+1)]=E[Xn]+12βˆ’12=E[Xn].(5.20)\EE[X_{n+1}] = \EE[X_n] + \EE\bigl[\mathbf{1}_{+}^{(n+1)}\bigr] - \EE\bigl[\mathbf{1}_{-}^{(n+1)}\bigr] = \EE[X_n] + \tfrac12 - \tfrac12 = \EE[X_n]. \tag{5.20}

So one step never changes the expected location.

Theorem 5.3 (Mean of the symmetric random walk)

For every nβ‰₯0n \ge 0, E[Xn]=0\EE[X_n] = 0.

Proof.

By induction on nn. Base case: X0=0X_0 = 0 is a constant, so E[X0]=0\EE[X_0] = 0. Inductive step: suppose E[Xn]=0\EE[X_n] = 0. By (5.20), E[Xn+1]=E[Xn]=0\EE[X_{n+1}] = \EE[X_n] = 0. Unrolled, this is just a chain of equalities:

E[Xn]=E[Xnβˆ’1]=E[Xnβˆ’2]=β‹―=E[X1]=E[X0]=0.\EE[X_n] = \EE[X_{n-1}] = \EE[X_{n-2}] = \cdots = \EE[X_1] = \EE[X_0] = 0.

Two more ways to see it.

  1. Sum of steps. Let ΞΎi∈{+1,βˆ’1}\xi_i \in \{+1, -1\} be the ii-th step, so Xn=ΞΎ1+ΞΎ2+β‹―+ΞΎnX_n = \xi_1 + \xi_2 + \cdots + \xi_n. Each step has mean E[ΞΎi]=(+1)β‹…12+(βˆ’1)β‹…12=0\EE[\xi_i] = (+1)\cdot\tfrac12 + (-1)\cdot\tfrac12 = 0, and by linearity E[Xn]=βˆ‘i=1nE[ΞΎi]=0\EE[X_n] = \sum_{i=1}^n \EE[\xi_i] = 0.
  2. Symmetry. Flipping the direction of every step turns the walk XnX_n into βˆ’Xn-X_n, and since left and right are equally likely, the flipped walk has exactly the same distribution. So E[Xn]=E[βˆ’Xn]=βˆ’E[Xn]\EE[X_n] = \EE[-X_n] = -\EE[X_n], which forces E[Xn]=0\EE[X_n] = 0.

A zero mean does not mean the walker stays near the origin. He drifts away, but is equally likely to drift left or right, so the drifts cancel in the average. To measure how far he wanders, we need the second moment.

The second moment: 𝔼[Xβ‚™Β²] = n

Suppose Xn=kX_n = k. The next step is Β±1\pm 1 with probability 12\tfrac12 each, independent of the past, so

P(Xn+12=(k+1)2∣Xn=k)=12,P(Xn+12=(kβˆ’1)2∣Xn=k)=12.\PP\bigl(X_{n+1}^2 = (k+1)^2 \mid X_n = k\bigr) = \tfrac12, \qquad \PP\bigl(X_{n+1}^2 = (k-1)^2 \mid X_n = k\bigr) = \tfrac12.

These are the same two equally likely moves (kβ†’k+1k \to k+1 and kβ†’kβˆ’1k \to k-1), just with the resulting location squared. Therefore

E[Xn+12∣Xn=k]=12(k+1)2+12(kβˆ’1)2=12(k2+2k+1)+12(k2βˆ’2k+1)=k2+1.\begin{aligned} \EE\bigl[X_{n+1}^2 \mid X_n = k\bigr] &= \tfrac12 (k+1)^2 + \tfrac12 (k-1)^2 \\ &= \tfrac12 (k^2 + 2k + 1) + \tfrac12 (k^2 - 2k + 1) \\ &= k^2 + 1. \end{aligned}

The edge case k=0k = 0. When k=0k = 0, (k+1)2=(kβˆ’1)2=1(k+1)^2 = (k-1)^2 = 1, so the two events above are the same event, which has probability 11 (not 12\tfrac12). The computation of E[Xn+12∣Xn=0]\EE[X_{n+1}^2 \mid X_n = 0] is still correct, because we are really averaging over the two moves: 12β‹…1+12β‹…1=1=02+1\tfrac12 \cdot 1 + \tfrac12 \cdot 1 = 1 = 0^2 + 1.

We found g(k)=E[Xn+12∣Xn=k]=k2+1g(k) = \EE[X_{n+1}^2 \mid X_n = k] = k^2 + 1. Replacing the value kk by the random variable XnX_n (the “pattern matching” step, exactly as in E[X∣N]=N/2\EE[X \mid N] = N/2):

E[Xn+12∣Xn]=Xn2+1.\EE\bigl[X_{n+1}^2 \mid X_n\bigr] = X_n^2 + 1.

By the law of iterated expectation,

E[Xn+12]=E[E[Xn+12∣Xn]](law of iterated expectation)=E[Xn2+1]=1+E[Xn2].(linearity)\begin{aligned} \EE\bigl[X_{n+1}^2\bigr] &= \EE\bigl[\EE[X_{n+1}^2 \mid X_n]\bigr] && \text{(law of iterated expectation)} \\ &= \EE\bigl[X_n^2 + 1\bigr] \\ &= 1 + \EE\bigl[X_n^2\bigr]. && \text{(linearity)} \end{aligned}

Each step adds exactly 11 to the second moment, starting from E[X02]=0\EE[X_0^2] = 0:

E[X02]=0,E[X12]=1,E[X22]=2,β€¦βŸΉE[Xn2]=n,\EE[X_0^2] = 0, \quad \EE[X_1^2] = 1, \quad \EE[X_2^2] = 2, \quad \ldots \quad\Longrightarrow\quad \EE[X_n^2] = n,

(formally, by induction exactly as for the mean). In particular E[Xn+12]=1+n\EE[X_{n+1}^2] = 1 + n.

Theorem 5.4 (Spread of the symmetric random walk)

For every nβ‰₯0n \ge 0,

Var⁑(Xn)=E[Xn2]βˆ’(E[Xn])2=nβˆ’02=n,soΟƒXn=n.\Var(X_n) = \EE[X_n^2] - (\EE[X_n])^2 = n - 0^2 = n, \qquad\text{so}\qquad \sigma_{X_n} = \sqrt{n}.

Cross-check with Lecture 4. With Xn=ΞΎ1+β‹―+ΞΎnX_n = \xi_1 + \cdots + \xi_n and independent steps, each step has Var⁑(ΞΎi)=E[ΞΎi2]βˆ’02=1\Var(\xi_i) = \EE[\xi_i^2] - 0^2 = 1 (since ΞΎi2=1\xi_i^2 = 1 always). The variance of a sum of independent random variables is the sum of the variances, so Var⁑(Xn)=n\Var(X_n) = n. Same answer, no conditioning needed.

Key takeaway. The walker’s expected position is 00 forever, but his typical distance from the origin is n\sqrt{n}: after 10,00010{,}000 steps he is typically about 100100 units away. This n\sqrt{n} growth is the signature of diffusion, and it reappears throughout the course.

Conditional Variance and the Law of Total Variance

Definition 5.1 (Conditional variance)

For yy with P(Y=y)>0\PP(Y = y) > 0, the conditional variance of XX given Y=yY = y is the variance of the conditional distribution of XX:

Var⁑(X∣Y=y)β‰œE[(Xβˆ’E[X∣Y=y])2∣Y=y]=E[X2∣Y=y]βˆ’(E[X∣Y=y])2.\Var(X \mid Y = y) \triangleq \EE\Bigl[\bigl(X - \EE[X \mid Y = y]\bigr)^2 \Bigm| Y = y\Bigr] = \EE[X^2 \mid Y = y] - \bigl(\EE[X \mid Y = y]\bigr)^2.

This is a number for each yy, so Var⁑(X∣Y)\Var(X \mid Y), obtained by replacing yy with YY, is a function of YY, and hence a random variable.

(The second form follows exactly as in Lecture 3, applied to the conditional distribution of XX given Y=yY = y.)

Theorem 5.5 (Law of total variance)

For random variables X,YX, Y with E[X2]<∞\EE[X^2] < \infty,

Var⁑(X)=E[Var⁑(X∣Y)]+Var⁑(E[X∣Y]).\Var(X) = \EE\bigl[\Var(X \mid Y)\bigr] + \Var\bigl(\EE[X \mid Y]\bigr).
Proof.

Abbreviate ΞΌ(Y)=E[X∣Y]\mu(Y) = \EE[X \mid Y], so that Var⁑(X∣Y)=E[X2∣Y]βˆ’ΞΌ(Y)2\Var(X \mid Y) = \EE[X^2 \mid Y] - \mu(Y)^2.

Take expectations and use the tower rule on E[X2∣Y]\EE[X^2 \mid Y]:

E[Var⁑(X∣Y)]=E[E[X2∣Y]]βˆ’E[ΞΌ(Y)2]=E[X2]βˆ’E[ΞΌ(Y)2].\EE\bigl[\Var(X \mid Y)\bigr] = \EE\bigl[\EE[X^2 \mid Y]\bigr] - \EE\bigl[\mu(Y)^2\bigr] = \EE[X^2] - \EE\bigl[\mu(Y)^2\bigr].

Next, μ(Y)\mu(Y) is a random variable; apply the variance formula to it, and use the tower rule on its mean, E[μ(Y)]=E[E[X∣Y]]=E[X]\EE[\mu(Y)] = \EE[\EE[X\mid Y]] = \EE[X]:

Var⁑(ΞΌ(Y))=E[ΞΌ(Y)2]βˆ’(E[ΞΌ(Y)])2=E[ΞΌ(Y)2]βˆ’(E[X])2.\Var\bigl(\mu(Y)\bigr) = \EE\bigl[\mu(Y)^2\bigr] - \bigl(\EE[\mu(Y)]\bigr)^2 = \EE\bigl[\mu(Y)^2\bigr] - (\EE[X])^2.

Adding the two, the E[ΞΌ(Y)2]\EE[\mu(Y)^2] terms cancel:

E[Var⁑(X∣Y)]+Var⁑(E[X∣Y])=E[X2]βˆ’E[ΞΌ(Y)2]+E[ΞΌ(Y)2]βˆ’(E[X])2=Var⁑(X).\begin{aligned} \EE\bigl[\Var(X \mid Y)\bigr] + \Var\bigl(\EE[X \mid Y]\bigr) &= \EE[X^2] - \EE\bigl[\mu(Y)^2\bigr] + \EE\bigl[\mu(Y)^2\bigr] - (\EE[X])^2 \\ &= \Var(X). \end{aligned}

Interpretation: within-group plus between-group. Group the outcomes by the value of YY. Then E[Var⁑(X∣Y)]\EE[\Var(X \mid Y)] is the average spread within each group, and Var⁑(E[X∣Y])\Var(\EE[X \mid Y]) is the spread between the group means. Total spread is the sum of the two. Since both terms are nonnegative, Var⁑(X)β‰₯E[Var⁑(X∣Y)]\Var(X) \ge \EE[\Var(X \mid Y)]: on average, learning YY can only reduce our uncertainty about XX.

Example 5.5 (Variance of heads in a random number of tosses)

Continue the example with N∼Geometric⁑(p)N \sim \Geom(p) and X∣N∼Binomial⁑(N,12)X \mid N \sim \Bin(N, \tfrac12). From the binomial formulas of Lecture 4,

E[X∣N]=N2,Var⁑(X∣N)=Nβ‹…12β‹…12=N4.\EE[X \mid N] = \frac{N}{2}, \qquad \Var(X \mid N) = N \cdot \tfrac12 \cdot \tfrac12 = \frac{N}{4}.

Using E[N]=1p\EE[N] = \frac1p and Var⁑(N)=1βˆ’pp2\Var(N) = \frac{1-p}{p^2}:

E[Var⁑(X∣N)]=E[N4]=14β‹…1p=14p,Var⁑(E[X∣N])=Var⁑(N2)=14 Var⁑(N)=1βˆ’p4p2.\begin{aligned} \EE\bigl[\Var(X \mid N)\bigr] &= \EE\Bigl[\frac{N}{4}\Bigr] = \frac14 \cdot \frac1p = \frac{1}{4p}, \\ \Var\bigl(\EE[X \mid N]\bigr) &= \Var\Bigl(\frac{N}{2}\Bigr) = \frac14\,\Var(N) = \frac{1-p}{4p^2}. \end{aligned}

By the law of total variance,

Var⁑(X)=14p+1βˆ’p4p2=p4p2+1βˆ’p4p2=14p2.\Var(X) = \frac{1}{4p} + \frac{1-p}{4p^2} = \frac{p}{4p^2} + \frac{1-p}{4p^2} = \frac{1}{4p^2}.

Sanity check: if p=1p = 1, then N=1N = 1 and X∼Bernoulli⁑(12)X \sim \Bern(\tfrac12), whose variance is 12β‹…12=14\tfrac12\cdot\tfrac12 = \tfrac14. βœ“

Continuous Random Variables

Continuous random variables (CRVs) let us measure uncertainty in a principled way for quantities that vary continuously, such as velocity, time, and distance.

Coming up. Important continuous distributions include the uniform, exponential, and gamma distributions, which we develop in upcoming lectures. The exponential is the continuous analog of the geometric (Lecture 4).

Why discrete tools break. Let X∼Uniform⁑[0,1]X \sim \operatorname{Uniform}[0,1], a number chosen “uniformly at random” from [0,1][0,1]. Then:

  • P(X=x)=0\PP(X = x) = 0 for any specific number xx (there are uncountably many equally likely candidates);
  • yet P(X<12)=12\PP(X < \tfrac12) = \tfrac12.

So we cannot build probabilities by adding up point masses as we did with PMFs. Instead, we assign probabilities directly to sets that are “allowable events.”

Definition 5.2 (Allowable events (Borel sets))

The allowable events for a real-valued random variable include all intervals,

(βˆ’βˆž,a),[a,b],(a,b],…(-\infty, a), \qquad [a, b], \qquad (a, b], \qquad \ldots

together with everything obtainable from intervals by countable unions, countable intersections, and complements, e.g. (a,b)βˆͺ(c,d)(a, b) \cup (c, d). These are the Borel sets; they form the Borel Οƒ\sigma-algebra from Lecture 1. In practice, any “reasonable” set is allowable.

Recall that for a discrete random variable (DRV) and a set of values BB,

P(X∈B)=βˆ‘x∈BpX(x).\PP(X \in B) = \sum_{x \in B} p_X(x).

For a CRV, the sum over points becomes an integral of a density.

Definition 5.3 (Continuous random variable and PDF)

A random variable XX is continuous if there is a function fX:Rβ†’[0,∞)f_X : \RR \to [0, \infty), called its probability density function (PDF), such that for every allowable set BB,

P(X∈B)=∫BfX(x) dx.\PP(X \in B) = \int_B f_X(x)\,dx.

A density must satisfy

fX(x)β‰₯0β€…β€ŠΒ forΒ allΒ x,andβˆ«βˆ’βˆžβˆžfX(x) dx=1.f_X(x) \ge 0 \;\text{ for all } x, \qquad\text{and}\qquad \int_{-\infty}^{\infty} f_X(x)\,dx = 1.

In particular, P(a≀X≀b)=∫abfX(x) dx\PP(a \le X \le b) = \int_a^b f_X(x)\,dx is the area under the density between aa and bb.

A density curve f_X(x) with the area between a and b shaded, labeled P(a ≀ X ≀ b)
Theorem 5.6 (Single points have probability zero)

If XX is continuous, then P(X=x)=0\PP(X = x) = 0 for every x∈Rx \in \RR.

Proof.

Fix xx and any Ξ΄>0\delta > 0. The event {X=x}\{X = x\} is contained in {x≀X<x+Ξ΄}\{x \le X < x + \delta\}, so

P(X=x)≀P(x≀X<x+Ξ΄)=∫xx+Ξ΄fX(u) du.\PP(X = x) \le \PP(x \le X < x + \delta) = \int_x^{x+\delta} f_X(u)\,du.

(We use the dummy variable uu inside the integral so as not to confuse it with xx, which now appears in the limits.) As Ξ΄β†’0\delta \to 0, this is the integral of fXf_X over a shrinking interval, which tends to 00. If fXf_X is continuous at xx, we can see this explicitly: the sliver is approximately a rectangle of height fX(x)f_X(x) and width Ξ΄\delta, so the integral is β‰ˆfX(x) δ→0\approx f_X(x)\,\delta \to 0. Since P(X=x)\PP(X = x) is smaller than a quantity that can be made arbitrarily small, P(X=x)=0\PP(X = x) = 0.

A thin sliver under the density f_X(u) between x and x + Ξ΄, with area approximately f_X(x) Ξ΄

A density is not a probability. fX(x)f_X(x) has units of probability per unit length. Wherever fXf_X is continuous,

fX(x)=lim⁑δ→0P(x≀X<x+Ξ΄)Ξ΄,i.e.P(x≀X<x+Ξ΄)β‰ˆfX(x) δ forΒ smallΒ Ξ΄.f_X(x) = \lim_{\delta \to 0} \frac{\PP(x \le X < x + \delta)}{\delta}, \quad\text{i.e.}\quad \PP(x \le X < x + \delta) \approx f_X(x)\,\delta \text{ for small } \delta.

Don’t confuse fX(x)f_X(x) with a probability: it is a density. Only areas under fXf_X are probabilities, so fX(x)f_X(x) itself may exceed 11, as the next example shows.

Example 5.6 (An unbounded density)

Let

fX(x)={12x0<x≀1,0otherwise.f_X(x) = \begin{cases} \dfrac{1}{2\sqrt{x}} & 0 < x \le 1, \\[4pt] 0 & \text{otherwise.} \end{cases}

It is a valid PDF. Clearly fXβ‰₯0f_X \ge 0. For the total area, write 12x=12xβˆ’1/2\frac{1}{2\sqrt{x}} = \frac12 x^{-1/2} and integrate:

βˆ«βˆ’βˆžβˆžfX(x) dx=∫0112 xβˆ’1/2 dx=12[ 2x1/2]01=[x ]01=1βˆ’0=1.\int_{-\infty}^{\infty} f_X(x)\,dx = \int_0^1 \tfrac12\, x^{-1/2}\,dx = \tfrac12 \Bigl[\, 2x^{1/2} \Bigr]_0^1 = \Bigl[\sqrt{x}\,\Bigr]_0^1 = 1 - 0 = 1.

It exceeds 11, and is even unbounded. fX(1)=12f_X(1) = \tfrac12, but fX(0.01)=12(0.1)=5f_X(0.01) = \frac{1}{2(0.1)} = 5, and fX(x)β†’βˆžf_X(x) \to \infty as xβ†’0+x \to 0^+. Nevertheless every probability is finite, e.g. P(X≀0.01)=∫00.0112xβˆ’1/2 dx=0.01=0.1\PP(X \le 0.01) = \int_0^{0.01}\tfrac12 x^{-1/2}\,dx = \sqrt{0.01} = 0.1.

Graph of f_X(x) = 1/(2√x) on (0, 1], rising without bound as x approaches 0, with the area underneath shaded

The Cumulative Distribution Function

Definition 5.4 (Cumulative distribution function (CDF))

The cumulative distribution function of any random variable XX (discrete or continuous) is

FX(x)β‰œP(X≀x),x∈R.F_X(x) \triangleq \PP(X \le x), \qquad x \in \RR.

For a continuous random variable,

FX(x)=βˆ«βˆ’βˆžxfX(u) du.F_X(x) = \int_{-\infty}^{x} f_X(u)\,du.
Theorem 5.7 (Properties of the CDF)

Every CDF satisfies:

  1. Nondecreasing: if x≀yx \le y then FX(x)≀FX(y)F_X(x) \le F_X(y), since {X≀x}βŠ†{X≀y}\{X \le x\} \subseteq \{X \le y\}.
  2. Limits: lim⁑xβ†’βˆ’βˆžFX(x)=0\displaystyle\lim_{x \to -\infty} F_X(x) = 0 and lim⁑xβ†’+∞FX(x)=1\displaystyle\lim_{x \to +\infty} F_X(x) = 1.
  3. Intervals: P(a<X≀b)=FX(b)βˆ’FX(a)\PP(a < X \le b) = F_X(b) - F_X(a), because {X≀b}\{X \le b\} is the disjoint union of {X≀a}\{X \le a\} and {a<X≀b}\{a < X \le b\}.
Theorem 5.8 (Recovering the PMF or PDF from the CDF)
  • CRV: wherever fXf_X is continuous, fX(x)=dFX(x)dx.f_X(x) = \frac{dF_X(x)}{dx}.
  • Integer-valued DRV: for every integer kk, P(X=k)=FX(k)βˆ’FX(kβˆ’1).\PP(X = k) = F_X(k) - F_X(k-1).
Proof.

CRV: FX(x)=βˆ«βˆ’βˆžxfX(u) duF_X(x) = \int_{-\infty}^x f_X(u)\,du, so this is the fundamental theorem of calculus. DRV: because XX only takes integer values, there are no possible values strictly between kβˆ’1k-1 and kk. So {X≀k}\{X \le k\} is the disjoint union of {X≀kβˆ’1}\{X \le k-1\} and {X=k}\{X = k\}, giving FX(k)=FX(kβˆ’1)+P(X=k)F_X(k) = F_X(k-1) + \PP(X = k). (For a discrete random variable taking non-integer values, use the jump of FXF_X at the value instead.)

Picturing the two kinds of CDF. For an integer-valued DRV, FXF_X is a staircase. On each open interval (kβˆ’1,k)(k-1, k) there are no possible values, so no new probability accumulates and FXF_X stays flat, holding the value FX(kβˆ’1)F_X(k-1). It does not drop back to zero: all the probability collected up through kβˆ’1k-1 is still counted. At kk itself, FXF_X jumps up by exactly P(X=k)\PP(X = k). For a CRV, single points carry no probability, so FXF_X rises continuously with no jumps.

Left: the staircase CDF of an integer-valued discrete random variable, jumping by P(X = k) at k. Right: the continuous CDF of Uniform[0,1], rising linearly from 0 to 1
Corollary 5.1 (Consequences for continuous random variables)

If XX is continuous, then for all a≀ba \le b:

P(a≀X≀b)=∫abfX(x) dx=FX(b)βˆ’FX(a),\PP(a \le X \le b) = \int_a^b f_X(x)\,dx = F_X(b) - F_X(a),P(X=a)=0,P(X<a)=P(X≀a).\PP(X = a) = 0, \qquad \PP(X < a) = \PP(X \le a).

Because points do not contribute, it does not matter whether the endpoints of an interval are included: P(a<X<b)=P(a≀X<b)=P(a<X≀b)=P(a≀X≀b)\PP(a < X < b) = \PP(a \le X < b) = \PP(a < X \le b) = \PP(a \le X \le b). This is false for discrete random variables, where P(X<k)\PP(X < k) and P(X≀k)\PP(X \le k) differ by P(X=k)\PP(X = k).

Example: A Random Dart on a Disk

Example 5.7 (Random dart on the unit disk)

A dart lands uniformly at random on a disk of radius 11 (the unit circle and its interior). “Uniformly” means the probability of landing in any region is proportional to its area:

P(dartΒ landsΒ inΒ regionΒ R)=Area(R)AreaΒ ofΒ fullΒ disk=Area(R)Ο€(1)2.\PP(\text{dart lands in region } R) = \frac{\text{Area}(R)}{\text{Area of full disk}} = \frac{\text{Area}(R)}{\pi (1)^2}.

Let YY be the dart’s distance from the center. Find the PDF fY(y)f_Y(y).

Unit disk with a shaded inner disk of radius y, labeled Y ≀ y, and a dart landing inside the unit disk

It is easiest to compute FY(y)=P(Y≀y)F_Y(y) = \PP(Y \le y) first. For 0≀y≀10 \le y \le 1, the event {Y≀y}\{Y \le y\} means the dart lands in the shaded disk of radius yy:

FY(y)=P(Y≀y)=AreaΒ shadedAreaΒ fullΒ disk=Ο€y2Ο€(1)2=y2.F_Y(y) = \PP(Y \le y) = \frac{\text{Area shaded}}{\text{Area full disk}} = \frac{\pi y^2}{\pi (1)^2} = y^2.

Outside [0,1][0,1]: FY(y)=0F_Y(y) = 0 for y<0y < 0 (distance is never negative) and FY(y)=1F_Y(y) = 1 for y>1y > 1 (the dart is always on the disk).

Differentiating,

fY(y)=dFY(y)dy=ddy(y2)=2y,0≀y≀1,f_Y(y) = \frac{dF_Y(y)}{dy} = \frac{d}{dy}\bigl(y^2\bigr) = 2y, \qquad 0 \le y \le 1,

and fY(y)=0f_Y(y) = 0 otherwise. Check: ∫012y dy=[y2]01=1\int_0^1 2y\,dy = \bigl[y^2\bigr]_0^1 = 1. βœ“

By the CDF,

P(Y∈(0.5,0.6))=FY(0.6)βˆ’FY(0.5)=0.62βˆ’0.52=0.36βˆ’0.25=0.11.\PP\bigl(Y \in (0.5, 0.6)\bigr) = F_Y(0.6) - F_Y(0.5) = 0.6^2 - 0.5^2 = 0.36 - 0.25 = 0.11.

Or, equivalently, by the PDF: ∫0.50.62y dy=[y2]0.50.6=0.11\int_{0.5}^{0.6} 2y\,dy = \bigl[y^2\bigr]_{0.5}^{0.6} = 0.11.

Three traps in this example.

  1. YY is not uniform on [0,1][0,1]. The dart’s position is uniform on the disk, but its distance from the center is not: the density 2y2y grows with yy, because there is more area at larger radii. The ring 0.5<Y<0.60.5 < Y < 0.6 has probability 0.110.11, while the same-width band 0<Y<0.10 < Y < 0.1 near the center has only 0.12=0.010.1^2 = 0.01.
  2. Don’t stop at the CDF. y2y^2 is FYF_Y, not fYf_Y; the PDF requires the extra differentiation step.
  3. 1Ο€r2\frac{1}{\pi r^2} is a 2-D density. The constant 1Ο€r2\frac{1}{\pi r^2} (here 1Ο€\frac1\pi) is the density of the dart’s position per unit area. It is not fYf_Y, which is a density per unit length along the radius.

The CDF method. This “CDF first, then differentiate” strategy is the standard way to find the density of a function of a random variable.

Example: Meeting at Random Times

Not finished in lecture. We ran out of time after posing this problem. The full solution is given below.

Example 5.8 (Will A and B meet?)

AA and BB each arrive at a meeting spot at a uniformly random time between 4:00 pm and 5:00 pm, independently of each other. Whoever arrives first waits 1010 minutes and then leaves. What is P(AΒ andΒ BΒ meet)\PP(A \text{ and } B \text{ meet})?

Setup. Measure time in minutes after 4:00 pm, so AA and BB are independent Uniform⁑[0,60]\operatorname{Uniform}[0, 60] random variables. The pair (A,B)(A, B) is a point in the 60Γ—6060 \times 60 square [0,60]2[0,60]^2. Because AA and BB are independent and each uniform, the point (A,B)(A, B) is uniform on the square:

P((A,B)∈R)=Area(R)60Γ—60=Area(R)3600.\PP\bigl((A, B) \in R\bigr) = \frac{\text{Area}(R)}{60 \times 60} = \frac{\text{Area}(R)}{3600}.

(Joint distributions of several continuous random variables are developed formally in upcoming lectures; this “probability == area fraction” rule is the two-dimensional version of the uniform distribution, exactly as in the dart example.)

When do they meet? Whoever arrives first waits 1010 minutes, so they meet exactly when their arrival times differ by at most 1010 minutes:

{AΒ andΒ BΒ meet}={∣Aβˆ’Bβˆ£β‰€10}.\{A \text{ and } B \text{ meet}\} = \bigl\{|A - B| \le 10\bigr\}.

This is the shaded diagonal band in the figure below.

The 60 by 60 square of arrival times (A, B), with the diagonal band |A βˆ’ B| ≀ 10 shaded as the meeting region and two corner triangles where they miss each other

Compute the complement. The two blue corner regions, where they miss each other, are right triangles:

  • {Aβˆ’B>10}\{A - B > 10\}: AA arrives more than 1010 minutes after BB, so BB has already left. The triangle has vertices (10,0),(60,0),(60,50)(10,0), (60,0), (60,50) and legs of length 5050.
  • {Bβˆ’A>10}\{B - A > 10\}: the mirror image, with vertices (0,10),(0,60),(50,60)(0,10), (0,60), (50,60), also with legs of length 5050.

Each triangle has area 12β‹…50β‹…50=1250\tfrac12 \cdot 50 \cdot 50 = 1250, so together they cover 25002500 out of 36003600:

P(miss)=2β‹…12503600=25003600=2536.\PP(\text{miss}) = \frac{2 \cdot 1250}{3600} = \frac{2500}{3600} = \frac{25}{36}.

Therefore

P(AΒ andΒ BΒ meet)=1βˆ’2536=1136β‰ˆ0.306.\PP(A \text{ and } B \text{ meet}) = 1 - \frac{25}{36} = \frac{11}{36} \approx 0.306.

Equivalently, the band has area 3600βˆ’2500=11003600 - 2500 = 1100, and 11003600=1136\frac{1100}{3600} = \frac{11}{36}.

Checks and generalization.

  1. Boundaries don’t matter. The lines ∣Aβˆ’B∣=10|A - B| = 10 have zero area, so it makes no difference whether “waits 1010 minutes” includes the endpoint. This is the two-dimensional version of P(X<a)=P(X≀a)\PP(X < a) = \PP(X \le a).
  2. General formula. With a window of length TT and waiting time w≀Tw \le T, the same picture gives P(meet)=1βˆ’(1βˆ’wT)2.\PP(\text{meet}) = 1 - \Bigl(1 - \frac{w}{T}\Bigr)^2. With w=10w = 10, T=60T = 60: 1βˆ’(5/6)2=11/361 - (5/6)^2 = 11/36. Sanity checks: w=0w = 0 gives 00 (they must arrive at the exact same instant), and w=Tw = T gives 11.

4629 words