ArticleslgStudy

computer science

Reservoir sampling

Reservoir sampling is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Reservoir sampling rather than just read about it. In short: Reservoir sampling is a family of randomized online algorithms for choosing a simple random sample, without replacement, of k items from a population of unknown size n in a single pass over the items. The size of the population n is not known to the algorithm and is typically too large for all n items to fit into main memory.

Key takeaways

  • Reservoir sampling belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Reservoir sampling to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Reservoir sampling from memory before moving on to harder problems.

Reference excerpt

Reservoir sampling is a family of randomized online algorithms for choosing a simple random sample, without replacement, of k items from a population of unknown size n in a single pass over the items. The size of the population n is not known to the algorithm and is typically too large for all n items to fit into main memory. The population is revealed to the algorithm over time, and the algorithm cannot look back at previous items. At any point, the current state of the algorithm must permit extraction of a simple random sample without replacement of size k over the part of the population seen so far.

Example Consider the case when we want to maintain a sample of one item. A simple algorithm is to store the first item with probability 1 {\displaystyle 1} and the k {\displaystyle k} -th item with probability 1 k {\displaystyle {\frac {1}{k}}} . We want to show that the sample is uniformly distributed over all of the items that we have seen at each step since. The proof is by induction on the number of items n {\displaystyle n} . If there is 1 item we always choose it and we are done. If there are already n {\displaystyle n} items, then the probability of one of the items already in the store remaining the store becomes n n + 1 × 1 n = 1 n + 1 {\displaystyle {\frac {n}{n+1}}\times {\frac {1}{n}}={\frac {1}{n+1}}} so the probability of any of the n + 1 {\displaystyle n+1} items remaining in the sample is now 1 n + 1 . {\displaystyle {\frac {1}{n+1}}.} The proof of correctness for the general algorithm where we have an arbitrary number of slots is an extension of this argument.

History and Algorithm R Reservoir sampling was introduced as early as 1962 by Fan, Muller, and Rezucha, who described a sampling algorithm which did not need to know the population size n in advance. A simpler algorithm, known as Algorithm R, was discovered independently by Alan G. Waterman and McLeod & Bellhouse; it can also be viewed as a variant of the Fisher–Yates shuffle. Algorithm R works as follows.

Initialize an array R {\displaystyle R} indexed from 1 {\displaystyle 1} to k {\displaystyle k} , containing the first k items of the input x 1 , . . . , x k {\displaystyle x_{1},...,x_{k}} . This is the reservoir. For each new input x i {\displaystyle x_{i}} , generate a random number j uniformly in { 1 , . . . , i } {\displaystyle \{1,...,i\}} . If j ∈ { 1 , . . . , k } {\displaystyle j\in \{1,...,k\}} , then set R [ j ] := x i . {\displaystyle R[j]:=x_{i}.} Otherwise, discard x i {\displaystyle x_{i}} . Return R {\displaystyle R} after all inputs are processed.

Optimizing running time by skipping elements The Algorithm R described in the previous section has linear runtime, since it generates a random number for every element in the input stream to determine whether it should be included in the reservoir. It was observed by Jeffrey Vitter that it is possible to obtain faster algorithms with sublinear runtime when the algorithm is allowed to skip past sections of the input stream without examining them. Vitter gave a reservoir sampling algorithm which runs in time O ( k ( 1 + log ⁡ ( n / k ) ) {\displaystyle O(k(1+\log(n/k))} , and proved that this is optimal. Kim-Hung Li described a simple algorithm, Algorithm L, achieving the same asymptotic runtime, and which is described in this section. The starting point for Algorithm L is the observation that if n {\displaystyle n} random numbers u 1 , . . . , u n ∼ U [ 0 , 1 ] {\displaystyle u_{1},...,u_{n}\sim U[0,1]} are generated uniformly and independently, then the indices of the smallest k {\displaystyle k} of them form a uniform sample of the k {\displaystyle k} -subsets of { 1 , . . . , n } {\displaystyle \{1,...,n\}} .

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Reservoir sampling

Start with the simplest possible case. Write down what Reservoir sampling claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Reservoir sampling before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Reservoir sampling ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Reservoir sampling

In research
Reservoir sampling appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Reservoir sampling in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Reservoir sampling is common in secondary-school and first-year university syllabi. It links to neighbouring topics Algorithms, Analysis of algorithms, Randomized algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Reservoir sampling outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Reservoir sampling in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Reservoir sampling means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Reservoir sampling out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Reservoir sampling in simple terms?

Reservoir sampling is a family of randomized online algorithms for choosing a simple random sample, without replacement, of k items from a population of unknown size n in a single pass over the items. The size of the population n is not known to the algorithm and is typically too large for all n it…

Why does Reservoir sampling matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Reservoir sampling?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Reservoir sampling.

Tags

  • Algorithms
  • Analysis of algorithms
  • Randomized algorithms

Keep exploring