Multi-armed bandits: exploration vs exploitation
One state, many actions, unknown rewards: ε-greedy, UCB and Thompson sampling, and regret.
Animated explanation
The idea
Five slot machines pay out with different, unknown probabilities. You have 1,000 pulls. Every pull on a machine you already think is good earns reward (exploitation); every pull on an uncertain one teaches you something (exploration). Too much of either loses money.
The bandit problem is reinforcement learning stripped to one state, so it isolates the exploration–exploitation dilemma that every RL agent faces. It is also hugely practical: A/B testing ads and headlines, choosing treatments in adaptive trials, and recommending items are all bandits.
Three classic strategies
- ε-greedy: exploit the best estimate, but with probability ε pick a random arm. Simple and robust, but it keeps wasting pulls on obviously bad arms.
- UCB1: pick the arm with the highest upper confidence bound . Uncertainty is treated as opportunity; exploration fades automatically.
- Thompson sampling: keep a Beta posterior over each arm's success rate, draw one sample per arm and pull the highest. Probability matching; excellent in practice.
Regret
Performance is measured by regret: the reward lost relative to always pulling the best arm. Greedy and fixed-ε strategies have regret growing linearly in time; UCB and Thompson sampling achieve logarithmic regret, which is the best possible order (Lai–Robbins lower bound).
Beyond the basic bandit
Contextual bandits see features before choosing (user profile → which article): LinUCB and neural bandits. Non-stationary bandits use a constant step size or sliding windows. Full RL adds states that change with your actions.
The algorithm, step by step
- 1Keep counts and value estimates for each arm (or Beta(α, β) beliefs).
- 2Each round, choose an arm with the strategy (ε-greedy, UCB1 or Thompson).
- 3Observe the reward; update and (or α += r, β += 1 − r).
- 4Track cumulative regret when the true best arm is known (simulation).
The maths
Incremental mean
UCB1
Thompson (Bernoulli rewards)
Regret
Strengths and weaknesses
Strengths
- Simple, principled exploration
- Logarithmic regret with UCB/Thompson
- Directly useful for A/B testing and recommendations
Weaknesses
- No states: actions don't change the future
- Assumes rewards are stationary (unless adapted)
- Needs careful reward definitions in practice
Reach for it when…
Check your understanding
Lambdian Lab
Three bandit strategies
Bernoulli bandit with win probabilities probs. Each function plays T rounds with its own np.random.default_rng(seed) and returns the list of chosen arms.
- epsilon_greedy(probs, T, eps, seed): ties broken by the lowest index (np.argmax).
- ucb1(probs, T, seed): pull each arm once first, then UCB1.
- thompson(probs, T, seed): Beta(1 + wins, 1 + losses) samples.
- regret(probs, arms): cumulative expected regret array.