kumulant

UnivariateRegressionStat

class UnivariateRegressionStat(val penalty: Penalty = StatDefaults.regressionPenalty, val concurrency: Concurrency = Concurrency.None) : PairedStat<UnivariateRegressionResult> (source)

Online univariate linear regression backed by Chan's parallel Welford accumulator on (x, y). A single hot path drives every Penalty: accumulation is identical; the penalty's closed-form projection is applied only at read.

  • Penalty.None: slope = sxy / sxx (ordinary least squares).

  • Penalty.L1: soft-thresholded slope = sign(sxy) * max(0, |sxy| - lambda * w) / sxx (Lasso).

  • Penalty.L2: slope = sxy / (sxx + lambda * w) (Ridge).

intercept = meanY - slope * meanX in every case. Merging consumes a result type carrying the raw UnivariateRegressionResult.sxy, so the round trip is exact for every penalty including the L1 case where the regularised slope can be zero.

Use cases: single-feature streaming regression; calibration of a scalar predictor, dose-response curves, anything where y ~ slope·x + intercept covers it. For multi-feature regression reach for DiagonalRegressionStat (factorised posterior) or BayesianRegressionStat (full posterior).

Memory: O(1); six 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

UnivariateRegressionStat

constructor(penalty: Penalty = StatDefaults.regressionPenalty, 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.

meanX

Live view of the running mean of x.

meanY

Live view of the running mean of y.

penalty

Regularisation applied at read() time; defaults to plain OLS.

totalWeights

Live view of the cumulative observation weight.

Functions

create

open override fun create(concurrency: Concurrency? = null): UnivariateRegressionStat(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: UnivariateRegressionResult, 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()): UnivariateRegressionResult(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(x: Double, y: Double, weight: Double = 1.0)

Record the pair (x, y) with weight at the current monotonic time.

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

Record the pair (x, y) 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(x: Double, y: Double, timestampNanos: Long, weight: Double = 1.0)(source)

Record the pair (x, y) with weight at timestampNanos.