kumulant

DecayingVarianceStat

class DecayingVarianceStat(val weighting: DecayWeighting.HalfLife, val concurrency: Concurrency = Concurrency.None) : SeriesStat<DecayingVarianceResult> (source)

Exponentially decaying weighted variance over the recent time window.

Holds a Welford-style accumulator (W, mean, M2) whose effective weight and second-central-moment are decayed in lockstep with elapsed wall-clock time. Each update advances the landmark to the event timestamp, decays W and M2 by exp(-alpha*Deltat), then applies the standard increment M2 += w*delta*(value - meanNew). This avoids the catastrophic cancellation of the E[X^2] - E[X]^2 form when stdDev << |mean|.

Reads before the newest accepted event return the state at that event's timestamp. The snapshot carries that effective timestamp so merging it does not apply the same decay twice.

Use cases: recency-biased dispersion (rolling latency variance, recent variance for control charts). Reach for this over com.eignex.kumulant.stat.summary.VarianceStat when older observations should fade.

Memory: O(1); landmark + three doubles plus a lock.

Update: O(1) per observation; one exp() decay + Welford increment.

Concurrency: Body locked under any concurrent Concurrency level (no-op under Concurrency.None). The multi-cell decay-then-Welford transition cannot survive lock-free CAS. Exact under every level up to floating-point reorder ULPs.

Constructors

DecayingVarianceStat

constructor(weighting: DecayWeighting.HalfLife, concurrency: Concurrency = Concurrency.None)(source)
constructor(halfLife: Duration, concurrency: Concurrency = Concurrency.None)(source)

Properties

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.

halfLife

Wall-clock half-life of past contributions.

weighting

Time-decay schedule applied to past contributions.

Functions

create

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

Create an independent accumulator at the configured baseline. A null concurrency preserves this accumulator's mode; a non-null value overrides it. Modality subtypes narrow the return type to preserve the input shape.

merge

open override fun merge(values: DecayingVarianceResult, 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()): DecayingVarianceResult(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.

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.

Link copied to clipboard

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.

update

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

Record value with weight at timestampNanos.