Search

Search papers and lessons

Epsilon-Greedy Policy

Level 2 · 9 min read

An epsilon-greedy policy takes the action it currently rates best most of the time, and a uniformly random action with probability epsilon, so the agent keeps exploring.

On this page

  • Colour = topic mix
  • Probability
  • Discrete math
  • Calculus
  • Reinforcement learning
  • Linear algebra
  • Stochastic processes
  • ReadReady to readNot yet

TL;DR

An epsilon-greedy policy is a rule for choosing actions. With probability 1−ε1 - \varepsilon it picks the action with the highest estimated value, and with probability ε\varepsilon it picks an action uniformly at random. The small random share keeps the agent trying options it has not yet learned enough about, so it does not lock onto an early lucky choice.

Builds on: Value Function, Multi-Armed Bandit, Uniform Distribution · Leads to: Deep Q-Network, Double Q-Learning

Where it's used

Learning to play Atari games. The deep Q-network of 2015 learned to play dozens of Atari games from screen pixels. Its agent chose actions with an epsilon-greedy rule, and it started with ε\varepsilon near 1 (almost every action random) and lowered it over the first part of training. Early in training the agent knows little, so random play gathers the experience it needs.

Recommendations and ads. A system that shows one item from a set has estimated click rates for each item. Showing only the best-known item stops it finding out whether a new item is better. Showing the best item most of the time, and a random item a small share of the time, keeps the estimates improving while still earning most of the clicks.

Textbook reinforcement learning. Tabular Q-Learning on small grid worlds is usually taught with a fixed ε\varepsilon such as 0.1. It is the default exploration rule in many introductory examples, because it works with any method that estimates action values.

How it works

Take an agent with three actions, A, B and C. Its current estimates of their values are Q(A)=0.5Q(A) = 0.5, Q(B)=0.8Q(B) = 0.8 and Q(C)=0.2Q(C) = 0.2. Set ε=0.2\varepsilon = 0.2.

  1. Find the greedy action. The highest estimate is Q(B)=0.8Q(B) = 0.8, so B is the greedy action. Check: no other estimate is above 0.8.
  2. Decide whether to explore. Draw a random number uu between 0 and 1. If u<0.2u < 0.2, explore. Otherwise exploit. Check: exploring happens in about one step in five.
  3. If exploring, pick at random. Each of A, B and C gets probability one third. Check: the three probabilities add to 1.
  4. If exploiting, pick B. Check: the agent now takes the action with the highest estimate.
  5. Combine the two branches. The chance of picking B is the chance of exploiting (0.8) plus the chance that exploring picks B (0.2 times one third): 0.8 + 0.0667 = 0.8667. The chance of picking A is 0.2 times one third, about 0.0667, and the same for C. Check: 0.8667 + 0.0667 + 0.0667 = 1.

The picture: with a large ε\varepsilon, the policy is close to random; with ε=0\varepsilon = 0, it is purely greedy, and it never tries anything but its current favourite.

The math

Definition: Epsilon-greedy policy
Given action values Q(s,a)Q(s, a) for the actions aa in a set A\mathcal{A}, and a number ε\varepsilon between 0 and 1, the epsilon-greedy policy chooses the greedy action a∗=arg⁡max⁡aQ(s,a)a^\ast = \arg\max_{a} Q(s, a) with probability 1−ε+ε/∣A∣1 - \varepsilon + \varepsilon / |\mathcal{A}|, and every other action with probability ε/∣A∣\varepsilon / |\mathcal{A}|.

Step 2 in symbols: the exploration coin

Let ε\varepsilon be the exploration rate. The policy can be written as a mixture of two distributions:

πε(a∣s)=(1−ε) 1[a=a∗]+ε⋅1∣A∣\pi_\varepsilon(a \mid s) = (1 - \varepsilon)\,\mathbf{1}[a = a^\ast] + \varepsilon \cdot \frac{1}{|\mathcal{A}|}

Here 1[⋅]\mathbf{1}[\cdot] is 1 when its condition is true and 0 otherwise. The first term is the exploit branch; the second is the explore branch, a uniform draw over the actions. The probabilities add to 1, because (1−ε)+ε=1(1 - \varepsilon) + \varepsilon = 1.

Step 5 in symbols: the example

