Chapter 02 · Part 1: Estimating from experience
Bandits as sequential experiments
A bandit is a sequential experiment whose allocation adapts to the data; UCB1 turns a Hoeffding confidence bound into a decision rule, and adaptivity biases the sample mean.
Chapter 1 described RL as estimation plus decision under feedback and promised a special case with no future to worry about. This is it. Take an MDP with a single state: the action affects only the reward you get now, not where you will be next. What is left is the cleanest version of the central conflict. You must learn which action is best by trying them, and every trial of a worse action costs you. Think of a clinical trial that can reassign patients, or a site choosing which headline to show.
What is a multi-armed bandit?
There are arms (actions). Arm has an unknown reward distribution with mean . In each round the learner picks an arm and observes a reward , drawn independently of the past given . In a bandit the reward of round is written , as in the notation page, since there is no next state to index against. Only the pulled arm's reward is observed.
Write and for the gap of arm : how much you lose per pull by choosing instead of the best arm. Let be the number of times arm was pulled in the first rounds, and the sample mean of the rewards from those pulls. The learner's choice may depend on everything seen before round : that is what a policy means here, and it is where the statistical difficulty enters.
In this chapter rewards lie in (for example Bernoulli). That is what makes Hoeffding's inequality applicable.
How does a bandit differ from an A/B test?
A classical A/B/n test fixes the design in advance. It allocates units to each of the arms, estimates each with , tests, and then acts on the result. The design is non-adaptive, so each is a plain sample mean of i.i.d. draws: unbiased, with standard error , and the usual intervals and tests apply. What you pay is the allocation. During the whole experiment, a fraction of the units go to arms that are not the best, and if the test never stops (a "uniform" policy over a horizon ), you lose a constant fraction of the best arm's reward forever.
A bandit algorithm adapts: it shifts allocation toward arms that look better as data accumulate. This cuts the loss from experimentation dramatically, but destroys the fixed design. Which units go to arm is now a random function of earlier outcomes, so the data are not i.i.d. in the usual sense. The price is paid in estimation, and the last half of this chapter is about that price.
What is regret?
The learner's performance is measured against an oracle that knows the means and always pulls the best arm. The expected regret over rounds is
The expectation is over the rewards and any randomisation in the policy. Regret has a decomposition that is used constantly.
Regret decomposition. .
Proof. Conditional on the past and on , the reward has mean , so (tower property; this is where "the reward is drawn from independently of the past" is used). Hence . Since , .
So regret is a sum over suboptimal arms of gap times expected number of pulls. To have small regret you must pull each bad arm rarely, and the smaller its gap the more pulls you need to rule it out. The conflict is quantitative: to tell an arm with gap from the best one takes on the order of samples, which costs about in regret. Hold that scaling in mind; it reappears in every bound below.
A policy that allocates uniformly has regret , linear in . The goal is sublinear regret, ideally for fixed gaps.
In simulations we use the pseudo-regret , which replaces the observed rewards by their means. It has the same expectation as the regret (by the decomposition) and removes reward noise from the estimate.
What does Hoeffding's inequality say, and where does it come from?
To decide whether an arm is worse, we need to bound how far a sample mean can sit from its true mean. Hoeffding's inequality (Hoeffding, 1963) does so for bounded variables: if are independent with and mean , then for
Unlike the CLT it holds at every with no normality assumption, at the price of using only the range, not the variance.
Sketch of the derivation. This is the Chernoff method, with one lemma.
- Hoeffding's lemma. If and , then for every real . (Proof idea: the log of the moment-generating function is with derivative at , and its second derivative is the variance of an exponentially tilted copy of , a variable on an interval of length , hence at most . Taylor's theorem then gives .)
- Chernoff step. For , Markov's inequality applied to gives
using independence for the product and the lemma for each factor. 3. Optimise . The exponent is minimised at and equals .
Inverting it gives a confidence bound. Setting the right-hand side to and solving for , with probability at least ,
This is a one-sided confidence bound whose width shrinks like , as in the CLT interval.
What is UCB1, and why is it a confidence bound?
If you had a valid upper confidence bound on each arm's mean, a natural rule would be to behave as if each arm were as good as its bound allows, and pull the arm whose bound is highest. This is the principle of optimism in the face of uncertainty. It is self-correcting: an arm with few pulls has a wide interval, so its upper bound is high and it gets sampled; as it is sampled the interval shrinks, and if the arm really is bad its bound falls below the best arm's and sampling stops. An arm is pulled either because it looks good or because we do not know enough about it, and the algorithm needs no separate "explore" switch.
UCB1 (Auer, Cesa-Bianchi and Fischer, 2002) makes this concrete. Pull each arm once. Then, at round , pull
The width is Hoeffding's with : . The confidence level rises with , so that over the whole run only finitely many "bad events" are expected.
Why it works (sketch). Fix a suboptimal arm with gap . If arm is pulled at round with pulls so far, then its index is at least the index of a best arm. That can happen only if at least one of three things holds:
- the best arm's index is below its true mean, (its confidence bound failed), which by Hoeffding has probability at most for each possible count of pulls;
- arm 's sample mean is too high (), also with probability at most for each count;
- neither failed, but the width itself is large: , that is, .
Summing the failure probabilities over all rounds and all pairs of pull counts gives at most . Case 3 can only occur while is below . So , and by the decomposition:
This is a finite-time bound: it is valid for every , with no asymptotics. The regret grows like , so the per-round regret goes to zero.
Worked example by hand: two UCB indices
Suppose there are two arms with Bernoulli rewards and the algorithm has run for rounds. Arm was pulled times with successes, arm was pulled times with success. At round :
- , . Width . Since , and the width is . Index .
- , . Width . Index .
Arm has the larger index and is pulled.
Now take a different state at : arm pulled times with sample mean , arm pulled only twice with sample mean . Here .
- Arm : width , index .
- Arm : width , index .
Arm is pulled, even though its sample mean is lower, because it has hardly been tried. This is optimism at work. The script reproduces both calculations (they are the first block of output below).
What is the best any algorithm can do? The Lai–Robbins lower bound
UCB1's bound has the form . Can any algorithm do better? Not in the following sense. Call a policy consistent over a class of bandit instances if for every instance in the class and every , : it learns on every instance, whichever is the truth. This rules out "always pull arm 1", which has zero regret on some instances and linear regret on others.
Lai–Robbins (1985). For a consistent policy over, say, Bernoulli bandits, on every instance with a unique best arm,
Here is the KL divergence between and . This is the result of Lai and Robbins (1985); for general reward families, is replaced by the infimum of over distributions with mean above (see Lattimore and Szepesvári, 2020, Chapter 16).
In words: any consistent policy must pull each suboptimal arm at least about times. The reason is a hypothesis-testing one. The data from arm must rule out the alternative world in which arm is slightly better than the best arm, since there neglecting it would cost linear regret. Distinguishing from at an error level that tightens like takes about samples, by the likelihood-ratio logic of Neyman and Pearson.
Compare constants. By Pinsker's inequality , so the lower bound is at most , while UCB1's is . Both scale as , but UCB1's constant is up to times larger in the worst case. Auer et al. note that UCB2 reduces the leading constant to nearly per arm. Closing that constant gap takes indices built from KL divergences rather than Hoeffding widths; chapter 3 approaches the same goal from the Bayesian side.
Why is the sample mean of an adaptively sampled arm biased?
Back to estimation. After the bandit run, you hold for each arm and might want to report it, with an interval, as the estimate of . Under a fixed design that is a textbook sample mean. Under adaptive allocation it is biased.
A three-round example by hand. Two arms, both , so both true means are . The policy: pull arm 1 once (reward ), arm 2 once (reward ), then pull again the arm whose first reward was higher (ties go to arm 1), and stop after three rounds. Let be the third reward. The four equally likely first outcomes give:
| arm pulled third | |||||
|---|---|---|---|---|---|
| 1 | |||||
| 2 | |||||
| 1 | |||||
| 1 |
Averaging over the four rows (probability each): , and by the same arithmetic . Both estimators are biased by , with no difference between the arms. The mechanism is visible in the table. When arm 2's first reward is a , it is not pulled again, so its low value is frozen into the estimate. When arm 1's first reward is , it is pulled again and has a chance to regress towards the truth. Bad luck gets locked in; good luck gets diluted.
Why the sum is fine and the ratio is not. Let . The process is a martingale (the indicator is determined by the past, and has conditional mean zero when ), so it has mean zero:
a version of Wald's identity. But the sample mean is , and . The numerator has mean zero, but the denominator is random and correlated with it: arms that did badly get a small . This is the classical failure of a ratio estimator when the denominator depends on the data, as in .
Naive intervals therefore lose their stated coverage after an adaptive run. Remedies exist, none free: sample splitting, inverse-propensity weighting when allocation probabilities are known (the subject of chapter 6), and the selective-inference correction of Nie et al. (2018).
Experiment: regret and bias
The companion code, ch02_bandits.py, runs these policies on five Bernoulli arms with means and horizon , averaged over runs:
- uniform: round-robin allocation forever, an A/B/n test that never acts on its result;
- explore-then-commit (ETC): pull each arm times, then commit to the arm with the best sample mean ( and );
- epsilon-greedy: with probability pull a uniformly random arm, otherwise the arm with the best sample mean; constant , and a decaying ;
- UCB1, with the index above.
The second part measures the bias of the final sample mean at for two-armed problems, under uniform allocation (as a control), epsilon-greedy, UCB1 and pure greedy (always pull the best sample mean after one pull of each arm). Verbatim output:
1. UCB1 indices (value = mean + sqrt(2 ln t / N)) for the worked example
case A: t=11 N=(6, 4) means=(0.667, 0.25) indices=(1.561, 1.345) -> pull arm 1
case B: t=22 N=(20, 2) means=(0.6, 0.5) indices=(1.156, 2.258) -> pull arm 2
2. Expected regret, K=5 Bernoulli arms mu=(0.6, 0.5, 0.45, 0.4, 0.3), T=5000, 2000 runs
entries: mean pseudo-regret (standard error)
algorithm T=100 T=500 T=1000 T=5000
uniform (A/B/n, no stopping) 15.0 ( 0.0) 75.0 ( 0.0) 150.0 ( 0.0) 750.0 ( 0.0)
ETC, m=25 per arm 15.0 ( 0.0) 26.3 ( 0.3) 33.3 ( 0.7) 86.5 ( 3.9)
ETC, m=100 per arm 15.0 ( 0.0) 75.0 ( 0.0) 76.6 ( 0.2) 80.6 ( 1.0)
eps-greedy, eps=0.1 8.1 ( 0.1) 25.9 ( 0.5) 40.7 ( 0.8) 112.8 ( 1.6)
eps-greedy, eps_t=min(1,5K/t) 11.1 ( 0.1) 25.1 ( 0.4) 34.1 ( 0.7) 74.7 ( 3.1)
UCB1 12.2 ( 0.0) 48.5 ( 0.1) 80.8 ( 0.2) 199.6 ( 0.6)
Reference curves (not simulated):
Lai-Robbins slope: sum Delta/kl 56.6 76.3 84.8 104.6
UCB1 bound (Auer et al. Thm 1) 924.3 1246.1 1384.8 1706.7
Lai-Robbins coefficient sum_a Delta_a / kl(mu_a, mu*) = 12.28 (regret ~ coef * ln T)
UCB1 coefficient 8 * sum_a 1 / Delta_a = 200.0
3a. Exact toy: two Bernoulli(0.5) arms, pull each once, pull the arm with the
higher first reward a third time (tie: arm 1). Exact E[mean_a] after 3 rounds.
E[mean_1] = 0.4375, E[mean_2] = 0.4375, true mean 0.5, bias = -0.0625 each
3b. Bias of the final sample mean, T=200, 40000 runs. bias = E[mean_a - mu_a] (se)
arms mu policy bias arm 1 bias arm 2 E[N_1]
(0.5, 0.5) uniform +0.0003 (0.0002) -0.0002 (0.0002) 100.0
(0.5, 0.5) eps-greedy 0.1 -0.0359 (0.0004) -0.0499 (0.0005) 111.3
(0.5, 0.5) UCB1 -0.0087 (0.0003) -0.0089 (0.0003) 100.1
(0.5, 0.5) greedy -0.1377 (0.0011) -0.2614 (0.0012) 125.5
(0.6, 0.4) uniform +0.0001 (0.0002) -0.0000 (0.0002) 100.0
(0.6, 0.4) eps-greedy 0.1 -0.0105 (0.0003) -0.0304 (0.0006) 170.7
(0.6, 0.4) UCB1 -0.0020 (0.0002) -0.0189 (0.0004) 150.1
(0.6, 0.4) greedy -0.1027 (0.0011) -0.2275 (0.0011) 160.4Fixed allocation is expensive. Uniform allocation has regret exactly the mean gap, : at , with no randomness in the pseudo-regret. By every other row is at or below against . ETC with is uniform for its first rounds, hence the identical at .
Constant- has linear regret. Between and it adds regret over rounds, about per round, close to the (mean gap ) you would expect from pure random exploration plus occasional wrong greedy choices. Decaying fixes this, and at this horizon it is among the best, but its schedule uses constants we chose by hand.
ETC is excellent here, and fragile. With the policy pays to explore, almost always commits to the right arm, and ends at . With the exploration is cheap ( per rounds) but the commitment sometimes goes to the wrong arm. Its standard error at (, against for ) reveals a heavy-tailed outcome: most runs commit well, a few lock onto a bad arm for the remaining rounds. Tuning needs the gaps in advance, which is what you do not know.
UCB1 has the slowest start and the weakest numbers here, but the most robust guarantee. Its regret at is , higher than ETC and decaying , which exploit tuned constants. Its growth from to is over units of , a slope near per unit of . That is between the Lai–Robbins coefficient () and UCB1's guaranteed coefficient (), consistent with both, and the proved bound () is loose by almost an order of magnitude. UCB1 needs no tuning and no knowledge of the gaps or . Its width is conservative for Bernoulli rewards (Hoeffding uses only the range); shrinking it helps in practice, as exercise 3 shows, but voids the guarantee.
The Lai–Robbins line is an asymptotic lower bound, not a finite- floor. Some finite-horizon rows lie below it: ETC at () and decaying epsilon-greedy () are under . That is not a contradiction: the bound constrains consistent policies as . ETC with a fixed has a positive probability of committing to a wrong arm forever, so it is not consistent and its regret is linear in the long run; at it simply has not yet paid for that.
Adaptive sampling biases the sample mean. The toy (3a) shows the exact bias, per arm, from the hand calculation, matching the table above. Table 3b shows it at : uniform allocation is unbiased within noise (all entries within two standard errors of ); every adaptive policy is negative, by for UCB1 up to and for greedy sampling. Three patterns:
- More aggressive exploitation, more bias. Greedy locks onto an arm after a few lucky pulls and never revisits the other, which stays frozen near whatever its early noise said. UCB1 keeps revisiting every arm, so its bias is smallest.
- The weaker arm is more biased. With means , UCB1's bias is for the best arm and for the worse one, which is pulled less (E of ) and so has less opportunity to correct a bad start.
- Tie-breaking can matter. In the epsilon-greedy row, arm 2 is biased more than arm 1 ( against ) even though the arms are identical, because ties in the greedy step go to arm 1 in this code. The design is not symmetric.
All the biases above are negative, consistent with the sampling mechanism studied in the cited papers. We did not simulate optimistic stopping or choosing, where Shin et al. show the bias can be positive.
Exercises
- (Pen and paper.) Two arms with gap . ETC pulls each arm times, then commits to the arm with the higher sample mean, for horizon . Show (apply Hoeffding to the i.i.d. variables ). Then choose to make the two terms comparable and say how the resulting regret scales with .
- (Pen and paper.) At round , arm 1 has pulls and sample mean ; arm 2 has pulls and sample mean . Compute both UCB1 indices and say which arm is pulled. How many successes would arm 2's five pulls need for its sample mean to match arm 1's?
- (Change the code and predict.) In the UCB1 index replace the constant in by . Predict whether regret at goes up or down, and whether the standard error changes. Then run it and also try .
- (Pen and paper.) In the three-round toy, show that (Wald) while . Write down the first quantity from the table.
Sources
- Tor Lattimore and Csaba Szepesvári (2020). Bandit Algorithms. Cambridge University Press. Chapter 5 (concentration), Chapter 7 (UCB), Chapter 16 (instance-dependent lower bounds).
- Richard S. Sutton and Andrew G. Barto (2018). Reinforcement Learning: An Introduction, 2nd edition. MIT Press. Chapter 2 (multi-armed bandits).
- Peter Auer, Nicolò Cesa-Bianchi and Paul Fischer (2002). Finite-time analysis of the multiarmed bandit problem. Machine Learning 47, 235–256. (Open copy: author's PDF.)
- Tze Leung Lai and Herbert Robbins (1985). Asymptotically efficient adaptive allocation rules. Advances in Applied Mathematics 6(1), 4–22.
- Wassily Hoeffding (1963). Probability inequalities for sums of bounded random variables. Journal of the American Statistical Association 58(301), 13–30.
- Xinkun Nie, Xiaoying Tian, Jonathan Taylor and James Zou (2018). Why adaptively collected data have negative bias and how to correct for it. AISTATS 2018.
- Jaehyeok Shin, Aaditya Ramdas and Alessandro Rinaldo (2019). Are sample means in multi-armed bandits positively or negatively biased? NeurIPS 2019.