5.1.8 · D4Reinforcement Learning Foundations

Exercises — Epsilon-greedy strategy

3,332 words15 min readBack to topic

This page is a self-test. Each problem has a hidden solution — try it first, then reveal. Everything here builds on the parent note, and touches 5.1.01-Multi-armed-Bandits, 5.2.04-Exploration-vs-Exploitation and 6.3.02-Q-Learning.

The picture below is epsilon-greedy in one glance: the biased coin at the top, its two outcomes (exploit vs. explore), and where each branch's arrow lands among the arms. Trace the blue arrow (exploit → the tall estimated-best bar) and the pale-yellow arrows (explore → any bar, uniformly). Keep this diagram in mind for every probability we compute.

Figure — Epsilon-greedy strategy

Two facts we reuse constantly. Keep them in view:

The next figure animates the incremental-average update as a staircase: each reward nudges the running guess toward the sample, and the nudges shrink as . This is exactly the update you will hand-compute in Exercise 2.1 — watch the blue dots climb toward the pink true-mean line.

Figure — Epsilon-greedy strategy

Level 1 — Recognition

Exercise 1.1 (L1)

State, in plain words, what the two branches of the epsilon-greedy rule do, and give the probability of each branch when .

Recall Solution 1.1
  • Exploit branch (probability ): pick the arm with the highest current estimate .
  • Explore branch (probability ): pick an arm uniformly at random, ignoring the estimates.

With : exploit of the time, explore of the time. (This is exactly the coin at the top of the first figure.)

Exercise 1.2 (L1)

A 4-armed bandit uses . Arm is currently the estimated best. What is ? What is (a non-best arm)?

Recall Solution 1.2

Here , so the explore branch lands on any specific arm with probability .

  • Best arm : .
  • Non-best arm : .

Sanity check: probabilities sum to . ✓ (These are exact here — no rounding — because terminates in decimal. Contrast Exercise 2.2 where does not, so the digits are truncated.)

Exercise 1.3 (L1)

True or false: "With a fixed , the policy eventually always picks the best arm." Explain.

Recall Solution 1.3

False. Even after the estimates are perfect, the explore branch fires with probability forever. The best arm is chosen with probability , never . Only a decaying makes the policy converge to pure greedy. (The estimates converge either way — that is a different statement, drawn side-by-side in the third figure below.)

The two timelines below make this concrete side by side: left, the estimate climbs and locks onto the true value (converges); right, the probability of picking the best arm rises but flattens at , a dashed ceiling strictly below (does not converge to greedy).

Figure — Epsilon-greedy strategy

Level 2 — Application

Exercise 2.1 (L2)

An arm has been pulled 3 times giving rewards . Compute using the incremental average, showing every intermediate step. Start from .

Recall Solution 2.1

Use .

  • : .
  • : .
  • : .

Check against the plain mean: . ✓ The incremental formula gives the same answer without storing all past rewards. This is the staircase in the second figure — each step's vertical jump is , getting smaller as grows.

Exercise 2.2 (L2)

A 3-armed bandit with . Estimates are . Give the probability of selecting each arm on the next step.

Recall Solution 2.2

Estimated best is (highest ). Here , so

On the "" here: has no finite decimal, so and are truncated — the true values are and . If you add the truncated decimals you get , off by ; add the exact fractions and they sum to exactly. Always sum the fractions when checking normalization, never the rounded digits. ✓

Exercise 2.3 (L2)

With and , over steps roughly how many times is each non-best arm chosen through exploration alone?

Recall Solution 2.3

Each step, a specific non-best arm is chosen by exploration with probability . Over steps: (The best arm also gets these ~33 explore-visits plus ~900 exploit-visits.) The word "roughly" is honest: this is an expected count; the actual number is random and fluctuates by about (standard deviation of a binomial). So "33" really means "33 give or take a handful."


Level 3 — Analysis

Exercise 3.1 (L3)

Same 3-armed bandit: , . Assume estimates have converged so is the estimated best. Compute the expected regret per step.

Recall Solution 3.1

Regret gap for each suboptimal arm: .

  • , with .
  • , with .

Expected regret per step: Over steps, cumulative regret . This never drops to for fixed — it is the permanent cost of exploration.

Exercise 3.2 (L3)

For the same bandit, by how much does the expected regret per step change if we cut from to ? Compute both and the ratio.

Recall Solution 3.2

Regret scales linearly with because every suboptimal selection probability is . Let . Then .

  • .
  • .
  • Ratio smaller.

The catch (analysis): regret dropped , but each arm now gets explored less often, so the estimates take longer to become reliable. This is the exploration–exploitation dial in one number.

The figure below plots that linear relationship. The yellow line is ; the two marked points are the answers to this exercise — read off how the blue dot (, ) sits ten times higher than the pink dot (, ). Because the graph is a straight line through the origin, "cut by " always means "cut steady-state regret by ."

Figure — Epsilon-greedy strategy

Exercise 3.3 (L3)

Optimistic init vs. exploration: two arms, true means . A pure greedy agent () initializes , pulls first (tie broken to ), sees reward . Show it may get stuck on the wrong arm, and explain why would rescue it.

Recall Solution 3.3

After pulling once: , . Greedy now always picks (its estimate is higher). It never pulls , so stays forever and the agent never learns that . It is stuck on the suboptimal arm.

With : the explore branch pulls with probability each step, so eventually , overtakes , and the agent switches. Exploration is what unsticks the greedy trap.


Level 4 — Synthesis

Exercise 4.1 (L4)

Decaying schedule. Use with . Find the step at which the decaying term first drops to or below (i.e. the floor kicks in).

Recall Solution 4.1

We want the smallest with , i.e. .

