Reservoir sampling
How to keep a fair random sample from a stream when you cannot store it and do not know how long it is. Five items, a reservoir of two, and the arithmetic of why it is fair.
In 30 seconds
Reservoir sampling picks a random sample from a stream of unknown length in one pass, holding only the sample itself in memory.
The first k items fill the reservoir. After that, item number i rolls a number from 1 to i; if the roll is k or less, it replaces the item in that slot, otherwise it is skipped.
Later items get in less often, but earlier items have had more chances to be pushed out. The two effects cancel exactly, so after n items every one of them is in the sample with the same chance, k in n.
Reservoir sampling, step by step
This is the basic method, known as Algorithm R. It needs a roll for every item; faster variants work out how many items to skip before the next replacement. The rolls must be uniformly random.
- Step 1: A stream with no known end. Items arrive one at a time and nobody knows how many there will be. We want two of them, chosen so that every item has the same chance. We cannot store them all and pick later, and we cannot pick positions in advance, because the total is unknown.
- Step 2: Fill the reservoir. The reservoir is the small set of items held at any moment. The first two items go straight in, because with only two items seen, the fair sample of two is both of them.
- Step 3: Item 3: roll for a place. From now on every new item rolls for a place. Item number 3 rolls a number from 1 to 3. A roll of 1 or 2 means it takes that slot and the old occupant is let go; a 3 means it is skipped. So C gets in with chance 2 in 3. It rolls 1 and replaces A.
- Step 4: Item 4: usually skipped. Item number 4 rolls from 1 to 4, so its chance of getting in drops to 2 in 4. This time the roll is 4 and D is skipped. The later an item arrives, the less likely it is to get in, which is exactly what balances the head start of the earlier items.
- Step 5: Item 5, and the stream ends. Item number 5 rolls from 1 to 5 and gets 2, so E takes slot 2 and B is let go. Then the stream stops. Whatever is in the reservoir at that moment is the answer: C and E. Only two items were ever held, and each item was looked at once.
- Step 6: Why every item gets 2 in 5. An item is in the final sample if it got in and was never pushed out. When item number i arrives, a given slot is hit with chance 1 in i, so its occupant stays with chance (i − 1) in i. Multiply along each row and the fractions cancel: early or late, every item ends with the same chance, 2 in 5.
- Step 7: Fair at every moment. The same rule works for any reservoir size k and any length. Item number i gets in with chance k in i, pushing out a random occupant, and after n items each one is in the sample with chance k in n. Stop at any moment and the reservoir is a fair sample of everything seen so far.
Try it yourself
Learn this first
Nothing. This concept starts from scratch.
Spotted a mistake on this page?