Upper confidence bound (UCB) selects actions by adding an uncertainty bonus to each action’s estimated reward. It therefore favors promising actions while still giving less-tested actions a chance to improve their estimates. section 12.1.6
A practical UCB loop follows these steps:
- Play every arm once to initialize its play count and accumulated reward. section 12.1.6
- For each later trial, calculate estimated mean reward plus c × sqrt(log(t) / number of plays). section 12.1.6
- Select the arm with the greatest score, sample its reward, and update its count and reward total. section 12.1.6
- Tune c to control exploration, then compare average regret with explore-only, greedy, and epsilon-greedy strategies. section 12.1.6
Three slot machines
Consider 90 trials with machines whose payoff distributions have means and standard deviations of (8, 0.5), (10, 0.7), and (5, 0.45). A NumPy implementation samples each machine initially, computes UCB scores thereafter, and records regret against the best mean reward. With c = 2, the reported average regret is 0.0378, compared with 0.3466 for epsilon-greedy and 2.246 for explore-only. section 12.1.6
Figure 12.7 Three slot machines as a noncontextual multi-armed bandit (MAB) problem. The payoff distribution’s mean and standard deviations of these three machines are (8,0.5), (10, 0.7), and (5, 0.45), respectively.
Use the slot-machine figure to compare the arms’ payoff means and variability before tracing how UCB balances estimated reward with uncertainty. section 12.1.6

