Skip to content

Commit

Permalink
accumulators.py: add class ReservoirSampling
Browse files Browse the repository at this point in the history
Samples a random collection from a series of elements.
  • Loading branch information
JLeutloff committed Oct 19, 2024
1 parent 163a57e commit 206e817
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions generatorpipeline/accumulators.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import abc
import numpy as np
from collections import deque
import random
import time
import heapq

Expand Down Expand Up @@ -472,6 +473,39 @@ def n(self):
return self._n


class ReservoirSampling(Accumulator):
"""
Uses the Algorithm R which chooses k items from a simple random sample of unknown size n,
without replacement, in a single pass over the items.
length: k
see also: https://en.wikipedia.org/wiki/Reservoir_sampling
"""

def __init__(self, length=10):
self._n = 0
self.length = length
self._reservoir = []

def _accumulate_obj(self, obj):
self._n += 1
# Filling of Reservoir until length is reached
if self._n <= self.length:
self._reservoir.append(obj)
else:
j = random.randint(1, self._n)
if j <= self.length:
self._reservoir[j-1] = obj

@property
def value(self):
return list(self._reservoir)

@property
def n(self):
return self._n


class CDFEstimator(Accumulator):
'''
Estimates the Cumulative Distribution Function (CDF).
Expand Down

0 comments on commit 206e817

Please sign in to comment.