Explore or Exploit

A row of slot machines with unknown payouts. Play them yourself, then race ε-greedy, UCB and Thompson sampling and watch regret grow like log t.

Explore or Exploit

Before states, before time, there is a row of slot machines with unknown payout rates. Every pull either learns something about a machine (explore) or cashes in on what you already know (exploit). You cannot do both at once — and that tension runs through all of reinforcement learning.

The Slot Machines

Tap the machines to pull their levers and hunt for the one that pays best, while a learning agent plays an identical row beside you. Press Reveal to see the true rates and what your exploring cost.

0 / 0
your coins / pulls
0 / 0
Thompson’s coins / pulls
?
your regret (expected coins lost)
?
Thompson’s regret
Tap a machine (or press its number key) to pull the lever. Each one pays a coin with its own hidden probability. Every time you pull, Thompson pulls once on an identical copy of the row.

Under each machine, the orange bar is the share of your pulls that paid out, and the second bar is how often the agent has pulled it (with its observed rate). Reveal marks each true rate with a white tick. Regret after n pulls is n × (best rate) minus the sum of the true rates of the machines pulled: the expected cost of not knowing, independent of luck. The agent plays its own copy of the same machines with its own coin flips.

Go deeper:The Multi-Armed Bandit and Regret

A multi-armed bandit has K arms. Pulling arm i pays a random reward drawn from a distribution with unknown mean μᵢ; here each arm pays 1 with probability pᵢ and 0 otherwise. The goal is to collect as much reward as possible over T pulls. The name comes from “one-armed bandit”, slang for a slot machine: a gambler facing a row of them must decide which levers to pull.

Performance is measured by regret, the cost of not knowing which arm is best. With best mean μ* and gaps Δᵢ = μ* − μᵢ, the (pseudo-)regret after T pulls is R(T) = T·μ* − Σₜ μ(aₜ) = Σᵢ Δᵢ · Nᵢ(T), where Nᵢ(T) counts pulls of arm i. Every pull of a worse arm adds its gap. An omniscient player has zero regret; a player choosing at random has regret growing linearly in T.

Pure greedy — always pull the arm with the best average so far — can lock onto a bad arm forever. If the best arm happens to lose its first few pulls, its estimate looks poor, greedy never pulls it again, and the estimate is never corrected. Mistakes that stop you gathering data are the ones that never get fixed.

Herbert Robbins posed the multi-armed bandit problem in its modern form in 1952, in “Some aspects of the sequential design of experiments”. Earlier, in 1933, William R. Thompson had proposed the randomised rule now called Thompson sampling, motivated by clinical trials: how to give more patients the better of two treatments while still learning which one that is.

The Regret Race

Press Run the race to average hundreds of runs of each method as the curves fill in. Then switch on the log time axis: the good methods straighten into lines while fixed ε keeps bending upward.

Arms
Gaps between rates
Horizon (pulls per run)
Runs averaged
0 / 200
runs (paused)
Every fixed-ε method keeps exploring forever, so its regret climbs in a straight line. Greedy sometimes locks onto the wrong arm and never leaves. The smart methods bend over. Switch on the log time axis to see the difference in shape, and hover or tap to read off values.

Every run plays the same 5 arms with fresh random seeds; this set’s payout rates are 0.90, 0.50, 0.70, 0.30, 0.10 (best in bold). Regret is the expected reward lost to worse arms, computed from the true rates. The dashed white curve is the Lai–Robbins bound C · ln t with C = Σ Δᵢ / KL(pᵢ ‖ p*) = 3.1: an asymptotic limit on the slope against ln t, so good methods can sit below it at these horizons. The lower panel shows the share of runs pulling the best arm at each moment (lightly smoothed).

Go deeper:Logarithmic Regret and the Lai–Robbins Bound

ε-greedy pulls a random arm with probability ε and the best-looking arm otherwise. With a fixed ε it never stops exploring, so each pull costs on average at least ε · (mean gap) and regret grows linearly: R(T) ≈ ε · T · (1/K) Σᵢ Δᵢ. A small ε explores too little at the start; a large one wastes pulls forever. Letting ε decay over time fixes the slope but needs tuning to the gaps.

