Cost of blind exploration on FrozenLake: Forcing the optimal policy \pi^* to act randomly with probability \epsilon = 0.05 drops its 100-step success rate from 73.0\% to 54.2\%.
Blind vs. directed exploration:\epsilon-greedy wastes trials on actions already known to be disastrous as often as on promising alternatives.
Exploration in LLM post-training: When all K sampled responses to a prompt receive the same verifier score, the relative group update vanishes.
10-armed benchmark: Best arm \mu^* = \mu_2 = 0.90 and smallest gap \Delta_{\min} = 0.90 - 0.60 = 0.30 (arm 6).
Linear vs. sublinear regret: Pulling a 0.50 arm forever loses \Delta_a = 0.40 per pull; sublinear regret (\textrm{Regret}(t)/t \to 0) requires pulling suboptimal arms ever more rarely.
The Bandit Simulation Harness
Tracking Empirical Wins, Counts, and Shortfalls
bandit_run maintains per-arm tallies wins and count (so the empirical mean is \hat{\mu}(a) = \textrm{wins}[a]/\textrm{count}[a]) and accumulates expected shortfall \mu^* - \mu_{a_t} at each round:
arms = np.array([0.50, 0.42, 0.90, 0.25, 0.55, 0.38, 0.60, 0.32, 0.50, 0.45])def bandit_run(arms, rule, num_pulls, rng):"""One state, one step: pull an arm, log the shortfall, update tallies. Returns the cumulative regret after each pull.""" wins, count = np.zeros(len(arms)), np.zeros(len(arms)) gap = np.empty(num_pulls)for t inrange(num_pulls): a = rule(wins, count, t, rng) count[a] +=1 wins[a] +=float(rng.random() < arms[a]) gap[t] = arms.max() - arms[a]return np.cumsum(gap)
Logging the expected gap arms.max() - arms[a] rather than noisy binary rewards removes reward-flip variance from the evaluation metric while keeping the learner blind to arms.
Greedy, Epsilon-Greedy, and Boltzmann Selection
Turning Value Estimates into Action Probabilities
Given a row of estimated values \hat{Q}(s, \cdot) = (0.20, 0.90, 0.55, 0.10), three heuristic rules allocate exploration probability differently:
Greedy (\epsilon = 0): Places 100\% mass on the current empirical maximizer \mathrm{argmax}_a \hat{\mu}(a).
\epsilon-greedy: Reserves \epsilon/m probability (0.3/4 = 0.075) for every action regardless of how poorly it has performed.
Boltzmann (softmax) \propto \exp(\hat{Q}/\beta): Ranks actions smoothly by estimated value, trying near-optimal actions more often than terrible ones.
The first-win lottery: Starting at \hat{\mu}_0(a) = 0, once any arm a returns 1, its empirical mean \hat{\mu}(a) > 0 beats every untried arm (0) forever.
Lottery odds: Best arm \mu_2 = 0.90 wins the initial draw in only 0.90 / 4.87 \approx 18.5\% of runs.
Fixed vs. Annealed Epsilon-Greedy Regret
Linear Tax vs. Logarithmic Decay
Comparing pure greedy, constant \epsilon = 0.1, and harmonic annealing \epsilon_t = \min(1, 20/(t+1)) across 20 runs of 2{,}000 pulls exposes the cost of undirected exploration:
greedy: regret after 2000 pulls: median 800 (quartiles 700 to 980)
epsilon = 0.1: regret after 2000 pulls: median 106 (quartiles 92 to 132)
epsilon = 20/t: regret after 2000 pulls: median 46 (quartiles 44 to 53)
Constant \epsilon = 0.1 pays a linear tax: A random pull loses the average gap 0.90 - 0.487 = 0.413, costing 0.1 \times 0.413 \approx 0.041 per pull (\approx 83 of its 106 median regret) forever.
Harmonic \epsilon_t = 20/t achieves O(\ln t) regret: Makes only 20 + 20\ln(2000/20) \approx 112 random pulls total (\approx 46 exploration cost, median regret 46), though its constant 20 must be tuned to \Delta_{\min}^{-2}.
Optimism Under Uncertainty: Upper Confidence Bounds
Directing Exploration via Hoeffding Bonuses
Instead of exploring blindly, Upper Confidence Bound (UCB) adds an uncertainty bonus to each empirical mean and deterministically pulls the highest optimistic index (Auer et al., 2002):
Hoeffding concentration foundation: For n independent rewards in [0, 1], \Pr(\mu_a > \hat{\mu}_n(a) + h) \leq e^{-2nh^2}; setting h = \kappa\sqrt{\log t / n(a)} bounds the probability of underestimating arm a by t^{-2\kappa^2}.
Two reasons an arm is pulled: Either its empirical mean \hat{\mu}(a) is large (exploitation) or its pull count n(a) is small relative to \log t (directed exploration).
Visualizing UCB Indices and Pull Allocations
How Optimism Eliminates Bad Arms
After pulling each arm once to initialize n(a) \geq 1, UCB concentrates almost all pulls on the optimal arm while keeping suboptimal indices just below \mu^*:
Shrinking uncertainty bars: In panel (a), the bonus \kappa\sqrt{\log t / n(a)} decays as n(a)^{-1/2} with pulls and grows only as \sqrt{\log t} while idle.
Self-balancing indices: In panel (b), after 2{,}000 pulls with \kappa = 0.5, arm 2 (\mu^* = 0.90) receives 1{,}852 pulls, while bad arms stop once their upper confidence bound drops below 0.90.
Probability matching without tuning: Draw one sample \tilde{\mu}_a per arm and pull \mathrm{argmax}_a \tilde{\mu}_a, matching each arm’s posterior probability of optimality.
Comparing All Five Bandit Rules on Log-Log Axes
Empirical Regret Curves
Plotting cumulative regret on logarithmic axes separates linear-regret rules (slope 1) from logarithmic-regret rules (bending flat):
Three-fold improvement: UCB (\kappa = 0.5) and Thompson sampling finish with median regret 30–38, roughly one-third of fixed \epsilon = 0.1(106) and below annealed \epsilon_t = 20/t(46).
UCB, kappa = 0.5: regret after 2000 pulls: median 38 (quartiles 29 to 46)
Thompson: regret after 2000 pulls: median 30 (quartiles 26 to 36)
Sensitivity to the UCB Exploration Coefficient
Worst-Case Guarantees vs. Empirical Variance
Sweeping \kappa \in \{0.15, 0.50, \sqrt{2}\} across 40 runs explains why theoretical UCB1 (\kappa = \sqrt{2}) trails \kappa = 0.5 on this benchmark:
kappa = 0.15: regret after 2000 pulls over 40 runs: median 8, min 4, max 709
kappa = 0.50: regret after 2000 pulls over 40 runs: median 38, min 21, max 59
kappa = 1.41: regret after 2000 pulls over 40 runs: median 204, min 173, max 241
Why UCB1 (\kappa = \sqrt{2}) costs 5\times more here: UCB pulls arm a until \kappa\sqrt{\log t / n(a)} \approx \Delta_a, so n(a) \propto \kappa^2 \log t / \Delta_a^2; Hoeffding bounds the worst-case Bernoulli variance 1/4, whereas our best arm has variance 0.9 \times 0.1 = 0.09.
Why \kappa = 0.15 is dangerous: Although its median regret is 8, an unlucky start traps slow runs on a suboptimal arm (max regret 709); Thompson sampling adapts automatically without tuning \kappa.
The Lai-Robbins Information-Theoretic Lower Bound
Why No Algorithm Beats Logarithmic Regret
Lai and Robbins (1985) proved that any consistent bandit algorithm must pull each suboptimal arm a at least in proportion to \ln t divided by the KL divergence D(\mu_a \,\|\, \mu^*):
Distinguishing near-optimal arms: For the second-best arm (\mu_6 = 0.60 vs. \mu^* = 0.90), D(0.60 \,\|\, 0.90) = 0.311, requiring at least \ln(2000)/0.311 \approx 24.4 pulls (\approx 7.3 regret) by t = 2000.
Rate optimality: Summing \Delta_a n_t(a) shows \textrm{Regret}(t) = \Omega(\ln t) is unimprovable; both UCB and Thompson sampling achieve this optimal \Theta(\ln t) growth rate.
Deep Exploration in MDPs: The N-State Chain
When Reaching an Unknown State Takes k Steps
Geometric decay of random walks: When action 1 advances and action 0 resets to state 0, reaching state N-1 requires N - 1 right moves in a row (2^{-(N-1)} random-walk probability).
Zero-initialized Q-learning: Until the first goal reward is collected, all entries read 0 and \epsilon-greedy wanders as a blind random walk.
Optimistic Initialization vs. Random Walk Scaling
Polynomial vs. Exponential Exploration
Initializing all table entries optimistically to Q_0(s, a) = 1.0 (an upper bound on return) with pure greedy selection (\epsilon = 0) eliminates the exponential random-walk bottleneck:
N = 4: episodes to the first success: random 2 (mean), optimistic 2 (median, max 2)
N = 8: episodes to the first success: random 23 (mean), optimistic 3 (median, max 4)
N = 12: episodes to the first success: random 273 (mean), optimistic 4 (median, max 5)
N = 16: episodes to the first success: random 3449 (mean), optimistic 6 (median, max 7)
N = 20: episodes to the first success: random 45590 (mean), optimistic 8 (median, max 10)
Why optimism marches systematically: One visit to a zero-reward pair updates Q(s, a) toward \gamma \max_{a'} Q(s', a') = \gamma < 1, dropping it below every untried pair (1.0) so greedy selection systematically sweeps the chain (8 episodes at N = 20 vs. 46{,}000 for random).
Count Bonuses and Coverage on FrozenLake
From Deterministic Chains to Stochastic Grids
On stochastic MDPs, a single visit cannot identify P(\cdot \mid s, a), while on small deterministic grids even random tie-breaking covers most pairs:
Count-based UCB bonuses: Adding c / \sqrt{n(s, a)} to rewards in Bellman backups drives the agent toward under-visited pairs, achieving \tilde{O}(\sqrt{T}) regret (Jin et al., 2018).
Calm FrozenLake coverage: Because the goal is only 6 moves away, greedy tie-breaking tries 93\% of live pairs and solves the calm lake in 19/20 seeds while winning 60\% of training episodes.
Contextual Bandits and pass@K in LLM Post-Training
In group-based policy optimization (such as GRPO in §14.8), the learner samples K responses per prompt x from \pi_\theta(\cdot \mid x) and updates toward responses that beat the group average:
P(\text{all } K \text{ responses score identically}) = (1 - p)^K + p^K, \qquad p = \pi_\theta(\textrm{correct} \mid x)
reference, K = 2: identical [0.78 0.78 1. 1. ], mean 0.89, exact 0.89
reference, K = 8: identical [0.35 0.33 1. 1. ], mean 0.67, exact 0.67
reference, K = 32: identical [0.01 0.02 1. 1. ], mean 0.51, exact 0.51
solved, K = 2: identical [0.89 0.93 1. 1. ], mean 0.96, exact 0.96
solved, K = 8: identical [0.67 0.7 1. 1. ], mean 0.84, exact 0.85
solved, K = 32: identical [0.21 0.21 1. 1. ], mean 0.60, exact 0.61
Zero-signal groups: When all K responses fail ((1-p)^K) or all K succeed (p^K), every advantage is zero; the policy explores solely through its own sampling probability \text{pass@}K = 1 - (1 - p)^K.
Concept Check: Regret Bounds and Exploration
Check Your Understanding
Work through these three quantitative exploration problems:
Near vs. far suboptimal arms: Under the Lai-Robbins bound at t = 2000(\ln 2000 \approx 7.6), the 0.60 arm (D = 0.31) needs \approx 24 pulls. The 0.25 arm has D(0.25 \,\|\, 0.90) \approx 1.19; how many pulls does it need, and which arm adds more cumulative regret?
Thompson sampling probability: Arm A has 3 wins in 4 pulls (\mathrm{Beta}(4, 2)) and arm B is untried (\mathrm{Beta}(1, 1)). With what probability does Thompson sampling pull the untried arm B?
Group size for LLM exploration: On an answerable prompt where p = 0.125, what is the smallest group size K such that a group of K responses has a mixed score (not all identical) at least 50\% of the time?
Answers:
The 0.25 arm requires \approx 7.6 / 1.19 \approx 6.4 pulls. Its regret is 6.4 \times 0.65 \approx 4.2, whereas the 0.60 arm costs 24.4 \times 0.30 \approx 7.3—near-optimal arms cost more total regret because they take longer to distinguish.
For X \sim \mathrm{Beta}(4, 2) and U \sim \mathrm{Uniform}(0, 1), \Pr(U > X) = \mathbb{E}[1 - X] = 1 - \frac{4}{4+2} = 1/3.
We need 1 - (0.875^K + 0.125^K) \geq 0.5: at K = 5, 1 - 0.513 = 0.487 < 0.5; at K = 6, 1 - 0.449 = 0.551 \geq 0.5, so K = 6 (or via \ln 2 / p \approx 5.5).
Summary: Multi-Armed Bandits and Exploration
Key Takeaways
Strategy
Action Selection Rule
Regret Scaling & Practical Trade-Off
Pure Greedy
\mathrm{argmax}_a \hat{\mu}(a)
\Theta(t) linear regret; locks onto first arm that pays out
Fixed \epsilon-Greedy
Random w.p. \epsilon, else \mathrm{argmax}_a \hat{\mu}(a)
\Theta(\epsilon t) linear regret; wastes pulls on known bad arms forever
Annealed \epsilon_t \propto 1/t
Random w.p. \min(1, c/t)
O(\ln t) if c \gtrsim m/\Delta_{\min}^2, but requires knowing \Delta_{\min}
UCB
\mathrm{argmax}_a [\hat{\mu}(a) + \kappa\sqrt{\log t / n(a)}]
O(\ln t) matching Lai-Robbins bound; sensitive to scale \kappa
Thompson Sampling
Sample \tilde{\mu}_a \sim \mathrm{Beta}(1+w_a, 1+n_a-w_a), pull max
O(\ln t) optimal rate with zero tuning hyperparameters
Optimistic Init / Bonus
Q_0(s, a) = R_{\max} or r + c/\sqrt{n(s, a)}
Replaces \Theta(2^N) random-walk time on chains with O(N) directed sweeps