Sub topic 2
What a Uniform Sample Without Replacement Means
This chapter assumes you know basic Python, including lists, sets, and the random module. It also assumes elementary probability: counting outcomes, independence, and conditional probability. Before building any streaming algorithm, we need a precise definition of a correct result. Later chapters prove that Algorithm R meets this definition, so the wording here matters.
The definition
Suppose a population has n distinct items and we want a sample of size k, where k ≤ n.
- Without replacement means each item can appear in the sample at most once. The sample is a set of
kdifferent items. - Uniform means every possible set of
kitems is equally likely to be the one we return.
There are C(n, k) = n! / (k! (n − k)!) possible sets. So a uniform sample without replacement returns each particular set with probability 1 / C(n, k).
The order inside the sample does not matter. A reservoir is a list, but we treat [3, 7] and [7, 3] as the same sample.
Inclusion probability: a consequence, not the definition
A useful fact follows from the definition. Any single item x belongs to C(n − 1, k − 1) of the size-k sets. Its chance of being chosen is therefore:
C(n − 1, k − 1) / C(n, k) = k / n
This inclusion probability of k/n is what the correctness proof in a later chapter establishes for Algorithm R. It is easier to prove than the full definition, and Algorithm R in fact satisfies the stronger property too.
The reverse does not hold. A sampler can give every item probability k/n and still be far from uniform.
A counterexample you can run
Systematic sampling picks a random start and then takes every (n/k)-th item. With n = 6 and k = 2, it can return only {0, 3}, {1, 4}, or {2, 5}. Each item still appears with probability 1/3, which equals k/n, but 12 of the 15 possible pairs can never occur.
import random
from collections import Counter
n, k, trials = 6, 2, 60_000
items = range(n)
def uniform_subset(rng=random):
return frozenset(rng.sample(items, k))
def systematic(rng=random):
step = n // k
start = rng.randrange(step)
return frozenset(range(start, n, step))
for name, sampler in [("uniform", uniform_subset), ("systematic", systematic)]:
subsets = Counter()
per_item = Counter()
for _ in range(trials):
s = sampler()
subsets[s] += 1
per_item.update(s)
print(name)
print(" distinct subsets seen:", len(subsets)) # 15 vs 3
print(" item frequencies:",
[round(per_item[x] / trials, 3) for x in items]) # all near 0.333Both samplers print item frequencies near 0.333. Only the uniform one produces all 15 subsets, each about 1/15 of the time.
With versus without replacement
| Property | Without replacement | With replacement |
|---|---|---|
| Duplicates allowed | No | Yes |
| Possible outcomes | C(n, k) sets | n^k ordered sequences |
| Requires k ≤ n | Yes | No |
| Python standard tool | `random.sample` | `random.choices` |
Reservoir sampling produces samples without replacement. If you need sampling with replacement from a stream, you can run k independent single-item reservoirs, which the warm-up chapter builds.
Why this matters for streams
random.sample needs the whole population and its length up front. A stream gives us neither. The goal of the rest of the tutorial is to produce exactly the distribution defined here, with every k-subset at probability 1 / C(n, k), while reading each item once and storing only k of them.
Exercise
With n = 4 items and k = 2, what is the probability that a uniform sample without replacement is exactly {a, b}? What is the probability that item a is included?
Show answer
There are C(4, 2) = 6 possible pairs, so {a, b} has probability 1/6. Item a appears in 3 of the 6 pairs, so its inclusion probability is 3/6 = 1/2, which matches k/n = 2/4.
Exercise
For a uniform sample without replacement, what is the probability that two specific items x and y are both included? Use it to check the systematic sampler with n = 6, k = 2 on the pair {0, 1}.
Show answer
The pair is in C(n − 2, k − 2) of the C(n, k) sets, giving k(k − 1) / (n(n − 1)). For n = 6 and k = 2 that is 2/30 = 1/15. The systematic sampler gives {0, 1} probability 0, so it is not uniform.