kumulant

stat.decay

Exponentially weighted sums and moments over a stream. Each member implements com.eignex.kumulant.core.SeriesStat. Timestamp-based stats discount observations by elapsed time; EWMA stats discount history by cumulative observation weight.

Picking an estimator

StatClockReach for it when
DecayingSumStatEvent timestampsAccumulate a total whose contributions fade with age, or read a time-decayed rate.
DecayingMeanStatEvent timestampsEstimate a recent weighted mean when observation intervals vary.
DecayingVarianceStatEvent timestampsTrack recent dispersion with a coupled mean and variance.
EwmaMeanStatObservation weightSmooth a signal according to how much evidence arrives, independent of elapsed time.
EwmaVarianceStatObservation weightTrack dispersion using the same weight-based smoothing clock.

Both families estimate the same exponentially weighted moments and differ only in the clock. With unit weights, a bias-corrected EWMA equals a ratio of decayed sums on an observation-count clock. They stay separate classes because the implementations do not share a cost: the timestamp stats carry landmark rotation and underflow recovery, while EWMA is a two-cell update cheap enough for per-event hot paths.

Timestamp-based decay

DecayWeighting.HalfLife takes a Duration and sets alpha = ln(2) / halfLifeNanos. A contribution stamped at t has weight exp(-alpha * (readTime - t)) at a later read. The stats also accept the half-life directly through their halfLife: Duration constructors.

DecayingSumStat rotates its numerical landmark after long elapsed intervals. DecayingMeanStat composes two such sums for the weighted numerator and denominator. DecayingVarianceStat decays its weight and second central moment together before applying a weighted Welford update.

Weight-based EWMA

DecayWeighting.Alpha specifies a rate per unit of observation weight. An observation of weight w uses the smoothing fraction a(w) = 1 - exp(-alpha * w). With unit weights, history is discounted once per observation; larger weights advance the smoothing clock further. Reads correct for the initial zero seed using cumulative weight.

EWMA does not use timestamp spacing. Choose the timestamp-based stats when elapsed time should determine how quickly evidence fades.

Result shapes

ResultProduced byFields
DecayingSumResultDecayingSumStatSum, snapshot timestamp, and half-life; derives the decayed rate.
DecayingMeanResultDecayingMeanStatMean, decayed total weight, and snapshot timestamp.
DecayingVarianceResultDecayingVarianceStatMean, variance, decayed total weight, and snapshot timestamp; derives stdDev.
com.eignex.kumulant.stat.summary.WeightedMeanResultEwmaMeanStatBias-corrected mean and cumulative observation weight.
com.eignex.kumulant.stat.summary.WeightedVarianceResultEwmaVarianceStatBias-corrected mean and variance, plus cumulative observation weight.

Timestamp-based total weights fade with elapsed time. EWMA total weights count accepted observation weight and supply the bias correction. The dedicated timestamp-based results do not implement the summary family's center/scale traits.

Merge

All members support merging. With matching half-lives, timestamp-based sums and moments can align evidence to a common timestamp before combining it. Mean and variance snapshots retain their effective weights for this combine.

EWMA merges blend already-smoothed summaries using their bias-correction weights. The result approximates a pooled summary; it does not reproduce a single chronological replay, and grouping repeated merges can change the result.

Decay schedules are configuration and are absent from these result types. Matching half-lives or alpha rates is the caller's responsibility.

Concurrency

DecayingSumStat serializes updates, reads, merges, resets, and landmark changes under every concurrent mode. DecayingMeanStat inherits that protection for each of its two sums, but the pair is updated and read separately: a concurrent reader can observe a partial numerator/denominator pair. DecayingVarianceStat locks its whole coupled state under every concurrent mode.

EwmaMeanStat and EwmaVarianceStat lock their coupled state under com.eignex.kumulant.core.Concurrency.Strict and com.eignex.kumulant.core.Concurrency.HighWrite. com.eignex.kumulant.core.Concurrency.Relaxed uses independent atomic cells without a body lock, so concurrent results can drift. EWMA also depends on update order. com.eignex.kumulant.core.Concurrency.None provides no synchronization.

Types

Link copied to clipboard
@Serializable
@SerialName(value = "DecayingMeanResult")
data class DecayingMeanResult(val mean: Double, val totalWeights: Double, val timestampNanos: Long) : HasObservationCount

Snapshot of an exponentially time-decayed weighted mean at timestampNanos.

Link copied to clipboard
class DecayingMeanStat(val weighting: DecayWeighting.HalfLife, val concurrency: Concurrency = Concurrency.None) : SeriesStat<DecayingMeanResult>

Exponentially decaying weighted mean: Sum(v_i*w_i*decay) / Sum(w_i*decay).

Link copied to clipboard
@Serializable
@SerialName(value = "DecayingSumResult")
data class DecayingSumResult(val sum: Double, val timestampNanos: Long, val halfLifeNanos: Long) : HasRate

Snapshot of an exponentially time-decayed sum at timestampNanos.

Link copied to clipboard
class DecayingSumStat(val weighting: DecayWeighting.HalfLife, val concurrency: Concurrency = Concurrency.None) : SeriesStat<DecayingSumResult>

Exponentially decaying sum driven by wall-clock elapsed time.

Link copied to clipboard
@Serializable
@SerialName(value = "DecayingVarianceResult")
data class DecayingVarianceResult(val mean: Double, val variance: Double, val totalWeights: Double, val timestampNanos: Long) : HasObservationCount

Snapshot of an exponentially time-decayed weighted variance at timestampNanos.

Link copied to clipboard
class DecayingVarianceStat(val weighting: DecayWeighting.HalfLife, val concurrency: Concurrency = Concurrency.None) : SeriesStat<DecayingVarianceResult>

Exponentially decaying weighted variance over the recent time window.

Link copied to clipboard
sealed interface DecayWeighting

Shared decay strategy for exponentially weighted stats.

Link copied to clipboard
class EwmaMeanStat(val weighting: DecayWeighting.Alpha, val concurrency: Concurrency = Concurrency.None) : SeriesStat<WeightedMeanResult>

Exponentially weighted moving average driven by cumulative observation weight.

Link copied to clipboard
class EwmaVarianceStat(val weighting: DecayWeighting.Alpha, val concurrency: Concurrency = Concurrency.None) : SeriesStat<WeightedVarianceResult>

Exponentially weighted moving variance driven by cumulative observation weight.