RL Bible · Chapter 2
Bandits & the Exploration Problem
k-armed bandits, action-value estimation, ε-greedy, UCB, gradient bandits, Thompson sampling, and regret.
Chapter 1 ended with an agent pulling slot-machine arms, and a knob — ε — that we tuned by hand and by vibes. This chapter replaces the vibes with theory. The multi-armed bandit is the reinforcement learning problem with everything removed except the exploration–exploitation dilemma: one state, no credit assignment, no dynamics. That austerity is what makes it solvable — and the solutions it yields (optimism in the face of uncertainty, posterior sampling) are not toy tricks. They are the intellectual ancestors of the exploration bonuses in DQN variants, the UCB term inside AlphaGo's tree search (Chapter 8), and the curiosity signals of Chapter 15. Bandits are also a serious field in their own right: ad placement, clinical-trial design, and recommendation engines are bandit problems running at planetary scale.
By the end of this chapter you will be able to define regret and prove why it is the right yardstick, derive UCB from a concentration inequality, derive the gradient-bandit algorithm as stochastic gradient ascent, implement Thompson sampling, and say precisely — with the Lai–Robbins bound — how good any exploration strategy can possibly be.
1. The k-Armed Bandit Problem
You face actions ("arms," from the one-armed bandits of casino slang). At each step you select an action and receive a reward drawn from a fixed but unknown distribution attached to that arm. Each arm has a true expected payoff
and if you knew the you would trivially always pull . You don't. Everything that follows is about acting well while estimating them.
Note what has been deleted from the full RL problem of Chapter 1: there is no state (every pull faces the same situation), rewards are immediate (no credit assignment), and your actions do not change the environment (arm pays the same tomorrow regardless of what you pull today). Only the evaluative-feedback problem remains: you learn about an arm only by pulling it, and every pull of an uncertain arm is a pull not spent on the best-known one.
Measuring performance: regret. Total reward is an awkward yardstick — it depends on the arm distributions, so a lucky problem instance flatters a bad algorithm. The standard fix is to measure what you lost relative to omniscience. Define the per-arm gap , the expected shortfall of arm . The (expected) cumulative regret after steps is
where counts pulls of arm up to time . The second equality (Exercise 2.2) is the useful one: regret is gap times pull count, summed over suboptimal arms. An algorithm is good precisely to the extent that it keeps small for every arm with large — while still pulling each arm enough to be sure its gap is large. The whole dilemma is now one sentence.
Two regimes matter. If regret grows linearly in , the algorithm never stops making mistakes at a constant rate — this is what fixed ε-greedy does, as we will see. If regret grows like , mistakes become vanishingly rare; the algorithm has, for practical purposes, solved the problem. The gold standard is:
Lai–Robbins lower bound (1985). For any algorithm that is "consistent" (sub-polynomial regret on every problem instance) and Bernoulli rewards, the regret on any instance satisfies
where is the KL divergence between arm 's reward distribution and the optimal arm's.
Read it as an exchange rate: distinguishing arm from the best arm requires information, information costs pulls (about of them), and each pull costs . No cleverness beats it — regret is the floor, and the algorithms of Sections 4 and 6 achieve it up to constants. That an ceiling meets an floor is a rare and beautiful thing; almost nowhere else in this book is a problem so completely closed.
2. Estimating Action Values
The estimation half is ordinary statistics. The sample-average estimate after arm has been pulled times with rewards is , computed incrementally (Chapter 1, Exercise 1.4) as
By the law of large numbers, — if the arm keeps getting pulled, and if holds still.
The second "if" fails often in practice (user tastes drift; an opponent adapts), and the fix is the constant step size :
an exponential recency-weighted average: recent rewards dominate, ancient ones decay geometrically, and the estimate never stops adapting. The price is that no longer converges — it fluctuates forever with variance proportional to — which is exactly the right behavior when the target itself moves. Classical stochastic-approximation theory (Robbins & Monro, 1951) gives the precise conditions on a decaying step-size sequence for convergence:
satisfied by , violated (second condition) by constant α. File these conditions away: they return verbatim as the convergence conditions for TD learning in Chapter 6, and deep RL violates them on purpose, everywhere, for the nonstationarity reason above.
3. ε-Greedy and Optimistic Initialization
ε-greedy, from Chapter 1: exploit with probability , explore uniformly with probability ε. Its virtue is total indifference to assumptions; its flaw is that exploration never targets anything and never stops. Every suboptimal arm — including ones known to be terrible for a thousand pulls — receives probability forever, so regret grows linearly: . Decaying ε on a schedule can recover logarithmic regret in theory (Auer et al. analyze ), but the schedule constants depend on the unknown gaps, which is why practitioners either tune by hand or use the methods below.
Optimistic initialization is the first appearance of the deepest idea in exploration. Initialize every estimate far above any plausible payoff — say when rewards are with — then act purely greedily with a constant step size. Whatever arm the agent pulls first will disappoint it (), the estimate drops, some other still-optimistic arm becomes the greedy choice, and the agent is marched systematically through every arm several times before optimism burns off. Exploration emerges from greed plus a lie.
The trick is elegant and limited: it is a burst of exploration at the start, encoded in initial conditions. In a nonstationary problem the burst is ancient history by the time the world changes, and the agent has no mechanism to re-explore (Exercise 1.8). The permanent lesson is not the trick but the principle: uncertainty should look attractive. The next section makes the principle quantitative.
4. Upper Confidence Bounds
Optimism, done right, means being optimistic in proportion to uncertainty. For each arm maintain not just an estimate but a confidence interval around it, and act greedily with respect to the interval's upper end. Uncertain arms get big intervals, hence inflated upper bounds, hence pulls; each pull shrinks the interval; certainty disarms optimism automatically.
The width comes from a concentration inequality. Hoeffding's inequality: for i.i.d. rewards in with true mean , the sample mean of draws satisfies
Set the right side to a confidence level and solve: . Choosing — confidence tightening as the experiment ages, chosen so that the total probability of ever being fooled is summable — gives the UCB1 rule of Auer, Cesa-Bianchi & Fischer (2002):
with from the derivation and treated as a tunable in practice. The bonus term is the whole story: it grows (slowly, logarithmically) for arms left unpulled, guaranteeing no arm is abandoned on skimpy evidence, and shrinks like as evidence accumulates.
Why it works, in one paragraph. A suboptimal arm keeps getting pulled only while its upper bound exceeds the best arm's upper bound. Once , Hoeffding says arm 's entire interval sits below with high probability, and the pulls stop. Plug into the regret decomposition:
matching the Lai–Robbins floor up to constants. Note the poignant role of : small gaps produce large regret coefficients — nearly-equal arms are expensive to tell apart, though each confusion also costs little.
UCB's descendants are everywhere. Replace "arm" with "move" and you get the selection rule inside Monte Carlo Tree Search and AlphaGo (Chapter 8). Replace the count with a learned density over image states and you get the count-based exploration bonuses of Chapter 15. Optimism in the face of uncertainty is arguably the single most productive principle exploration research has found.
Check your understanding
UCB pulls every arm once at the start (the bonus is infinite when N(a) = 0). After that, is it possible for UCB to never pull some arm again?
5. Gradient Bandit Algorithms
Everything so far estimates values and derives choices from them. There is a second road, and it matters far beyond bandits: skip values entirely and directly adjust preferences for actions by gradient ascent on expected reward. This is the policy-gradient idea (Chapter 11) in its smallest habitat, and we can derive it completely.
Give each arm a numerical preference , and choose actions by softmax:
We want to ascend . Differentiate with respect to one preference , using the softmax derivative (Exercise 2.5):
Because , we may also subtract any action-independent baseline from without changing the gradient — remember this freedom; it is the variance-reduction lever of every policy-gradient method to come. Now the estimator: we cannot evaluate the sum (it needs all the unknown ), but the single sampled reward from the single sampled action suffices. The update
with the running average of all rewards (the baseline), has expectation exactly equal to the true gradient — the full verification is Sutton & Barto §2.8; the key step is that sampling turns the sum over arms into an expectation. So this is genuine stochastic gradient ascent on .
The mechanics are intuitive: if the reward beats the baseline, the taken action's preference rises and all others fall (softmax renormalizes); a below-baseline reward does the opposite. The baseline does not change the direction of the expected update, but it collapses the variance — without it, a problem whose rewards hover around +4 would push every taken action's preference up, and learning would ride on the small differences between large numbers (Exercise 2.7 measures this).
6. Thompson Sampling: the Bayesian Road
The oldest bandit algorithm (Thompson, 1933) is also, by many empirical measures, still the best. Where UCB summarizes uncertainty by an interval, Thompson sampling keeps the whole posterior distribution over each arm's value and explores by sampling from beliefs:
- Maintain a posterior for each arm.
- At each step, draw one sample from each arm's posterior.
- Pull ; update that arm's posterior with the observed reward.
The agent pulls each arm with exactly the probability that it is the best arm, according to current beliefs — the posterior probability of optimality, computed without ever computing it. An uncertain arm's wide posterior occasionally produces a huge sample and wins the argmax (exploration); a well-understood mediocre arm's narrow posterior almost never does (exploitation). Certainty disarms exploration automatically, just as with UCB, but the randomization makes Thompson far more robust to delayed feedback and batching in practice (Chapelle & Li, 2011).
For Bernoulli rewards (click / no-click) the machinery is three lines. Give each arm a prior, starting from (uniform); Beta is conjugate to Bernoulli, so the posterior after successes and failures is simply :
import numpy as np
def thompson_bernoulli(true_p, steps, rng):
k = len(true_p)
succ, fail = np.ones(k), np.ones(k) # Beta(1,1) priors
total = 0.0
for t in range(steps):
theta = rng.beta(succ, fail) # one sample per arm
a = theta.argmax() # pull the most promising sample
r = float(rng.random() < true_p[a])
succ[a] += r
fail[a] += 1.0 - r
total += r
return totalAgrawal & Goyal (2012) proved the matching theory: Thompson sampling achieves the Lai–Robbins bound for Bernoulli bandits — optimal regret, 79 years after the algorithm was proposed. For Gaussian rewards with known variance, the same recipe runs with Gaussian posteriors (Exercise 2.8). For arms whose values are outputs of a neural network, exact posteriors die, and the struggle to approximate them — bootstrapped ensembles, randomized priors — is a running subplot of Chapter 15.
7. The Testbed: the Strategies Head to Head
The standard proving ground (Sutton & Barto §2.3) is the one from Chapter 1: , true values , rewards , 1,000 steps, averaged over 2,000 random instances.
UCB1
Initialize , for all
Loop for :
If some : that (pull each arm once first)
Else:
; ;
import numpy as np
def run(strategy, steps=1000, runs=2000, seed=0, **kw):
rng = np.random.default_rng(seed)
avg_reward = np.zeros(steps)
for _ in range(runs):
q_star = rng.normal(0, 1, 10)
Q, N = np.zeros(10), np.zeros(10)
H = np.zeros(10) # gradient-bandit preferences
rbar = 0.0
for t in range(steps):
if strategy == "eps":
if rng.random() < kw["eps"]:
a = rng.integers(10)
else:
a = rng.choice(np.flatnonzero(Q == Q.max()))
elif strategy == "ucb":
if N.min() == 0:
a = int(N.argmin())
else:
a = int(np.argmax(Q + kw["c"] * np.sqrt(np.log(t + 1) / N)))
elif strategy == "grad":
pi = np.exp(H - H.max()); pi /= pi.sum()
a = rng.choice(10, p=pi)
r = rng.normal(q_star[a], 1.0)
N[a] += 1
Q[a] += (r - Q[a]) / N[a]
if strategy == "grad":
rbar += (r - rbar) / (t + 1)
onehot = np.zeros(10); onehot[a] = 1.0
H += kw["alpha"] * (r - rbar) * (onehot - pi)
avg_reward[t] += r
return avg_reward / runs
for name, curve in {
"eps=0.1": run("eps", eps=0.1),
"UCB c=2": run("ucb", c=2.0),
"gradient": run("grad", alpha=0.1),
}.items():
print(f"{name:9s} late avg reward: {curve[-100:].mean():.3f}")Representative results on this testbed: ε-greedy (ε = 0.1) settles near 1.40; UCB () near 1.47 after an early dip while it pulls everything once; the gradient bandit near 1.45. The differences look small because the testbed is easy; the slopes differ — run 10,000 steps and ε-greedy's flat tax becomes visible while UCB keeps creeping upward. The deeper comparison is the parameter study (Sutton & Barto Fig. 2.6): sweep each method's knob across orders of magnitude and plot final performance. UCB and gradient bandits are both better at their best and flatter around it — robustness to hyperparameters, a virtue you will crave in deep RL, is measurable already here.
What breaks — bandit lessons that fail to transfer
Two cautions before you carry these tools into full RL. First, everything above assumed stationary arms; under drift, pair any strategy with constant-α estimates, and note that UCB's count-based bonus has no mechanism to re-inflate when the world changes — sliding-window and discounted UCB variants exist for exactly this. Second, and more important: in full MDPs, the exploration problem is no longer local. A bandit explores by choosing a different arm now; an MDP agent may need a coordinated hundred-step detour to reach the unvisited region at all — dithering ε-greedy needs time exponential in the detour's depth to stumble there by luck. The principles — optimism, posterior sampling — survive the trip to Chapter 15; the specific algorithms do not.
8. Contextual Bandits: One Step Toward Full RL
Between the bandit and the MDP lies a waypoint worth naming, because a surprising amount of industry runs on it. In a contextual bandit, each round presents a context (user features, time of day), the agent chooses an action, and the reward depends on both: . Ad systems and recommenders live here — each decision is personalized, but today's recommendation does not (to first approximation) change tomorrow's user. LinUCB (Li et al., 2010), which powered news recommendation at Yahoo!, is exactly UCB with a linear model of supplying both the estimate and the confidence width.
What contextual bandits still lack is the defining difficulty of Chapter 3 onward: actions that change the state. Once today's action determines tomorrow's situation, you must plan — and value long-term consequences — rather than merely predict rewards one step out. That single addition costs us the closed theory of this chapter and buys the rest of the book.
9. Summary
- The -armed bandit isolates exploration: one state, immediate rewards, unknown arm values .
- Regret is the yardstick: gap × pulls. Linear regret means never learning; regret is optimal, by the Lai–Robbins bound.
- Sample averages converge on stationary arms under the Robbins–Monro conditions (, finite); constant α tracks nonstationary ones and is what deep RL uses.
- ε-greedy: simple, assumption-free, linear regret — it taxes every step forever.
- Optimism: optimistic initialization explores via greed alone; UCB makes optimism proportional to uncertainty via Hoeffding, achieving regret. Its descendants run MCTS and deep-RL exploration bonuses.
- Gradient bandits ascend directly on softmax preferences — the policy gradient theorem in miniature, baseline and all.
- Thompson sampling samples from the posterior and pulls the argmax: optimal regret, superb practical performance, and the ancestor of posterior-based deep exploration.
- Contextual bandits add state without dynamics; MDPs add dynamics, and the closed theory ends.
10. Papers & Further Reading
- Sutton & Barto, Ch. 2 — incompleteideas.net/book/the-book-2nd.html. The testbed and the gradient-bandit derivation this chapter follows.
- Lattimore & Szepesvári, Bandit Algorithms (Cambridge UP, 2020) — banditalgs.com. The definitive graduate text: several hundred pages of exactly this chapter, done with full rigor. Free PDF.
- Auer, Cesa-Bianchi & Fischer, "Finite-time Analysis of the Multiarmed Bandit Problem" (Machine Learning, 2002) — doi.org/10.1023/A:1013689704352. UCB1 and its finite-time guarantee; the argument sketched in Section 4 lives here.
- Lai & Robbins, "Asymptotically Efficient Adaptive Allocation Rules" (Advances in Applied Mathematics, 1985) — doi.org/10.1016/0196-8858(85)90002-8. The lower bound; the paper that made regret the field's currency.
- Thompson, "On the Likelihood that One Unknown Probability Exceeds Another in View of the Evidence of Two Samples" (Biometrika, 1933) — doi.org/10.1093/biomet/25.3-4.285. Posterior sampling, proposed for clinical trials, ignored for most of a century.
- Chapelle & Li, "An Empirical Evaluation of Thompson Sampling" (NeurIPS, 2011) — papers.nips.cc. The paper that revived Thompson sampling by showing it beat UCB in display advertising.
- Agrawal & Goyal, "Analysis of Thompson Sampling for the Multi-armed Bandit Problem" (COLT, 2012) — arxiv.org/abs/1111.1797. Near-optimal regret for Thompson sampling, closing the theory.
- Russo, Van Roy, Kazerouni, Osband & Wen, "A Tutorial on Thompson Sampling" (Foundations and Trends in ML, 2018) — arxiv.org/abs/1707.02038. Modern, readable, and honest about when posterior sampling breaks.
- Li, Chu, Langford & Schapire, "A Contextual-Bandit Approach to Personalized News Article Recommendation" (WWW, 2010) — arxiv.org/abs/1003.0146. LinUCB in production at Yahoo!; contextual bandits meeting reality.
11. Exercises
2.1 (understand) In ε-greedy with , , and current estimates , what is the probability that the next action is arm 2? Arm 4? (Careful: the exploration branch can also land on the greedy arm.)
2.2 (derive) Prove the regret decomposition from the definition . Hint: write and condition on the actions (tower rule).
2.3 (derive) Show that ε-greedy with fixed ε has regret at least and hence cannot achieve sublinear regret. Then explain qualitatively why any algorithm whose exploration probability does not decay must suffer linear regret.
2.4 (derive) Starting from Hoeffding's inequality with confidence , derive the UCB1 bonus and verify the constant . Why must the confidence level tighten over time — what goes wrong with a fixed δ? Hint: a union bound over infinitely many time steps.
2.5 (derive) Derive the softmax derivative , then complete the expectation calculation showing the gradient-bandit update is unbiased: , treating as a constant baseline.
2.6 (implement) Reproduce the parameter study: for each method (ε-greedy over ε, UCB over , gradient bandit over α, optimistic-greedy over ), sweep the knob over and plot average reward over the first 1,000 steps. Which method's curve is flattest near its peak, and why does that matter more than the peak itself?
2.7 (implement) Rerun the gradient bandit on a shifted testbed where , with and without the baseline . Explain the gap you observe using the variance argument of Section 5.
2.8 (implement) Implement Gaussian Thompson sampling for the standard testbed: prior on each , known reward variance 1, so the posterior after pulls with sample mean is . Compare its learning curve to UCB's. Which handles the first 50 steps better, and why?
2.9 (extend) Batched feedback: rewards for all pulls in a batch of 100 arrive only at the batch's end. Modify UCB and Thompson sampling to run in this regime and compare degradation. (Thompson's randomization keeps batch-mates diverse while deterministic UCB repeats one arm 100 times — this robustness is why ad systems chose it; Chapelle & Li discuss exactly this.)
2.10 (research) UCB needs the count . In Chapter 15, states are images and exact counts are meaningless — every frame is unique. Sketch two distinct proposals for a "generalized count" over continuous observations, and one failure mode of each. (You are re-deriving pseudo-counts and RND; check your ideas against that chapter afterward.)