kumulant

ReservoirHistogramStat

class ReservoirHistogramStat(val capacity: Int = StatDefaults.RESERVOIR_HISTOGRAM_CAPACITY, val seed: Long = Random.Default.nextLong(), val concurrency: Concurrency = Concurrency.None) : SeriesStat<ReservoirResult> (source)

Weighted reservoir sample of size capacity via Algorithm A-Res (Efraimidis & Spirakis): each item gets a key u^(1/w) and the top-k keys are retained, giving an unbiased weight-proportional sample.

Use cases: keeping a representative sample of a stream you intend to post-process; model training over a uniform/weighted subsample, ad-hoc percentile lookups via the sample distribution, scratch-pad for downstream analytics that need raw values rather than aggregates.

Memory: O(capacity) key/value slots and, outside relaxed mode, a priority index.

Update: O(1) to reject a key and O(log capacity) to replace one with the priority index. Equal minimum priorities or a merged NaN priority require O(capacity) scans to preserve slot selection. Relaxed mode scans per CAS attempt and can retry under contention.

Concurrency: Concurrency.Strict and Concurrency.HighWrite use an outer lock across updates, merges, reads, and resets. Concurrency.Relaxed uses CAS admission of each key/value pair and a separate RNG lock; reads are not an atomic snapshot across slots, and no sampling-error bound is guaranteed under contention. Concurrency.None runs without synchronisation.

Constructors

ReservoirHistogramStat

constructor(capacity: Int = StatDefaults.RESERVOIR_HISTOGRAM_CAPACITY, seed: Long = Random.Default.nextLong(), concurrency: Concurrency = Concurrency.None)(source)

Properties

capacity

Reservoir size (capacity of retained samples).

concurrency

open override val concurrency: Concurrency(source)

Thread-safety mode fixed at construction. See Concurrency for the shared contract and the implementation's KDoc for its cell and lock strategy.

seed

PRNG seed for reproducible reservoir admission.

Functions

create

open override fun create(concurrency: Concurrency? = null): ReservoirHistogramStat(source)

Derives the copy's seed rather than passing this stat's own.

A windowed reservoir builds one of these per slice, and slices sharing a seed would draw the same admission keys in the same order - so whether an observation survived would turn on its position within its slice rather than on its weight. The trees and forests derive for the same reason; advancing a counter here is what makes each copy distinct.

merge

open override fun merge(values: ReservoirResult, timestampNanos: Long = currentTimeNanos())(source)

Fold a compatible snapshot values into this accumulator at timestampNanos. Implementations document whether the combine is exact or approximate and which state the snapshot omits. The source accumulator is unaffected.

Time-dependent statistics, such as windows, place the snapshot at timestampNanos, which must share the clock the caller uses for updates and reads. Time-independent statistics ignore it.

read

open override fun read(timestampNanos: Long = currentTimeNanos()): ReservoirResult(source)

Return the current result evaluated at timestampNanos. Time-independent statistics ignore the timestamp. Reading does not add an observation.

Snapshot consistency depends on the implementation's synchronisation. Locks protect coupled state under Concurrency.Strict and Concurrency.HighWrite; independently updated cells and JVM striped adders can still yield a best-effort snapshot. Concurrency.Relaxed permits drift across coupled cells.

reset

open override fun reset()(source)

Restore the configured baseline, including any prior seed. Preserves the configuration and concurrency.

validateMerge

open override fun validateMerge(values: ReservoirResult)(source)

Validate values for merge without changing accumulator state. Containers validate every child before merging any of them. Successful validation checks compatibility; it does not make a concurrent merge transactional.

Link copied to clipboard
open fun update(value: Double, weight: Double = 1.0)

Record value with weight at the current monotonic time.

open override fun update(value: Double, timestampNanos: Long, weight: Double = 1.0)

Record value with weight at timestampNanos.

update

open override fun update(value: Double, timestampNanos: Long, weight: Double = 1.0)(source)

Record value with weight at timestampNanos.