kumulant

SoftmaxRegressionStat

class SoftmaxRegressionStat(val featureSize: Int, val numClasses: Int, val optimizer: OptimizerSpec = StatDefaults.regressionOptimizer(), val biasOptimizer: OptimizerSpec = optimizer, val concurrency: Concurrency = Concurrency.None) : RegressionStat<SoftmaxRegressionResult> (source)

Online multinomial logistic regression by stochastic gradient descent on the softmax cross-entropy loss. Generalises com.eignex.kumulant.stat.regression.glm.StochasticRegressionStat with Link.Logit from binary to K-way classification.

Update step per observation (true class c = y.toInt()):

p[k]      = softmax(W[k] . x + b[k])
grad[k]i  = (p[k] - 1[k == c]) * x[i]    // per-coordinate gradient
W[k]i    += optimizer.computeDelta(k, i, grad[k]i, weight)
b[k]     += biasOptimizer.computeDelta(k, p[k] - 1[k == c], weight)

One OptimizerSpec is materialised per class for the weight matrix; bias is a single optimizer over numClasses slots.

Use cases: K-way online classification with calibrated probabilities; the discriminative counterpart to GaussianNaiveBayesStat.

Memory: O(numClasses * featureSize) for weights + per-optimizer aux state, plus a pooled length-numClasses logit buffer held for the life of the stat under every level but Concurrency.Relaxed.

Update: O(numClasses * nnz(x)) per observation.

Concurrency: Welford-locked; the optimizer aux state honours the same Concurrency passed in. The per-observation logit scratch is owned per stat and guarded by that lock, so a Concurrency.None stat updated from two threads can fail inside the linear-algebra layer rather than only drift. Concurrency.Relaxed holds no scratch and allocates per update, because its lock is a no-op and racing writers there must drift rather than throw.

Constructors

SoftmaxRegressionStat

constructor(featureSize: Int, numClasses: Int, optimizer: OptimizerSpec = StatDefaults.regressionOptimizer(), biasOptimizer: OptimizerSpec = optimizer, concurrency: Concurrency = Concurrency.None)(source)

Properties

biasOptimizer

Bias optimizer, materialised once over numClasses slots. 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.

crossEntropy

Live view of the accumulated weighted cross-entropy.

featureSize

open override val featureSize: Int(source)

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

numClasses

Number of classes; the input y must round to [0, numClasses).

optimizer

Per-class weight-matrix optimizer; one instance is materialised per class.

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): SoftmaxRegressionStat(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: SoftmaxRegressionResult, 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()): SoftmaxRegressionResult(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: SoftmaxRegressionResult)(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.