AI in Motion

Reinforcement LearningDeep diveIntermediate10:59 video31 chapters

Exploration and Multi-Armed Bandits — lecture notes

The purest form of the explore–exploit dilemma: greedy, ε-greedy, UCB and Thompson sampling, regret, and where bandits run in the real world — from A/B tests to recommendations.

▶ Watch the animated lecture

0:001. Introduction

Introduction — Exploration and Multi-Armed Bandits

Should you order your favourite dish again, or try something new on the menu? That everyday question is the exploration versus exploitation dilemma, and multi armed bandits are its purest mathematical form. In this deep dive we compare four strategies with real simulations.

0:182. The bandit problem

The bandit problem — Exploration and Multi-Armed Bandits

Imagine a row of slot machines, sometimes called one armed bandits. Each arm pays out with its own unknown probability. Every round you pull one arm and see whether it pays. Your goal is to win as much as possible. It is reinforcement learning with a single state: actions have no long term consequences.

0:413. The dilemma

The dilemma — Exploration and Multi-Armed Bandits

The dilemma is sharp. Exploiting means pulling the arm that looks best so far, earning reward now, but perhaps based on too little evidence. Exploring means pulling an uncertain arm to learn about it, which costs reward if it turns out worse. Good strategies balance the two.

1:014. Our bandit

Our bandit — Exploration and Multi-Armed Bandits

Our test bandit has five arms with true win rates of twenty five, fifty, eighty, thirty five and sixty percent. The agent does not know them. Arm three is the best, and arm five, at sixty percent, is a tempting runner up that can fool a careless strategy.

1:215. Estimating values

Estimating values — Exploration and Multi-Armed Bandits

Every strategy keeps an estimate of each arm’s value, usually the average reward it has paid so far: total reward divided by the number of pulls. It can be updated incrementally, nudging the estimate towards each new reward, exactly the same pattern as the TD updates in the previous lecture.

1:426. Greedy

Greedy — Exploration and Multi-Armed Bandits

The greedy strategy always pulls the arm with the highest current estimate. Here it tries each arm once, gets lucky with arm five, and then sticks with it for almost three hundred pulls. It never gives arm three, the true best, a fair second chance. That lost opportunity is the price of never exploring.

2:057. Pause and think

Pause and think — Exploration and Multi-Armed Bandits

Pause and think. The greedy agent pulled the best arm once and happened to get nothing. Why can it never recover? Its estimate for that arm is now zero, lower than the runner up, so it never pulls it again. Without new data, the wrong estimate can never be corrected.

2:268. ε-greedy

ε-greedy — Exploration and Multi-Armed Bandits

Epsilon greedy adds a little randomness. Ninety percent of the time it exploits the best estimate, and ten percent of the time it pulls a random arm. That occasional exploration is enough to discover arm three. After three hundred pulls it has chosen arm three two hundred and seventy five times.

2:489. Regret

Regret — Exploration and Multi-Armed Bandits

How do we score a strategy? With regret: the reward you missed compared with always pulling the best arm. Constant exploration with a fixed epsilon has regret that keeps growing in proportion to time. The best strategies achieve regret that grows only logarithmically, which means they eventually almost stop wasting pulls.

3:1010. Linear vs logarithmic regret

Linear vs logarithmic regret — Exploration and Multi-Armed Bandits

The shape of regret matters. A strategy that keeps exploring at a fixed rate wastes a constant fraction of pulls forever, so its regret grows in a straight line. The best strategies have logarithmic regret: they make fewer and fewer mistakes, so the curve flattens out over time.

3:3011. Optimistic initial values

Optimistic initial values — Exploration and Multi-Armed Bandits

A simple trick is optimistic initial values. Start every estimate high, for example at one for win rates. Even a purely greedy agent then tries every arm, because untried arms look perfect. Our greedy agent used exactly this, which is why it sampled each arm once before locking in.

3:5112. Optimism

Optimism — Exploration and Multi-Armed Bandits

