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
I toss a fair coin until I get a heads. If I toss times, I give you . For example,
Question: how much would you pay to play this game?
Let be the number of tosses in the game and the amount of money you win. The game ends on the first heads, so :
(The first flips are tails and the -th is heads; each has probability .) By LOTUS,
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 , and . The infinite mean is driven entirely by astronomically rare, astronomically large payouts (each outcome contributes exactly to the mean). Two standard resolutions:
- 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: (the geometric mean from Lecture 4), which corresponds to a “fair price” of about .
- Finite bankroll. No casino can pay more than some . If payouts are capped at , then Even a billion-dollar bankroll () gives an expected payout of only .
Conditional Expectation as a Random Variable
Recall from Lecture 4 that for each value with ,
is a number. Letting , the conditional expectation is a function of , and therefore is a random variable.
We toss a fair coin times, where is itself a random variable taking values in . Let be the total number of heads. Given , the tosses are independent fair Bernoulli trials, so .
: one toss gives or heads, each with probability :
: two tosses give , , or heads, with probabilities (), ( or ), and ():
General : by the binomial mean from Lecture 4,
Sweeping over all values and replacing by the random variable ,
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 , averaging the possible head counts as happens to give the right answer, but only by coincidence: the three outcomes have probabilities , not each. For the same shortcut would still give by symmetry, but for a biased coin it fails immediately. Always weight by the conditional PMF.
The Law of Iterated Expectation (Tower Property)
Since is a random variable, we can take its expectation. The answer is remarkably simple.
For random variables with ,
The inner expectation is over (with held fixed); the outer expectation is over .
Write with . The outer expectation is the expectation of a function of , so by LOTUS (summing over the values with ):
The third line uses , i.e. . Swapping the sums is justified by (absolute convergence).
Intuition: an average of averages. is the average of within the “group” where . 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, , is the special case where records which event of a partition occurred.
A useful companion result lets us pull functions of through the inner expectation: once we condition on , any function of is a known constant.
For any function with ,
and consequently
Fix with . Given , the random variable equals the constant , so it factors out of the conditional expectation:
Replacing by gives . Now apply the tower rule to the random variable :
Where the brackets go. In (5.13), stays inside the outer expectation, multiplied by the random variable . It is not true in general that ; the right-hand side of that would not even be a number. For instance, with : .
Suppose in Example 5.2 that is generated by a first coin with heads probability , tossed until its first heads, so that . We then toss a second, fair coin times and let be the number of heads. From (5.9), .
Mean. By the tower rule, linearity, and for (Lecture 4),
Sanity check: if , then always, and one fair toss gives . β
A mixed moment. Taking out what is known with , and using from Lecture 4,
The Symmetric Random Walk
A drunk man is walking along the integer line. He starts at the origin, . At each step, independently of everything before, he moves one unit right with probability or one unit left with probability . Let be his location after steps.
Questions: What is ? What is , and how far from the origin is he typically?
Write the -th step using two indicator random variables,
Exactly one of them equals , so the step is , and
Notation. An indicator only takes the values and , so a step of is written by subtracting the indicator . The superscript records that each step is a fresh, independent coin flip. Similarly, the location and the step taken at time 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 ,
So one step never changes the expected location.
For every , .
By induction on . Base case: is a constant, so . Inductive step: suppose . By (5.20), . Unrolled, this is just a chain of equalities:
Two more ways to see it.
- Sum of steps. Let be the -th step, so . Each step has mean , and by linearity .
- Symmetry. Flipping the direction of every step turns the walk into , and since left and right are equally likely, the flipped walk has exactly the same distribution. So , which forces .
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 . The next step is with probability each, independent of the past, so
These are the same two equally likely moves ( and ), just with the resulting location squared. Therefore
The edge case . When , , so the two events above are the same event, which has probability (not ). The computation of is still correct, because we are really averaging over the two moves: .
We found . Replacing the value by the random variable (the “pattern matching” step, exactly as in ):
By the law of iterated expectation,
Each step adds exactly to the second moment, starting from :
(formally, by induction exactly as for the mean). In particular .
For every ,
Cross-check with Lecture 4. With and independent steps, each step has (since always). The variance of a sum of independent random variables is the sum of the variances, so . Same answer, no conditioning needed.
Key takeaway. The walker’s expected position is forever, but his typical distance from the origin is : after steps he is typically about units away. This growth is the signature of diffusion, and it reappears throughout the course.
Conditional Variance and the Law of Total Variance
For with , the conditional variance of given is the variance of the conditional distribution of :
This is a number for each , so , obtained by replacing with , is a function of , and hence a random variable.
(The second form follows exactly as in Lecture 3, applied to the conditional distribution of given .)
For random variables with ,
Abbreviate , so that .
Take expectations and use the tower rule on :
Next, is a random variable; apply the variance formula to it, and use the tower rule on its mean, :
Adding the two, the terms cancel:
Interpretation: within-group plus between-group. Group the outcomes by the value of . Then is the average spread within each group, and is the spread between the group means. Total spread is the sum of the two. Since both terms are nonnegative, : on average, learning can only reduce our uncertainty about .
Continue the example with and . From the binomial formulas of Lecture 4,
Using and :
By the law of total variance,
Sanity check: if , then and , whose variance is . β
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 , a number chosen “uniformly at random” from . Then:
- for any specific number (there are uncountably many equally likely candidates);
- yet .
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.”
The allowable events for a real-valued random variable include all intervals,
together with everything obtainable from intervals by countable unions, countable intersections, and complements, e.g. . These are the Borel sets; they form the Borel -algebra from Lecture 1. In practice, any “reasonable” set is allowable.
Recall that for a discrete random variable (DRV) and a set of values ,
For a CRV, the sum over points becomes an integral of a density.
A random variable is continuous if there is a function , called its probability density function (PDF), such that for every allowable set ,
A density must satisfy
In particular, is the area under the density between and .
If is continuous, then for every .
Fix and any . The event is contained in , so
(We use the dummy variable inside the integral so as not to confuse it with , which now appears in the limits.) As , this is the integral of over a shrinking interval, which tends to . If is continuous at , we can see this explicitly: the sliver is approximately a rectangle of height and width , so the integral is . Since is smaller than a quantity that can be made arbitrarily small, .
A density is not a probability. has units of probability per unit length. Wherever is continuous,
Don’t confuse with a probability: it is a density. Only areas under are probabilities, so itself may exceed , as the next example shows.
Let
It is a valid PDF. Clearly . For the total area, write and integrate:
It exceeds , and is even unbounded. , but , and as . Nevertheless every probability is finite, e.g. .
The Cumulative Distribution Function
The cumulative distribution function of any random variable (discrete or continuous) is
For a continuous random variable,
Every CDF satisfies:
- Nondecreasing: if then , since .
- Limits: and .
- Intervals: , because is the disjoint union of and .
- CRV: wherever is continuous,
- Integer-valued DRV: for every integer ,
CRV: , so this is the fundamental theorem of calculus. DRV: because only takes integer values, there are no possible values strictly between and . So is the disjoint union of and , giving . (For a discrete random variable taking non-integer values, use the jump of at the value instead.)
Picturing the two kinds of CDF. For an integer-valued DRV, is a staircase. On each open interval there are no possible values, so no new probability accumulates and stays flat, holding the value . It does not drop back to zero: all the probability collected up through is still counted. At itself, jumps up by exactly . For a CRV, single points carry no probability, so rises continuously with no jumps.
If is continuous, then for all :
Because points do not contribute, it does not matter whether the endpoints of an interval are included: . This is false for discrete random variables, where and differ by .
Example: A Random Dart on a Disk
A dart lands uniformly at random on a disk of radius (the unit circle and its interior). “Uniformly” means the probability of landing in any region is proportional to its area:
Let be the dart’s distance from the center. Find the PDF .
It is easiest to compute first. For , the event means the dart lands in the shaded disk of radius :
Outside : for (distance is never negative) and for (the dart is always on the disk).
Differentiating,
and otherwise. Check: . β
By the CDF,
Or, equivalently, by the PDF: .
Three traps in this example.
- is not uniform on . The dart’s position is uniform on the disk, but its distance from the center is not: the density grows with , because there is more area at larger radii. The ring has probability , while the same-width band near the center has only .
- Don’t stop at the CDF. is , not ; the PDF requires the extra differentiation step.
- is a 2-D density. The constant (here ) is the density of the dart’s position per unit area. It is not , 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.
and 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 minutes and then leaves. What is ?
Setup. Measure time in minutes after 4:00 pm, so and are independent random variables. The pair is a point in the square . Because and are independent and each uniform, the point is uniform on the square:
(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 minutes, so they meet exactly when their arrival times differ by at most minutes:
This is the shaded diagonal band in the figure below.
Compute the complement. The two blue corner regions, where they miss each other, are right triangles:
- : arrives more than minutes after , so has already left. The triangle has vertices and legs of length .
- : the mirror image, with vertices , also with legs of length .
Each triangle has area , so together they cover out of :
Therefore
Equivalently, the band has area , and .
Checks and generalization.
- Boundaries don’t matter. The lines have zero area, so it makes no difference whether “waits minutes” includes the endpoint. This is the two-dimensional version of .
- General formula. With a window of length and waiting time , the same picture gives With , : . Sanity checks: gives (they must arrive at the exact same instant), and gives .