Search

Search papers and lessons

Multi-Armed Bandit

Level 3 · 14 min read

A multi-armed bandit is a repeated choice among options with unknown payoffs, where every pull both earns reward and reveals information, so the agent must balance trying options against using the best one.

On this page

  • Colour = topic mix
  • Probability
  • Reinforcement learning
  • Discrete math
  • Sign in to see your progress

TL;DR

A multi-armed bandit is a repeated choice among KK options, called arms. Each arm pays a random reward with an unknown average, and you learn those averages only by pulling the arms. Every pull therefore does two jobs at once: it earns a reward, and it gives information about the arm you pulled. The problem is to keep the total reward as high as possible while still testing arms that look worse.

Where you'll see it: in choosing which advert or headline to show when click rates are unknown, in clinical trials that assign patients to treatments as they learn, and as the exploration step inside Monte Carlo Tree Search.

Intuition

Picture a row of slot machines. Each has its own payout rate, and you do not know any of them. You have a budget of a hundred pulls. If you find a machine that paid well on the first three pulls and then play only that one, you are exploiting what you know. If you keep trying the others, you are exploring, and you pay for it in reward, because some of those pulls go to worse machines.

The trade-off is the whole problem. Pure exploitation looks sensible but can lock onto a machine that was lucky early. Pure exploration wastes pulls on machines already known to be poor. A good strategy explores more when it is uncertain and exploits more when the evidence is clear.

The figure below shows the bonus that one standard strategy, UCB1, adds to an arm's estimate. The bonus is large for an arm pulled few times and shrinks as it is pulled more, so uncertain arms keep getting a look.

5010015020000.511.52total pulls texploration bonus
  • arm pulled 4 times
  • arm pulled 40 times
The bonus grows slowly with total pulls t and shrinks with the arm's own pull count n. A rarely tried arm keeps a large bonus.

The math

Setup

There are KK arms. Arm ii pays a reward XiX_{i} with unknown mean μi=E[Xi]\mu_i = \mathbb{E}[X_i]. The best arm has mean μ∗=max⁡iμi\mu^* = \max_i \mu_i, and the gap of arm ii is Δi=μ∗−μi\Delta_i = \mu^* - \mu_i.

At each round t=1,2,…,Tt = 1, 2, \dots, T the agent picks an arm AtA_t and observes its reward. The rewards are assumed independent across pulls and, for simplicity, lie in [0,1][0, 1].

Each arm has a running estimate. Let ni(t)n_i(t) be the number of times arm ii was pulled up to round tt. The sample mean is

μ^i(t)=1ni(t)∑s≤t: As=iXs.\hat{\mu}_i(t) = \frac{1}{n_i(t)} \sum_{s \le t:\, A_s = i} X_{s}.

By the Law of Large Numbers, μ^i(t)\hat{\mu}_i(t) converges to μi\mu_i as ni(t)n_i(t) grows. The difficulty is that the agent must decide which arms to keep sampling while the estimates are still noisy.

Regret

The natural measure of performance is regret: how much reward is lost compared with always playing the best arm.

RT=Tμ∗−E[∑t=1TXAt]=∑iΔi E[ni(T)].R_T = T\mu^* - \mathbb{E}\Big[\sum_{t=1}^{T} X_{A_t}\Big] = \sum_{i} \Delta_i\, \mathbb{E}[n_i(T)].

The second form shows the structure. Regret comes from pulling suboptimal arms, weighted by how much worse they are. A strategy is good when its regret grows slowly in TT. A strategy that never stops exploring has regret that grows linearly in TT.

Strategy 1: epsilon-greedy

With probability ε\varepsilon pull a uniformly random arm. Otherwise pull the arm with the highest sample mean. The rule is simple and has a single knob, ε\varepsilon. With a fixed ε\varepsilon it keeps exploring forever, so its regret is linear in TT. A decaying schedule such as εt=1/t\varepsilon_t = 1/t fixes that but needs knowledge of the gaps to tune well.

Strategy 2: UCB1

