Overview

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.
Comparing multi-armed bandits, contextual bandits, and full reinforcement learning.
Incremental update formula applied in all bandit algorithms.
Incremental update formula applied in all bandit algorithms.
Upper confidence bound (UCB) core algorithm.
Upper confidence bound (UCB) core algorithm.
Bernoulli mass distribution.
Bernoulli mass distribution.
Beta distribution and its components.
Beta distribution and its components.
Evolution of probability beliefs for one Ad (arm) in Thomson sampling.
Evolution of probability beliefs for one Ad (arm) in Thomson sampling.
Comparison of running bandit algorithms after 5000 iterations.
Comparison of running bandit algorithms after 5000 iterations.
Cumulative Regret comparisons of different policies for 500 iterations (for better comparison).
Cumulative Regret comparisons of different policies for 500 iterations (for better comparison).
Expected reward (payoff) in LinUBC algorithm.
Expected reward (payoff) in LinUBC algorithm.
LinUCB algorithms and its components.
LinUCB algorithms and its components.
Performance comparison on the test set.
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.

FAQ

What is a contextual bandit, and how does it differ from a multi-armed bandit?A contextual bandit is a bandit problem where the algorithm observes side information, called context, before choosing an action. Unlike a plain multi-armed bandit, which looks for one best action overall, a contextual bandit learns the best action for each specific context. The context is observed but does not evolve as a result of the action.
Why are bandits considered “lightweight” reinforcement learning?Bandits are called lightweight reinforcement learning because they keep the core loop of choose an action, observe a reward, and update the strategy, but remove the complexity of state transitions and long-term planning. They are useful when the decision only affects the immediate reward, not a future sequence of states.
What is the exploration–exploitation trade-off?Exploration means trying uncertain actions to learn more about them, while exploitation means choosing the action that currently looks best. The trade-off is balancing short-term reward against the need to gather information that may improve future decisions. Good bandit algorithms must manage both.
How is an ad campaign a good example of a bandit problem?In an ad campaign, each ad creative can be treated as an arm, showing an ad is the action, and a click is the reward. The goal is to identify the best-performing ad quickly while still exploring enough to avoid missing a better option. This matches the bandit setting very well.
What does the incremental update rule do in bandit algorithms?The incremental update rule updates an arm’s estimated value using only the previous estimate, the number of times the arm has been selected, and the new reward. This avoids storing all past rewards and makes learning efficient in both memory and computation.
How does the ε-greedy algorithm work?ε-greedy chooses a random action with probability ε to explore, and chooses the best-known action with probability 1−ε to exploit. It is simple and effective, because it mostly uses what it has learned while still occasionally trying new actions.
What is the idea behind UCB (Upper Confidence Bound)?UCB selects actions using a score made of two parts: the current estimated reward and an uncertainty bonus. Actions that have been tried less often get a larger bonus, which encourages structured exploration. Over time, the bonus shrinks as more data is collected.
How does Thompson Sampling balance exploration and exploitation?Thompson Sampling keeps a probability distribution for each arm’s possible reward rate, then samples from each distribution and chooses the arm with the highest sample. Arms with more uncertainty are more likely to get sampled high, so the method naturally explores while still favoring promising actions.
Why is the Beta distribution used in Thompson Sampling for click/no-click problems?The Beta distribution is defined on the interval [0,1], so it is a natural model for probabilities like click-through rate. It is also conjugate to the Bernoulli distribution, which means it updates neatly after observing clicks and non-clicks. This makes it convenient for Bayesian bandit learning.
What is LinUCB and why is it useful for personalized discounting?LinUCB is a contextual bandit algorithm that combines linear modeling with an upper confidence bound strategy. It learns how customer features relate to reward for each action, then uses both predicted reward and uncertainty to choose a discount. This makes it well suited for personalized offers, because it can adapt decisions to the customer’s context.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Applied Reinforcement Learning Applied Reinforcement Learning ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Applied Reinforcement Learning Applied Reinforcement Learning ebook for free