Overview

5 Contextual Bandit: Optimizing Stochastic One-step Decisions

The chapter introduces bandits as a practical, lightweight form of reinforcement learning for one-step decisions when the full dynamics of a system are unknown or unnecessary. Rather than modeling transitions as in a Markov decision process, bandit methods repeatedly choose an action, observe an immediate reward, and update beliefs from experience. This makes them well suited to problems such as ad placement, email optimization, website testing, and promotional offers, where the goal is to learn which option works best while acting. The chapter also distinguishes simple multi-armed bandits from contextual bandits, where observable information like customer profile or time of day can guide the decision even though the context itself does not evolve in response to the action.

A central theme is the exploration–exploitation trade-off: exploitation uses current knowledge to choose the best-known option, while exploration tries less certain actions to gather information and avoid premature commitment. The chapter explains this balance through several algorithms, starting with random and greedy baselines, then moving to epsilon-greedy, UCB, and Thompson Sampling. Epsilon-greedy injects randomness to keep learning, UCB adds a structured uncertainty bonus to favor under-sampled arms, and Thompson Sampling uses Bayesian belief updates with Beta distributions to sample actions according to uncertainty. All of these methods rely on incremental updates of value estimates, allowing them to learn efficiently without storing full histories.

The later part of the chapter extends these ideas to contextual bandits through LinUCB, using personalized discounting as the business example. Here, each customer impression comes with features such as customer segment, purchase behavior, and timing, and the algorithm learns separate linear models per action to predict reward from context. By combining predicted reward with an uncertainty term, LinUCB can target exploration more intelligently than non-contextual epsilon-greedy, and in the example it produces substantially higher revenue on offline test data. The chapter closes by stressing regret as the formal way to measure learning performance, and by warning that real environments are often non-stationary, delayed, multi-objective, and subject to cold start and drift—so successful bandit systems must keep exploring, personalize decisions, and adapt continuously.

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.

FAQ

What is the main idea of contextual bandits in this chapter?Contextual bandits extend the basic bandit setup by adding observable context, such as customer profile or time of day, before making a decision. The algorithm uses that context to choose the best action for the current situation, while still treating the problem as a one-step decision rather than a full sequential reinforcement learning problem.
How is a multi-armed bandit different from full reinforcement learning?In a multi-armed bandit, there is no meaningful state transition to model and each round is essentially independent. In full reinforcement learning, actions change the state of the environment over time, so decisions are sequential and future rewards depend on current choices.
Why are bandits called “lightweight reinforcement learning”?They are called lightweight because they keep the core RL loop of choosing an action, observing a reward, and updating beliefs, but without the complexity of state transitions, long-term planning, or a full environment model. This makes them simpler and practical for immediate decision problems.
What is the exploration–exploitation trade-off?Exploitation means choosing the action that currently looks best based on what the algorithm has learned so far. Exploration means trying less-known actions to gather information that could lead to better decisions later. Good bandit algorithms balance both to maximize long-term reward.
How does the epsilon-greedy algorithm work?Epsilon-greedy mostly exploits by choosing the action with the highest current estimated value, but with probability ε it explores by picking a random action. This gives the algorithm a simple way to keep learning while still favoring what appears best.
What is the UCB algorithm and why is it useful?Upper Confidence Bound (UCB) chooses actions using both estimated reward and an uncertainty bonus. Actions that have been tried less often receive a larger bonus, so UCB explores in a structured way rather than randomly, while still favoring promising options.
How does Thompson Sampling balance exploration and exploitation?Thompson Sampling keeps a probability distribution for each arm’s reward and samples from those distributions each round. The arm with the highest sampled value is chosen. This naturally balances exploration and exploitation because uncertain arms can still win by chance, while strong arms are more likely to be selected.
Why are Beta and Bernoulli distributions used in Thompson Sampling for clicks?Clicks are binary outcomes, so they are naturally modeled with a Bernoulli distribution. The Beta distribution is a conjugate prior for Bernoulli data, which means it updates neatly after each click or no-click observation and represents the algorithm’s belief about the true click-through rate.
What is regret in bandit problems?Regret is the difference between the reward an agent actually obtained and the reward it could have obtained by always choosing the best action from the start. It measures the cost of learning and is used to evaluate how efficiently a bandit algorithm learns over time.
How does LinUCB personalize decisions in a contextual bandit?LinUCB learns a linear relationship between context features and expected reward for each action. For each new customer context, it computes an upper confidence bound and chooses the action with the highest score. This allows it to tailor decisions, such as discounts, to different customer segments.

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


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 ebook for free
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 ebook for free