Skip to main content
© 2026 ePowerAI — instrumented learning, no login required.
CoursesContact
ePOWERAI
CoursesContact
Bandits: Exploration, Exploitation, and Regret
Reinforcement Learning
01Reinforcement Learning: Agents, Rewards, and MDPs
02Bandits: Exploration, Exploitation, and Regret
03Returns, Value, and the Bellman Equation
04Bandits: UCB1 and Thompson Sampling
05Week 1: Reinforcement Learning Problem Formulation
06Week 2: Multi-Armed Bandits
07Week 3: Dynamic Programming for Finite MDPs
08Week 4: Monte Carlo and Temporal-Difference Learning
09Week 5: Function Approximation in Reinforcement Learning
10Week 6: Deep Q-Learning and Variants
11Week 7: Policy Gradient and Actor–Critic Methods
12Week 8: Modern Deep Reinforcement Learning Algorithms
13Week 9: Exploration, Partial Observability, and Multi-Agent Reinforcement Learning
14Week 10: Model-Based Reinforcement Learning and Planning
15Week 11: Offline Reinforcement Learning
16Week 12: Reinforcement Learning from Human Feedback
17Week 13: Direct Preference Optimization and GRPO
18Week 14: Agentic Systems and Course Capstone
· Reinforcement Learning· Guided Path · Beginner10 min read

Bandits: Exploration, Exploitation, and Regret

Bandits: Exploration, Exploitation, and Regret

Learning Outcomes

By the end of this step you will:

  • Describe a multi-armed bandit problem and why it strips reinforcement learning down to one hard question
  • Explain the exploration–exploitation trade-off with an everyday example
  • Define regret and read the decomposition R(T)=∑aΔa E[NT(a)]\mathcal{R}(T) = \sum_a \Delta_a\, \mathbb{E}[N_T(a)]R(T)=∑a​Δa​E[NT​(a)]
  • Explain why a fixed exploration rate produces linear regret
Prerequisites
  • Agents, Rewards, and MDPs: rewards, policies, and the idea of choosing actions under uncertainty.
  • Expectation, Variance, and Covariance: averages, and averages of sums.

The next step continues directly from this one. Every cell runs in the page.

Strip away states and long-term consequences, and reinforcement learning still has a hard problem left: you can only learn how good an option is by taking it, and taking it might be the wrong choice right now. The bandit is that problem in its purest form.

A row of slot machines and no history to read#

A multi-armed bandit is a row of KKK slot machines — the "arms". Each arm aaa pays out a reward drawn at random from an unknown distribution with mean μa\mu_aμa​. Pull a lever, get a number, repeat.

That is the whole problem. There are no states, so nothing you do now changes what is available later. Each round stands alone. The agent's only goal is to collect as much reward as possible over TTT rounds.

Why study something so stripped down? Because it isolates the part of learning from experience that no amount of cleverness removes: the agent is uncertain about the world, and it can only reduce that uncertainty by acting. Every additional complication in a full MDP — states, delayed consequences, planning — sits on top of this statistical core. Get the core right here, and it transfers.

Where you meet bandits in real systems

Choosing which version of a web page to show users, which headline to test, which recommendation to place at the top — these are bandit problems, or close cousins of them. So is the comparison step when a language model is tuned from human preferences: each prompt is a round, and each response is an arm.

Explore or exploit#

At every round, the agent chooses between two kinds of action:

  • Exploit. Pull the arm that currently looks best. That maximises the reward right now.
  • Explore. Pull an arm you are unsure about. That gives up immediate reward in exchange for information that will improve later choices.

Everyone already understands this trade-off. You have a favourite restaurant; a new one opened across the street. Eating at the favourite is the safer, better meal tonight. Trying the new one costs you one good dinner, but without trying it you will never know whether it is better.

The failure of pure exploitation is worth seeing concretely. Suppose there are two arms. On the first two rounds, arm 1 happens to pay 1 and arm 2 happens to pay 0. A purely greedy agent now believes arm 1 is better and chooses it forever. But suppose the true means are μ1=0.1\mu_1 = 0.1μ1​=0.1 and μ2=0.9\mu_2 = 0.9μ2​=0.9 — the agent was simply unlucky once, and now it will lose almost everything for the rest of the run. The problem is not the decision it makes at round 3. The problem is that a single noisy sample has been treated as knowledge.

Exploiting without tracking uncertainty is fragile

Any rule of the form "pick the best estimate" has this weakness: it has no way to tell the difference between an estimate built from a thousand observations and one built from a single lucky draw. A good algorithm does not just track what it believes — it tracks how confident it should be.

