Epsilon-greedy
Learning objective
Section titled “Learning objective”Be able to implement epsilon-greedy from memory, predict how changing changes both learning speed and final performance, and explain why the best depends on how long you get to play.
Intuition
Section titled “Intuition”A purely greedy agent always pulls the arm with the highest current estimate. That fails in a specific and avoidable way: one lucky early win on a mediocre arm makes it look best, the agent keeps pulling it, and the genuinely best arm is never tried again. Its estimate stays at its initial value forever. The agent locks onto a bad answer and never collects the evidence that would change its mind.
Epsilon-greedy fixes this with the least machinery possible. Before each pull, flip a biased coin. With probability , ignore everything you know and pick uniformly at random. Otherwise act greedily.
That is the entire algorithm. Its guarantee is crude but real: every arm gets pulled infinitely often in the limit, so every converges to , so the greedy action eventually is the optimal one.
Algorithm
Section titled “Algorithm”Epsilon-greedy, sample-average estimates
Initialise, for to : ,
Loop forever:
Note that the greedy branch takes a random action of the time by coincidence, so the true probability of selecting the greedy action is .
Worked example
Section titled “Worked example”Three arms, , all estimates starting at 0.
| Pull | Coin | Branch | Action | Reward | Update |
|---|---|---|---|---|---|
| 1 | 0.83 | exploit | 3-way tie → arm 2 | 1 | |
| 2 | 0.42 | exploit | arm 2 (highest ) | 0 | |
| 3 | 0.07 | explore | arm 3 (random) | 1 | |
| 4 | 0.91 | exploit | arm 3 (highest ) | 0 | |
| 5 | 0.55 | exploit | 2-way tie → arm 2 | 1 |
Two things to notice. Pull 3 is the whole point of the algorithm: arm 3 would never have been tried by a greedy agent, and it turned out to be worth trying. And after a single win an estimate reads 1.00 — early estimates are wildly overconfident because they average over one sample.
Interactive demo
Section titled “Interactive demo”Step through it one pull at a time and watch the branch taken, or turn on auto-play and watch the estimates converge. The seed field makes any run reproducible.
What to observe
Section titled “What to observe”Set and step repeatedly. The agent latches onto whichever arm happens to win first and never leaves. Reveal the true rates and you will usually find it is sitting on a mediocre arm. This is the failure epsilon-greedy exists to prevent.
Set . Every pull is random. The estimates become excellent — every arm is sampled evenly — and the reward is terrible. Perfect knowledge, no exploitation.
Set and run a few hundred steps. The best arm accumulates most of the pulls while the others keep getting sampled occasionally. Watch the ”% optimal action” curve rise and then flatten.
Note the ceiling. With and 5 arms, the agent takes a random action 10% of the time forever, so it plays optimally at most of the time. The curve flattens below 100% by construction, not because learning stalled.
Run the comparison. This is the most important panel on the page. Four values, 150 independent runs each, 2000 steps, all facing the same set of environments.
- climbs fastest for about fifty steps, then goes flat well below everything else. Across 200 seeds it ends up on a clearly suboptimal arm roughly 70% of the time.
- learns quickly and then stays low: it throws away three pulls in ten forever.
- leads for the first ~1500 steps.
- is worst early and crosses above at around step 1500, ending near 0.89 against 0.86.
The crossover is the entire lesson. Neither value is better; the horizon decides. Note also that still has the higher cumulative average at step 2000 — it banked its lead early — even though is clearly ahead instantaneously by then. Which number you quote changes the answer.
Common mistakes
Section titled “Common mistakes”Breaking ties by index. With all estimates at 0, argmax returns arm 0 every
time. The agent then only ever explores by accident and the demo looks broken.
Ties must be broken uniformly at random.
Comparing values on one run. A single run is dominated by luck. The comparison panel averages 150 independent runs for this reason, and holds the set of environments fixed so every faces the same problems. Compare two methods on one seed and you are mostly measuring the seed.
Exploring uniformly among the obviously-bad. Epsilon-greedy’s random branch is uniform: an arm already pulled 200 times with a terrible payout is just as likely to be explored as a promising one that has been pulled twice. This is the weakness UCB addresses.
Leaving constant when the run is long. Fixed means regret grows linearly forever. Decaying it — , say — gets the early exploration without paying for it indefinitely.
Experiments to try
Section titled “Experiments to try”- Set for the first 50 pulls, then drop it to 0.01. Compare the total against a constant over the same seed.
- Find a seed where beats over 200 steps, and explain why that is not evidence that greedy is better.
- Predict the flat-line value of ”% optimal action” for before running it, using . Then run it.
- Find the crossover step where overtakes . Re-run with a different seed and see how much it moves.
- Watch regret rather than reward. Note that it never decreases, and compare its slope across values.
Personal takeaways
Section titled “Personal takeaways”The thing that surprised me is that the best is a function of your time budget, not a property of the problem. Small wins asymptotically and loses over a short horizon. Asking “what is the best ” without saying how many steps you get is asking an incomplete question.
The second thing: epsilon-greedy explores stupidly. It knows which arms it is uncertain about and throws that information away when it explores. Everything better — optimistic initialisation, UCB, Thompson sampling — comes from spending the exploration budget where the uncertainty actually is.
Where next
Section titled “Where next”Both this page and the last assume the arms hold still. They do not, usually. Tracking a nonstationary problem is what changes when drifts while you play.
References
Section titled “References”- Sutton, R. S. & Barto, A. G. Reinforcement Learning: An Introduction, 2nd ed., §2.2–2.3.