Week 01 · Bandits and exploration
Measure the cost of settling on the first promising option.
Measure the cost of settling on the first promising option.
Work through the problem
A bandit has actions with unknown expected rewards μₐ. A greedy policy selects the highest estimated mean. Epsilon-greedy explores uniformly with probability ε and exploits otherwise. Update the selected mean from n observations by Q ← Q+(r−Q)/n.
With arm means 0.4 and 0.7, choosing arm 0 incurs expected regret 0.3 on that step. Cumulative pseudo-regret sums μ*−μ_selected using the known simulator means. A real learner never receives those means. Repeat across seeds to average over lucky and unlucky first pulls.
Exploration reduces early lock-in.
Run the Python lab
import numpy as np
means = np.array([.4,.7])
curves = {}
for eps in [0.,.1]:
traces = []
for seed in range(120):
rng = np.random.default_rng(seed)
q,n,regret = np.zeros(2),np.zeros(2),[]
for step in range(300):
a = step if step<2 else (rng.integers(2) if rng.random()<eps else q.argmax())
n[a] += 1
q[a] += ((rng.random()<means[a])-q[a])/n[a]
regret.append(means.max()-means[a])
traces.append(np.cumsum(regret))
curves[str(eps)] = np.mean(traces,axis=0)
print(eps,"mean final regret",round(curves[str(eps)][-1],2))
assert curves["0.1"][-1] < curves["0.0"][-1]
Output from the lab
0.0 mean final regret 31.89
0.1 mean final regret 9.48
Practice
- Why can constant exploration eventually cost more than decaying exploration?
Check your reasoning
- A constant probability keeps trying the worse action even when the mean estimates are already accurate.