Why take of both sides? The unknown is stuck in the exponent. The natural log is the tool that pulls an exponent down into a plain coefficient, because . That converts an awkward exponential inequality into a linear one in that we can just divide. And is a strictly increasing function, so applying it to both sides keeps the inequality direction (): Now the crucial sign step: is a negative number (since ). Dividing an inequality by a negative number flips the direction, so: The smallest integer satisfying this is — from then on is pinned at .

Design reading: ~460 steps of aggressive early exploration, then a permanent trickle to stay robust to a changing (non-stationary) environment.

Exercise 4.2 (L4)

Connecting to UCB/Thompson. In one sentence each, contrast how epsilon-greedy, UCB, and Thompson sampling decide which arm to explore, then argue why epsilon-greedy wastes exploration budget.

Recall Solution 4.2
  • Epsilon-greedy: explores by picking uniformly at random — no regard for uncertainty.
  • UCB: explores the arm with the largest optimistic bound — deliberately targets arms it is most uncertain about.
  • Thompson: samples a plausible value from each arm's posterior and picks the max — probability-matched exploration.

Waste argument: epsilon-greedy spends the same on an obviously terrible arm as on a promising, uncertain one. UCB and Thompson concentrate exploration where it is informative, so they achieve lower regret. Epsilon-greedy's flat regret per step is the price of that indiscriminate coin flip.

The schematic below shows this visually: three arms drawn with their uncertainty (the pale error bars). Epsilon-greedy's flat uniform arrows waste equal weight on the clearly-bad left arm; UCB's arrow points at the tallest optimistic bar (widest uncertainty on top of its estimate); Thompson draws a sample (the pink dots) from each arm's spread and picks whichever sample is highest — so its exploration automatically favors arms that could plausibly be best.

Figure — Epsilon-greedy strategy

Exercise 4.3 (L4)

Q-learning link. In 6.3.02-Q-Learning with 7.1.05-Experience-Replay, epsilon-greedy is the behavior policy. Explain why we still want during training even though the target we compute is (greedy).

Recall Solution 4.3

The target uses greedy because we want to learn the value of acting optimally. But if the behavior that collects data were also greedy, the replay buffer would fill with only the currently-favored actions, and states/actions the agent wrongly thinks are bad would never be visited — the same "stuck" trap as Exercise 3.3, now over a whole state space. A behavior keeps the buffer diverse, so the greedy target is estimated over the full action set. (This split — greedy target, exploratory behavior — is off-policy learning.)


Level 5 — Mastery

Exercise 5.1 (L5)

Full closed form. A -armed bandit with converged estimates uses fixed . Let the gaps be for the suboptimal arms. Derive the expected regret per step in closed form, then evaluate for , , gaps .

Recall Solution 5.1

Derivation. Once estimates are correct, the exploit branch always picks the optimal arm , so it contributes zero regret. Regret only comes from the explore branch, which lands on any specific arm with probability . A suboptimal arm costs each time it is chosen. Multiply probability by cost and sum over the suboptimal arms: The factors out because every suboptimal arm shares the same explore probability — this is why regret is exactly linear in .

Evaluate. Plug in and : Over steps: cumulative regret .

Exercise 5.2 (L5)

Optimal fixed is a trade-off with a real minimum. Suppose (toy model) the regret per step has two competing parts: exploration cost (as above) plus a "slow-learning" penalty that shrinks as you explore more. With and , find the that minimizes total per-step cost .

Recall Solution 5.2

Why a derivative? We want the bottom of a U-shaped cost curve — the where cost stops falling and starts rising. At that flat bottom the slope (derivative) is exactly zero, so we compute and set it to .

Step 1 — differentiate. Write . Term by term: , and . So

Step 2 — set to zero and solve for .

\;\Rightarrow\; \varepsilon^2 = \frac{c}{S} \;\Rightarrow\; \varepsilon^\star = \sqrt{\frac{c}{S}}$$ (we take the positive root since $\varepsilon>0$). **Step 3 — confirm it is a minimum, not a maximum.** Take the second derivative: $$f''(\varepsilon) = \frac{d}{d\varepsilon}\!\left(-c\,\varepsilon^{-2}\right) = 2c\,\varepsilon^{-3} = \frac{2c}{\varepsilon^3}.$$ With $c>0$ and $\varepsilon>0$ this is **positive**, so the curve is concave-up (a valley) — the critical point is genuinely a **minimum**. ✓ **Step 4 — plug in numbers.** $\varepsilon^\star = \sqrt{0.5/2.0} = \sqrt{0.25} = \mathbf{0.5}$, and the minimum cost is $f(0.5) = 2.0(0.5) + 0.5/0.5 = 1.0 + 1.0 = \mathbf{2.0}$. **Reading:** too small $\varepsilon$ → the $c/\varepsilon$ penalty (slow learning) blows up; too large → the $S\varepsilon$ term (wasteful pulls) blows up. The sweet spot balances them.
Figure — Epsilon-greedy strategy

Exercise 5.3 (L5)

Convergence of the selection probability. For fixed and arms, once estimates are correct, write and prove it is strictly between and for any , .

Recall Solution 5.3

  • Greater than : the extra term (since , finite), so the limit exceeds .
  • Less than : . For and , the subtracted term , so the value is strictly below .

Hence . The best arm is favored but never certain — the exact statement of "fixed explores forever."


Recall Self-test checklist

Fixed epsilon: does the policy converge to greedy? ::: No — only estimates converge; policy keeps exploring at rate . Probability a non-best arm is chosen? ::: . Probability the estimated-best arm is chosen? ::: . Steady-state regret per step? ::: . Why can pure greedy get stuck? ::: It stops pulling an arm after one bad sample, so that arm's estimate never corrects. Condition for decaying to still guarantee convergence? ::: Each arm pulled infinitely often, e.g. .