Foundations to Frontier
Tests
B. Statistical Learning Theory › B5 Online learning and bandits

Multi-Armed Bandits: ε-Greedy, UCB and Thompson Sampling

A bandit learner repeatedly chooses one of K actions and sees only the reward of the action it chose, so it must balance exploring uncertain actions against exploiting the best-looking one. This topic defines regret for stochastic bandits and derives the Lai–Robbins lower bound (regret ≳ Σ Δᵢ/kl(μᵢ, μ*) · ln T), shows why fixed-ε exploration gives linear regret, proves UCB1's logarithmic regret from concentration inequalities, presents Thompson sampling with Beta posteriors, and covers the minimax √(KT) rate. In the exercises you race the three algorithms, compare them with the asymptotic bound, and map regret against the gap to find the hardest problems.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Try the new restaurant or go back to the good one?

Every time you eat out you choose: your favourite place (reliably good) or somewhere new (maybe better, maybe worse). Only by trying a place do you learn how good it is. Too much trying and you waste many evenings on bad food; too little and you may never discover the best place in town. This is the exploration–exploitation dilemma, and the multi-armed bandit (named after rows of slot machines, “one-armed bandits”) is its cleanest mathematical form.

Explore or exploit? Three strategies, three slot machines
cumulative regret (mean of 60 runs)round → 1000
ε-greedy: regret 21.5
UCB1: regret 48.8
Thompson: regret 19.1
bars: share of pulls per arm (solid = best arm)
You only learn about an arm by pulling it, so every pull is a trade-off: exploit the arm that looks best, or explore one that might be better. Fixed ε-greedy explores forever and its regret grows in a straight line. UCB1 is optimistic: it pulls the arm whose plausible upper value is highest, and its regret grows like log T. Thompson sampling pulls each arm with the probability that it is the best (by sampling from Beta posteriors) and is usually best in practice. Make two arms nearly equal: regret grows, but so does how long it takes to matter.
Why engineers care

Bandits decide which sensing mode to use when you can afford only one, which experiment to run next, which antenna or frequency to try, how to allocate observation time among targets, and which controller variant to deploy (A/B testing). They are also the simplest setting in which to learn the exploration ideas used in reinforcement learning.

📝 Quick check (Level 0)
0/3 answered
  1. 1. In a bandit problem, after choosing an arm you see…
  2. 2. Exploring means…
  3. 3. Never exploring risks…

Level 1: Beginner

★★☆☆☆ Basic⏱ 25 minSecondary-school mathematics: arithmetic and elementary algebra.

Stochastic bandits and regret

Arms i = 1, …, K give i.i.d. rewards with unknown means μi\mu_i. With μ⋆=max⁡iμi\mu^\star = \max_i\mu_i, gaps Δi=μ⋆−μi\Delta_i = \mu^\star - \mu_i and ni(T)n_i(T) the number of pulls of arm i:

RegT=Tμ⋆−E∑t=1Trt=∑iΔi E[ni(T)].\mathrm{Reg}_T = T\mu^\star - \mathbb E\sum_{t=1}^Tr_t = \sum_i\Delta_i\,\mathbb E[n_i(T)].

Good algorithms pull bad arms only about log T times each.

Three strategies

  • ε-greedy: with probability ε pull a random arm, otherwise the best empirical mean. With fixed ε it keeps exploring forever, so regret grows like εΔˉT\varepsilon\bar\Delta T, linear in T.
  • UCB1 (Auer, Cesa-Bianchi, Fischer, 2002): pull arg⁡max⁡i μ^i+2ln⁡t/ni\arg\max_i\ \hat\mu_i + \sqrt{2\ln t/n_i}. “Optimism in the face of uncertainty.”
  • Thompson sampling: keep a posterior per arm (Beta(1 + successes, 1 + failures) for Bernoulli rewards), draw one sample from each, and pull the largest.
📝 Quick check (Level 1)
0/3 answered
  1. 1. UCB1 index with mean 0.5, n = 2, t = e:
  2. 2. Beta posterior mean after 3 successes, 1 failure (uniform prior):
  3. 3. Fixed-ε greedy's long-run regret is…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.

Lower bound (Lai and Robbins, 1985)

For any algorithm that is good on every instance (consistent), and every suboptimal arm,

lim inf⁡T→∞E[ni(T)]ln⁡T≥1kl(μi,μ⋆) ⇒ RegT≳∑i:Δi>0Δikl(μi,μ⋆)ln⁡T,\liminf_{T\to\infty}\frac{\mathbb E[n_i(T)]}{\ln T}\ge\frac1{\mathrm{kl}(\mu_i, \mu^\star)}\ \Rightarrow\ \mathrm{Reg}_T\gtrsim\sum_{i:\Delta_i\gt0}\frac{\Delta_i}{\mathrm{kl}(\mu_i, \mu^\star)}\ln T,

where kl is the Bernoulli KL divergence. Arms that are hard to distinguish from the best (small kl) must be sampled more. The bound is asymptotic: at a finite T, regret can sit below the constant times ln T.

UCB1’s guarantee

RegT≤∑i:Δi>08ln⁡TΔi+(1+π23)∑iΔi.\mathrm{Reg}_T\le\sum_{i:\Delta_i\gt0}\frac{8\ln T}{\Delta_i} + \Big(1 + \frac{\pi^2}3\Big)\sum_i\Delta_i.

Logarithmic, but with a worse constant than Lai–Robbins (1/Δ vs Δ/kl ≈ 1/(2Δ)). KL-UCB and Thompson sampling attain the optimal constant.

Minimax rate

Over all instances, the worst-case regret is Θ(KT)\Theta(\sqrt{KT}). The hardest gaps are about K/T\sqrt{K/T}: big enough to cost, small enough to be hard to detect.

