kumulant

bandit

Multi-armed and contextual bandits built on the same Stat / Result foundation as the rest of the library.

Why bandits live here

A multi-armed bandit is the simplest reinforcement-learning shape: on each round you pick one of K actions ("arms"), receive a reward, and the only feedback you get is the reward for the arm you actually played. You never learn what would have happened for the arms you did not pick. The bandit's job is to balance exploitation (play the arm that looks best) against exploration (play a different arm to learn whether it might be better), so cumulative reward converges toward what an oracle who knew the best arm in advance would have collected.

In a contextual bandit each round also comes with a feature vector, and the reward depends on both the chosen arm and the context. Linear, nearest-neighbour, and tree-based contextual bandits all fit that shape and live in the contextual family.

Typical use cases:

  • A/B-test-style optimisation under a budget (picking which creative, variant, or arm to show next).

  • Online recommendation (which item to show this user given their feature vector).

  • Operator selection inside meta-heuristics (which neighbourhood move to try inside an LNS solver).

  • Any sequential decision problem where the data collection itself should adapt as evidence accumulates rather than running a fixed-size experiment.

Bandits fit kumulant naturally because they are themselves streaming problems. Observations arrive one at a time, the per-arm state has to stay bounded no matter how long the run goes, and the same evidence that drives choose is the evidence the rest of the library is already tracking. A bandit arm is a kumulant accumulator viewed through a scoring rule, so the same Welford means, exponential-weight cells, and regression posteriors that power streaming summaries also power Thompson sampling, UCB, EXP3, and LinUCB. Two replicas of a bandit can train in parallel and stitch their snapshots back together with merge, the same way two parallel mean estimators do.

Interface hierarchy

The action surface and the state surface are orthogonal, so each bandit family implements exactly the pieces that fit.

  • Bandit: common root: nbrArms, random, reset.

  • UnivariateBandit: choose() and update(arm, value, weight) for indexless arms.

  • ContextualBandit: choose(x) and update(arm, x, reward, weight) for per-round context vectors.

  • Snapshotable: snapshot, merge, create(random). State shape is whatever the bandit family needs.

  • PerArmBandit: convenience for the common case where state is one Result per arm; extends Snapshotable over a list of results and adds a per-arm armResult accessor.

  • Scorable: opt-in: exposes evaluate(armIndex) when selection is an argmax over independent per-arm scores. ContextualScorable is the contextual analogue.

Joint-sampling bandits (top-two Thompson) do not expose Scorable because no per-arm score is meaningful in isolation. Exp3 does not fit PerArmBandit because its snapshot stores joint sampling state. Each concrete bandit's KDoc states which interfaces it implements and why.

Subpackages

  • bandit.univariate: Indexless arms: epsilon-greedy / decreasing, UCB1 / KL-UCB / MOSS, Thompson, Boltzmann, EXP3, multi-armed shells, roulette-wheel selection.

  • bandit.contextual: Per-arm regression bandits over the com.eignex.kumulant.stat.regression family: linear (Bayesian, diagonal, stochastic), tree- and forest-based.

  • bandit.policy: Pluggable scoring policies (Greedy, EpsilonGreedy, UCB1, ThompsonSampling, KLUcb, MOSS, etc.) shared by univariate bandits.

Wire portability

A bandit's BanditSpec round-trips through the same skema-based mechanism as com.eignex.kumulant.schema.spec.StatSpec: declare a spec, encode to JSON / CBOR, ship, decode, materialise. The same data classes parameterise both Bandit construction in code and Bandit construction from the wire.

Types

Link copied to clipboard
interface Bandit

Shared configuration and reset contract for bandits.

Link copied to clipboard

Context-aware bandit: each round the caller observes a feature vector, uses it to choose an arm, plays the arm, observes a reward, and feeds the (context, reward) pair back to the bandit.

Link copied to clipboard

