Epsilon-greedy strategy
5.1.8· AI-ML › Reinforcement Learning Foundations
Yeh Kaunsi Problem Solve Karta Hai?
Reinforcement learning mein, ek agent ko balance karna hota hai:
- Exploitation: Woh actions choose karo jo pehle high rewards dete the
- Exploration: Nayi actions try karo taaki potentially better rewards discover ho sakein
Pure exploitation tab fail hota hai jab tumhare initial estimates galat hote hain. Pure exploration obviously bure actions par time waste karta hai. Epsilon-greedy ek principled randomization provide karta hai jo ensure karta hai ki value estimates converge hon true action values ki taraf, aur yeh computationally bilkul trivial bhi rehta hai. (Note: fixed ke saath policy khud kabhi purely greedy nahi banti—woh hamesha ke liye explore karti rehti hai.)
Epsilon-greedy policy time par action select karti hai:
Yeh kaam kyun karta hai: term guarantee karta hai ki har action infinitely often try hoga (jab ), isliye estimates true values par converge ho jaate hain. Lekin policy pure greedy par converge nahi karti—woh hamesha rate se explore karti rehti hai.
First Principles Se Derivation
Starting point: Multi-armed bandit problem. Har action ka unknown true reward hai. Hum estimates maintain karte hain observed rewards ke basis par.
Step 1: Pure greedy fail hota hai Greedy policy: hamesha pick karo
Problem: Agar action pehli try par reward 5 deta hai, aur hum kabhi explore nahi karte, toh hum kabhi discover nahi kar paate ki hai. Humara estimate ek lucky sample par based ho sakta hai, true mean par nahi.
Step 2: Exploration add karo probability se, current estimates ignore karo aur uniformly at random pick karo. Yeh ensure karta hai:
jahan . Infinite time mein, har action infinitely often try hota hai.
Step 3: Actually kya converge hota hai Jaise jaise har action ko baar try kiya jaata hai, law of large numbers se:
Kyunki ensure karta hai ki sabhi ke liye , value estimates converge ho jaate hain: eventually (high probability ke saath). Lekin policy hamesha optimal action pick karne par converge nahi karti. Limit mein, optimal action select karne ki probability hai:
na aur na . Ek aisi policy paane ke liye jo sach mein pure greedy par converge kare, tumhe decay karna hoga over time (neeche dekho).
Kisi bhi suboptimal action ke liye jahan :
Yeh formulas kyun hain: Probability do mutually exclusive cases mein split hoti hai:
- Hum exploit karte hain (prob ) AUR yeh action abhi best estimated hai (indicator function)
- Hum explore karte hain (prob ) AUR randomly yeh action pick hoti hai (uniform )
Jab estimates accurate ho jaate hain, optimal action hi estimated-best hota hai, isliye (strictly less than 1 fixed ke liye).

