Reinforcement Learning

ε-greedy, UCB & Thompson Sampling

Solving the classic Exploration vs Exploitation trade-off in decision systems, recommendation feeds, and multi-armed bandits.

🟡 intermediate5 min readrlbandits
Exploration vs Exploitation is the fundamental dilemma in Reinforcement Learning and recommendation systems: should the agent Exploit the current best-known action to maximize short-term reward, or Explore unknown actions to discover potentially higher long-term rewards? Key strategies include ε-greedy (random uniform exploration), Upper Confidence Bound (UCB - Optimism in the face of uncertainty), and Thompson Sampling (Bayesian posterior probability matching).

The Exploration-Exploitation Dilemma

                              THE DECISION DILEMMA
┌───────────────────────────────────────┬───────────────────────────────────────┐
│           EXPLOITATION                │             EXPLORATION               │
├───────────────────────────────────────┼───────────────────────────────────────┤
│ Pick action with highest known reward.│ Pick under-sampled action to gain     │
│ Maximizes short-term revenue.         │ new information.                      │
│ Risk: Misses hidden optimal action!   │ Risk: Wastes short-term user clicks.  │
└───────────────────────────────────────┴───────────────────────────────────────┘

Three Core Exploration Strategies

1. $\epsilon$-Greedy

Drawback: Explores all suboptimal arms equally, even those known to be terrible.

2. Upper Confidence Bound (UCB1)

Principle: Optimism in the Face of Uncertainty.

$$a_t = \arg\max_{a \in A} \left[ \hat{Q}(a) + c \cdot \sqrt{\frac{\ln N}{N_a}} \right]$$

  Arm A (Sampled 100 times): Mean = 0.70, Bonus = 0.05 ──► Upper Bound = 0.75
  Arm B (Sampled 2 times):   Mean = 0.50, Bonus = 0.40 ──► Upper Bound = 0.90 (WINNER -> Explore B!)

3. Thompson Sampling (Bayesian Posterior Sampling)

Maintains Beta distribution $\text{Beta}(\alpha_a, \beta_a)$ prior for each binary arm $a$:

  1. For each arm $a$, sample $\theta_a \sim \text{Beta}(\alpha_a, \beta_a)$.
  2. Pull arm $a^* = \arg\max_a \theta_a$.
  3. Observe reward $r \in {0, 1}$. Update: $\alpha_{a^} \leftarrow \alpha_{a^} + r$, $\beta_{a^} \leftarrow \beta_{a^} + (1-r)$.

Strategy Comparison Matrix

StrategyExploration TypeRegret BoundBest Use Case
$\epsilon$-GreedyRandom UniformLinear $O(T)$ (unless $\epsilon_t \to 0$)Simple baseline, low-overhead systems
UCB1Deterministic OptimismLogarithmic $O(\ln T)$Cold-start news / ad ranking feeds
Thompson SamplingBayesian Probability MatchingLogarithmic $O(\ln T)$E-commerce recommendations, A/B testing

Say this out loud

"Exploration vs Exploitation balances short-term reward against long-term learning. ε-greedy explores via uniform random sampling. UCB1 implements 'Optimism in the face of uncertainty', adding an upper confidence bound bonus c√(ln N / N_a) to under-sampled arms. Thompson Sampling samples from Bayesian posterior distributions Beta(α,β), offering state-of-the-art logarithmic regret in production recommendation engines."

Follow-ups to expect

Check yourself

Question 1 of 3

What is the core principle of the Upper Confidence Bound (UCB1) algorithm?

More in Reinforcement Learning

See all →
Value-Based vs Policy-Based Methods5 minMulti-Armed Bandits4 minProximal Policy Optimization5 min