kumulant

StochasticRegressionStat

class StochasticRegressionStat(val featureSize: Int, val optimizer: OptimizerSpec = StatDefaults.regressionOptimizer(), val biasOptimizer: OptimizerSpec = optimizer, val penalty: Penalty = StatDefaults.regressionPenalty, val link: Link = StatDefaults.regressionLink, val concurrency: Concurrency = Concurrency.None) : RegressionStat<StochasticRegressionResult> (source)

Online generalised linear regression by stochastic gradient descent on the canonical Link's negative log-likelihood plus optional Penalty. The cheapest of the multivariate regressors; point estimates only, no posterior, fast updates.

The per-coordinate update rule is owned by optimizer (Sgd / com.eignex.kumulant.schema.optimizer.Adam). The bias has its own biasOptimizer schedule because the intercept usually wants a different cadence than the coefficients.

Penalty.L1 and Penalty.L2 require optimizer (and biasOptimizer) to be Sgd; the lazy-update tricks they rely on (Bottou-style multiplicative scaling for L2; cumulative truncated gradient for L1) are SGD-specific. With a non-Sgd optimizer the penalty must be Penalty.None; folding L1/L2 into Adam-class updates is left for a future refactor.

Use cases: high-throughput online regression where point estimates suffice and the per-update cost must stay small. Reach for DiagonalRegressionStat when uncertainty is needed; for BayesianRegressionStat when the full posterior is needed.

Memory: O(featureSize); weights vector, bias, plus optimizer aux state.

Update: O(nnz(x)) per observation under Penalty.None; the L1/L2 paths add lazy-update bookkeeping with the same asymptotic cost.

Concurrency: Welford-coupled per-slot atomic under Concurrency.Relaxed (HOGWILD-style asynchronous SGD), serialised under Concurrency.Strict / Concurrency.HighWrite.

Samples

val sgd = StochasticRegressionStat(featureSize = 3)
sgd.update(doubleArrayOf(0.5, 1.0, -0.2), y = 1.5)
val r = sgd.read()
println(r.weights)

Constructors

StochasticRegressionStat

constructor(featureSize: Int, optimizer: OptimizerSpec = StatDefaults.regressionOptimizer(), biasOptimizer: OptimizerSpec = optimizer, penalty: Penalty = StatDefaults.regressionPenalty, link: Link = StatDefaults.regressionLink, concurrency: Concurrency = Concurrency.None)(source)

Properties

bias

Live view of the running intercept.

biasOptimizer

Update rule for the bias scalar. Defaults to optimizer.

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.

featureSize

open override val featureSize: Int(source)

Number of features expected in x on each update. Mismatched lengths throw.

optimizer

Per-coordinate update rule for the weight vector.

penalty

Regularisation applied during the gradient step; requires Sgd optimizers.

sse

Live view of the accumulated per-link loss.

step

Live view of the per-observation step counter.

totalWeights

Live view of the cumulative observation weight folded in.

Functions

create

open override fun create(concurrency: Concurrency? = null): StochasticRegressionStat(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: StochasticRegressionResult, timestampNanos: Long = currentTimeNanos())(source)

Sample-weighted blend of weights and bias. SGD has no second-moment information, so this is an approximation; for principled merges use BayesianRegressionStat. Step counts add, and the learning-rate schedules continue from the combined step; adaptive optimizer accumulators are not in the snapshot and stay local.

read

open override fun read(timestampNanos: Long = currentTimeNanos()): StochasticRegressionResult(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.

validateMerge

open override fun validateMerge(values: StochasticRegressionResult)(source)

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.

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

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

open fun update(x: DoubleArray, y: Double, weight: Double = 1.0)

Convenience overload that wraps x as a DenseVector.

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

Timestamped convenience overload that wraps x as a DenseVector.

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

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

update

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

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