kumulant

TopTwoThompsonBandit

class TopTwoThompsonBandit<R : Result>(val nbrArms: Int, val policy: ThompsonSampling<R>, val beta: Double = 0.5, val maxResamples: Int = 32, val random: Random = Random.Default, val concurrency: Concurrency = Concurrency.None) : UnivariateBandit, PerArmBandit<R> (source)

Top-Two Thompson Sampling (Russo 2020); pure-exploration variant of Thompson sampling for best-arm identification: sample every arm's posterior, take the argmax arm1, play it with probability beta, or else resample until the argmax differs from arm1 and play that runner-up.

The forced resampling keeps a fraction of the budget on the runner-up so the gap to the best arm is identified asymptotically optimally. Converges to the optimal exploration fraction beta = 0.5 for two-armed problems; tune lower to bias toward exploitation when running in the regret-minimisation regime.

Arm selection uses joint sampling and conditional resampling. Per-arm posterior state is available through PerArmBandit.

Use cases: best-arm identification and pure-exploration problems where shrinking the regret gap matters more than minimising cumulative regret; any posterior expressible as a ThompsonSampling policy.

Arms: indexless, nbrArms ≥ 2 fixed at construction; each arm owns one posterior cell from policy.createArm().

Memory: O(nbrArms · arm-state); one inner MultiArmedBandit over policy.

Choose: O(nbrArms) expected; O(maxResamples · nbrArms) worst case when the second-arm resample loop spins to the cap.

Update: O(1) on the targeted arm, delegated to policy.update.

Randomness: every posterior sample and the beta coin flip use the caller-supplied random; reproducible under a fixed seed if the policy is.

Concurrency: per-arm com.eignex.kumulant.core.SeriesStats are built with concurrency, as in MultiArmedBandit. Cross-arm snapshot consistency during choose is best-effort under racing updates.

Constructors

TopTwoThompsonBandit

constructor(nbrArms: Int, policy: ThompsonSampling<R>, beta: Double = 0.5, maxResamples: Int = 32, random: Random = Random.Default, concurrency: Concurrency = Concurrency.None)(source)

Properties

beta

Probability of playing the round's top sample; default 0.5 is Russo's recipe.

concurrency

Thread-safety mode of every per-arm accumulator.

maxResamples

Cap on the resample loop when searching for a different second arm.

nbrArms

open override val nbrArms: Int(source)

Number of arms in the population. Fixed at construction; arm indices are [0, nbrArms).

policy

Per-arm posterior + arm spec.

random

open override val random: Random(source)

Single source of randomness for UnivariateBandit.choose / ContextualBandit.choose and any policy-internal sampling. Callers pass a Random(seed) at construction for reproducible exploration; the bandit threads the same instance through every randomised decision.

Functions

armResult

open override fun armResult(armIndex: Int): R(source)

Per-arm snapshot at armIndex. Default implementation reads from the full snapshot; implementations may override to avoid building the entire list when only one arm is needed.

choose

open override fun choose(): Int(source)

Pick an arm to play next. Uses Bandit.random for any sampling. The returned index is in [0, nbrArms). Repeated calls without intervening updates may return different arms (for randomised selection) or the same arm (for argmax-style policies once the leading arm is well-separated).

create

open override fun create(random: Random): TopTwoThompsonBandit<R>(source)

Spawn a fresh bandit with the same configuration; state resets to the prior seed. The random source is replaced; pass the source you want the new bandit to use for exploration (which is independent of merging in another snapshot's state).

Useful when a worker accepts a stream of snapshots to apply sequentially: create(random).also { it.merge(snapshot) }.

merge

open override fun merge(other: List<R>)(source)

Fold another replica's other state into this bandit. Most families merge exactly via the underlying stat's parallel-merge formula; SGD- based contextual bandits merge approximately. Each concrete bandit's KDoc documents its merge semantics.

reset

open override fun reset()(source)

Clear all state back to the prior-seeded baseline. Equivalent to spawning a fresh bandit with the same configuration via Snapshotable.create, but in place; keeps the same arm count, policy, concurrency mode, and random instance.

snapshot

open override fun snapshot(): List<R>(source)

Return the current state without adding observations or choosing an arm. Consistency follows the implementation's concurrency contract; a snapshot of independently updated cells need not represent a single instant.

update

open override fun update(armIndex: Int, value: Double, weight: Double = 1.0)(source)

Record reward value for armIndex with weight. Arm indices must be in [0, nbrArms). Weight and reward handling follow the implementation's accumulator or policy; see com.eignex.kumulant.core.Stat for the shared accumulator weight rules.

Link copied to clipboard
open fun updateAll(armIndices: IntArray, values: DoubleArray, weights: DoubleArray? = null)

Record each arm/reward pair in order by calling update. armIndices and values must have equal sizes. Non-null weights must have the same size; null applies 1.0 to every observation. Size checks run before any update, but a later update failure can leave earlier observations applied.