kumulant

UnivariateRegressionStat

class UnivariateRegressionStat(val penalty: Penalty = Penalty.None, 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 the body; exact match to a serial run up to floating-point reorder ULPs. Concurrency.Relaxed drops the lock; the six cells race and coefficients drift ~1e-5 relative under contention but never throw.

Constructors

Link copied to clipboard
constructor(penalty: Penalty = Penalty.None, concurrency: Concurrency = Concurrency.None)

Properties

Link copied to clipboard
open override val concurrency: Concurrency

The thread-safety contract this stat was constructed with. Each stat picks the cell-encoding and lock strategy that honours this contract for its mathematical structure:

Link copied to clipboard

Live view of the running mean of x.

Link copied to clipboard

Live view of the running mean of y.

Link copied to clipboard

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

Link copied to clipboard

Live view of the cumulative observation weight.

Functions

Link copied to clipboard
open override fun create(concurrency: Concurrency? = null): UnivariateRegressionStat

Spawn a fresh accumulator with the same configuration. Optionally override the Concurrency; useful for materialising a wire spec at a different concurrency level than the source.

Link copied to clipboard
open override fun merge(values: UnivariateRegressionResult)

Fold another accumulator's snapshot into this one. The unit of merge is the immutable Result; not a live Stat; which is what lets the merge cross a process boundary. Many workers track slices of the same stream, call read periodically, ship snapshots to a coordinator, and the coordinator merges them in.

Link copied to clipboard
open override fun read(timestampNanos: Long = currentTimeNanos()): UnivariateRegressionResult

Materialise the current state as an immutable Result. Reads never mutate, so the caller can read as often as it likes without affecting the stream.

Link copied to clipboard
open override fun reset()

Reset the stat to its prior-seeded baseline. Equivalent to constructing a fresh stat with the same configuration, but in place; keeps the same Concurrency and any per-stat tunables.

Link copied to clipboard
open fun update(x: Double, y: Double, weight: Double = 1.0)

Record an (x, y) observation with the given weight at the current time.

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

Record an (x, y) observation at timestampNanos with the given weight.

UnivariateRegressionStat

constructor(penalty: Penalty = Penalty.None, concurrency: Concurrency = Concurrency.None)(source)

concurrency

open override val concurrency: Concurrency(source)

The thread-safety contract this stat was constructed with. Each stat picks the cell-encoding and lock strategy that honours this contract for its mathematical structure:

Picked at construction; immutable after.

create

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

Spawn a fresh accumulator with the same configuration. Optionally override the Concurrency; useful for materialising a wire spec at a different concurrency level than the source.

The returned stat is independent: its state starts at the configured baseline, not at the source's current state. Each modality subtype narrows the return type so chaining doesn't lose the modality.

meanX

Live view of the running mean of x.

meanY

Live view of the running mean of y.

merge

open override fun merge(values: UnivariateRegressionResult)(source)

Fold another accumulator's snapshot into this one. The unit of merge is the immutable Result; not a live Stat; which is what lets the merge cross a process boundary. Many workers track slices of the same stream, call read periodically, ship snapshots to a coordinator, and the coordinator merges them in.

Most stat families implement merge exactly (Chan-style parallel formulas for Welford, cell-wise additions for histograms, cell-wise max for HLL). SGD-based regressors merge approximately; they have no second-moment information for the principled combine. Each stat's KDoc documents its merge semantics.

penalty

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

read

open override fun read(timestampNanos: Long = currentTimeNanos()): UnivariateRegressionResult(source)

Materialise the current state as an immutable Result. Reads never mutate, so the caller can read as often as it likes without affecting the stream.

Snapshot consistency depends on the configured Concurrency. Under Concurrency.Strict / Concurrency.HighWrite a read locks against writers so coupled cells stay consistent. Under Concurrency.Relaxed the cells race and the snapshot may drift by ULPs of the workload under heavy contention; the drift is bounded and the read never throws.

timestampNanos is the read timestamp. Stats that don't care about time silently drop it; stats that do (rates, decay families, recency, windowed wrappers) use it as the ordering signal.

reset

open override fun reset()(source)

Reset the stat to its prior-seeded baseline. Equivalent to constructing a fresh stat with the same configuration, but in place; keeps the same Concurrency and any per-stat tunables.

totalWeights

Live view of the cumulative observation weight.

update

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

Record an (x, y) observation at timestampNanos with the given weight.