🐍 Exercise 1: ε-greedy vs UCB1 vs Thompson sampling
Simulate 100 parallel runs of each algorithm on a 5-armed Bernoulli bandit (means 0.7, 0.6, 0.5, 0.4, 0.3) for 5000 rounds. Compare regret curves, check that fixed-ε regret grows linearly at ε·mean gap per round, and compare Thompson sampling with the Lai–Robbins constant.
📝 Quick check (Level 2)
0/3 answered
  1. 1. The Lai–Robbins constant for each bad arm is…
  2. 2. UCB1's regret bound scales like…
  3. 3. The worst-case (minimax) regret is…

Level 3: Advanced

★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.

Why UCB1 works (sketch)

By Hoeffding, with high probability ∣μ^i−μi∣≤2ln⁡t/ni|\hat\mu_i - \mu_i|\le\sqrt{2\ln t/n_i} for all arms at time t. If a bad arm i is pulled at time t, then its index beats the best arm’s, and on the good event μi+22ln⁡t/ni≥μ⋆\mu_i + 2\sqrt{2\ln t/n_i}\ge\mu^\star, i.e. ni≤8ln⁡t/Δi2n_i\le8\ln t/\Delta_i^2. So each bad arm is pulled at most 8ln⁡T/Δi28\ln T/\Delta_i^2 times plus the (summable) failures of the good event, and multiplying by Δi\Delta_i gives the bound. ∎

Thompson sampling

Thompson sampling selects arm i with exactly its posterior probability of being the best. It explores automatically: an arm’s posterior stays wide until it is pulled enough. Agrawal and Goyal (2012) and Kaufmann et al. (2012) proved it achieves the Lai–Robbins constant for Bernoulli bandits. Its Bayesian regret is O(KTln⁡T)O(\sqrt{KT\ln T}) on any prior (Russo and Van Roy, 2014).

Why the lower bound holds (idea)

To be sure arm i is not the best, an algorithm must collect enough evidence to rule out the alternative world in which μi\mu_i is slightly above μ⋆\mu^\star. Distinguishing the two worlds needs about ln⁡T/kl(μi,μ⋆)\ln T/\mathrm{kl}(\mu_i, \mu^\star) samples of arm i (a change-of-measure argument), or else it suffers linear regret in the alternative world.

📝 Quick check (Level 3)
0/3 answered
  1. 1. UCB1 pulls a bad arm at most about…
  2. 2. Thompson sampling pulls an arm with probability equal to…
  3. 3. The Lai–Robbins proof uses…

Level 4: Expert

★★★★★ Expert⏱ 35 minResearchers: original papers and open problems.

Bandits, measured

Exercise 1 (5 arms, 100 runs). Regret at T = 500, 1000, 2000 and 5000:

algorithm regret
ε-greedy (ε = 0.1) 24.8, 39.6, 63.6, 126.8
UCB1 51.0, 79.4, 117.4, 181.8
Thompson sampling 24.5, 29.9, 34.9, 41.4

ε-greedy’s regret grows linearly at 0.021 per round, matching ε·mean gap = 0.02. UCB1’s per-round regret falls, but its large constant makes it the worst here at this horizon. Thompson sampling’s regret grows by 7.1 per unit of ln T. The Lai–Robbins constant is 9.46 (× ln 5000 = 80.6), and Thompson sits below it at T = 5000. That’s no contradiction: the bound is asymptotic, and the slope is still climbing toward it.

Exercise 2: which gaps are hardest? On two arms (0.5 vs 0.5 − Δ) at T = 2000, UCB1’s regret is 9.2, 17.1, 30.9, 38.6, 36.9 and 26.0 for Δ = 0.01, 0.02, 0.05, 0.1, 0.2 and 0.4, and Thompson’s is 8.3, 13.6, 17.2, 14.4, 8.8 and 5.8. Tiny gaps cost little because the arms are nearly equal; big gaps are found quickly. The worst sits in between, always below √(KT) = 63. Thompson’s worst case over gaps grows from 9.6 at T = 500 to 17.2 at T = 2000 (×1.8 for 4× the rounds, close to √4 = 2).

🐍 Exercise 2 (research): the hardest gap and the √T minimax rate
For a two-armed bandit, measure final regret as a function of the gap Δ for UCB1 and Thompson sampling, locate the hardest gap, compare with √(KT), and estimate how the worst case over gaps grows with T.

Bandits in AI, robotics and aerospace

  • Adaptive sensing: choose which instrument mode or target to observe when time is limited (Earth observation scheduling).
  • Communication: select frequencies, beams or relays under unknown, changing channel quality.
  • Experiment design and A/B testing of controller variants, with far less regret than fixed splits.
  • RL exploration: UCB and Thompson ideas underpin exploration bonuses and posterior sampling in reinforcement learning.

References

  1. T. L. Lai, H. Robbins (1985). “Asymptotically efficient adaptive allocation rules.” Advances in Applied Mathematics, 6.
  2. P. Auer, N. Cesa-Bianchi, P. Fischer (2002). “Finite-time analysis of the multiarmed bandit problem.” Machine Learning, 47.
  3. W. R. Thompson (1933). “On the likelihood that one unknown probability exceeds another in view of the evidence of two samples.” Biometrika, 25.
  4. S. Agrawal, N. Goyal (2012). “Analysis of Thompson sampling for the multi-armed bandit problem.” COLT.
  5. D. Russo, B. Van Roy (2014). “Learning to optimize via posterior sampling.” Mathematics of Operations Research, 39.
  6. T. Lattimore, C. Szepesvári (2020). Bandit Algorithms. Cambridge University Press.