A smarter principle is optimism in the face of uncertainty. Treat any arm you are unsure about as if it might be excellent. Rarely tried arms get a bonus, so they keep being tried until the uncertainty is resolved. Exploration then focuses on arms that could plausibly be the best, instead of being purely random.

4:1413. UCB

UCB — Exploration and Multi-Armed Bandits

UCB puts this into a formula. Each arm’s score is its estimated value plus an exploration bonus, the square root of two log t divided by the number of pulls. Rarely tried arms get a large bonus, and the bonus shrinks as evidence accumulates. Always pull the arm with the highest optimistic score.

4:3614. Compute a bonus

Compute a bonus — Exploration and Multi-Armed Bandits

Let us compute a bonus. At pull one hundred, an arm has been tried only four times. The bonus is the square root of two times the natural log of one hundred, about nine point two, divided by four. That is about one point five, larger than any possible win rate, so this arm will be tried again soon.

5:0015. UCB in action

UCB in action — Exploration and Multi-Armed Bandits

Here is UCB. Its exploration is systematic rather than random: every arm keeps getting occasional pulls while its bonus is large. On our bandit it still spends quite a few pulls on the weaker arms after three hundred rounds, because arms three and five are not so far apart. Over a longer horizon the bonus shrinks and UCB commits.

5:2416. Thompson sampling

Thompson sampling — Exploration and Multi-Armed Bandits

Thompson sampling, first proposed in 1933, takes a Bayesian view. It keeps a probability distribution over each arm’s win rate. Every round it samples one plausible value for each arm and pulls the arm whose sample is highest. Arms are explored in proportion to the probability that they are actually the best.

5:4617. Beliefs sharpen

Beliefs sharpen — Exploration and Multi-Armed Bandits

This is the kind of belief Thompson sampling maintains for each arm. A Beta distribution starts wide, and every success or failure sharpens it. An arm with a wide, uncertain belief sometimes produces a high sample and gets explored. An arm with a sharp, low belief is rarely chosen.

6:0718. Thompson in action

Thompson in action — Exploration and Multi-Armed Bandits

Here is Thompson sampling on our bandit. Early on it spreads its pulls across all the arms. As the beliefs sharpen, arm three wins the sampling contest more and more often. It concentrates on the best arm smoothly, without any tuning parameter at all.

6:2619. Head to head

Head to head — Exploration and Multi-Armed Bandits

Now the fair comparison: each strategy runs a hundred times for five hundred pulls, and we plot how often it chooses the best arm. By the end, Thompson sampling picks the best arm about ninety eight percent of the time, epsilon greedy about eighty nine, UCB about seventy five, and greedy about seventy four.

6:4920. Pause and think

Pause and think — Exploration and Multi-Armed Bandits

Pause and think. Why does epsilon greedy never reach one hundred percent best arm choices, even once it knows which arm is best? Because it keeps exploring at random ten percent of the time forever, so it plateaus around ninety percent. Decaying epsilon over time fixes this.

7:0921. Decaying exploration

Decaying exploration — Exploration and Multi-Armed Bandits

The simple fix for epsilon greedy is to decay epsilon over time: explore a lot at first, when little is known, and less later, once estimates are reliable. A schedule like c divided by t gives logarithmic regret in theory. Deep Q networks use the same idea, lowering epsilon from one to point one.

7:3222. Changing worlds

Changing worlds — Exploration and Multi-Armed Bandits

Real payout rates often drift over time. Plain averages give ancient evidence as much weight as fresh evidence, so they adapt slowly. Using a constant step size instead produces a recency weighted average in which old rewards fade away, so the agent can track a changing world.

7:5223. Strategy summary

Strategy summary — Exploration and Multi-Armed Bandits

Here is the summary. Greedy never really explores and can get stuck forever. Epsilon greedy explores blindly and needs decay. UCB adds a principled bonus, and Thompson sampling samples from its beliefs. Both UCB and Thompson achieve logarithmic regret, and Thompson is often strongest in practice.

8:1124. Bandits vs A/B tests

Bandits vs A/B tests — Exploration and Multi-Armed Bandits

