TopTwoThompsonBandit
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
Properties
beta
concurrency
Thread-safety mode of every per-arm accumulator.
maxResamples
Cap on the resample loop when searching for a different second arm.
nbrArms
policy
Per-arm posterior + arm spec.
random
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
choose
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
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
reset
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
update
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.
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.