Contextual analog of Scorable: per-arm score under the current state and a supplied context vector. Implemented by com.eignex.kumulant.bandit.contextual.RegressionContextualBandit, whose selection rule decomposes into per-arm scores.

Link copied to clipboard

Bandit state represented as one Result per arm, in arm-index order. armResult allows inspection of one arm without requiring a full snapshot.

Link copied to clipboard
interface Scorable

Opt-in per-arm scoring for inspection / debugging / custom selectors. Bandits whose UnivariateBandit.choose is an argmax over independent per-arm scores expose this; UCB1, Thompson, epsilon-greedy, etc.

Link copied to clipboard
interface Snapshotable<S>

Snapshot, merge, and replica creation for bandits, independent of their input shape.

Link copied to clipboard
class TrackedContextualBandit<B : ContextualBandit>(val inner: B, val contextFeatureSize: Int, chooseTemplate: RegressionStat<out Result>? = null, updateJointTemplate: RegressionStat<out Result>? = null, updateMarginalTemplate: RegressionStat<out Result>? = null, updateArmRewardTemplate: PairedStat<out Result>? = null, nowNanos: () -> Long = { 0L }) : ContextualBandit

Observability wrapper around any ContextualBandit. Every event flows into a small set of aggregate side stats, each modelling a different question about the bandit's behaviour. Arm-level bucketing is a separate stratify op; until that lands, encode the arm into the observation (via the joint template) and read the slope/contrast off the resulting stat.

Link copied to clipboard
class TrackedUnivariateBandit<B : UnivariateBandit>(val inner: B, chooseTemplate: SeriesStat<out Result>? = null, updateArmRewardTemplate: PairedStat<out Result>? = null, nowNanos: () -> Long = { 0L }) : UnivariateBandit

Observability wrapper around any UnivariateBandit. Univariate has no context vector so two aggregate slots cover the surface:

Link copied to clipboard

Online optimizer over a fixed set of unindexed arms. Each round the caller:

Functions

Link copied to clipboard

Build a live BanditPolicy from its spec.

fun Exp3Spec.materialize(random: Random = Random.Default): Exp3Bandit

Build a live Exp3Bandit from its spec, resolving null eta / gamma to defaults.

Build a live RouletteWheelBandit from its spec.

fun <R : Result> RegressionContextualSpec<R>.materialize(random: Random = Random.Default, concurrency: Concurrency = Concurrency.None): RegressionContextualBandit<R>

Build a live RegressionContextualBandit from its composed regression specs.

fun MultiArmedSpec<*>.materialize(random: Random = Random.Default, concurrency: Concurrency = Concurrency.None): MultiArmedBandit<Result>

Build a live UnivariateBandit from its spec.

fun <R : Result> TopTwoThompsonSpec<R>.materialize(random: Random = Random.Default, concurrency: Concurrency = Concurrency.None): TopTwoThompsonBandit<R>

Build a live TopTwoThompsonBandit from its spec.

fun ContextualBanditSpec.materialize(random: Random = Random.Default, concurrency: Concurrency = Concurrency.None, nowNanos: () -> Long = { 0L }): Bandit

Dispatch any ContextualBanditSpec to its concrete bandit.

fun TrackedContextualSpec.materialize(random: Random = Random.Default, concurrency: Concurrency = Concurrency.None, nowNanos: () -> Long = { 0L }): TrackedContextualBandit<ContextualBandit>

Build a tracked contextual bandit; nowNanos supplies observation timestamps at runtime.

fun TrackedUnivariateSpec.materialize(random: Random = Random.Default, concurrency: Concurrency = Concurrency.None, nowNanos: () -> Long = { 0L }): TrackedUnivariateBandit<UnivariateBandit>

Build a tracked univariate bandit; nowNanos supplies observation timestamps at runtime.

fun UnivariateBanditSpec.materialize(random: Random = Random.Default, concurrency: Concurrency = Concurrency.None, nowNanos: () -> Long = { 0L }): Bandit

Dispatch any UnivariateBanditSpec to its concrete bandit.