Bandits are closely related to A B testing. A classic A B test splits traffic evenly for a fixed period, then decides. That gives clean statistics, but half the users see the worse version throughout. A bandit shifts traffic towards the better variant as evidence grows, losing less along the way.

8:3325. When to use which

When to use which — Exploration and Multi-Armed Bandits

Which should you use? A fixed A B test gives clean, well understood statistics and is best when you want to learn a durable truth. A bandit is better when earning reward during the test matters, like short lived promotions, though its statistics are harder to analyse and it can be fooled by trends over time.

8:5626. Contextual bandits

Contextual bandits — Exploration and Multi-Armed Bandits

Real systems usually know something about each situation. A contextual bandit observes a context, such as who the user is or what time it is, and learns which arm works best for that context. News sites have used contextual bandits to decide which headline to show each reader.

9:1727. Bandits in the wild

Bandits in the wild — Exploration and Multi-Armed Bandits

Bandits run in many places: recommendation and advertising systems, adaptive clinical trials that assign more patients to better treatments, hyperparameter search methods that give more compute to promising configurations, and the UCT rule inside Monte Carlo tree search, which AlphaGo used to decide which moves to explore.

9:3728. Exploration in full RL

Exploration in full RL — Exploration and Multi-Armed Bandits

In full reinforcement learning, exploration is harder, because the agent must reach new states, not just try new actions. Ideas from bandits carry over: optimistic initial values, bonuses for rarely visited states, curiosity rewards for surprising observations, and noisy networks. Hard games like Montezuma’s Revenge need these.

9:5729. Thompson sampling in code

Thompson sampling in code — Exploration and Multi-Armed Bandits

Thompson sampling is only a few lines of code. Start each arm with a flat Beta belief. Each round, sample a plausible win rate for every arm, pull the arm with the highest sample, observe the reward, and update that arm’s wins or losses. The Beta distribution does the rest.

10:1830. Pause and think

Pause and think — Exploration and Multi-Armed Bandits

Pause and think. The best headline on a news site changes every few days. Which strategies are most at risk? Any strategy that stops exploring, including fully converged UCB or Thompson sampling. Changing, non stationary problems need continued exploration, such as forgetting old data or keeping a minimum epsilon.

10:3931. Recap

Recap — Exploration and Multi-Armed Bandits

To recap. Bandits isolate the explore exploit dilemma. Greedy can lock onto a mediocre arm forever. Epsilon greedy explores randomly and should decay. UCB adds an optimism bonus, Thompson sampling samples from its beliefs, and regret is how we score them. Contextual bandits personalise the choice.

Key takeaways

  • A multi-armed bandit is RL with one state: pull arms, observe rewards, maximise the total.
  • Pure greedy can get stuck on a sub-optimal arm after an unlucky early estimate.
  • ε-greedy explores at random; with fixed ε it never stops exploring.
  • UCB adds a bonus √(2 ln t / N(a)); Thompson sampling samples from Beta posteriors.
  • In our 100-run test, Thompson chose the best arm ~98% of the time by pull 500, ε-greedy ~89%, UCB ~75%, greedy ~74%.
  • Contextual bandits power recommendations and ads; bandits also run inside MCTS (UCT).

Check yourself

  1. What does regret measure?
    Show answer

    Reward missed compared with always pulling the best arm — Regret = T·μ* − Σ rewards.

  2. In UCB, which arms receive a large bonus?
    Show answer

    Arms pulled rarely — The bonus shrinks with N(a).

  3. Thompson sampling chooses the arm with…
    Show answer

    The highest value sampled from its posterior belief — It samples plausible values from Beta posteriors.

  4. Why can pure greedy fail badly?
    Show answer

    An unlucky early estimate can make it ignore the best arm forever — No exploration means no correction.

  5. A contextual bandit differs from a plain bandit because it…
    Show answer

    Uses information about the situation to choose the arm — Choices depend on context.

Go deeper

© 2026 Janin A Apurba, CSE, AUST · Advanced ICT Officer, CNRS-UNHCR. All rights reserved. Notes for the animated lecture at https://ai-in-motion.vercel.app/watch/exploration-and-multi-armed-bandits.html