Skip to content
Rainbow docs menu

Reference · The Applied Higher-Order Functions catalogue

rainbow.algorithms.proofs.ahof.reservoir-sampling

The Applied Higher-Order Functions cataloguesrc/rainbow/algorithms/proofs/ahof/reservoir_sampling.clj1 definitions, 3 theorems

AHoF Reservoir Sampling (Algorithm R): draw a uniform random sample of fixed size k from a STREAM of unknown, possibly huge length in a SINGLE PASS and O(k) memory -- used for log sampling, streaming analytics, and dataset subsampling. The reservoir holds the first k items; for each later item i (1-indexed) a random slot j in [1, i] is drawn, and the item REPLACES reservoir slot j exactly when the slot falls within the reservoir: replaces? slot k = decide (slot <= k) which happens with probability k/i, keeping every seen item equally likely. Proved native and axiom-free: the replacement test; that a slot within the reservoir triggers a replacement; the certificate that a replacing slot really is within the reservoir (native nnrat/at-most) -- so uniformity is preserved; and that a slot beyond the reservoir discards the item. The RNG and the reservoir array are executable; what is proved is the replacement decision. Requires nnrat/at-most, audit/decide-nnrat-at-most, nnrat/decide-at-most-true-sound (all in the D63 checkpoint). Scope (measured by the kernel): this module has 3 worked instances (statements about specific values). None of its theorems is a guarantee about the algorithm for all inputs; `lein verify rainbow.algorithms.proofs.ahof.reservoir-sampling` prints this classification.

reservoir-sampling/replaces?

DefinitionSource, line 59

reservoir-sampling/replaces? :
  (x₁ x₂ : ℚ≥0)
  → data/Bool

ℚ≥0 = nnrat/NonnegativeRational

reservoir-sampling/slot-in-reservoir-replaces

TheoremWorked exampleNo axiomsSource, line 70

reservoir-sampling/slot-in-reservoir-replaces :
  → lis/extends? 2 3 = data/true

= = equality/Eq

reservoir-sampling/replacement-within-reservoir

TheoremWorked exampleNo axiomsSource, line 78

reservoir-sampling/replacement-within-reservoir :
  → 2 ≤ 3

≤ = nnrat/at-most

reservoir-sampling/slot-beyond-reservoir-discards

TheoremWorked exampleNo axiomsSource, line 84

reservoir-sampling/slot-beyond-reservoir-discards :
  → lis/extends? 5 3 = data/false

= = equality/Eq