UCB1 ("upper confidence bound") pulls each arm once, then at round tt pulls the arm with the largest index

UCBi(t)=μ^i(t)+2ln⁡tni(t).\mathrm{UCB}_i(t) = \hat{\mu}_i(t) + \sqrt{\frac{2 \ln t}{n_i(t)}}.

The first term favours arms that have paid well. The second is the exploration bonus. It is large when ni(t)n_i(t) is small compared with ln⁡t\ln t, which is the case for an arm that has been tried rarely. The index is an optimistic estimate: the arm's true mean is unlikely to lie above it.

A standard result (Auer, Cesa-Bianchi and Fischer, 2002) bounds the regret of UCB1 for rewards in [0,1][0, 1]:

RT≤∑i: Δi>08ln⁡TΔi+(1+π23)∑iΔi.R_T \le \sum_{i:\,\Delta_i > 0} \frac{8 \ln T}{\Delta_i} + \Big(1 + \frac{\pi^2}{3}\Big)\sum_{i} \Delta_i.

The first term grows only logarithmically in TT. That is the sense in which UCB1 explores efficiently: it stops pulling an arm at a rate proportional to its gap.

Strategy 3: Thompson sampling

Keep a posterior belief about each arm's success probability. For a Bernoulli arm, which pays 1 with probability pip_i and 0 otherwise, a uniform prior on pip_i gives a Beta(1,1)(1,1) prior. After sis_i successes and fif_i failures, the posterior is Beta(1+si,1+fi)(1 + s_i, 1 + f_i). Each round, draw one sample from each posterior and pull the arm with the largest sample. Exploration happens because posteriors of uncertain arms are wide and sometimes produce the largest draw.

Worked examples

Example
One UCB1 decision

Two arms. After t=10t = 10 total pulls:

  • Arm A has been pulled nA=4n_A = 4 times with sample mean μ^A=0.25\hat{\mu}_A = 0.25.
  • Arm B has been pulled nB=6n_B = 6 times with sample mean μ^B=0.50\hat{\mu}_B = 0.50.
  1. ln⁡10≈2.3026\ln 10 \approx 2.3026, so 2ln⁡10≈4.60522 \ln 10 \approx 4.6052.
  2. Bonus for A: 4.6052/4=1.1513≈1.0730\sqrt{4.6052 / 4} = \sqrt{1.1513} \approx 1.0730. So UCBA=0.25+1.0730=1.3230\mathrm{UCB}_A = 0.25 + 1.0730 = 1.3230.
  3. Bonus for B: 4.6052/6=0.7675≈0.8761\sqrt{4.6052 / 6} = \sqrt{0.7675} \approx 0.8761. So UCBB=0.50+0.8761=1.3761\mathrm{UCB}_B = 0.50 + 0.8761 = 1.3761.
  4. B has the larger index, so UCB1 pulls B again.

The result is close, and the reason matters. A has a much lower sample mean, but it has been pulled fewer times, so its bonus is larger. Had A's mean been 0.400.40 instead, the index would be 0.40+1.0730=1.47300.40 + 1.0730 = 1.4730 and A would be pulled. The bonus is what keeps the choice honest when the evidence is thin.

Example
Epsilon-greedy and its exploration cost

Two arms with true success rates pA=0.3p_A = 0.3 and pB=0.5p_B = 0.5, horizon T=1000T = 1000. The gap is ΔA=0.2\Delta_A = 0.2.

  1. Always playing A costs T⋅0.2=200T \cdot 0.2 = 200 in regret, so it is a bad policy.
  2. Epsilon-greedy with ε=0.1\varepsilon = 0.1 spends about εT=100\varepsilon T = 100 pulls on random arms.
  3. Half of those random pulls land on A, the worse arm. That is 5050 wasted pulls, each costing 0.20.2.
  4. Regret from exploration alone is about 50⋅0.2=1050 \cdot 0.2 = 10.
  5. Once the estimates settle, the greedy pulls mostly go to B, so the remaining regret is small.

