5 Contextual Bandit: Optimizing Stochastic One-step Decisions
This chapter introduces bandits as a practical bridge from full reinforcement learning to simpler one-step decision problems. It explains that when transition dynamics are unknown or unnecessary, the agent can still learn from experience by choosing an action, observing an immediate reward, and updating its beliefs. This makes multi-armed bandits a lightweight form of reinforcement learning, especially useful in business settings such as ad selection, email optimization, website testing, and promotional offers. The chapter also distinguishes pure bandits from contextual bandits, where observed information like customer traits or time of day helps tailor the decision without requiring a full state transition model.
A central theme is the exploration–exploitation trade-off: the agent must keep using what seems best while still trying uncertain options that may turn out better. The chapter develops this idea through several bandit strategies, beginning with naive random and greedy agents and then moving to ε-greedy, Upper Confidence Bound, and Thompson Sampling. These methods differ mainly in how they choose actions, but they share the same incremental update rule for estimating arm values efficiently. Through an ad-campaign simulation, the chapter shows how each algorithm behaves in practice and why balanced exploration is necessary to avoid getting stuck with a misleading early winner.
The chapter then moves to performance evaluation with regret, defining it as the cost of not always choosing the optimal action. Regret provides a formal way to compare policies and understand whether an algorithm learns efficiently over time, with strong methods achieving sublinear regret. Finally, it extends the framework to contextual bandits for personalized discounting, where LinUCB uses customer features to predict reward and choose among discount levels more intelligently than a non-contextual baseline. The result is a substantial lift in revenue, underscoring the value of personalization, while the closing discussion highlights real-world complications such as drift, delayed feedback, cold start, and nonstationarity.
Comparing multi-armed bandits, contextual bandits, and full reinforcement learning.
Incremental update formula applied in all bandit algorithms.
Upper confidence bound (UCB) core algorithm.
Bernoulli mass distribution.
Beta distribution and its components.
Evolution of probability beliefs for one Ad (arm) in Thomson sampling.
Comparison of running bandit algorithms after 5000 iterations.
Cumulative Regret comparisons of different policies for 500 iterations (for better comparison).
Expected reward (payoff) in LinUBC algorithm.
LinUCB algorithms and its components.
Performance comparison on the test set.
Summary
- Bandits are the lightweight version of reinforcement learning with no state transitions, just choose observe update.
- Bandit algorithms are best for problems with immediate payoffs and static context such as ads emails layouts and discounts.
- The central challenge in bandit algorithms is balancing exploration and exploitation to maximize long term reward.
- We do balance between exploration and exploitation because we don’t know the true probability distributions, and we have to estimate.
- Multi armed bandits ignore context and look for the best arm overall.
- Contextual bandits use features like demographics or time of day to personalize decisions in real time.
- Random strategies are baselines and should always be beaten.
- Greedy agents often underperform because they lock in too early on a suboptimal option.
- Epsilon greedy ensures steady exploration with occasional random actions.
- UCB uses confidence bounds to target exploration where uncertainty is high.
- Thompson Sampling is a Bayesian approach that balances exploration and exploitation automatically.
- Incremental update formulas allow efficient learning without storing the full history.
- Regret is the theoretical way to measure algorithm performance and the goal is sub linear regret growth.
- In real campaigns true regret cannot be calculated so proxies like business KPIs must be used.
- Off policy evaluation with logged data is a conservative but reliable way to test contextual bandits offline.
- LinUCB learns to target discounts intelligently maximizing revenue and protecting margin.
- Exploration must never be eliminated completely to avoid policy collapse.
- Drift and non-stationarity are inevitable in real markets and demand techniques like forgetting, sliding windows, or resets.
- Delayed or censored rewards require proxies and buffers to keep learning timely and accurate.
- Multiple objectives must be handled explicitly by shaping rewards or adding constraints to respect profit and business rules.
- Combinatorial bandits extend to slates, bundles, or rankings where payoffs depend on sets of actions, not single choices.
- Adversarial environments like auctions or reactive competitors may require robust algorithms such as EXP3.
- Covariate shift means contexts change over time and exploration floors are essential to avoid blind spots.
- Cold start problems for new ads or users can be tackled with optimistic priors or warm-start approaches.
- Bandits should be treated as continuous experimentation systems rather than one-off A/B tests.
- The three guiding principles are simple but powerful: keep exploring, personalize with context and expect drift.
Applied Reinforcement Learning ebook for free