5.1.8Reinforcement Learning Foundations

Epsilon-greedy strategy

2,971 words14 min readdifficulty · medium1 backlinks

What Problem Does This Solve?

In reinforcement learning, an agent must balance:

  • Exploitation: Choose actions that gave high rewards in the past
  • Exploration: Try new actions to discover potentially better rewards

Pure exploitation fails when your initial estimates are wrong. Pure exploration wastes time on obviously bad actions. Epsilon-greedy provides a principled randomization that ensures the value estimates converge to the true action values, while remaining computationally trivial. (Note: with a fixed ε\varepsilon the policy itself never becomes purely greedy—it keeps exploring forever.)

The epsilon-gredy policy selects action at time tt as:

at={argmaxaAQ(a)with probability 1ε (exploit)random action from Awith probability ε (explore)a_t = \begin{cases} \arg\max_{a \in \mathcal{A}} Q(a) & \text{with probability } 1-\varepsilon \text{ (exploit)} \\ \text{random action from } \mathcal{A} & \text{with probability } \varepsilon \text{ (explore)} \end{cases}

Why this works: The ε\varepsilon term guarantees that every action gets tried infinitely often (as tt \to \infty), so the Q(a)Q(a) estimates converge to true values q(a)q_*(a). The policy, however, does NOT converge to pure greedy—it always explores with rate ε\varepsilon.

Derivation From First Principles

Starting point: Multi-armed bandit problem. Each action aa has unknown true reward q(a)q_*(a). We maintain estimates Qt(a)Q_t(a) based on observed rewards.

Step 1: Pure gredy fails Gredy policy: always pick argmaxaQt(a)\arg\max_a Q_t(a)

Problem: If action a1a_1 gives reward5 on first try, and we never explore a2a_2, we never discover that q(a2)=10q_*(a_2) = 10. Our estimate Q(a1)Q(a_1) might be based on a lucky sample, not the true mean.

Step 2: Add exploration With probability ε\varepsilon, ignore current estimates and pick uniformly at random. This ensures:

Pr[action a chosen at time t]εk\Pr[\text{action } a \text{ chosen at time } t] \geq \frac{\varepsilon}{k}

where k=Ak = |\mathcal{A}|. Over infinite time, each action is tried infinitely often.

Step 3: What actually converges As each action aa is tried nan_a times, by law of large numbers:

Q(a)=1nai=1narinaq(a)Q(a) = \frac{1}{n_a} \sum_{i=1}^{n_a} r_i \xrightarrow{n_a \to \infty} q_*(a)

Since ε>0\varepsilon > 0 ensures nan_a \to \infty for all aa, the value estimates converge: eventually argmaxaQ(a)=argmaxaq(a)\arg\max_a Q(a) = \arg\max_a q_*(a) (with high probability). But the policy does NOT converge to always picking the optimal action. In the limit, the probability of selecting the optimal action is:

limtPr[at=a]=(1ε)+εk\lim_{t\to\infty}\Pr[a_t = a^*] = (1-\varepsilon) + \frac{\varepsilon}{k}

not 1ε1-\varepsilon and not 11. To get a policy that does converge to pure greedy, you must decay ε0\varepsilon \to 0 over time (see below).

Pr[at=a]=(1ε)1[a=argmaxaQt(a)]+εk\Pr[a_t = a^*] = (1-\varepsilon) \cdot \mathbb{1}[a^* = \arg\max_a Q_t(a)] + \frac{\varepsilon}{k}

For any suboptimal action aia_i where q(ai)<q(a)q_*(a_i) < q_*(a^*):

Pr[at=ai]=(1ε)1[ai=argmaxaQt(a)]+εk\Pr[a_t = a_i] = (1-\varepsilon) \cdot \mathbb{1}[a_i = \arg\max_a Q_t(a)] + \frac{\varepsilon}{k}

Why these formulas: The probability splits into two mutually exclusive cases:

  1. We exploit (prob 1ε1-\varepsilon) AND this action is currently estimated best (indicator function)
  2. We explore (prob ε\varepsilon) AND randomly pick this action (uniform 1/k1/k)

Once estimates are accurate, the optimal action is the estimated-best, so Pr[at=a]=(1ε)+ε/k\Pr[a_t = a^*] = (1-\varepsilon) + \varepsilon/k (strictly less than1 for fixed ε>0\varepsilon>0).

Figure — Epsilon-greedy strategy

We use ε=0.1\varepsilon = 0.1 (explore 10% of the time).

Initial state (t=0): Q0(a1)=Q0(a2)=Q0(a3)=0Q_0(a_1) = Q_0(a_2) = Q_0(a_3) = 0 (no knowledge)

Step 1: Random exploration choses a1a_1, observe r1=1.2r_1 = 1.2

  • Update: Q1(a1)=1.2Q_1(a_1) = 1.2, others stay 0
  • Current best: a1a_1

Step 2: With prob 0.9, exploit → choose a1a_1. Suppose we get r2=0.8r_2 = 0.8

  • Q2(a1)=(1.2+0.8)/2=1.0Q_2(a_1) = (1.2 + 0.8)/2 = 1.0

Why this step? We're exploiting current knowledge, but our estimate is based on only 2 samples.

Step 10: After several explores, suppose Q10(a1)=1.1Q_{10}(a_1)=1.1, Q10(a2)=4.8Q_{10}(a_2)=4.8, Q10(a3)=2.9Q_{10}(a_3)=2.9

  • Current best: a2a_2
  • Next action: With prob 0.9→ choose a2a_2 (exploit), with prob 0.1 → uniform random

Why epsilon matters: Even though a2a_2 looks best, we still occasionally try a1a_1 and a3a_3 in case our estimates are wrong. Over1000 steps, each action gets tried (0.1/3)×1000=33\approx (0.1/3) \times 1000 = 33 times during exploration, plus extra for the current best. Note: even after estimates are perfect, we choose a2a_2 only 90%+3.3%=93.3%90\% + 3.3\% = 93.3\% of the time—never 100%.

Regret per step: When we choose suboptimal action aia_i, we lose Δi=q(a)q(ai)\Delta_i = q_*(a^*) - q_*(a_i) reward.

After the estimates converge (they are accurate, but the policy still explores), the expected regret per step is:

L=aaPr[at=a]Δa\mathcal{L} = \sum_{a \neq a^*} \Pr[a_t = a] \cdot \Delta_a

For our bandit with ε=0.1\varepsilon = 0.1:

Pr[at=a2]=0.9+0.13=0.933Pr[at=a1]=0.13=0.033(Δ1=5.01.0=4.0)Pr[at=a3]=0.13=0.033(Δ3=5.03.0=2.0)\begin{align} \Pr[a_t = a_2] &= 0.9 + \frac{0.1}{3} = 0.933 \\ \Pr[a_t = a_1] &= \frac{0.1}{3} = 0.033 \quad (\Delta_1 = 5.0 - 1.0 = 4.0) \\ \Pr[a_t = a_3] &= \frac{0.1}{3} = 0.033 \quad (\Delta_3 = 5.0 - 3.0 = 2.0) \end{align}

Expected regret per step:

L=0.033×4.0+0.033×2.0=0.132+0.066=0.198\mathcal{L} = 0.033 \times 4.0 + 0.033 \times 2.0 = 0.132 + 0.066 = 0.198

Why this matters: We lose ~0.2 reward per step on average due to exploration. Over 1000 steps, cumulative regret 198\approx 198. This never goes to zero for fixed ε>0\varepsilon>0—it's the permanent cost of exploration. This is precisely why the policy does not converge to optimal: the constant ε\varepsilon-exploration keeps sampling bad actions forever.

Trade-off: Smaller ε\varepsilon → less regret after convergence, but slower estimate convergence. Larger ε\varepsilon → faster discovery, but more long-term regret.

Choosing Epsilon

Fixed epsilon:

  • ε=0.1\varepsilon = 0.1 (10% exploration): Standard default, good general-purpose
  • ε=0.01\varepsilon = 0.01 (1% exploration): When environment is stable and convergence matters
  • ε=0.3\varepsilon = 0.3 (30% exploration): Early in learning or non-stationary environments

Decaying epsilon (more sophisticated):

εt=max(εmin,ε0γt)\varepsilon_t = \max\left(\varepsilon_{\min}, \varepsilon_0 \cdot \gamma^t\right)

where ε0=1.0\varepsilon_0 = 1.0 (pure exploration initially), γ(0.995,0.999)\gamma \in (0.995, 0.999), εmin=0.01\varepsilon_{\min} = 0.01.

Why decay? Early on, explore agressively to build good estimates. Later, exploit more since estimates are reliable. Crucially, only if εt0\varepsilon_t \to 0 does the policy itself converge to the purely optimal (greedy) policy. With a fixed εmin>0\varepsilon_{\min} > 0 you retain permanent exploration to handle non-stationary environments (at the cost of never being fully optimal).

Why it feels right: You're exploiting 99.9% of the time, so most actions are high-reward.

The problem: In a10-armed bandit, each non-gredy action is tried with probability 0.001/10=0.00010.001/10 = 0.0001 per step. To try each action 10 times (bare minimum for decent estimates), you need 10/0.0001=100,00010 / 0.0001 = 100{,}000 steps. If the true optimal action isn't the one you tried first by luck, you might never discover it in reasonable time.

Fix: Start with ε0.1\varepsilon \geq 0.1 for first few thousand steps. Estimates built on 2-3 samples are unreliable.

Forgot to update Q_values[action] with observed reward!


**Why it feels right**: The epsilon-greedy logic looks complete.

**The problem**: Without updating $Q(a)$ after each action, your estimates never improve. You're exploring and exploiting based on *initial random guesses* forever.

**Fix**: Always include the update rule (sample-average or incremental):

$$
Q_{n+1}(a) = Q_n(a) + \frac{1}{n} \left[r_n - Q_n(a)\right]
$$

after every action. This is the *learning* part of reinforcement learning.

> [!mistake] Common Mistake 3: "The policy converges to optimal"
> **Wrong idea**: "With fixed $\varepsilon$, epsilon-greedy converges to always picking the best action (with probability $1-\varepsilon$)."

**Why it feels right**: The value estimates $Q(a)$ *do* converge to $q_*(a)$, so it's tempting to think the behavior converges to optimal too.

**The problem**: Only the *estimates* converge. The *policy* keeps exploring at rate $\varepsilon$ forever. In the limit, $\Pr[a_t = a^*] = (1-\varepsilon) + \varepsilon/k$, which is **strictly less than 1** (and not simply $1-\varepsilon$—you gain a small $\varepsilon/k$ from randomly stumbling onto $a^*$ during exploration). Expected regret stays positive.

**Fix**: Distinguish "estimate convergence" (guaranteed) from "policy convergence to optimal" (requires $\varepsilon_t \to 0$). If you truly need an optimal policy in the limit, use a decay schedule.

> [!mistake] Common Mistake 4: Explore = Pick Second-Best
> **Wrong idea**: "Exploration means trying the second-best action to see if estimates are wrong."

**Steel-man**: This seems smarter than pure random—why waste time on obviously terrible actions?

**The problem**: This is a different scheme, not epsilon-greedy. If the true ranking is $a_3 > a_2 > a_1$ but your estimates are $Q(a_1) > Q(a_2) > Q(a_3)$ (you got unlucky early), you'll never try $a_3$ and never discover it's best.

**The math**: Probability of trying the true optimal action $a_3$ under this wrong scheme is0 if it's not in the top 2 estimates. Under true epsilon-greedy, $\Pr[a_3 \text{ chosen}] \geq \varepsilon/k > 0$ always.

**Fix**: During exploration phase, pick *uniformly at random* from all actions, not from some subset.

## Algorithm Pseudocode

```python
# Initialize
Q = {a: 0 for a in actions}  # Value estimates
N = {a: 0 for a in actions}  # Action counts

for t in range(num_steps):
    # Epsilon-greedy action selection
    if random() < epsilon:
        action = random_choice(actions)  # Explore
    else:
        action = argmax(Q)  # Exploit
    
    # Take action, observe reward
    reward = environment.step(action)
    
    # Incremental update (why: efficient, online learning)
    N[action] += 1
    Q[action] += (reward - Q[action]) / N[action]
    # This is equivalent to Q[a] = mean of all rewards for action a

Why incremental update? Instead of storing all past rewards andcomputing the mean (memory intensive), we update the estimate online:

Qn+1=1n+1i=1n+1ri=1n+1(rn+1+i=1nri)=1n+1(rn+1+nQn)=Qn+1n+1(rn+1Qn)\begin{align} Q_{n+1} &= \frac{1}{n+1} \sum_{i=1}^{n+1} r_i \\ &= \frac{1}{n+1} \left(r_{n+1} + \sum_{i=1}^{n} r_i\right) \\ &= \frac{1}{n+1} \left(r_{n+1} + n \cdot Q_n\right) \\ &= Q_n + \frac{1}{n+1}(r_{n+1} - Q_n) \end{align}

This is a moving average that converges to the sample mean.

Recall Explain to a 12-year-old

Imagine you're trying to find the best ice cream flavor at a new shop with 10 flavors. You taste chocolate first and it's yummy (8/10). Now you have a choice:

Always pick chocolate (gredy): You'll eat chocolate every time. But what if mint chocolate chip is actually10/10 and you never find out?

Try random flavors (pure exploration): You waste money tasting the gross bubblegum flavor over and over.

Epsilon-greedy is smarter: Most of the time (like 9 out of 10 visits), eat the best flavor you've found so far. But 1 out of 10 times, try something random—maybe you'll discover an even better flavor!

The "ε\varepsilon" (epsilon) is just a fancy name for "how often do I try something random?" If ε=0.1\varepsilon = 0.1, that's 1 out of 10 times. Here's the twist: even after you've figured out which flavor is truly best, you STILL try random ones 1-in-10 times. So you never eat the best flavor every single time—you keep "wasting" a few visits exploring. That's the price of never missing a better flavor. If you want to stop wasting visits once you're sure, you slowly lower ε\varepsilon toward zero.

Connections

  • 5.1.01-Multi-armed-Bandits: Epsilon-greedy is the simplest effective solution
  • 5.1.09-Upper-Confidence-Bound: Smarter exploration based on uncertainty
  • 5.1.10-Thompson-Sampling: Probabilistic alternative to epsilon-greedy
  • 5.2.04-Exploration-vs-Exploitation: Fundamental RL tradeoff this addresses
  • 6.3.02-Q-Learning: Uses epsilon-greedy for action selection in MDPs
  • 7.1.05-Experience-Replay: Deep RL also uses epsilon-greedy with neural Q-networks
  • 3.4.07-Stochastic-Gradient-Descent: Similar incremental update rule for Q(a)Q(a)

#flashcards/ai-ml

What is the epsilon-greedy policy?
With probability 1ε1-\varepsilon, choose the action with highest estimated value (exploit). With probability ε\varepsilon, choose a random action uniformly (explore).
What exactly converges under fixed-ε epsilon-greedy—the policy or the value estimates?
Only the value estimates Q(a)Q(a) converge to the true q(a)q_*(a). The policy does NOT converge to purely optimal; it keeps exploring at rate ε\varepsilon forever.
In the limit, what is the probability of selecting the optimal action under fixed ε?
(1ε)+εk(1-\varepsilon) + \frac{\varepsilon}{k}, which is strictly less than 1 (and not just 1ε1-\varepsilon, because exploration randomly picks aa^* with probability ε/k\varepsilon/k).
How do you make the epsilon-greedy policy converge to optimal?
Decay εt0\varepsilon_t \to 0 over time (e.g., εt=max(εmin,ε0γt)\varepsilon_t = \max(\varepsilon_{\min}, \varepsilon_0 \gamma^t) with εmin=0\varepsilon_{\min}=0). Only then does the policy become purely greedy in the limit.
Why does the estimate Q(a)Q(a) converge for every action?
Because ε>0\varepsilon>0 guarantees each action is tried infinitely often (nan_a \to \infty), so by the law of large numbers the sample-average estimate converges to the true mean q(a)q_*(a).
What is the probability that a non-greedy action aia_i is chosen under epsilon-greedy?
Pr[at=ai]=(1ε)1[currently estimated best]+εk\Pr[a_t = a_i] = (1-\varepsilon) \cdot \mathbb{1}[\text{currently estimated best}] + \frac{\varepsilon}{k} where kk is the number of actions.
What happens if epsilon is too small (e.g., 0.001) in a 10-armed bandit?
Each action is explored with probability 0.001\approx 0.001 per step. Building reliable estimates requires thousands or millions of steps, so estimate convergence is extremely slow.
What is the incremental update rule for action values in epsilon-greedy?
Qn+1(a)=Qn(a)+1n+1[rn+1Qn(a)]Q_{n+1}(a) = Q_n(a) + \frac{1}{n+1}[r_{n+1} - Q_n(a)] which is an efficient online computation of the sample average.
What is the expected per-step regret under fixed-ε epsilon-greedy after estimates converge?
L=εkaa(q(a)q(a))\mathcal{L} = \frac{\varepsilon}{k} \sum_{a \neq a^*} (q_*(a^*) - q_*(a)), which stays strictly positive—it never goes to zero for fixed ε>0\varepsilon>0.

Concept Map

must balance

first principles

motivates

resolves

with prob 1 minus epsilon

with prob epsilon

controls

every action tried

law of large numbers

fixed epsilon

argmax matches

Exploration vs Exploitation Tradeoff

Reinforcement Learning Agent

Epsilon-greedy Policy

Exploit: argmax Q of a

Explore: random action

Parameter epsilon

Multi-armed Bandit

Pure Greedy fails

Value estimates Q of a

Q converges to q*

Policy never fully greedy

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho yaar, socho tum ek naye food court mein ho jahan 10 restaurants hain. Ek jagah try kiya, achha laga (7/10). Ab dilemma yeh hai — hamesha wahi "known good" jagah jaate raho, ya kabhi kabhi risk lekar naya try karo jo shayad aur behtar nikle? Yahi hai exploration vs exploitation ka core tradeoff. Epsilon-greedy iska ekdum simple solution deta hai: ek biased coin flip karo. Zyada tar time (probability 1ε1-\varepsilon) apni current best option pe jao — yeh hai exploit. Aur kabhi kabhi (probability ε\varepsilon) koi bhi random option try karo — yeh hai explore. Bas itni si randomization tumhe ek galat jagah pe hamesha ke liye phasne se bacha leti hai.

Ab yeh kyun important hai? Kyunki agar tum sirf exploit karoge (pure greedy), toh problem yeh hai ki tumhara pehla estimate galat bhi ho sakta hai — shayad action a1a_1 ne lucky reward de diya ho, aur a2a_2 jo actually 10/10 hai woh tumne kabhi try hi nahi kiya. Aur agar sirf explore karoge, toh tum obviously bekaar actions pe time waste karte rahoge. Epsilon-greedy dono ke beech ka balanced, principled raasta hai. Kyunki ε>0\varepsilon > 0 hai, har action infinite time mein infinitely often try hota hai, isliye law of large numbers ke hisaab se tumhare value estimates Q(a)Q(a) dheere dheere true values q(a)q_*(a) tak converge kar jaate hain.

Ek important baat yaad rakhna jo log confuse karte hain: fixed ε\varepsilon ke saath tumhare estimates toh converge ho jaate hain, lekin policy kabhi pure greedy nahi banti — woh hamesha ε\varepsilon rate se explore karti rehti hai. Isliye optimal action choose karne ki probability limit mein (1ε)+εk(1-\varepsilon) + \frac{\varepsilon}{k} hoti hai, na ki poore 1. Agar tumhe chahiye ki policy eventually hamesha best action hi pick kare, toh tumhe ε\varepsilon ko time ke saath dheere dheere zero tak decay karna padega. Yeh chhota sa idea RL ka foundation hai — computationally bilkul sasta, lekin practically bahut powerful.

Go deeper — visual, from zero

Test yourself — Reinforcement Learning Foundations

Connections