The cost is 1010 out of a possible 200200, a large saving. But it is linear in ε\varepsilon: doubling the horizon doubles the regret from exploration. UCB1 would reduce the exploration of A as its evidence accumulates, which is why its regret grows only logarithmically.

Tradeoffs and failure modes

SituationProblemBetter choice
Rewards are stationary and you want a simple baselineEpsilon-greedy explores at a fixed rate foreverStart with epsilon-greedy and a decaying schedule
You need provable logarithmic regretEpsilon-greedy has linear regretUCB1
You can encode a prior and want good empirical performanceUCB1 ignores prior knowledgeThompson sampling
Rewards drift over timeSample means average over stale dataDiscounted or sliding-window estimates, or a non-stationary bandit
Choices change the state of the worldA bandit assumes the same choice set and no stateA Markov decision process, see Markov Decision Process
Early samples are very fewConfidence bounds are loose and estimates are noisyUse a prior, or initialise each arm with a few pulls

The most common failure is treating a bandit as a supervised problem. Each pull only reveals the reward of the arm chosen, so the data is biased toward arms the agent already favours. Averaging the observed rewards without accounting for this gives misleading estimates of the arms that were rarely tried.

History

William R. Thompson described a posterior-sampling rule for choosing between two treatments in a 1933 paper, which is now called Thompson sampling. The name "bandit" comes from the slot machine analogy ("one-armed bandit"). Herbert Robbins's early 1950s work on the sequential design of experiments helped make the problem a standard object of study.

Work on the optimal solution followed. John Gittins showed in the late 1970s that a simple index policy solves a discounted version of the problem exactly, now called the Gittins index. Peter Auer and colleagues gave the UCB1 algorithm and its finite-time regret bound in 2002, which made the strategy practical for general use. Bandit algorithms are now widely used in web experimentation and recommendation, and the same exploration idea appears in reinforcement learning methods such as tree search.

Check yourself

QuestionThree arms have sample means 0.6, 0.4 and 0.5. Which arm does pure greedy choose, and what can go wrong?Show answer

Pure greedy pulls the arm with mean 0.6. The problem is that a sample mean from few pulls is noisy. If the 0.6 arm got lucky early, greedy will keep pulling it and never learn the true means of the others. The gap may be real, but the agent has no way to know how confident its estimates are, and that is exactly what exploration bonuses and posterior sampling try to supply.

QuestionIn the first worked example, what sample mean would arm A need, with its 4 pulls and the same total of t=10t = 10, to have a UCB1 index above arm B's?Show answer

Arm B's index is 1.37611.3761, and A's bonus is 1.07301.0730. A needs μ^A+1.0730>1.3761\hat{\mu}_A + 1.0730 > 1.3761, so μ^A>0.3031\hat{\mu}_A > 0.3031. A sample mean above about 0.300.30 would make A the choice. This shows how large the bonus is relative to the reward scale: with only 4 pulls, the bonus alone can outweigh a gap of 0.250.25 in observed means.

QuestionWhy does epsilon-greedy with a fixed epsilon have linear regret, while UCB1 has logarithmic regret?Show answer

Epsilon-greedy keeps spending a fixed fraction of pulls on random arms, and each such pull on a suboptimal arm costs its gap. That loss accumulates at a constant rate, so total regret grows linearly with TT. UCB1's bonus shrinks as an arm is sampled, so the number of pulls it gives a suboptimal arm grows only like ln⁡T\ln T, and the regret bound reflects that: the leading term is ∑8ln⁡T/Δi\sum 8 \ln T / \Delta_i.

QuestionA news site has three headlines with unknown click rates and receives 50,000 visitors a day. Which strategy would you choose, and what would you watch for?Show answer

Thompson sampling is a good fit: it handles small differences well, uses a prior from earlier campaigns if available, and needs little tuning. Watch for non-stationarity, since headline interest changes with the news cycle. Sample means from the whole history would then lag reality, so a discounted or windowed version is safer. Also check that each headline gets enough impressions to make the posterior meaningful before you retire it.

Sources