---
title: Variance-Optimal Reservoir Sampling
url: https://www.emergentmind.com/topics/variance-optimal-reservoir-sampling
type: topic
---

# Variance-Optimal Reservoir Sampling

Variance-optimal reservoir sampling refers to an online sampling methodology that maintains a bounded-size sample (reservoir) from a stream of weighted items, such that subset sum estimates—over any subset, of any size—are unbiased and achieve minimal possible average variance given the sample size constraint. The technique achieves strict optimality in terms of average subset sum estimation variance for every possible query size, outperforming all other known schemes, and is efficient with respect to both per-item processing time and memory requirements [0803.0473].

## 1. Problem Context and Formal Definition

Given a data stream comprised of items indexed by $i = 1, ..., n$, each with a positive weight $w_i > 0$, the objective is to support approximate queries for subset sums: for any subset $A \subseteq \{1, ..., n\}$, estimate $W_A = \sum_{i \in A} w_i$ using a maintained reservoir $S$ of at most $k$ samples. Each sample in the reservoir carries an adjusted weight $\hat{w}_i$, so the estimator for any subset $A$ is $\hat{W}_A = \sum_{i \in A \cap S} \hat{w}_i$. The requirements are as follows:

- **Unbiasedness**: $E[\hat{W}_A] = W_A$ for any $A$.
- **Variance Minimization (Average Optimality)**: For any subset size $m$, the average variance over all subsets of size $m$ is minimized simultaneously for all $m$ and all possible weight assignments [0803.0473].
- **Worst-case control**: Single-subset, single-item, and total estimation variances are tightly bounded.
- **Efficient Online Implementation**: Space $O(k)$ and per-item time $O(\log k)$.

## 2. Core Reservoir Sampling Procedure (varoptₖ)

The variance-optimal reservoir sampling scheme, denoted as $\mathrm{varopt}_k$, incrementally constructs and maintains the optimal reservoir as follows [0803.0473]:

1. **Initialization**: For the first $k$ arrivals, add each with $\hat{w}_i = w_i$.
2. **Update on Arrival $j$ (for $n \geq k$):**
   - Form $T = S \cup \{j\}$, so $|T| = k+1$.
   - Compute the unique threshold $\tau > 0$ solving $\sum_{i \in T} \min\{1, \hat{w}_i / \tau\} = k$.
   - For each $i \in T$, set $p_i = \min\{1, \hat{w}_i/\tau\}$ and $q_i = 1 - p_i$.
   - Randomly pick $d \in T$ to drop: sample $r \in [0,1)$ uniformly and select $d$ so that $\sum_{i < d} q_i \leq r < \sum_{i \leq d} q_i$.
   - Resulting reservoir $S = T \setminus \{d\}$; for each $i \in S$, set $\hat{w}_i \leftarrow \max\{\hat{w}_i,\tau\}$.

In effect, this procedure exactly simulates a *k-out-of-(k+1)* variance-optimal sample, maintaining the unbiasedness and optimal average-variance properties.

### Implementation

Efficient implementation is achieved via two balanced binary trees over $S$:
- Tree $L$ for items with $\hat{w}_i > \tau$ (sorted by $\hat{w}_i$; supports subtree statistics).
- Tree $T$ for items with $\hat{w}_i = \tau$ (arbitrary order; supports split/concatenation).

All updates, threshold computations, and item removals are carried out in $O(\log k)$ worst-case per item.

## 3. Variance Optimality and Statistical Properties

The estimator $\hat{W}_A$ is unbiased for every $A$, as each inclusion probability satisfies Horvitz–Thompson conditions:
$$
E[\hat{w}_i \cdot 1_{i \in S}] = p_i \cdot (\hat{w}_i/p_i) = \hat{w}_i,\quad E[\hat{w}_i] = w_i
$$
(since $\hat{w}_i$ tracks the original $w_i$ at final inclusion).

For any fixed subset size $m\leq n$, let $V_m = E_{|A|=m}[\, \mathrm{Var}[\,\hat{W}_A] \,]$ denote the average user-facing query variance. An identity (ST07, Eq. 1 in [0803.0473]) expresses this as:
$$
V_m = \frac{m}{n}\left( \frac{n - m}{n-1} + \frac{m-1}{n-1} V_{\mathrm{full}} \right)
$$
where $V_{\mathrm{full}} = \mathrm{Var}[\hat{W}_{\{1, ..., n\}}]$ is the variance of the total weight estimator. $\mathrm{varopt}_k$ ensures $V_{\mathrm{full}} = 0$, and in addition minimizes the total individual variances $\sum_{i=1}^n \mathrm{Var}[\hat{w}_i]$ among all schemes with $k$ samples in expectation.

No other online or offline sampling scheme with $k$ samples (even one optimized post hoc for the particular data stream) achieves lower $V_m$ for any $m$, establishing strict simultaneous optimality [0803.0473].

