You don't know which one is best. You still have to pick one now. 🎰
A multi-armed bandit for Ruby. You have a handful of options and no idea which pays best. Ask the bandit which to try, and it hands you an arm and a ticket. When you find out how it went, you hand the ticket back with a reward. It balances trying the arm that looks best against trying the ones it knows too little about, with UCB1 or epsilon-greedy, and it refuses a reward that shows up for an arm you've since reset.
Pre-1.0: breaking changes land in minor versions. It is a policy core, not an experiment platform: it picks arms and counts rewards, and everything else is yours.
gem 'bandit_machines'VARIANTS = %i[blue_button green_button orange_button].freeze
bandit = BanditMachines::Bandit.new(BanditMachines::Config.new(1, 0, :lowest_index))
VARIANTS.each_index { |arm| bandit.open(arm) } # every arm starts closed
def show_checkout(bandit, session)
selection = bandit.select_ucb1(0)
return render(VARIANTS.first) if selection.is_a?(Symbol) # :no_open_arm, :tickets_full and friends
session[:ticket] = selection.ticket
render(VARIANTS[selection.arm])
end
def checkout_finished(bandit, session, purchased)
bandit.reward(session[:ticket], purchased ? 1 : 0)
endBandit.new(config) is scope zero. Two bandits built with new are the same scope and issue the same tickets, so one will count the other's rewards. Run more than one and give each its own scope:
checkout = BanditMachines::Bandit.with_scope(BanditMachines::Config.new(1, 0, :lowest_index), 1)
banner = BanditMachines::Bandit.with_scope(BanditMachines::Config.new(1, 0, :lowest_index), 2)One bandit, two ways to select, one way to report. A Config is the core's: max_reward, prior, tie_break, in that order, and bandit.config hands it back. A new bandit has every arm closed and selects nothing until you open some. Arms are indices, rewards are Integers from 0 to max_reward, and any randomness is a number you pass in.
So you have three checkout buttons and a meeting where someone said "let's A/B test it." Two weeks at a third of the traffic each. For two weeks, two thirds of your customers get a button that, by the end, you'll know was worse. Then someone reads a p-value off a dashboard, ships the winner, and the test is over forever, including the day the winner stops winning.
Or you skip the meeting and write variants.max_by { |v| v.conversions.fdiv(v.views) }. The first variant to get a lucky sale wins every request from then on, and the other two never get a view to prove otherwise.
Or you write rand < 0.1 ? variants.sample : best. Now it works, and nobody can reproduce what it did on Tuesday, and the reward for a page shown before the redesign gets credited to the page that replaced it.
The piece you keep rebuilding is small: counts and sums per option, a rule for picking, and a way to say which pick a result belongs to. That piece is this gem.
AMS Robbins, NCC-4227, Robbins-class exploration scout. Every jump spent on a known system is a jump not spent finding a better one.
The scout doesn't mine anything. It decides where the next probe goes. Each system it could visit has a record: how many probes went out, how much they brought back, how many haven't reported yet. A system with a thin record gets the benefit of the doubt. A system with a thick record gets judged on it.
On a bridge somewhere, a cadet volunteers:
def next_system
@systems.max_by { |s| s.ore.fdiv(s.probes) }
end"That," says the scout's navigator, "sends the whole fleet to the first rock that glittered."
Each ticket the bandit issues is in one of these states:
stateDiagram-v2
[*] --> outstanding: select, returns a Selection
outstanding --> counted: reward(ticket, value)
outstanding --> returned: abandon(ticket)
outstanding --> returned: revoke_before(serial) / revoke_all
outstanding --> void: reset(arm) / reset_all
counted --> [*]
returned --> [*]
void --> [*]
And the afternoon the green button was redesigned while a customer was still at the till:
sequenceDiagram
participant C as Checkout
participant B as Bandit
participant O as Operator
C->>B: select_ucb1
B-->>C: Selection(arm 1, ticket, generation 0)
Note over C: customer is still typing a card number
O->>B: reset(1)
B-->>O: 1 ticket voided
Note over B: arm 1 is generation 1, cold
C->>B: reward(ticket, 1)
B-->>C: :stale_ticket
Note over B: the new green button owes nothing to the old one
bandit = BanditMachines::Bandit.new(BanditMachines::Config.new(10, 5, :lowest_index))
bandit.stats(0).open # => false
bandit.select_ucb1(0) # => :no_open_arm
bandit.open(0) # => :opened
bandit.open(1) # => :openedThe bandit has 16 arms and a new one has all of them closed. It explores only what you open, so nothing is ever selected that you didn't name. Until you open an arm, both selections answer :no_open_arm and issue no ticket.
bandit = BanditMachines::Bandit.new(BanditMachines::Config.new(10, 5, :lowest_index))
2.times { |arm| bandit.open(arm) }
first = bandit.select_ucb1(0)
first.arm # => 0
first.reason # => :cold_start
first.ticket # => #<BanditMachines::Ticket>The argument is pick, a draw from 0..2**64-1, and it has no default. UCB1 reads it only to break a tie under tie_break: :draw; with any other rule a constant such as 0 is fine. A Selection holds the ticket and the reason; selection.arm is the ticket's arm.
An arm nobody has tried is cold, and cold arms go first, one per call. Once every arm has been tried, UCB1 scores each as mean / max_reward + sqrt(2 * ln(total tries) / tries) and takes the highest. A well-tried arm is judged on its mean. A neglected one gets a bonus that grows until it earns another look.
bandit.reward(first.ticket, 0) # arm 0 paid nothing
bandit.reward(bandit.select_ucb1(0).ticket, 10) # arm 1 paid everything
third = bandit.select_ucb1(0)
third.arm # => 1
third.reason # => :upper_boundThe score is computed with integers only, so it's the same on every machine. ucb1_index_q56(arm) shows it, scaled by 2**56. It is never above the true value and less than 2**-26 below it.
epsilon = BanditMachines::Ratio.new(1, 10) # numerator, denominator: explore one time in ten
draws = BanditMachines::Draws.new(rand(2**64), rand(2**64)) # gate: explore or exploit; pick: which arm
selection = bandit.select_epsilon_greedy(epsilon, draws)
selection.reason # => :explore or :exploitRatio and Draws are the core's two argument types, passed in the core's order. A Ratio is numerator over denominator, between zero and one; Ratio::ZERO and Ratio::ONE are ready-made. The bandit has no random number generator. You supply two draws from 0..2**64-1. It explores when gate * denominator / 2**64 (rounded down) is below numerator, and then takes open arm number pick * open_arms / 2**64. Otherwise it takes the highest mean, compared exactly. Log the two draws and you can replay the decision.
Cold arms still go first, whatever the gate says.
bandit.reward(selection.ticket, 7) # => :rewarded
bandit.reward(selection.ticket, 7) # => :unknown_ticket, already spentA ticket is good for one reward. A reward above max_reward answers :reward_out_of_range and leaves the ticket usable, so you can fix the value and try again.
A selection that will never have a result gets handed back. It's as if it never happened:
selection = bandit.select_ucb1(0)
bandit.abandon(selection.ticket) # => :abandonedTickets that are out count as tries. Ask three times before any reward comes back and you get three different cold arms, not the same one three times. An arm with tickets out and no reward yet competes using prior as its mean.
bandit = BanditMachines::Bandit.new(BanditMachines::Config.new(10, 5, :lowest_index))
3.times { |arm| bandit.open(arm) }
3.times.map { bandit.select_ucb1(0).arm } # => [0, 1, 2]
bandit.stats(0).pending # => 1
bandit.stats(0).tries # => 1When two arms score exactly the same, the config's tie_break decides. It is required: there is no default, and leaving it out raises Ruby's own ArgumentError for the missing argument. An unknown rule such as :coin raises RangeError.
BanditMachines::Config.new(1, 0, :draw)
BanditMachines::Config.new(1, 0) # => ArgumentError, missing keyword: :tie_break:lowest_index, :highest_index, :least_tried (fewest tries, then lowest index), or :draw, which spreads the tie evenly using your pick.
bandit = BanditMachines::Bandit.new(BanditMachines::Config.new(10, 5, :lowest_index))
2.times { |arm| bandit.open(arm) }
ticket = bandit.select_ucb1(0).ticket
bandit.reset(0) # => 1, one ticket voided
bandit.reward(ticket, 10) # => :stale_ticket
bandit.stats(0).generation # => 1Reset is for when an arm is no longer the thing it was. Its counts go to zero, its generation goes up, and every ticket issued before the reset is refused. reset_all does it to every arm.
bandit.decay(0, BanditMachines::Ratio.new(1, 2)) # => :decayed, arm 0 keeps half its history
bandit.decay_all(BanditMachines::Ratio.new(9, 10)) # => :decayedDecay is for when the world drifts and old results should weigh less. The argument is keep, a Ratio. The reward count is multiplied by it and rounded up, the sum is rounded down. Tickets are untouched: a reward that arrives after a decay still counts in full. Nothing decays unless you call it.
bandit.close(1) # => :closed, arm 1 is no longer selected
bandit.close(1) # => :already_closed, nothing changed
bandit.open(1) # => :opened, and now it is again
bandit.open(1) # => :already_open, nothing changedopen answers :opened or :already_open; close answers :closed or :already_closed. All four are honoured calls, none is a refusal: asking for what already holds is a repeat that changes nothing.
Opening is always explicit: the constructor opens nothing. A closed arm keeps its history and still accepts rewards for tickets issued before it closed. Wire this to whatever decides an option is available today.
bandit.open(16) # => :unknown_arm
bandit.reset(99) # => :unknown_arm
bandit.stats(16) # => nil
bandit.ucb1_index_q56(16) # => nil
bandit.open(-1) # => RangeErrorThere are 16 arms, 0...16. An index that is a valid Integer but names no arm is the core's Refusal::UnknownArm: open, close, reset and decay answer :unknown_arm and change nothing, and the two readers answer nil. Only an index that could not be an index at all raises: a non-Integer is a TypeError, a negative one or one past 2**64-1 a RangeError.
watermark = bandit.next_serial
# ... an hour later, by your clock ...
bandit.revoke_before(watermark) # => how many tickets were abandoned
bandit.revoke_all # => every ticket still out, no serial neededEvery ticket has a serial, counting up from 1. The bandit has no clock, so expiring old selections is yours: remember a serial, and later revoke everything before it. A serial carries its bandit's scope (below), so one taken from a bandit in another scope answers :unknown_ticket and revokes nothing. next_serial is nil once a bandit has issued all 2**48-1 serials of its scope.
To call everything back, use revoke_all, not a big serial: 2**64-1 belongs to scope 65535 and is refused everywhere else, and no serial sits above the last ticket of a scope. revoke_all never refuses, answers how many tickets it abandoned, and leaves every arm's counts and generation alone. reset_all also voids every ticket, but it forgets what the arms learned.
A ticket is only meaningful to the bandit that issued it. Run more than one bandit and each needs its own scope, an Integer in 0..65535 carried by every ticket it issues:
config = BanditMachines::Config.new(1, 0, :lowest_index)
checkout = BanditMachines::Bandit.with_scope(config, 1)
banner = BanditMachines::Bandit.with_scope(config, 2)
[checkout, banner].each { |bandit| bandit.open(0) }
ticket = checkout.select_ucb1(0).ticket
banner.select_ucb1(0)
ticket.scope # => 1
ticket.serial # => 281474976710657, scope 1 in the top 16 bits, 1 below
banner.reward(ticket, 1) # => :unknown_ticket, not one of mine
checkout.reward(ticket, 1) # => :rewardedBandit.new(config) is scope zero, where serials are plain 1, 2, 3. The scope is the bandit's identity, not policy, so it isn't part of Config: two bandits may share a config and must not share a scope. The bandit can't pick a scope for you: it reads no clock and rolls no dice. Leave two bandits in the same scope and they issue the same tickets, so one will happily count the other's reward.
A restarted bandit counts from one again, so a ticket that outlived the restart could redeem a new selection. Resume from a saved count with Bandit.resumed(config, scope, bandit.issued), or give each incarnation a new scope. resumed brings back the serial sequence and nothing else: every arm is closed, nothing is outstanding, earlier tickets are refused, and an issued above 2**48-1 raises RangeError.
BanditMachines::Bandit::ARMS # => 16, the core's Bandit::ARMS
BanditMachines::Bandit::TICKETS # => 64, the core's Bandit::TICKETS
bandit = BanditMachines::Bandit.new(BanditMachines::Config.new(1, 0, :lowest_index))
3.times { |arm| bandit.open(arm) }
64.times { bandit.select_ucb1(0) }
bandit.select_ucb1(0) # => :tickets_fullAt most 64 selections can be waiting on a reward. Past that, selection is refused until a ticket is rewarded, abandoned, revoked or voided.
math = BanditMachines::Math
math::ONE # => 72057594037927936, 1.0 in Q8.56
math.mean_q56(5, 1, 10) # => 36028797018963968, sum / (count * range) = 0.5
math.bonus_q56(100, 4) # => sqrt(2 * ln(100) / 4) in Q8.56
math.ln_q56(3) # => ln(3) in Q8.56, never above the true value
math.log2_q56(8) # => 216172782113783808, exactly 3.0BanditMachines::Math is the core's math module, the integer routines the UCB1 index is built from: ucb1_index_q56(arm) is mean_q56 + bonus_q56 of that arm's numbers.
-
Everything is an Integer. Rewards,
max_reward,priorand ratio terms are0..2**32-1. Draws, serials and arm indices are0..2**64-1. Floats, nil, and strings raiseTypeError, and so does an Array or a Hash where aConfig,RatioorDrawsbelongs. Out of range raisesRangeError, and so do a zeromax_reward, apriorabove it, a ratio above one and an unknowntie_breakorreasonSymbol. Every value checks itself natively when it is built, by the gem or by hand:Selection.newneeds aTicketand one of the four reasons,ArmStats.newneedstrue/falseand counts in0..2**64-1. An invalid one never exists. The value classes are sealed (subclassing raisesTypeError,allocateis undefined); sealing stops accidents, not reflection, so a value the bandit reads must be an instance of exactly its class and its fields are checked again: a forged one raisesTypeErrororRangeErrorand never earns a refusal symbol. A missing argument is Ruby's ownArgumentError; the gem raises none. -
Ruby mirrors Rust.
Config,Ratio,Draws,SelectionandArmStatshave the fields of the core's structs, in order, and are passed positionally where the core takes them.tie_breakandreasonare the core's variants in snake case. ATicketanswersarm,generation,serialandscope. -
Scopes. A
Ticketis only meaningful to the bandit that issued it. Two bandits built withBandit.new(config)are both scope zero and alias: they issue the same tickets and honour each other's. Give each bandit its own scope withBandit.with_scope(config, scope); the core cannot pick one. ATicketor serial from a bandit in another scope is refused with:unknown_ticket(not:stale_ticket, which means the arm was reset). Tickets can't be built or copied by hand. -
Restarts. Resume from a saved count with
Bandit.resumed(config, scope, issued), or give each incarnation a new scope.resumedrestores the serial sequence only; reopening arms and what they learned is yours. -
A call that raises changed nothing. Every argument is checked before the core is asked, and every class was resolved once, when the extension loaded, so a
TypeErrororRangeErrornever leaves a ticket issued behind it. A selection is decided on a copy of the bandit, and theSelectionbuilt natively from that answer, without running any Ruby code; the copy is kept only once it exists. -
Refusals don't raise, and don't change anything.
:unknown_arm,:no_open_arm,:tickets_full,:exhausted,:stale_ticket,:unknown_ticket,:reward_out_of_rangeand:saturatedare answers. Only malformed input raises. -
Every answer, by method.
Method Honoured Refused Records the clock select_ucb1(pick),select_epsilon_greedy(epsilon, draws)a Selection:no_open_arm,:tickets_full,:exhaustedno reward(ticket, reward):rewarded:stale_ticket,:unknown_ticket,:reward_out_of_range,:saturatedno abandon(ticket):abandoned:stale_ticket,:unknown_ticketno revoke_before(serial)the number of tickets revoked (Integer) :unknown_ticketno revoke_allthe number of tickets revoked (Integer) none no reset(arm)the number of tickets voided (Integer) :unknown_arm,:exhaustedno reset_allthe number of tickets voided (Integer) :exhaustedno decay(arm, keep):decayed:unknown_armno decay_all(keep):decayednone no open(arm):opened,:already_open:unknown_armno close(arm):closed,:already_closed:unknown_armno No answer records a clock: the bandit has none, and nothing it answers depends on the time.
stats(arm)anducb1_index_q56(arm)answernilfor an index that names no arm. Those eight symbols are every variant of the core'sRefusal; there is no catch-all symbol. Two of them cannot be reached in practice::exhaustedneeds2**64-1selections or resets of one arm, and:saturatedneeds an arm's reward count or sum to pass2**64-1. -
No hidden randomness, no hidden clock. The same calls with the same draws give the same answers.
-
Frozen means frozen.
freezethe bandit and every method that would change it raisesFrozenError.
A rate built from four views is noise. Picking by rate alone locks in the early leader and starves the others of the views that would correct it. UCB1 adds a bonus for how little you know. Epsilon-greedy spends a fixed share of picks looking around.
A result belongs to the selection that produced it, not to whatever sits in that slot now. The ticket carries the arm's generation. Reset the arm and the old ticket is dead.
Delayed rewards, resets and decay are tools for the real world. They work. They are not covered by the textbook proof, and this gem doesn't pretend otherwise.
The bandit lives in your process and forgets everything when the process does. Persisting counts, sharing them between workers, and carrying a ticket across a request boundary are yours: Ticket is an in-memory object, not something to serialize. The random draws are yours, and so is their quality. Mapping a messy outcome onto 0..max_reward is yours. Deciding when the world has changed enough to reset or decay is yours. Which arms are eligible, and what an arm means, is yours. The bandit recommends; it never routes traffic or flips a flag.
MIT License. See LICENSE.txt for details.
Remember: the best arm so far is not the best arm. It's the best arm you've looked at.