Skip to content
Lambdian

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 Q(a)+2ln⁡t/N(a)Q(a) + \sqrt{2\ln t/N(a)}. 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

  1. 1
    Keep counts N(a)N(a) and value estimates Q(a)Q(a) for each arm (or Beta(α, β) beliefs).
  2. 2
    Each round, choose an arm with the strategy (ε-greedy, UCB1 or Thompson).
  3. 3
    Observe the reward; update N(a)N(a) and Q(a)←Q(a)+(r−Q(a))/N(a)Q(a) \leftarrow Q(a) + (r - Q(a))/N(a) (or α += r, β += 1 − r).
  4. 4
    Track cumulative regret when the true best arm is known (simulation).

The maths

Incremental mean

Qn+1=Qn+1n(rn−Qn)Q_{n+1} = Q_n + \tfrac1n(r_n - Q_n)

UCB1

at=arg⁡max⁡aQt(a)+2ln⁡tNt(a)a_t = \arg\max_a Q_t(a) + \sqrt{\frac{2\ln t}{N_t(a)}}

Thompson (Bernoulli rewards)

θa∼Beta(1+winsa, 1+lossesa)\theta_a \sim \mathrm{Beta}(1 + \text{wins}_a,\ 1 + \text{losses}_a)

Regret

Regret(T)=Tμ∗−E[∑t=1Trt]\mathrm{Regret}(T) = T\mu^\ast - \mathbb{E}\Big[\sum_{t=1}^T r_t\Big]

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…

Repeated choices among options with unknown payoffs: adaptive experiments, ad and content selection, pricing tests.

Check your understanding

Question 1

Q1Why can a purely greedy strategy fail badly?

Question 2

Q2In UCB1, an arm that has been pulled very rarely…

Question 3

Q3Regret measures…

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.
Open the labPython + NumPy runs in your browser. Sign-in keeps your code and attempts.