Sub topic 6
Proving Every Item Has Probability k/n
This chapter assumes you know Algorithm R from the previous chapter, conditional probability, and proof by induction. The goal is to show that after Algorithm R reads n items, each item is in the reservoir with probability exactly k/n. That holds no matter what n turns out to be.
The Claim
Number the items 1, 2, 3, … in arrival order. The Python code uses 0-based i, so item number i + 1 in this proof corresponds to index i in the code. In 1-based terms, Algorithm R does this:
- Items 1 through k go straight into the reservoir.
- For item t + 1 with t ≥ k, draw j uniformly from {1, …, t + 1}. If j ≤ k, overwrite slot j with the new item.
Invariant: for every t ≥ k, after item t has been processed, each of items 1 through t is in the reservoir with probability k/t.
Setting t = n gives the result. The invariant holds at every point in the stream, so the sampler stays correct whenever the stream ends. This is why it works when the length is unknown.
Base Case
At t = k, the reservoir holds items 1 through k. Each is present with probability 1 = k/k.
Inductive Step
Assume the invariant holds after item t. Now item t + 1 arrives. Two kinds of item need checking.
The new item
Item t + 1 enters when j ≤ k. j is uniform over t + 1 values, so this happens with probability k/(t + 1). That matches the claim.
An older item
Take any item x with x ≤ t. By the hypothesis, x is in the reservoir with probability k/t. Given that x is in some slot s, it gets evicted only if the new item is accepted and lands on slot s. Both events require j = s, which is a single value out of t + 1:
P(evicted | present) = 1/(t + 1)
So x survives with probability t/(t + 1), and
P(x present after t + 1) = (k/t) · (t/(t + 1)) = k/(t + 1).
Both cases give k/(t + 1), which completes the induction.
Unrolling the Product
You can also check one item directly. Item m > k enters with probability k/m. After that, it must survive every later step:
| Step | Survival probability |
|---|---|
| m + 1 | m/(m + 1) |
| m + 2 | (m + 1)/(m + 2) |
| … | … |
| n | (n − 1)/n |
The product telescopes to m/n, so P = (k/m)(m/n) = k/n. For m ≤ k, the item starts in the reservoir with probability 1, and its survival factors from k + 1 to n multiply to k/n.
A Stronger Statement
Each item having marginal probability k/n does not by itself prove the sample is uniform without replacement. A biased sampler could keep only "consecutive" groups and still give every item k/n. Algorithm R meets the stronger condition: every k-subset is equally likely, with probability 1/C(n, k). The proof is an induction of the same shape. Assume each k-subset of the first t items has probability 1/C(t, k). Then show that each subset containing item t + 1, and each subset not containing it, comes out at 1/C(t + 1, k).
Exercise
With k = 2 and n = 4, compute the probability that item 1 is still in the reservoir at the end, using the step-by-step survival factors.
Show answer
Item 1 starts in the reservoir. It survives step 3 with probability 2/3 and step 4 with probability 3/4. The product is 2/3 · 3/4 = 1/2, which equals k/n = 2/4.
Exercise
Suppose a bug draws j uniformly from {1, …, t} instead of {1, …, t + 1}. What probability does item t + 1 enter with, and does the invariant still hold?
Show answer
It enters with probability k/t instead of k/(t + 1), and an older item is evicted with probability 1/t. From k/t, the older item drops to (k/t)(1 − 1/t) = k(t − 1)/t², which is not k/(t + 1). New items are oversampled, so the invariant fails. At the first step (t = k) the new item is always accepted, so the bug can't be missed.