Optimism: Upper Confidence Bounds
Optimism in the face of uncertainty, UCB1 and logarithmic regret.
Greedy play fails because it trusts its estimates; ε-greedy fails because it explores blindly forever. Optimism fixes both with one rule: act as if every arm is as good as the data still allow. Built from a concentration inequality, the rule becomes UCB1, whose regret grows only logarithmically, and the same rule reappears in tree search, in exploration of whole MDPs and in the bonuses of deep RL agents.
What you’ll learn
- State Hoeffding's inequality and turn it into an upper confidence bound on an arm's mean.
- Implement UCB1 and explain its index as optimism in the face of uncertainty.
- Explain why each suboptimal pull shrinks its own bonus, and state UCB1's logarithmic regret bound, its conditions and its gap-free form.
- Compare UCB1 with ε-greedy and Thompson sampling in simulation, and explain where its constants hurt.
- Recognize the UCB rule in Monte Carlo tree search and in the exploration bonuses of reinforcement learning.
Optimism in the face of uncertainty
Every arm has a range of means that the data so far cannot rule out. Optimism in the face of uncertainty says: score each arm by the top of its range, and play the arm with the highest score. The rule is self-correcting. If the chosen arm really is as good as its optimistic score, playing it costs little. If it is not, playing it produces exactly the data that lower its score. Either you earn or you learn, and arms that are clearly bad stop being played because their optimistic scores fall below the best arm's.
Optimism is not wishful thinking: the top of the range is a calibrated confidence bound, so the score of the best arm sits above its true mean with high probability. That single property, the optimal arm is rarely underestimated, drives every analysis in this lesson.
Think first A confidence bound can fail in two directions. Which failure is dangerous for an optimistic algorithm: a suboptimal arm's upper bound that is too high, or the best arm's upper bound that is too low?
The second. A suboptimal arm whose bound is too high gets a few extra pulls, and those pulls bring its average and its bonus down: the error corrects itself. If the best arm's bound falls below a suboptimal arm's mean, the best arm can stop being played, and since it is not played its estimate does not improve. Its bonus does grow slowly, through the term, so it is eventually retried, but meanwhile regret accrues at a constant rate. This is why the confidence level must tighten over time, making such failures rarer as grows.
Confidence bounds from Hoeffding’s inequality
Set the lower-tail probability equal to a confidence level and solve for . With probability at least the mean lies below the upper confidence bound:
An algorithm consults the bound in every round, so the failure probability must shrink as rounds accumulate: a union bound over rounds, and over the possible sample counts, must stay summable. UCB1 takes in round :
So UCB1's index is a Hoeffding upper confidence bound at confidence level . Its bonus shrinks like as an arm is played and grows like while it is not.
Rewards lie in . After pulls, how wide is the Hoeffding margin at confidence level ?
Show the answer
0.12
. With probability at least 95%, the mean is at most the average plus 0.12. Quadrupling the data halves the margin (0.061 at ), and asking for more confidence widens it only logarithmically (0.152 at ).
| Pulls | UCB1 at , | ||
|---|---|---|---|
| 25 | 0.245 | 0.303 | 0.743 |
| 100 | 0.122 | 0.152 | 0.372 |
| 400 | 0.061 | 0.076 | 0.186 |
UCB1
import numpy as np
def ucb1(pull, k, T):
"""pull(i) returns a reward in [0, 1] from arm i. Runs UCB1 for T >= k rounds."""
n = np.zeros(k) # pulls per arm
mean = np.zeros(k) # sample averages
for t in range(1, T + 1):
if t <= k:
i = t - 1 # play every arm once
else:
i = int(np.argmax(mean + np.sqrt(2 * np.log(t) / n)))
r = pull(i)
n[i] += 1
mean[i] += (r - mean[i]) / n[i] # incremental average
return n, meanUCB1 (Auer, Cesa-Bianchi & Fischer, 2002), adapted from an index policy of Agrawal (1995). It is deterministic given the rewards, needs no parameter and no horizon, and costs per round.
An arm has not been pulled for a long time while the others have. What happens to its UCB1 index?
- It stays fixed, because its average and count do not change
- It rises slowly, because grows while its count stays fixed, so the arm is eventually pulled again
- It falls, because the other arms' indices rise
- It becomes infinite after a fixed number of rounds
Show the answer
B. It rises slowly, because grows while its count stays fixed, so the arm is eventually pulled again
The bonus grows with when is frozen, so no arm is ever abandoned for good. Because the growth is only logarithmic, a clearly bad arm is retried rarely: its count grows like , which is exactly the logarithmic regret.
Why the regret is logarithmic
Suppose every confidence bound holds. Then the best arm's index is at least , so a suboptimal arm is played in round only if its own index reaches :
Its average is also at most its mean plus the margin, . Combine the two and use :
So arm stops being played once it has about pulls, and each pull costs . The failures of the bounds have probability about for each round and each pair of counts, which sums to a constant. Regret, by the decomposition of Multi-Armed Bandits & Regret:
On the regret lab's 'easy' preset the best arm pays 0.9 and the runner-up 0.7 (). At , how many pulls of the runner-up does the leading term allow?
Show the answer
1520
: three quarters of the horizon. At this horizon the guarantee says almost nothing, although UCB1 does far better in practice: in the regret lab it puts about 86% of its 2,000 pulls on the best arm, so all four suboptimal arms together get about 280. The factor 8 comes from the worst case of the analysis; the asymptotic analysis of The Regret of UCB, Proved brings it down to 2, and KL-UCB further to the Lai–Robbins constant.
The bound blows up for tiny gaps, but a tiny gap also makes each mistake cheap. Fix a threshold . Arms with cost at most per pull, at most in total; the others obey the theorem with :
Balance the first two terms at :
No algorithm can beat order on every instance (Regret Lower Bounds), so UCB1 is within a factor of the best worst-case guarantee, while also being logarithmic on every fixed instance.
UCB in practice, and beyond bandits
Tuning the bonus. Practitioners often scale the bonus, , and tune (Sutton and Barto use on their testbed); a smaller explores less and can win at short horizons, at the cost of the guarantee. UCB1-Tuned replaces the range bound by the arm's empirical variance, with the sample variance plus a correction; Auer et al. found it substantially better than UCB1 in essentially all their experiments but could not prove a regret bound. UCB-V (Audibert, Munos & Szepesvári, 2009) obtains a provable variance-adaptive index from an empirical Bernstein inequality. KL-UCB and MOSS sharpen the constant and remove the (The Regret of UCB, Proved).
Optimism versus posterior sampling. On the bandit widget above Thompson sampling beat UCB1 by a factor of three, and this is common in experiments. Both have logarithmic regret; they differ in constants and in how they explore: UCB's bonus is a worst-case width that must hold for every plausible instance, while a posterior sample is wide only where the data leave real doubt.
Tree search. UCT (Kocsis & Szepesvári, 2006) treats every node of a search tree as a bandit over its children and selects the child maximizing , which turns Monte Carlo rollouts into a focused, asymptotically consistent search (Monte Carlo Tree Search). AlphaZero's PUCT rule descends from it, with a learned prior shaping the bonus.
Reinforcement learning. In an MDP the same idea becomes an exploration bonus added to the reward, as in MBIE-EB (Strehl & Littman, 2008), or confidence sets over whole models, as in UCRL2 and UCBVI. Deep RL agents replace the counts by pseudo-counts and prediction errors.
Why can't UCB1's regret theorem be applied verbatim at an internal node of a Monte Carlo search tree?
- Because tree nodes have more than two children
- Because rewards in games are not in
- Because UCB1 needs to know the horizon
- Because a child's returns are not i.i.d.: they drift as the search below that child improves its own choices
Show the answer
D. Because a child's returns are not i.i.d.: they drift as the search below that child improves its own choices
The value observed through a child depends on the policy used deeper in the tree, which keeps changing as statistics accumulate, so each node faces a nonstationary bandit. Kocsis and Szepesvári analyzed UCT under this drift, showing that the bias at the root vanishes as the number of simulations grows.
Key takeaways
- Optimism in the face of uncertainty plays the arm with the highest plausible mean: either the guess is right and you earn, or it is wrong and the pull corrects it.
- Hoeffding's inequality gives the upper confidence bound for rewards in ; UCB1's bonus is this bound at level .
- A suboptimal arm stops being played once its bonus is about , after about pulls; UCB1's regret is at most for rewards in , and in the worst case.
- The guarantee fixes the rate, not the constant: at short horizons UCB1's conservative bonus can lose to tuned ε-greedy and often loses to Thompson sampling; variance-aware and KL-based indices narrow the gap.
- The UCB rule reappears as UCT in tree search and as exploration bonuses and optimistic models in reinforcement learning.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
UCB1's bonus equals the Hoeffding margin for which confidence level?
- in every round
Show the answer
C.
gives , so . The failure probability shrinks fast enough that, summed over all rounds and sample counts, failures contribute only a constant to the regret.
Which statement about UCB1 on a fixed stochastic bandit is true?
- It never stops pulling a suboptimal arm entirely, but pulls it only times
- It stops pulling a suboptimal arm after exactly pulls
- It needs to know the horizon to set its bonus
- Its regret on a fixed instance grows like
Show the answer
A. It never stops pulling a suboptimal arm entirely, but pulls it only times
The in the bonus keeps every arm's index creeping up while it is idle, so every arm is retried forever, but ever more rarely: about pulls in total, hence logarithmic regret on any fixed instance.
UCB1's bound is huge when some gap is tiny. Why does that not make UCB1's worst-case regret huge?
- Because tiny gaps never occur in practice
- Because each pull of an arm with a tiny gap costs only that gap, so such arms contribute at most ; balancing the two regimes gives
- Because UCB1 eliminates arms with tiny gaps
- Because the bound holds only for gaps above 0.1
Show the answer
B. Because each pull of an arm with a tiny gap costs only that gap, so such arms contribute at most ; balancing the two regimes gives
Split the arms at a threshold : below it regret is at most , above it at most . Choosing gives on every instance.
Why does UCB add the confidence bonus to the average instead of subtracting it?
- Because rewards are nonnegative
- Because adding makes the index unbiased
- Because subtracting would make the algorithm randomized
- Because subtracting would make rarely tried arms look worse, so they would never be tried and greedy lock-in would become systematic
Show the answer
D. Because subtracting would make rarely tried arms look worse, so they would never be tried and greedy lock-in would become systematic
Optimism sends the learner to the arms it knows least about, where the information is. Pessimism (a lower bound) is right only when no more data can be collected, as in offline RL.
Go deeper
- The Regret of UCB, Proved · Concentration, a union bound and log T regret step by step, plus KL-UCB’s sharper bonus.
- Optimism in MDPs: UCRL2 & UCBVI · Confidence sets over models and values, and the regret of exploring a whole MDP.
- Exploration Bonuses at Scale · Counts, pseudo-counts, curiosity and random network distillation in deep RL.
Further reading
- P. Auer, N. Cesa-Bianchi & P. Fischer, "Finite-time analysis of the multiarmed bandit problem," Machine Learning 47, 235–256 (2002).
- W. Hoeffding, "Probability inequalities for sums of bounded random variables," Journal of the American Statistical Association 58(301), 13–30 (1963).
- T. Lattimore & C. Szepesvári, Bandit Algorithms, ch. 5, 7 and 8 (Cambridge University Press, 2020).
- L. Kocsis & Cs. Szepesvári, "Bandit based Monte-Carlo planning," European Conference on Machine Learning (2006).