Lecture 2 of 1417 minInteractiveFree lecture

Multi-armed bandits: explore or exploit?

Five slot machines, unknown odds and a limited number of pulls. The simplest reinforcement-learning problem contains its deepest dilemma — use what you know, or try something new? Play the machines yourself, then race greedy, ε-greedy, UCB and Thompson sampling.

In this lecture you will

  • Define the multi-armed bandit problem and why it matters in practice
  • Estimate an action’s value from the rewards it has produced
  • Explain the exploration–exploitation trade-off and why pure greed fails
  • Compare ε-greedy, upper confidence bounds and Thompson sampling

Imagine walking into a casino with five slot machines — the “one-armed bandits” that give this problem its name. Each machine pays out with its own hidden probability. You have a limited number of pulls. How do you walk out with the most money?

This is the multi-armed bandit problem: reinforcement learning with a single state. There is no position to move through, no long-term consequences of your choice — just repeated decisions between actions with unknown value. It is simple enough to solve well, and it contains the dilemma at the heart of all reinforcement learning.

Play the machines

Pull the levers yourself. Each bar shows your current estimate of that machine’s win rate — simply the fraction of its pulls that paid out.

InteractiveFive slot machines with hidden oddsClick a machine to pull it
Loading visual…

Estimates from a handful of pulls are noisy. Reveal the true odds after twenty or thirty pulls and see how close you got — and how many pulls you spent on bad machines.

The estimate for action aa after it has been tried nn times is its average reward:

Q(a)=r1+r2+⋯+rnnQ(a) = \frac{r_1 + r_2 + \dots + r_n}{n}

Notice how you played. At first you probably tried everything. Then, as one machine looked good, you pulled it more — but every pull on it was a pull not spent learning about the others.

The dilemma

Every decision is a choice between two goals:

  • Exploit: pull the machine that currently looks best, collecting reward now.
  • Explore: pull another machine, to improve your estimates and perhaps discover something better.

Exploit too early and you may lock onto a mediocre machine forever. Explore too much and you waste pulls on machines you already know are bad.

AnimationWhy pure greed gets stuck
Loading animation…
  1. Estimates after a few pullsMachine A has paid out 40% of the time and B 60%. Machine C has never been tried, so the agent knows nothing about it.
  2. Greedy keeps pulling BA greedy agent always chooses the highest estimate. B stays on top, so C is never tried — even though, unknown to the agent, C is the best machine.
  3. ε-greedy flips a coinε-greedy adds a little randomness. On each pull, with probability 1 − ε (here 90%) it exploits the best estimate; otherwise it explores.
  4. Exploration finds COn an exploration pull, a random machine is chosen — C, which pays out. One pull is not proof, but now C is on the radar.
  5. The estimates improveFurther pulls of C bring its estimate close to its true 80% rate. Exploiting now means choosing C, and the agent earns more from here on.

Smarter exploration

ε-greedy explores blindly: it is just as likely to re-test a machine it knows is terrible as one it knows little about. Two better ideas:

ε-greedy
Exploit the best estimate most of the time; with probability ε choose a random action. Simple and surprisingly effective, but it never stops wasting a fraction ε of its pulls.
Upper confidence bound (UCB)
Add an uncertainty bonus to each estimate — large for rarely tried actions, shrinking as evidence accumulates — and pick the highest total. “Optimism in the face of uncertainty.”
Thompson sampling
Keep a probability distribution over each action’s true value, draw one plausible value for each, and pick the highest. Actions are explored in proportion to the chance that they are the best.
Regret
The reward lost compared with always choosing the best action. Good bandit algorithms keep total regret growing only slowly over time.

The race below runs each strategy on 150 different casinos of 10 machines and averages the results.

InteractiveBandit strategies compared
Loading visual…

Greedy often locks onto the wrong machine and stays there. ε-greedy improves steadily but keeps paying for random exploration. UCB and Thompson sampling focus their exploration where it matters.

Try changing ε. A larger ε finds the best machine sooner but keeps exploring forever, capping its performance; a tiny ε is efficient once it knows the answer but slow to learn it.

Bandits at work

Bandit algorithms are among the most widely deployed forms of reinforcement learning, because many business decisions look exactly like this:

  • Website and email testing: which headline, layout or subject line to show. A bandit shifts traffic to the winner during the test, losing less revenue than a fixed 50/50 A/B test.
  • Recommendations and ads: which item to show a user, balancing proven favourites against new items that need a chance.
  • Clinical trials and pricing: allocating patients or price points adaptively as evidence comes in.

A bandit has just one situation. Real problems have many — the robot’s position, a game board, a customer’s journey — and today’s action changes tomorrow’s situation. The next lecture introduces the framework that handles this: the Markov decision process.

Key takeaways

  • A bandit is a one-state RL problem — choose among actions with unknown average rewards, again and again.
  • The value of an action can be estimated by the average reward it has produced so far.
  • Pure greed can lock onto a mediocre action forever; some exploration is essential.
  • UCB explores actions whose value is still uncertain, and Thompson sampling explores in proportion to the chance an action is best — both waste far less than random exploration.

Check your understanding

4 questions · answers explained

Q1Machine B has paid out 12 times in 20 pulls; machine C has never been pulled. What does a purely greedy strategy do?

Greedy always picks the highest estimate. C’s estimate is still 0 (or unknown), so greedy never tries it — even if C is actually the best machine.

Q2With ε-greedy and ε = 0.1, what happens on each pull?

ε-greedy exploits most of the time and explores at random with probability ε. The random pulls keep refreshing the estimates of every machine.

Q3What is the key idea behind upper confidence bound (UCB) strategies?

UCB is optimism in the face of uncertainty. Rarely tried machines get a large bonus, so they are explored until the agent is confident they are worse.

Q4Which real-world problem is a natural fit for a bandit algorithm?

Each headline is an arm, a click is a reward, and every visitor is a pull. Bandits shift traffic to the winning headline during the test, instead of waiting until the end like a classic A/B test.