The opposite mistake is just as bad. Pulling arms uniformly at random gathers information evenly, but it keeps paying the cost of bad arms forever. Learning and earning have to happen at the same time.

Regret: the reward you gave up#

Total reward alone is hard to interpret, because it depends on how generous the arms are. A better measure is relative: compare the agent against an oracle who knows the true means and always pulls the best arm.

Define the suboptimality gap of arm aaa as how far its mean sits below the best mean:

Δa=μ∗−μa,μ∗=max⁡a′μa′.\Delta_a = \mu^{*} - \mu_a, \qquad \mu^{*} = \max_{a'} \mu_{a'}.Δa​=μ∗−μa​,μ∗=a′max​μa′​.

The optimal arm has gap 0. A nearly-as-good arm has a tiny gap. A bad arm has a large one.

Regret after TTT rounds is the total reward the oracle would have collected beyond what the agent actually collected:

R(T)=Tμ∗−E ⁣[∑t=1Trt].\mathcal{R}(T) = T\mu^{*} - \mathbb{E}\!\left[\sum_{t=1}^{T} r_t\right].R(T)=Tμ∗−E[t=1∑T​rt​].

A useful rearrangement exposes exactly what the algorithm controls. Let NT(a)N_T(a)NT​(a) be the number of times arm aaa was pulled. Then, by linearity of expectation,

R(T)=∑a=1KΔa⋅E[NT(a)].\mathcal{R}(T) = \sum_{a=1}^{K} \Delta_a \cdot \mathbb{E}[N_T(a)].R(T)=a=1∑K​Δa​⋅E[NT​(a)].

Read that in words: regret is the sum, over arms, of how bad each arm is times how often you pulled it. This is the heart of bandit algorithm design. Two arms can both contribute heavily:

  • A terrible arm with a huge gap, pulled many times, is a clear mistake.
  • A nearly-optimal arm with a tiny gap is hard to tell apart from the best one, so identifying it takes many pulls — and those pulls add up.

A good algorithm spends its pulls where uncertainty is expensive: on arms that could plausibly be the best, and not on arms that have already been ruled out.

Good algorithms learn#

A sound algorithm should eventually stop wasting pulls. Formally, its regret should grow sublinearly:

R(T) grows slower than T.\mathcal{R}(T) \text{ grows slower than } T.R(T) grows slower than T.

Because regret is cumulative, "slower than TTT" means the average regret per round, R(T)/T\mathcal{R}(T)/TR(T)/T, tends to 0. The algorithm spends a shrinking fraction of its time making mistakes. Linear regret — regret proportional to TTT — means the opposite: the agent keeps pulling suboptimal arms at a constant rate and never finishes learning. That is the bar every algorithm in this lesson is measured against.

epsilon-greedy: the simplest fix, and its flaw#

The simplest algorithm that forces some exploration is ε\varepsilonε-greedy. With probability ε\varepsilonε it pulls an arm uniformly at random; the rest of the time it pulls the arm with the best average reward so far:

at={a random armwith probability εarg⁡max⁡aμ^awith probability 1−εa_t = \begin{cases} \text{a random arm} & \text{with probability } \varepsilon \\ \arg\max_a \hat{\mu}_a & \text{with probability } 1 - \varepsilon \end{cases}at​={a random armargmaxa​μ^​a​​with probability εwith probability 1−ε​

where μ^a\hat{\mu}_aμ^​a​ is the average reward observed from arm aaa. The parameter ε\varepsilonε is usually small, such as 0.1.

It is easy to implement and often works. But it has a structural flaw: ε\varepsilonε does not depend on what the algorithm has learned. In the first rounds, every arm is uncertain, so a 10% exploration rate is probably too low. In the thousandth round, when the best arm has been identified with near certainty, 10% is far too high — a tenth of all pulls are thrown away on arms already known to be worse. The exploration branch alone pulls each suboptimal arm about εT/K\varepsilon T / KεT/K times, so regret grows linearly in TTT:

R(T)=Ω ⁣(ε⋅TK∑aΔa).\mathcal{R}(T) = \Omega\!\left(\varepsilon \cdot \frac{T}{K} \sum_a \Delta_a\right).R(T)=Ω(ε⋅KT​a∑​Δa​).

You can shrink ε\varepsilonε over time — say εt=c/t\varepsilon_t = c/tεt​=c/t — and recover much better behaviour, but now the constant ccc has to be chosen using the gaps Δa\Delta_aΔa​, which the agent does not know. Auer, Cesa-Bianchi and Fischer (2002) analyse exactly this schedule and show that it needs a lower bound on the gaps to be set correctly. That is unsatisfying: the algorithm is supposed to discover the gaps.

The fix is to let the exploration rate follow uncertainty automatically, which is the subject of the next step.

Browser lab: watching an agent get stuck#

Run two agents on the same three-armed bandit. One exploits without exploring; the other explores a tenth of the time. Compare the pulls and the regret.

python · runs in browser
import numpy as np

rng = np.random.default_rng(3)

N_ARMS = 3
N_ROUNDS = 300
TRUE_MEANS = np.array([0.2, 0.5, 0.8])   # hidden from the agent
BEST_MEAN = TRUE_MEANS.max()


def run(epsilon, rng):
    """Pull arms for N_ROUNDS; return pull counts and total regret."""
    counts = np.zeros(N_ARMS)
    means = np.zeros(N_ARMS)     # running average reward per arm
    regret = 0.0

    for _ in range(N_ROUNDS):
        if epsilon > 0 and rng.random() < epsilon:
            arm = int(rng.integers(N_ARMS))          # explore
        else:
            arm = int(np.argmax(means))              # exploit

        reward = float(rng.random() < TRUE_MEANS[arm])   # Bernoulli payout
        counts[arm] += 1
        means[arm] += (reward - means[arm]) / counts[arm]

        # Regret: what the all-knowing oracle would have earned extra this round.
        regret += BEST_MEAN - TRUE_MEANS[arm]

    return counts.astype(int), regret


for label, epsilon in [("greedy (epsilon=0)", 0.0), ("epsilon-greedy (0.1)", 0.1)]:
    counts, regret = run(epsilon, rng)
    print(f"{label:<22} pulls per arm = {counts}, regret after {N_ROUNDS} rounds = {regret:.1f}")

print("Notice: with no exploration the agent can lock onto a mediocre arm, while a small epsilon keeps every arm sampled.")

What to look for.

  • The greedy agent often concentrates almost every pull on one arm — and the arm it locks onto is not always the best one. Its regret keeps climbing at a steady rate because it never collects evidence about the alternatives.
  • The ε\varepsilonε-greedy agent spreads its pulls across all three arms, including the best one. Its regret is lower, but it still keeps exploring at the same rate at the end.

Try it. Raise ε\varepsilonε to 0.3 and re-run. Exploration becomes more reliable — every arm is sampled — but the random branch now accounts for roughly a third of all pulls instead of a tenth, so a much larger share of the run is spent on arms the agent has already dismissed. Raising ε\varepsilonε buys certainty about the arms at a fixed price in reward; that price does not fall as the estimates improve, which is exactly what the next step fixes.

Key takeaways#

  • A multi-armed bandit has no states: a fixed set of arms, unknown average payouts, and TTT rounds to collect reward.
  • The exploration–exploitation trade-off is real and unavoidable: learning about an option requires giving up reward for it.
  • Pure exploitation is fragile because one unlucky sample can freeze a wrong belief forever; pure exploration pays for bad arms forever.
  • Regret measures the reward forgone against an all-knowing oracle, and it decomposes as ∑aΔaE[NT(a)]\sum_a \Delta_a \mathbb{E}[N_T(a)]∑a​Δa​E[NT​(a)] — how bad each arm is times how often it was pulled.
  • A good algorithm has sublinear regret, so its average regret per round shrinks to zero. Fixed-rate ε\varepsilonε-greedy cannot do this, because it explores at the same rate even when there is nothing left to learn.

Knowledge Check#

Check the trade-off and the regret decomposition.

Exercise · Fill in the blank

Trying an uncertain option in order to learn about it, at the cost of immediate reward, is called ______.

Exercise · Multiple choice

In the regret decomposition R(T) = sum over arms of (gap x expected pulls), which arm contributes the most regret in practice?

The worst arm, because its gap is largest
The optimal arm, because it is pulled most often
An arm with a small gap that is hard to distinguish from the best one
Every arm contributes equally by definition
Question 1 of 3

Why does a fixed epsilon in epsilon-greedy lead to linear regret?

Because the reward estimates are biased
Because it keeps exploring at the same rate even after the arms are known
Because it never explores the best arm
Because Bernoulli rewards are not bounded

Next step#

The flaw in ε\varepsilonε-greedy is that its exploration rate ignores what the agent knows. The next step replaces the fixed rate with a quantity computed from the data itself: how uncertain each arm still is.

Next: UCB1 and Thompson Sampling

← Previous
Reinforcement Learning: Agents, Rewards, and MDPs
Next →
Returns, Value, and the Bellman Equation
On this page
  • A row of slot machines and no history to read
  • Explore or exploit
  • Regret: the reward you gave up
  • Good algorithms learn
  • epsilon-greedy: the simplest fix, and its flaw
  • Browser lab: watching an agent get stuck
  • Key takeaways
  • Knowledge Check
  • Next step