## 4. Worst-Case, Tail Bounds, and Complexity

The $\mathrm{varopt}_k$ scheme has strong worst-case guarantees for individual items and subsets:

- For all $i$, $\mathrm{Var}[\hat{w}_i] \leq w_i \cdot W_{\mathrm{total}} / k$ where $W_{\mathrm{total}} = \sum w_i$; in particular, $\mathrm{Var}[\hat{w}_i] \leq (W_{\mathrm{total}}/2k)^2$ [0803.0473].
- For any subset $I$, $\mathrm{Var}[\hat{W}_I] \leq W_I \cdot W_{\mathrm{total}} / k$ and $\mathrm{Var}[\hat{W}_I] \leq |I| \cdot (W_{\mathrm{total}}/2k)^2$.

High-probability error control is achieved through Chernoff-type tail bounds that mirror those for independent Poisson sampling. For the sum of inclusion indicators $X_A$ over a subset $A$, and $\mu=E[X_A]=\sum_{i\in A} p_i$:
$$
\Pr[X_A \geq a] \leq \left( \frac{n-\mu}{n-a} \right)^{n-a} \left(\frac{\mu}{a}\right)^a \leq \exp(a-\mu) (\mu/a)^a
$$
The weighted estimator $\hat{W}_A = \tau X_A + \mathrm{const}$ thus satisfies standard multiplicative tail bounds [0803.0473].

Updates can be performed in $O(\log k)$ worst-case time per stream item, with $O(k)$ space for both the reservoir and supplemental structures. No general implementation achieves worst-case per-item time better than $\Omega(\log k/\log\log k)$ on the word-RAM model [0803.0473].

## 5. Comparison to Other Sampling Schemes

Variance-optimal reservoir sampling is compared to several canonical alternatives:

| Scheme                       | Average Variance | Online-friendly | Mergeable | Worst-case Guarantee |
|------------------------------|------------------|-----------------|-----------|---------------------|
| Uniform w/o replacement      | Arbitrarily bad (heavy tails) | Yes            | No        | Poor                |
| PPS with replacement         | Wastes samples (duplicates)   | Yes            | Yes       | Weak                |
| PPS w/o replacement          | No global optimum             | No             | No        | Moderate            |
| IPPS Poisson sampling        | Doubles large-$m$ variance    | Yes            | Yes       | Moderate            |
| Priority sampling            | Suboptimal for large $m$      | Yes            | Yes       | Good                |
| Chao/Tillé (offline)         | Optimum; $O(n)$–$O(n^2)$ time | No             | No        | Good                |
| $\mathrm{varopt}_k$          | Optimum for all $m$           | Yes            | Yes       | Best known          |

$\mathrm{varopt}_k$ strictly dominates all listed competitors in unbiasedness, variance minimization for every subset size, worst-case control, and support for efficient online and mergeable sampling.

## 6. Distributed Mergeability and Scalability

The mergeability property of $\mathrm{varopt}_k$ enables scalable, distributed monitoring and analytics. Given any set of disjoint streams $S_1, \ldots, S_m$, maintain separate reservoirs of size $k_x \geq k$, then merge the local reservoirs through a single $\mathrm{varopt}_k$ operation applied to the union. The resulting sample has the same distribution as if the scheme were run on the global union from scratch [0803.0473]:
$$
\mathrm{varopt}_k\left(\bigcup_x I_x\right) = \mathrm{varopt}_k\left(\bigcup_x \mathrm{varopt}_{k_x}\left(I_x\right)\right)
$$
This allows aggregation of independently-maintained samples in $O(\sum_x k_x + k \log k)$ time for data-parallel, distributed, or federated data settings.

## 7. Limitations, Assumptions, and Open Directions

- **Space/Time Complexity**: The reservoir requires $O(k)$ memory and $O(\log k)$ worst-case per update. A matching lower bound of $\Omega(\log k/\log\log k)$ rules out strict $O(1)$ worst-case updates in floating-point arithmetic.
- **Numerical Stability**: Accurate maintenance of sums and thresholds requires $O(\log n)$ bits of precision.
- **Assumptions for Merge Operations**: Standard mergeability assumes strict disjointness of item keys. Extensions to environments with key collisions (unaggregated keyed streams) require additional handling [0803.0473].
- **Open Problems**: Determining whether practical approximations can allow $O(\log\log k)$ or $O(1)$ worst-case update remains unresolved, though evidence suggests this is not feasible for exact implementations.

Variance-optimal reservoir sampling is the unique known scheme providing minimal average variance for subset sum estimation under constrained sample size, while remaining practical and adaptable for large-scale streaming and distributed systems [0803.0473].

Source: https://www.emergentmind.com/topics/variance-optimal-reservoir-sampling