VarianceStat
Weighted mean and variance via Welford with Chan-style parallel merge.
Population variance sst / totalWeights; use HasSampleVariance.sampleVariance on the result for the unbiased estimator.
Use cases: dispersion of any scalar quantity; the standard ingredient for control charts, anomaly thresholds, and bandit posteriors. Pairs with MomentsStat when skewness/kurtosis are also needed.
Weights: zero is a no-op; a negative weight downdates. See MeanStat for the shared contract. The sst recurrence inverts exactly: removing (x, w) from total W subtracts W * w * delta^2 / (W - w), which is algebraically the same quantity the forward step added.
Memory: O(1); three 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
VarianceStat
Properties
concurrency
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
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
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
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
Restore the configured baseline, including any prior seed. Preserves the configuration and concurrency.