Reinforcement Learning

Bandits

Bandits isolate the exploration problem: each pull reveals only one arm's reward, so the learner must trade sample-mean exploitation against information-gathering and regret.

status: reviewimportance: importantdifficulty 3/5math: undergraduateread: 22mlive demo

Concept Structure

Bandits

01Intuition

Start with the picture, metaphor, or geometric mechanism.

02Math

Make the objects explicit and connect them with notation.

03Code

Mirror the equations with runnable implementation details.

04Interactive Demo

Manipulate the mechanism and watch the idea respond.

1prerequisites
2next concepts
2related links

Learner Contract

What this page should let you do.

You are here becauseBandits isolate the exploration problem: each pull reveals only one arm's reward, so the learner must trade sample-mean exploitation against information-gathering and regret.

This Reinforcement Learning concept is the current idea: keep the same invariant visible across Intuition, Math, Code, Interactive Demo.

Before thisProbability Basics

1 prerequisite listed; refresh them before leaning on the math or code.

By the end4/4 sections ready | runnable code expected | live demo

Explain the mechanism, trace the main notation, and test one prediction in the live demo.

Do this firstIntuition

Read the intuition before the notation; the math should name a mechanism you already felt.

Test the linkManipulate one control and predict the visible change.Then continue to Exploration and Exploitation (review)

Claim/source review status

Substantive review recorded

1/1 claims have bounded review metadata; still check caveats and source scope.Metadata-derived; review may be AI-assisted. Not a human certification.
Claims1/1 reviewed
Sources3 cited
Codeattached
Demolive
Reviewed2026-07-03
Updatedpage 2026-07-03

Learning item flow

4/4 sections readyAsk about thisResearch room
ConceptBanditsReinforcement Learning
3 sources attachedLocal snapshot ready
concept:reinforcement-learning/bandits
01

01

Intuition

Build the mental picture first so the rest of the page has something to attach to.

Section prompt

A bandit is the smallest reinforcement-learning problem where exploration is real.

There is no state transition to model yet. You only choose an arm, receive a reward, update what you know about that one arm, and choose again. The catch is brutal: pulling Arm A tells you nothing directly about Arm B or Arm C. Your data is selected by your own past decisions.

That creates the central tension:

Should I exploit the arm that currently looks best, or explore an arm that might be better but has less evidence?

A greedy learner can get stuck because early lucky rewards make one arm look best. A purely random learner gathers information but wastes many pulls. Epsilon-greedy is the simplest compromise: usually exploit, sometimes explore. UCB is more directed: add a confidence bonus to arms with fewer samples, then choose the arm whose optimistic score is largest.

Regret makes the cost visible. If the best arm has mean reward μ\mu_\star and the chosen arm has mean reward μa\mu_a, the expected regret of that pull is μμa\mu_\star-\mu_a. The learner may observe a lucky reward from a bad arm or an unlucky reward from the best arm, but regret asks a cleaner question: how much expected reward did this policy give up by choosing this arm?

02

02

Math

Translate the story into symbols, assumptions, and a derivation you can inspect.

Section prompt

Consider kk arms. At round tt, the learner chooses an arm

At{1,,k},A_t \in \{1,\ldots,k\},

then observes reward RtR_t. In a stationary Bernoulli bandit, each arm aa has an unknown mean reward

μa=E[RtAt=a],\mu_a = \mathbb E[R_t \mid A_t=a],

and the rewards from that arm are independent draws with the same mean. The learner does not observe the rewards it would have received from the arms it did not pull.

Let Nt(a)N_t(a) be the number of times arm aa has been pulled before round tt, and let Qt(a)Q_t(a) be its sample-average estimate:

Qt(a)=1Nt(a)i<t:Ai=aRi,Nt(a)>0.Q_t(a) = \frac{1}{N_t(a)} \sum_{i < t: A_i=a} R_i, \qquad N_t(a)>0.

After choosing At=aA_t=a and observing reward RtR_t, the sample average can be updated incrementally:

Qt+1(a)=Qt(a)+1Nt(a)+1(RtQt(a)).Q_{t+1}(a) = Q_t(a) +\frac{1}{N_t(a)+1} \left(R_t-Q_t(a)\right).

Epsilon-greedy chooses greedily with probability 1ϵ1-\epsilon and explores uniformly at random with probability ϵ\epsilon:

At={argmaxaQt(a),with probability 1ϵ,a random arm,with probability ϵ.A_t = \begin{cases} \arg\max_a Q_t(a), & \text{with probability } 1-\epsilon, \\ \text{a random arm}, & \text{with probability } \epsilon. \end{cases}

UCB chooses the arm with the largest optimistic score:

At=argmaxa[Qt(a)+clntNt(a)].A_t = \arg\max_a \left[ Q_t(a) + c\sqrt{\frac{\ln t}{N_t(a)}} \right].

The first term exploits the current estimate. The second term explores arms whose estimate is still uncertain because Nt(a)N_t(a) is small. Pulling an arm increases its count, which usually shrinks its bonus.

Let

μ=maxaμa.\mu_\star = \max_a \mu_a.

The expected instantaneous regret of choosing arm AtA_t is

μμAt.\mu_\star-\mu_{A_t}.

The cumulative pseudo-regret over TT rounds is

RˉT=t=1T(μμAt).\bar R_T = \sum_{t=1}^{T} \left(\mu_\star-\mu_{A_t}\right).

This is not the same as "did the learner happen to receive a reward this round?" It measures the expected cost of the action choice.

03

03

Code

Keep the implementation aligned with the notation so the algorithm is legible.

Section prompt
import math
import numpy as np

true_means = np.array([0.62, 0.40, 0.75])
pulls = np.array([8, 6, 2])
successes = np.array([5, 2, 1])
t, c = 16, 0.70

q = successes / pulls
ucb = q + c * np.sqrt(np.log(t) / pulls)

epsilon = 0.20
rng_draw = 0.73
if rng_draw < epsilon:
    chosen = 1  # a fixed "random" draw for the teaching witness
    reason = "explore"
else:
    chosen = int(np.argmax(q))
    reason = "exploit"

ucb_chosen = int(np.argmax(ucb))
best_mean = true_means.max()

print("sample means:", q.round(3))
print("ucb scores:", ucb.round(3))
print("epsilon-greedy chose", chosen, reason)
print("ucb chose", ucb_chosen)
print("instant regret of UCB choice:", round(best_mean - true_means[ucb_chosen], 3))

The same estimates can lead to different actions. Epsilon-greedy uses an explicit random explore/exploit switch. UCB uses the uncertainty bonus to make under-sampled arms temporarily attractive.

04

04

Interactive Demo

Use direct manipulation to connect the explanation to a moving system.

Section prompt

Use the Bandit Regret Lab to predict why the policy pulls a particular arm.

Choose a case, inspect the arm estimates, counts, policy cue, and regret ledger, then commit to whether the next pull is driven by sample-mean exploitation, random exploration, or a UCB confidence bonus. After reveal, the lab shows the selected arm, sample-mean update, UCB scores, and expected regret for that one pull.

Live Concept Demo

Explore Bandits

The stage is code-native and interactive. Use it to test the explanation against the mechanism.

difficulty 3/5undergraduatecode-aligned
Demo Prediction Checkpoint

Manipulate one control and predict the visible change.

Commit to what Bandits should make visible before reading the result.

After The First Pass

Turn the concept into an inspected object.

Once the invariant is visible in the intuition, math, code, and demo, use these panels to inspect the mechanism visually, check source support, practice the idea, and attach a grounded research question.

Mechanism Storyboard

See the idea move before the page explains it

Bandits isolate the exploration problem: each pull reveals only one arm's reward, so the learner must trade sample-mean exploitation against information-gathering and regret.

Prediction open01 / Intuition
Prediction lens

Start with the picture, metaphor, or geometric mechanism.

Commit first

Before reading further, choose the kind of change Bandits should make visible.

Visual Inquiry

Make the image answer a mathematical question

Bandits isolate the exploration problem: each pull reveals only one arm's reward, so the learner must trade sample-mean exploitation against information-gathering and regret.

4/4 stages readyLive demo connected
Prediction

Which visible object should carry the first intuition?

Commit first

Pick the cue that should make Bandits easier to reason about before the page gives the answer.

Source Grounding

Canonical references for the mechanism on this page.

book · 2018Reinforcement Learning: An Introduction, Second EditionRichard S. Sutton and Andrew G. Barto

Canonical source for the k-armed bandit problem, sample-average action-value estimates, epsilon-greedy action selection, UCB action selection, and incremental action-value updates.

Open source
course-notes · 2026Stanford CS234: Lecture 1, Introduction to Reinforcement LearningStanford CS234

Graduate RL source for exploration, decision-dependent information, and bandits as actions that influence only immediate reward.

Open source
book · 2020Bandit AlgorithmsTor Lattimore and Csaba Szepesvari

Graduate source for stochastic Bernoulli bandits, regret, pseudo-regret, explore-then-commit, epsilon-greedy context, and UCB as optimism under uncertainty.

Open source

Claim Review

Bandits isolate the exploration problem: each pull reveals only one arm's reward, so the learner must trade sample-mean exploitation against information-gathering and regret.

Status1 substantive review recorded

Claims without a substantive review badge still need exact source-support review.

Sources3 references

sutton-barto-2018-rl, stanford-cs234-intro-rl-2026, lattimore-szepesvari-2020-bandit-algorithms

Local checks4 local checks

Use equations, runnable code, and demos to check whether the source support is operational.

Substantively reviewedFinite stationary bandits estimate selected-arm rewards; epsilon-greedy explores randomly, UCB explores with uncertainty bonuses, and regret measures loss against the best arm.Claim metadata: source checked

The sources support the finite-arm setting, observed-reward-only feedback, sample mean estimates, epsilon-greedy exploration/exploitation, UCB confidence bonuses, and regret as expected reward lost against the best arm.

Sources: Reinforcement Learning: An Introduction, Second Edition, Stanford CS234: Lecture 1, Introduction to Reinforcement Learning, Bandit AlgorithmsStationary finite Bernoulli teaching lab only; excludes contextual bandits, adversarial bandits, Thompson sampling, nonstationary rewards, Bayesian regret, formal finite-time proofs, delayed feedback, and MDP state transitions.A bounded review summary is present; still check caveats and exact reference scope.

Checked Sutton/Barto Chapter 2 for k-armed bandits, sample-average estimates, incremental updates, epsilon-greedy, and UCB action selection. Checked Stanford CS234 Lecture 1 for exploration as decision-dependent information and bandits as immediate-reward decision processes. Checked Lattimore/Szepesvari for stochastic Bernoulli bandits, regret/pseudo-regret definitions, explore-then-commit tradeoffs, epsilon-greedy context, and UCB optimism. GPT Pro critique remains pending because 127.0.0.1:51672 is unavailable.

Reviewer: codex-local-source-review; reviewed 2026-07-03

Practice Loop

Try the idea before it explains itself

Bandits isolate the exploration problem: each pull reveals only one arm's reward, so the learner must trade sample-mean exploitation against information-gathering and regret.

Readiness0/3 checks ready
Predict

Before touching the demo, predict one visible change that should happen in Bandits.

Hint 1

Reveal when your model needs a nudge.

Hint 2

Reveal when your model needs a nudge.

Hint 3

Reveal when your model needs a nudge.

Grounded research drawerClose
ConceptBanditsReinforcement Learning

Research Room

Attach the question to a claim, equation, code, or demo

Pick the concept, equation, source, runnable code, claim, misconception, or demo state before asking for help. The handoff keeps that page item in context.
Next local actionNo local draft saved yet

Open the draft below to save one note and next action in this browser.

conceptReinforcement Learning

Bandits

Attached question

What is the smallest example that makes Bandits click without losing the math?

Local action draftNo local draft saved yetExpand only when ready to capture one local next action
Local action draft

This draft stays in this browser, attached to the selected learning item.

No local draft saved.
Evidence to inspect
  • References to inspect: attached references on this page.
  • Definition, prerequisite, and contrast concept links
  • The equation or runnable code that makes the concept operational
  • One demo state that shows the invariant instead of a slogan
What would resolve this
  • The learner can state the mechanism in their own words
  • The learner can name the prerequisite that would repair confusion
  • The learner can predict how the mechanism changes under one perturbation
Grounded AI handoff

I am working in Continuous Function's research reading room. Object: concept - Bandits Selected item key: recorded for copy. Context: Reinforcement Learning Page anchor: recorded for copy. Open question: What is the smallest example that makes Bandits click without losing the math? Evidence to inspect: - References to inspect: attached references on this page. - Definition, prerequisite, and contrast concept links - The equation or runnable code that makes the concept operational - One demo state that shows the invariant instead of a slogan What would resolve this: - The learner can state the mechanism in their own words - The learner can name the prerequisite that would repair confusion - The learner can predict how the mechanism changes under one perturbation Answer as a careful research tutor: stay source-grounded, separate verified evidence from assumptions, name the relevant math objects, and end with one next action.

View it in context
concept/concept-notebook/reinforcement-learning/bandits concept:reinforcement-learning/bandits