Lai and Robbins (1985) proved a lower bound that holds for every consistent strategy (one whose regret is o(Tᵃ) for every a > 0 on every problem): liminf R(T) / ln T ≥ Σᵢ Δᵢ / KL(pᵢ ‖ p*), where KL(p ‖ q) = p ln(p/q) + (1 − p) ln((1 − p)/(1 − q)) is the Kullback–Leibler divergence between Bernoulli distributions. Regret must grow at least logarithmically, and arms that are hard to tell apart from the best (small KL) force more exploration. The bound is asymptotic: it limits the slope of regret against ln T as T → ∞, not its height at a finite horizon, which is why good methods can sit below the dashed curve.

UCB1 (Auer, Cesa-Bianchi and Fischer, 2002) pulls the arm maximising x̄ᵢ + √(2 ln t / nᵢ), an optimistic estimate — “optimism in the face of uncertainty”. They proved its expected regret is O(Σᵢ (ln T) / Δᵢ) at every T, not only in the limit. Its constant is not the optimal Lai–Robbins one, and at short horizons UCB1 can explore more than a well-tuned ε-greedy; try the narrow gaps, 10 arms and the c slider to see this (the guarantee is for c = 1).

Thompson sampling was shown to be asymptotically optimal for Bernoulli bandits — it matches the Lai–Robbins constant — by Kaufmann, Korda and Munos (2012), with finite-time regret bounds also proved by Agrawal and Goyal (2012). Empirically it often beats UCB1, as the race shows.

Beliefs at Work

Step through Thompson sampling one pull at a time: a sample drops out of each belief curve, the largest wins, and that curve sharpens. Switch to UCB1 and watch rarely tried arms keep their tall bars.

Arms
0
pulls
0
wins
0.0
regret so far
0%
pulls on the best arm (arm 3)
Chance Thompson picks each arm next (from 2,000 posterior draws): arm 1 26%, arm 2 25%, arm 3 24%, arm 4 25%. That is the posterior probability that each arm is the best — wide curves still get picked sometimes, narrow low curves almost never.

Drag the white triangles to set each arm’s true payout rate (this resets the beliefs). Thompson: each curve is the Beta posterior for that arm’s rate, starting flat at Beta(1, 1); each step draws one sample per arm (the dots), pulls the arm with the largest sample (ringed), and adds the win or loss to its counts. UCB1: the dot is the sample mean and the bar extends by the bonus √(2 ln t / n) to the index. The strip at the bottom shows the most recent pulls coloured by arm, with green or red ticks for wins and losses.

Go deeper:Bayesian Beliefs and Optimism

Thompson sampling keeps a probability distribution over each arm’s unknown rate. Starting from the flat prior Beta(1, 1), observing w wins and l losses gives the posterior Beta(1 + w, 1 + l) — the Beta family is conjugate to Bernoulli data, so updating is just counting: α += 1 on a win, β += 1 on a loss. The posterior mean is α / (α + β), and its spread shrinks roughly like 1 / √n.

Each round, Thompson draws one plausible rate from every posterior and pulls the arm with the largest draw. The effect is probability matching: each arm is pulled with exactly the posterior probability that it is the best. Uncertain arms still get chosen sometimes; arms that are confidently worse almost never are.

UCB1’s bonus √(2 ln t / nᵢ) comes from Hoeffding’s inequality: the chance that the true mean exceeds the sample mean by more than ε after n pulls is at most e−2nε². Setting that probability to t⁻⁴ gives a bound that is wrong with vanishing probability, while the slowly growing ln t makes sure every arm is revisited eventually.

Bandits are used well beyond casinos. Contextual bandits, which see features before choosing, power news recommendation and ad placement — Li, Chu, Langford and Schapire (2010) used LinUCB to personalise articles on the Yahoo! front page — and adaptive A/B tests shift traffic toward the better variant while the test runs. For the discounted Bayesian bandit, the Gittins index (Gittins, 1979) gives an exactly optimal policy: compute an index for each arm on its own and pull the largest.

Key Takeaways

  • A bandit forces a choice on every pull: learn more about the arms, or cash in on the best guess.
  • Regret, the expected reward lost to worse arms, measures the price of not knowing.
  • Greedy can lock onto a bad arm and fixed ε explores forever: both give linear regret.
  • UCB1 and Thompson sampling achieve logarithmic regret, the best possible rate (Lai–Robbins).

Spot a mistake or have an idea?

Search