Multi-armed bandits
The simplest setting where an agent must learn from rewards: one situation, k choices, and no instructions about which choice is best. It isolates the core tension of reinforcement learning, exploring to learn more versus exploiting what you already know, before states and delayed consequences enter the picture.
01The problem
Picture a row of k slot machines (“one-armed bandits”). On each time step t you pick one action At, pull that arm, and receive a reward Rt drawn from that arm's reward distribution. The distributions never change, but you don't know them. Your goal is to maximise the total reward over, say, 1000 pulls.
Each arm has a true action value, its expected reward:
If you knew q*, you would always pull the best arm. You don't, so you keep an estimate Qt(a) and choose based on it. Picking the arm with the highest estimate is exploiting; picking any other arm is exploring. Exploiting maximises the reward on this step; exploring may find a better arm and so earn more over many steps. You cannot do both on the same pull: that is the exploration–exploitation dilemma.
02Estimating action values
The natural estimate is the sample average: the mean of the rewards received so far from that arm. You don't need to store every reward. If Qn is the average of the first n−1 rewards from an arm and Rn is its n-th reward:
This is the pattern behind every method in this series: new estimate ← old estimate + step size × (target − old estimate). Here the target is the reward just received. Later the target becomes a return (Monte Carlo) or a bootstrapped guess (TD).
When the arms' values drift over time, an average over all history reacts too slowly. A constant step size α ∈ (0, 1] weights recent rewards more:
03Four ways to choose an arm
ε-greedy
With probability 1−ε pick the arm with the highest Q (breaking ties at random); with probability ε pick an arm uniformly at random. Simple and effective, but it explores blindly: a clearly bad arm gets tried as often as a promising one.
Optimistic initial values
Start every Q at a value well above any plausible reward, such as +5, and act greedily. Every arm disappoints at first, so the agent tries them all before settling. This only drives exploration at the start, so it is a poor fit when values change over time.
Upper confidence bound (UCB)
Add a bonus that is large for arms tried rarely. The bonus shrinks as an arm's count Nt(a) grows and grows slowly with total time t, so neglected arms are eventually revisited. An arm with Nt(a) = 0 counts as maximising.
Gradient bandit
Learn a numerical preference Ht(a) per arm instead of a value, and choose with a softmax over preferences. After each reward, raise the preference of the chosen arm if the reward beat the baseline R̄t (the average of earlier rewards) and lower the others in proportion to their probability.
Ht+1(At) = Ht(At) + α (Rt − R̄t)(1 − πt(At)), Ht+1(a) = Ht(a) − α (Rt − R̄t) πt(a) for a ≠ At
This is stochastic gradient ascent on expected reward. The baseline doesn't change the expected update, but it cuts its variance a great deal.
04Pull the arms yourself
Below is a five-armed bandit. Each arm's true value q*(a) was drawn from a normal distribution with mean 0 and variance 1, and each pull returns q*(a) plus normal noise with variance 1. Choose a method, pull, and read exactly how the arm was chosen and how the estimate changed.
A five-armed bandit
Estimates Q(a) and true values q*(a)
Average reward so far
The latest pull, number by number
What to try
- Set ε = 0 (pure greedy) and pull 100 times. Greedy often locks onto whichever arm looked good early and never learns the others are better. Press Reset estimates a few times and see how often that happens.
- Keep ε = 0 but set the initial Q1 to 5. Every arm starts out looking great and disappoints, so greedy tries them all. That is optimistic initialisation.
- Switch to UCB and watch the bonus column: an arm that hasn't been pulled for a while gets a growing bonus until it is tried again.
- Switch to the gradient bandit and turn the baseline off. Every reward here is close to 0 on average, so it still works, but more slowly and erratically.
05The 10-armed testbed
One bandit proves little. The standard comparison (Sutton & Barto, section 2.3) averages over many randomly generated 10-armed bandits. Below, each method plays 300 different bandits for 1000 steps, and the curves show the average reward at each step and the share of runs that picked the best arm. All methods face the same 300 bandits.
Average over 300 bandits
Average reward
% of runs choosing the best arm
What the testbed typically shows:
- Greedy improves fastest for a few steps, then stalls: it finds the best arm in only about a third of runs and settles on a worse arm for good in the rest.
- ε-greedy keeps improving because it keeps sampling every arm. With ε = 0.1 it finds the best arm sooner but can never choose it more than about 91% of the time, since it explores 10% of the time and only 1 in 10 random picks is the best arm. A smaller ε learns more slowly but ends higher.
- Optimistic greedy (Q1 = 5, α = 0.1) does badly at first, while it is busy being disappointed, then overtakes ε-greedy on this stationary problem.
- UCB ends among the best: its exploration goes to arms whose value is still uncertain instead of being spread evenly.
- Gradient bandit with a baseline is competitive too, but it depends strongly on α. Try α = 0.4.
06From bandits to full reinforcement learning
A bandit has a single situation, and an action affects only its own immediate reward. Real problems add two things. First, states: the best action depends on the situation (a contextual bandit is this step alone). Second, delayed consequences: an action changes the next state and therefore every reward after it, so credit for a reward must be assigned to the actions that led there. Formalising both gives the Markov decision process, the subject of the next part.
What carries over: estimates updated by old + step size × (target − old), the choice between sample averages and a constant α, and exploration strategies like ε-greedy, which reappear in Monte Carlo control, Sarsa and Q-learning.