←Back to Science Coding Hub/Part L/L7
L7Easy★ Core Essential · Industrial BedrockPart L · Classical ML & Statistical Simulation

Reservoir Sampling Algorithm

Industrial-grade implementation and mathematical foundations of Reservoir Sampling Algorithm.

⏱️ Time Complexity: O(N) 单遍流式扫描
💾 Space Complexity: O(k) 严格常驻内存
💡

Core Mental Anchor / Mnemonic

Master Reservoir Sampling Algorithm: enforce numerical stability, check tensor shapes, and eliminate redundant memory allocations.

📐 Mathematical Derivation & Core Formula

P(Item i in reservoir at step N)=ki×∏j=i+1N(1−1j)=kNP(\text{Item } i \text{ in reservoir at step } N) = \frac{k}{i} \times \prod_{j=i+1}^N \left(1 - \frac{1}{j}\right) = \frac{k}{N}
### Mathematical Derivation & Theoretical Principles
Detailed first-principles formulation and architectural mechanics for Reservoir Sampling Algorithm.

Refer to the LaTeX equation above for the core operator definition. The operator is designed to ensure strict numerical bounds, avoiding floating-point overflows and gradient anomalies.

🔄 Tensor Dimensions & Shape Flow

流式输入第 i 个元素 -> 随机整数 j 在 [0, i] -> 若 j < k 则 reservoir[j] = item

🛡️ Industrial Numerical Stability & Pitfalls

  • Ensure proper multi-dimensional tensor broadcasting and keepdims retention.
  • Enforce numerical guards (eps clamping and overflow thresholds) during exponentiation and division.
  • Verify train versus eval mode behavioral distinctions (e.g. frozen running statistics and dropout bypass).

💻 Industrial Code Implementation

import numpy as np

def reservoir_sample_stream(stream, k: int) -> list:
    """
    参数:
        stream: 可迭代的无限数据流发生器
        k: 期望等概率抽取的样本数量
    """
    reservoir = []
    
    for i, item in enumerate(stream):
        if i < k:
            # 前 k 个直接放入蓄水池
            reservoir.append(item)
        else:
            # 依概率 k / (i + 1) 决定是否录取
            j = np.random.randint(0, i + 1)
            if j < k:
                # 替换掉索引为 j 的旧样本
                reservoir[j] = item
                
    return reservoir

🧪 Runnable Assertions & Validation

Copy and run directly in Python / Jupyter to verify correctness:

import numpy as np
# 模拟 10000 个流数据抽取 5 个样本
stream = list(range(10000))
sample = reservoir_sample_stream(stream, k=5)
assert len(sample) == 5
assert len(set(sample)) == 5, "样本不应有重复"
print("✓ 蓄水池抽样算法自测通过")

🎯 Core Architecture Follow-up Q&A

Q1:What are the key trade-offs and memory bottlenecks when deploying Reservoir Sampling Algorithm in high-throughput inference?
Memory bandwidth (HBM to SRAM I/O) is the primary latency factor. Fusing element-wise operations and avoiding intermediate tensor materialization significantly outperforms naive implementations.
Q2:How does Reservoir Sampling Algorithm handle extreme numerical boundaries or precision reduction (FP16/BF16/INT8)?
Under low precision, operations must be upcasted to FP32 during accumulation to prevent underflow/overflow, followed by proper scaling and clamping before converting back to the target format.
←Prev: Welford's Algorithm for Online Mean & VarianceAll 69 KernelsNext: Moving Average (Sliding Window & Exponential)→