With ∣A∣=3|\mathcal{A}| = 3, ε=0.2\varepsilon = 0.2 and a∗=Ba^\ast = B:

π(B∣s)=0.8+0.23=0.8667,π(A∣s)=π(C∣s)=0.23=0.0667\pi(\text{B} \mid s) = 0.8 + \frac{0.2}{3} = 0.8667, \qquad \pi(\text{A} \mid s) = \pi(\text{C} \mid s) = \frac{0.2}{3} = 0.0667

Check: 0.8667+0.0667+0.0667=10.8667 + 0.0667 + 0.0667 = 1.

Why a fixed epsilon costs something

Suppose the agent acts for TT steps with a fixed ε\varepsilon. About εT\varepsilon T of those steps are random, whatever the agent has learned. If the best action is worth v∗v^\ast and the policy's average value is vˉ\bar{v}, the total shortfall against always playing the best action is about T(v∗−vˉ)T(v^\ast - \bar{v}). For a policy that explores a fixed share of the time, v∗−vˉv^\ast - \bar{v} stays positive, so the shortfall grows in proportion to TT. This accumulated shortfall is called the regret. A fixed-ε\varepsilon rule has linear regret, meaning regret proportional to TT; the bandit setting in which regret is defined is covered in Multi-Armed Bandit.

A decaying schedule such as εt=1/t\varepsilon_t = 1/t lets the exploration share shrink, so the shortfall grows more slowly.

Worked examples

Example
Example 1: two actions, tiny numbers
  1. Actions L and R. Estimates Q(L)=1Q(L) = 1 and Q(R)=0Q(R) = 0. Set ε=0.1\varepsilon = 0.1.
  2. The greedy action is L.
  3. The chance of L is 1−0.1+0.1/2=0.9+0.05=0.951 - 0.1 + 0.1/2 = 0.9 + 0.05 = 0.95.
  4. The chance of R is 0.1/2=0.050.1 / 2 = 0.05.
  5. Check: 0.95+0.05=10.95 + 0.05 = 1.

Interpretation: the policy takes the better action 95 times in 100, and the other action 5 times in 100.

Example
Example 2: four actions, expected reward
  1. Four actions with true values Q(1)=2.0Q(1) = 2.0, Q(2)=3.5Q(2) = 3.5, Q(3)=1.0Q(3) = 1.0 and Q(4)=3.0Q(4) = 3.0. Assume the estimates are correct and set ε=0.2\varepsilon = 0.2.
  2. The greedy action is 2, with value 3.5.
  3. Its probability is 1−0.2+0.2/4=0.8+0.05=0.851 - 0.2 + 0.2/4 = 0.8 + 0.05 = 0.85.
  4. Each of actions 1, 3 and 4 has probability 0.2/4=0.050.2/4 = 0.05.
  5. Check: 0.85+3×0.05=10.85 + 3 \times 0.05 = 1.
  6. Expected reward of the policy: 0.85×3.5+0.05×(2.0+1.0+3.0)=2.975+0.30=3.2750.85 \times 3.5 + 0.05 \times (2.0 + 1.0 + 3.0) = 2.975 + 0.30 = 3.275.
  7. A purely greedy policy would earn 3.5, so exploring costs 3.5−3.275=0.2253.5 - 3.275 = 0.225 per step.

Interpretation: the cost of exploring is small per step, but it is paid every step for as long as ε\varepsilon stays fixed.

Example
Example 3: choosing which item to show
  1. A shop shows one of three items. Estimated click rates are 0.12, 0.08 and 0.05. The best item has rate 0.12. Set ε=0.1\varepsilon = 0.1.
  2. The probability of showing the best item is 0.9+0.1/3=0.93330.9 + 0.1/3 = 0.9333.
  3. Each other item is shown with probability 0.1/3=0.03330.1/3 = 0.0333.
  4. Expected clicks per 1000 impressions: 0.9333×0.12=0.11200.9333 \times 0.12 = 0.1120 for the best item, plus 0.0333×(0.08+0.05)=0.004330.0333 \times (0.08 + 0.05) = 0.00433 for the others. Total: 0.11630.1163, or about 116 clicks per 1000.
  5. A purely greedy shop would earn 0.12×1000=1200.12 \times 1000 = 120 clicks per 1000.
  6. The exploration costs about 4 clicks per 1000 impressions, and in return the shop keeps learning whether the other items have improved.
  7. Try it: with ε=0.3\varepsilon = 0.3, how often is the best item shown? Answer: 0.7+0.3/3=0.7+0.1=0.80.7 + 0.3/3 = 0.7 + 0.1 = 0.8, so the best item is shown 80 times in 100.

