kumulant

MeanStat

class MeanStat(val concurrency: Concurrency = Concurrency.None) : SeriesStat<WeightedMeanResult> (source)

Weighted arithmetic mean via Welford-style online update.

Numerically stable across wide dynamic ranges; merges two MeanStats using Chan's parallel algorithm.

Use cases: central-tendency monitoring on any scalar quantity. Compose with withValue / withWeight on the speccom.eignex.kumulant.schema.spec.Mean to derive other means (event rate, conditional mean, etc.), then materialize() the composed spec.

Weights: zero is a no-op. A negative weight is a downdate: it removes an observation previously folded in, inverting the update exactly, which is how a caller drives a sliding window by hand. The one rejected case is a downdate that would take the accumulated weight to zero or below, since every step divides by the new total and the accumulator would be left permanently non-finite; that throws IllegalArgumentException.

Memory: O(1); two doubles plus a lock.

Update: O(1) per observation.

Concurrency: Welford-coupled cells. Concurrency.Strict and Concurrency.HighWrite lock updates and reads. Concurrency.Relaxed uses atomic cells without a shared lock, so coupled state can drift under contention. Floating-point results depend on update order.

Constructors

MeanStat

constructor(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.

Functions

create

open override fun create(concurrency: Concurrency? = null): MeanStat(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: WeightedMeanResult, 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()): WeightedMeanResult(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.