Hum use karte hain (10% time explore karo).
Initial state (t=0): (koi knowledge nahi)
Step 1: Random exploration choose karta hai, observe karo
- Update: , baaki 0 rehte hain
- Current best:
Step 2: 0.9 prob se, exploit karo → choose karo. Maano hume milta hai
Yeh step kyun? Hum current knowledge exploit kar rahe hain, lekin humara estimate sirf 2 samples par based hai.
Step 10: Kuch explores ke baad, maano , ,
- Current best:
- Next action: 0.9 prob se → choose karo (exploit), 0.1 prob se → uniform random
Epsilon kyun matter karta hai: Bhale hi best lagta hai, hum phir bhi kabhi kabhi aur try karte hain agar hamare estimates galat hain. 1000 steps mein, exploration ke dauran har action baar try hota hai, plus current best ke liye extra. Note: estimates perfect hone ke baad bhi, hum sirf time choose karte hain—kabhi 100% nahi.
Regret per step: Jab hum suboptimal action choose karte hain, hum reward kho dete hain.
Estimates converge hone ke baad (woh accurate hain, lekin policy abhi bhi explore karti hai), expected regret per step hai:
Hamare bandit ke liye ke saath:
Expected regret per step:
Yeh kyun important hai: Exploration ki wajah se hum average mein har step ~0.2 reward lose karte hain. 1000 steps mein, cumulative regret . Fixed ke liye yeh kabhi zero nahi jaata—yeh exploration ka permanent cost hai. Yahi reason hai ki policy optimal par converge nahi karti: constant -exploration hamesha ke liye bure actions ko sample karta rehta hai.
Trade-off: Chhota → convergence ke baad kam regret, lekin estimate convergence slow. Bada → faster discovery, lekin zyaada long-term regret.
Epsilon Kaise Choose Karein
Fixed epsilon:
- (10% exploration): Standard default, general-purpose ke liye achha
- (1% exploration): Jab environment stable ho aur convergence important ho
- (30% exploration): Learning ke shuruaat mein ya non-stationary environments mein
Decaying epsilon (zyaada sophisticated):
jahan (initially pure exploration), , .
Decay kyun? Shuruaat mein aggressively explore karo taaki achhe estimates ban sakein. Baad mein zyaada exploit karo kyunki estimates reliable ho chuke hain. Importantly, sirf agar ho tabhi policy khud purely optimal (greedy) policy par converge karti hai. Fixed ke saath tum permanent exploration rakhte ho non-stationary environments handle karne ke liye (is cost par ki tum kabhi fully optimal nahi ban paate).
Kyun sahi lagta hai: Tum 99.9% time exploit kar rahe ho, toh zyaadatar actions high-reward hain.
Problem yeh hai: 10-armed bandit mein, har non-greedy action probability per step se try hota hai. Har action ko 10 baar try karne ke liye (decent estimates ke liye bare minimum), tumhe steps chahiye. Agar true optimal action pehle lucky try kiya hua nahi hai, toh tum reasonable time mein shayad kabhi discover nahi kar paoge.
Fix: Pehle kuch thousand steps ke liye se start karo. 2-3 samples par bane estimates unreliable hote hain.
Forgot to update Q_values[action] with observed reward!
**Kyun sahi lagta hai**: Epsilon-greedy logic complete lagta hai.
**Problem yeh hai**: Har action ke baad $Q(a)$ update kiye bina, tumhare estimates kabhi improve nahi hote. Tum hamesha *initial random guesses* ke basis par explore aur exploit karte rehte ho.
**Fix**: Hamesha update rule shamil karo (sample-average ya incremental):
$$
Q_{n+1}(a) = Q_n(a) + \frac{1}{n} \left[r_n - Q_n(a)\right]
$$
har action ke baad. Yahi reinforcement learning ka *learning* part hai.
> [!mistake] Common Mistake 3: "Policy Optimal Par Converge Hoti Hai"
> **Galat idea**: "Fixed $\varepsilon$ ke saath, epsilon-greedy hamesha best action pick karne par converge hoti hai ($1-\varepsilon$ probability se)."
**Kyun sahi lagta hai**: Value estimates $Q(a)$ *sach mein* $q_*(a)$ par converge ho jaate hain, toh yeh sochna tempting hai ki behavior bhi optimal par converge ho jaata hai.
**Problem yeh hai**: Sirf *estimates* converge karte hain. *Policy* hamesha ke liye $\varepsilon$ rate se explore karti rehti hai. Limit mein, $\Pr[a_t = a^*] = (1-\varepsilon) + \varepsilon/k$, jo **strictly less than 1** hai (aur simply $1-\varepsilon$ nahi—tumhe exploration ke dauran randomly $a^*$ par stumble karne se chhota $\varepsilon/k$ gain bhi milta hai). Expected regret positive rehta hai.
**Fix**: "Estimate convergence" (guaranteed) aur "policy convergence to optimal" (jo $\varepsilon_t \to 0$ require karta hai) mein fark samjho. Agar tumhe limit mein sach mein optimal policy chahiye, toh decay schedule use karo.
> [!mistake] Common Mistake 4: Explore = Second-Best Pick Karna
> **Galat idea**: "Exploration ka matlab hai second-best action try karna dekhne ke liye ki estimates galat hain ya nahi."
**Steel-man**: Yeh pure random se smarter lagta hai—obviously terrible actions par time kyun waste karein?
**Problem yeh hai**: Yeh ek alag scheme hai, epsilon-greedy nahi. Agar true ranking $a_3 > a_2 > a_1$ hai lekin tumhare estimates $Q(a_1) > Q(a_2) > Q(a_3)$ hain (shuruaat mein unlucky the), toh tum kabhi $a_3$ try nahi karoge aur kabhi discover nahi karoge ki woh best hai.
**Math yeh hai**: Is galat scheme ke under true optimal action $a_3$ try karne ki probability 0 hai agar woh top 2 estimates mein nahi hai. True epsilon-greedy ke under, $\Pr[a_3 \text{ chosen}] \geq \varepsilon/k > 0$ hamesha.
**Fix**: Exploration phase ke dauran, sabhi actions mein se *uniformly at random* pick karo, kisi subset se nahi.
## 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
Incremental update kyun? Saare past rewards store karke mean compute karne (memory intensive) ki jagah, hum estimate online update karte hain:
Yeh ek moving average hai jo sample mean par converge hoti hai.
Recall Kisi 12 Saal ke Bacche ko Explain Karo
Socho tum ek naye shop par best ice cream flavor dhundh rahe ho jahan 10 flavors hain. Pehle chocolate taste ki aur woh yummy thi (8/10). Ab tumhare paas choice hai:
Hamesha chocolate lo (greedy): Tum har baar chocolate khaoge. Lekin kya hoga agar mint chocolate chip actually 10/10 ho aur tum kabhi pata nahi lagaoge?
Random flavors try karo (pure exploration): Tum bakwaas bubblegum flavor baar baar taste karne mein paise waste karoge.
Epsilon-greedy smarter hai: Zyaadatar time (jaise 10 visits mein se 9 baar), abhi tak joa sabse achha flavor mila hai woh khao. Lekin 10 mein se 1 baar, kuch random try karo—shayad tumhe koi aur bhi achha flavor mil jaaye!
"" (epsilon) bas ek fancy naam hai "main kitni baar kuch random try karunga?" ke liye. Agar hai, toh 10 mein se 1 baar. Yahan ek twist hai: pata lagane ke baad bhi ki truly best flavor kaunsa hai, tum PHIR BHI 1-in-10 baar random try karte ho. Isliye tum best flavor har baar nahi khaate—kuch visits explore karne mein "waste" hoti rehti hain. Yahi koi better flavor miss na karne ki kimat hai. Agar tum ek baar sure ho jaane ke baad visits waste karna band karna chahte ho, toh dhire dhire ko zero ki taraf le jaao.
Connections
- 5.1.01-Multi-armed-Bandits: Epsilon-greedy sabse simple effective solution hai
- 5.1.09-Upper-Confidence-Bound: Uncertainty ke basis par smarter exploration
- 5.1.10-Thompson-Sampling: Epsilon-greedy ka probabilistic alternative
- 5.2.04-Exploration-vs-Exploitation: Fundamental RL tradeoff jise yeh address karta hai
- 6.3.02-Q-Learning: MDPs mein action selection ke liye epsilon-greedy use karta hai
- 7.1.05-Experience-Replay: Deep RL bhi neural Q-networks ke saath epsilon-greedy use karta hai
- 3.4.07-Stochastic-Gradient-Descent: ke liye similar incremental update rule
#flashcards/ai-ml