﻿# Reservoir sampling: select a finite sample from an unknown-length stream

Understand reservoir replacement probabilities, observed-stream boundaries and reproducible selection for large AI dataset inspections.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/data-guide-reservoir-sampling-stream-snapshot/
Language: en

## The population size can be unknown initially

Reservoir sampling selects a bounded sample while processing records sequentially. The classic work by Vitter studies sampling without replacement when the total population size is unknown beforehand. This differs from first loading every example and then choosing array positions.

For inspecting a large language corpus, a reservoir can retain a manageable set of examples without storing the entire corpus in memory. Its usefulness depends on a clear definition of which incoming records count as members of the population.

## Replacement probability changes with progress

In the basic Algorithm R, the reservoir initially holds the first sample-sized group. Each later record receives an inclusion opportunity that decreases as the number of processed records grows. If selected, it replaces a uniformly chosen reservoir member. This maintains a random sample of the observed prefix under the algorithm's assumptions.

Keeping every later record with a fixed replacement probability implements a different policy. It can overweight recent arrivals. Likewise, selecting a replacement slot through a biased random rule can violate the intended uniformity even though the code still retains a constant number of examples.

## The stopping point defines what was sampled

If processing stops early, the reservoir represents the processed portion, not the remaining unobserved stream. Record an artifact version or a deliberate cutoff along with the number of accepted records. A network timeout should not silently become an undocumented sampling boundary.

Specify whether filtering occurs before sampling. Sampling accepted documents answers a different question from sampling raw documents and later discarding invalid ones. The latter can produce a smaller retained set and a different account of what the selection probabilities refer to.

## A seed is not the entire experiment

Python's random module exposes state capture and restoration, while array-oriented tools such as NumPy choice provide selection for known input populations. Choose an implementation suitable for the access pattern. An in-memory selection API is not automatically a reservoir algorithm for an arbitrary iterator.

Preserve input order, random-generator identity and the implementation revision. The same initial seed can produce a different selected set when preceding random calls or record order change. Retaining the selected example identifiers makes the actual inspection sample independently visible.

## Check the unit of equal probability

A uniform record sample is not automatically uniform over tokens, sources or authors. A long document and a short document each occupy a record position under that policy. Duplicate records also receive separate opportunities unless the population definition removes them first.

Use the reservoir for a stated inspection goal, then review the selected records against that goal. Report the observed population and selection rule with the sample. This is a methodological use of the algorithm, not a claim that a small random subset guarantees complete coverage of rare dataset problems.

## Primary references

[Vitter: Random Sampling with a Reservoir](https://www.cs.umd.edu/~samir/498/vitter.pdf).

[Python random](https://docs.python.org/3/library/random.html).

[NumPy Generator choice](https://numpy.org/doc/stable/reference/random/generated/numpy.random.Generator.choice.html).

Related explanations: [Exposure and repeated examples](/journal/training-deduplication-memorization/); [Independent evaluation splits](/journal/document-level-data-split-leakage/).