Common mistakes and tradeoffs

It is tempting to think ε\varepsilon is the chance of picking the best action. In fact the best action gets 1−ε1 - \varepsilon plus its share of the random draws, so the greedy action is chosen more often than 1−ε1 - \varepsilon and each other action gets ε\varepsilon divided by the number of actions.

It is tempting to think a fixed ε\varepsilon is a good setting forever. In fact a fixed rate keeps paying the exploration cost after the agent knows its options well. A schedule that lowers ε\varepsilon over time, such as the decay in the Atari example, usually does better in the long run.

It is tempting to think random exploration is always informative. In fact in tasks with sparse rewards, where most random actions earn nothing, epsilon-greedy explores almost uselessly. Methods that use uncertainty, such as the UCB rule or Thompson sampling in Multi-Armed Bandit, direct exploration to the actions that are least known.

Epsilon-greedy is a good choice when the action set is small or moderate, the value estimates are reasonable, and you want a rule with one knob that is easy to explain and debug. It is a poor choice when rewards are rare, when actions are very numerous, or when a bad random action is costly, such as in a physical robot. In those cases use a directed exploration method or a rule that explores by uncertainty.

History

The problem is old: a bandit player must decide whether to use what they know or try something new, and the early work on this trade-off goes back to the 1930s and to Robbins's 1952 paper on sequential choice. Epsilon-greedy as a named rule became standard in reinforcement learning through the textbook Reinforcement Learning: An Introduction by Sutton and Barto, whose first edition appeared in 1998. Its simplicity and the single knob made it the default exploration rule for early Q-Learning experiments.

It stayed popular when function approximators replaced tables. The 2013 and 2015 Atari papers on deep Q-networks used it with a decaying ε\varepsilon, and the rule is still a baseline against which newer exploration methods are compared.

Recap

  • An epsilon-greedy policy picks the highest-valued action with probability 1−ε1 - \varepsilon and a uniformly random action with probability ε\varepsilon.
  • The chance of the greedy action is 1−ε+ε/∣A∣1 - \varepsilon + \varepsilon/|\mathcal{A}|, and each other action gets ε/∣A∣\varepsilon/|\mathcal{A}|.
  • A fixed ε\varepsilon gives a steady exploration cost, which grows linearly with the number of steps.
  • Decaying ε\varepsilon over training is the usual fix, as in the Deep Q-Network.
  • Read next: Double Q-Learning, which changes how the action values are estimated, and Reinforcement Learning for the wider setting.

Check yourself

QuestionAn agent has three actions with estimated values 4, 6 and 5, and uses an epsilon-greedy policy with epsilon 0.3. Which action is the greedy one, and what is its probability?Show answer
The greedy action is the one with value 6. Its probability is 1−0.3+0.3/3=0.7+0.1=0.81 - 0.3 + 0.3/3 = 0.7 + 0.1 = 0.8.
QuestionCompute the probability that an epsilon-greedy policy with epsilon 0.2 takes each of the 4 actions, if the greedy action is action 1.Show answer
Action 1 has probability 1−0.2+0.2/4=0.851 - 0.2 + 0.2/4 = 0.85. Each of the other three actions has probability 0.2/4=0.050.2/4 = 0.05. Check: 0.85+3×0.05=10.85 + 3 \times 0.05 = 1.
QuestionWhy does a fixed epsilon cause regret that grows in proportion to the number of steps, rather than levelling off?Show answer
Because a fixed share of steps is always random, and random steps earn less than the best action. Each step therefore has a positive expected shortfall, and over TT steps the shortfall adds up to a quantity proportional to TT. The agent does not stop paying the cost, even after it knows its options well.
QuestionA robot must not take random actions that could damage it, but it must still learn. Why might epsilon-greedy be a poor choice, and what could replace it?Show answer
Random actions are the mechanism of exploration, so they are the risk. Epsilon-greedy takes them at a fixed rate with no regard to their danger. A better choice is a method that explores by uncertainty, or that restricts exploration to safe actions, so that the exploration is directed and bounded.

Sources