Contextual Bandits: UCB and Thompson Sampling Explained
Executive Summary
Standard multi-armed bandit algorithms treat every user the same: they learn one global policy and ignore who is asking, when, and from where. Contextual bandits remove that limit by conditioning each exploration and exploitation decision on features of the current situation, so the chosen action can differ from one context to the next.
This paper is an explanation of the method. It is not a report of an experiment or a deployment, and it contains no measured results. It describes how contextual bandits work, how the two main families of algorithm, Upper Confidence Bound (LinUCB) and Thompson Sampling, choose actions, and what to think about when applying them: context features, cold start, delayed rewards, logging, offline evaluation, and common pitfalls.
Key Points
- Context changes the best action. When the best option differs by segment, a context-free policy can only pick the best average option, and a contextual policy can do better.
- LinUCB and Thompson Sampling both explore through uncertainty. LinUCB adds a confidence bonus and picks the highest upper bound; Thompson Sampling samples plausible parameters and picks the best action under the sample.
- The practical difference is operational. LinUCB is deterministic given its state, which helps explanation and replay. Thompson Sampling is stochastic, which spreads exploration naturally but makes replay and audit harder.
- Features and reward definition come first. A bandit can only personalize on signals it is given, and it optimizes whatever reward it is told to optimize.
- Infrastructure decides whether it works in production. Decision logging with action propensities, delayed reward handling, drift monitoring, and offline evaluation need as much attention as the algorithm.
Suggested approach: Start with the context features and the reward definition, build the logging and evaluation path, and only then choose between LinUCB and Thompson Sampling using the practical criteria in this paper. Check any choice on your own data, because nothing here is a measured result.
1. Introduction
The exploration-exploitation dilemma pervades sequential decision-making: how do we balance learning about alternative actions (exploration) with using current knowledge to maximize immediate reward (exploitation)? Multi-armed bandit algorithms give a principled framework for this tradeoff, treating the problem as sequential allocation where each decision also produces information that improves later decisions.
Traditional multi-armed bandits have a well-known limitation: they learn a single global policy applied to every decision. An epsilon-greedy algorithm choosing among five promotional offers learns which offer has the highest average conversion rate across all users, and then offers the same thing to a first-time mobile visitor at 2 PM and to a returning desktop customer at 9 AM. That context-blindness ignores signals that could change which offer is best.
Contextual bandits extend the framework by conditioning action selection on an observed context vector. Instead of one value estimate per action, a contextual algorithm learns a function from context to expected reward. Recommendations can adapt to user preferences, promotions to purchase history, and treatment assignments to individual characteristics.
Scope and Objectives
This paper explains the method and the decisions around using it. It does not present experiments, benchmark numbers, or case studies, and it makes no claim about how much any approach improves any metric. Where an example uses numbers, it says so and the numbers are made up for illustration.
It focuses on two families of contextual bandit algorithm. LinUCB and its variants are the confidence-bound approach: they construct confidence intervals around reward estimates and select the action with the highest upper bound. Thompson Sampling is the Bayesian approach: it maintains a posterior distribution over parameters and selects actions according to their probability of being best.
The objectives are threefold: (1) explain how each algorithm works and why it explores, (2) describe the implementation concerns that apply regardless of algorithm, and (3) give decision criteria for choosing between them based on application requirements.
Why This Matters Now
Several trends make contextual bandits relevant. Personalization is now expected in many consumer applications. Privacy constraints push organizations toward first-party contextual signals. Streaming and cloud infrastructure make online learning practical to build.
The shift from batch supervised learning to online sequential optimization changes the goal. Rather than training a static model on historical data and minimizing prediction error, a contextual bandit learns from the decisions it makes and optimizes cumulative reward. That fits problems where the task is to choose an action under uncertainty, not to predict passively.
2. Background
The Multi-Armed Bandit Problem
The classical problem has K slot machines (arms), each with an unknown reward distribution. At each step the player picks an arm and observes a random reward from that arm's distribution. The goal is to maximize cumulative reward over T rounds, which requires learning which arms pay more (exploration) while mostly pulling those arms (exploitation).
Regret measures performance: the gap between the cumulative reward the algorithm collected and what it would have collected by always playing the best arm. Sublinear regret, meaning regret that grows more slowly than T, indicates the algorithm eventually acts near-optimally. Classical algorithms such as UCB1 and Thompson Sampling have logarithmic regret guarantees under standard assumptions, while epsilon-greedy with a fixed epsilon keeps exploring at a constant rate and so has linear regret.
Limitations of Context-Free Approaches
Standard bandit algorithms assume each arm has a fixed expected reward that does not depend on external factors. In many applications that assumption is wrong. A "recommended for you" module may perform differently for new and returning users, for mobile and desktop sessions, or for morning and evening traffic. A context-free algorithm learns one ranking averaged across all of those populations.
The illustrative example in Section 5 shows how a context-free policy can be forced to treat two offers as equivalent when each is clearly better for a different segment.
Contextual Bandit Formulation
At each round t, the environment presents a context vector x_t in R^d describing the current state. The algorithm selects an action a_t from an action set A and observes a reward r_t drawn from a distribution conditioned on both context and action: r_t ~ P(r | x_t, a_t). The goal is still to maximize cumulative reward, but the best action now depends on context.
This formulation contains the classical problem as the special case where contexts carry no information. It also connects to supervised learning: if rewards are generated by some function r = f(x, a) + noise, then choosing well requires learning f. Contextual bandits can use regression, classification, and function approximation while keeping the online, sequential structure.
Existing Approaches
The literature has developed along two main lines: confidence-bound methods and Bayesian sampling. LinUCB, introduced by Li et al. (2010), assumes a linear reward model r = x^T θ_a + noise for each action a, keeps a ridge regression estimate of θ_a, and adds a confidence bonus derived from the uncertainty of that estimate. It selects the action with the highest upper confidence bound.
Thompson Sampling for contextual bandits (Agrawal and Goyal, 2013) keeps a posterior distribution over θ_a, typically Gaussian and updated by Bayesian linear regression. At each round it samples a parameter vector from each posterior and selects the action with the highest predicted reward under the sample.
Neural contextual bandits replace the linear model with a neural network and use techniques such as a neural-linear last layer, dropout-based uncertainty, or ensembles to estimate uncertainty. They can represent non-linear reward functions but need more data and compute, and their uncertainty estimates are harder to get right.
3. Methodology
What This Paper Is, and Is Not
The method of this paper is exposition. It draws on the published literature listed in the references and on standard derivations, and it explains the algorithms, their assumptions, and the engineering issues around them. It does not report an experiment, a deployment, or a comparison run on real traffic. Any statement about which approach "performs better" should be read as a statement about design trade-offs, to be checked on your own data.
LinUCB
For each action a, LinUCB maintains a matrix A_a = λI + Σ x x^T and a vector b_a = Σ r x, summed over the rounds where action a was chosen. The ridge estimate is θ_a = A_a^(-1) b_a. For a new context x, the score of action a is:
score(a) = x^T θ_a + α · sqrt(x^T A_a^(-1) x)
The first term is the predicted reward (exploitation). The second term is a confidence bonus that is large when the context lies in a direction where few observations have been made (exploration). The parameter α sets how much weight exploration gets. The algorithm picks the action with the highest score, so given the same state and context it always makes the same choice. Updates can use the Sherman-Morrison formula to maintain A_a^(-1) incrementally rather than inverting a matrix on each step.
Thompson Sampling
With a Gaussian prior and Gaussian noise, the posterior for θ_a is Gaussian with mean θ_a (the same ridge estimate) and covariance proportional to A_a^(-1). At decision time the algorithm draws one sample of θ_a from each action's posterior and picks the action whose sampled prediction x^T θ_a is highest. Actions with uncertain posteriors sometimes draw optimistic samples and get tried; as data accumulates the posterior tightens and exploration fades without a separately tuned bonus.
For high-dimensional contexts the full covariance matrix is expensive, so a diagonal approximation is a common compromise. It trades some statistical accuracy for lower memory and compute.
Context and Feature Engineering
It helps to sort candidate context features into five types: user attributes (demographics, account characteristics), behavioral history (past actions, engagement), temporal features (time of day, day of week, seasonality), session state (device, referral source, current activity), and action-specific features (item attributes, treatment characteristics). Typical practice is to normalize numeric features, add an intercept term, and add explicit interaction terms (for example user topic preference by item category) when the model is linear, since a linear model cannot form interactions on its own.
Keep the dimension in proportion to the data volume. With few observations per action, a large feature vector produces wide confidence intervals and slow learning, whichever algorithm is used.
Reward Definition and Evaluation
The reward is the single most consequential design choice, because the bandit will optimize exactly what it is given. Clicks are quick to observe but can diverge from the outcome that matters, such as revenue or retention. When the real outcome is delayed, a common pattern is to use a fast proxy for online updates and reconcile against the real outcome later (Section 6).
Useful quantities to track are cumulative reward, regret against a reference policy where one can be estimated, how long the policy takes to stabilize, and the variance of outcomes. Because the policy changes the data it sees, evaluation of a new policy on logged data needs the logged action probabilities and a counterfactual estimator such as inverse propensity scoring or a doubly robust estimator.
4. Key Points
Point 1: Exploration Is Driven by Uncertainty, Not by Randomness Alone
Epsilon-greedy explores a fixed fraction of the time regardless of what it already knows. LinUCB and Thompson Sampling instead explore where their estimates are uncertain. As observations accumulate in a region of context space, uncertainty there shrinks and the policy exploits more. That is the property that makes both methods more economical than uniform exploration in principle: they spend exploration on the questions that are still open.
The cost of this is modeling. Both rely on a reasonable reward model and on honest uncertainty estimates. If the model is badly misspecified, or the uncertainty is too small, exploration stops early; if it is too large, the policy keeps exploring after it has learned enough.
Point 2: LinUCB and Thompson Sampling Differ Mainly in How Exploration Is Applied
| Aspect | LinUCB | Thompson Sampling |
|---|---|---|
| Decision rule | Highest upper confidence bound | Highest value under a sampled parameter |
| Determinism | Deterministic given state and context | Stochastic |
| Exploration control | Explicit parameter α | Through the prior and posterior scale |
| Explaining a decision | Predicted reward plus a stated confidence bonus | "This action won a random draw from the posterior" |
| Offline replay | Straightforward, same inputs give the same action | Needs logged propensities; replays differ by seed |
| Extra implementation work | Tuning α | Sampling infrastructure, prior and posterior calibration |
Which one gives higher cumulative reward depends on the problem, the tuning, and the traffic, and published comparisons do not show a single winner. Treat the choice as one of operational fit first, and measure on your own data second.
Point 3: Prefer LinUCB When Decisions Must Be Explained or Replayed
When decisions carry regulatory, clinical, or financial weight, being able to say why an action was chosen matters. LinUCB can answer with the predicted reward and the confidence bonus for each candidate. Its determinism also makes testing simpler: replaying logged contexts against a candidate model yields reproducible actions.
Thompson Sampling can still be audited, but the explanation is probabilistic ("the posterior gave this action the highest sampled value") and replay evaluation depends on the logged action probabilities rather than on re-running the policy.
Point 4: Cold Start Needs a Plan, Not a Default
A new user or a new action has no history, and a bandit that starts from maximal uncertainty will explore heavily at exactly the moment the experience matters most. Four approaches are commonly used, and they combine well:
- Uniform exploration: the baseline; simple, but the slowest to become useful.
- Epsilon-first: explore at random for a fixed number of rounds, then exploit. Easy to reason about, but the switch point is arbitrary.
- Cohort priors: start a new user from the estimates of similar users (for example nearest neighbors in feature space), and let individual data take over as it accumulates.
- Supervised initialization: fit a model on historical (context, action, reward) records to set initial parameters, then continue with bandit updates.
The main caution is calibration. A model initialized from historical data can look more certain than it should be, which leads to premature exploitation. Widen the initial uncertainty deliberately, for example by scaling the prior covariance, rather than copying the supervised model's confidence.
Point 5: Feature Engineering and Reward Design Usually Outweigh the Choice of Algorithm
A contextual bandit can only exploit signal that is present in its features. This is a design argument, not a measured result: if the features do not distinguish the contexts where different actions win, no exploration rule will recover the lost information. The general guidance is:
- Behavioral history (past interactions, engagement rates, purchase patterns) tends to describe preferences more directly than static demographics. Rolling-window aggregates balance recency against stability.
- Temporal features (hour, day of week, seasonality, recency) capture patterns that repeat.
- Demographics are often weak stand-ins for behavior that can be observed directly.
- Session state (device, referral source) helps separate situations that look alike otherwise.
- Interaction features between user and item attributes matter for linear models, which cannot form them unaided.
Which of these carry signal is specific to each application. Check them against your own data, for example with offline ablations, before relying on any of them.
5. Worked Example (Illustrative)
Illustrative example with made-up numbers. Suppose a checkout page can show one of three offers: a 10% discount, free shipping, or a bundled product. Suppose also that customers fall into two segments with opposite preferences, and that the conversion rates and segment sizes below are invented purely to show the mechanism. They are not measurements.
| Segment (made up) | 10% Discount | Free Shipping | Bundle | Share of visitors |
|---|---|---|---|---|
| Price-Sensitive | 0.12 | 0.04 | 0.03 | 60% |
| Convenience-Focused | 0.03 | 0.15 | 0.08 | 40% |
| Population Average | 0.084 | 0.084 | 0.05 | 100% |
The averages follow from the invented inputs: for the discount, 0.6 × 0.12 + 0.4 × 0.03 = 0.084; for free shipping, 0.6 × 0.04 + 0.4 × 0.15 = 0.084.
In this made-up setting a context-free bandit sees the discount and free shipping as tied at 0.084 and has no basis to prefer either. A contextual bandit that can tell the segments apart would route price-sensitive visitors to the discount and convenience-focused visitors to free shipping, for an overall rate of 0.6 × 0.12 + 0.4 × 0.15 = 0.132 in this example. The point is the mechanism: when the best action differs by context, the best average action can be a poor choice for everyone. The size of the gap in real data depends entirely on how strongly context separates the best actions, which is why it needs to be measured and not assumed.
6. Production Considerations
This section is guidance on what a production system around a contextual bandit needs. It describes what to build, not what any particular system achieved.
When the Algorithm Choice Matters
Thompson Sampling is a natural fit when the application can tolerate stochastic decisions, exploration should taper off on its own, and feedback arrives quickly enough that the posterior is updated while it is still relevant. LinUCB is a natural fit when decisions must be explained or reproduced, when stakeholders want an explicit exploration knob, or when offline replay needs to be simple. Traffic volume, context dimension, and reward delay all affect how fast either method learns, so they are worth estimating before committing.
A small expected difference between algorithms does not justify added complexity by itself. Weigh any expected gain against the extra work: sampling infrastructure, posterior calibration, and harder offline testing for Thompson Sampling, or tuning of the exploration parameter for LinUCB.
Infrastructure and Operations
Logging and replay: Log context, chosen action, the probability with which it was chosen, and a timestamp for every decision. Rewards arrive later and must be joined back to the decision. Without logged propensities, counterfactual evaluation of a new policy is not possible. Plan storage for the log for as long as replay evaluation will be needed.
Model update pipelines: Choose between per-decision, micro-batch, and periodic updates according to how quickly the environment changes and how costly an update is. Micro-batches are a common middle path. Slow updates mean the policy lags behind changing preferences.
Delayed rewards: When the true outcome takes hours or days, a staged design helps: use an immediate proxy (a click, an engagement signal) for online updates, and apply the delayed outcome (a conversion, revenue) in periodic recalibration. Check regularly that the proxy and the real outcome still move together, since they can diverge.
Distribution shift monitoring: Context distributions drift as cohorts, seasons, and catalogs change. Monitor feature distributions against a baseline period, and reward distributions per context cluster, and define in advance what triggers a retrain or a feature pipeline review.
Technical Debt and Maintenance
Contextual bandits carry ongoing costs. Feature pipelines need review as data sources change. Parameters grow with the number of actions, since each action has its own model. Thompson Sampling with full covariance stores a d by d matrix per action, which grows quadratically with the number of features unless a diagonal approximation is used.
Offline evaluation systems, such as inverse propensity scoring and doubly robust estimators, become dependencies in their own right, and a bug in logging or replay can quietly corrupt the numbers used to judge a policy. Budget engineering time for maintenance, monitoring, and debugging reward feedback loops, not only for the first implementation.
Common Pitfalls
- Optimizing the wrong reward. A proxy such as clicks can improve while the outcome that matters does not.
- Missing propensities. Without the logged probability of each action, logged data cannot be reused to evaluate other policies.
- Overconfident initialization. A warm start with tight uncertainty stops exploration too early.
- Feedback loops. The policy shapes the data it later learns from, so a bias in early decisions can persist.
- Ignoring drift. A model fitted to last season's behavior keeps acting on it until something tells it otherwise.
- Too many features for the data. High dimension with few observations per action leads to wide intervals and slow learning.
- Interference between users. If one user's treatment affects another's outcome, standard bandit assumptions no longer hold.
7. Recommendations
These are suggestions drawn from the design considerations above, not conclusions from a study.
Recommendation 1: Adopt a Context-First Framework
Work on context features and reward definition before algorithm selection. Capture rich context at serving time, analyze how context relates to reward in historical data, and engineer interaction features between user and item attributes. Choose the algorithm once the feature pipeline exists.
Implementation steps:
- Audit existing data collection to identify available context signals
- Implement logging that captures decision context at serving time
- Analyze historical data to see how context relates to reward
- Design the feature pipeline with versioning and checks
- Start with a simple contextual approach, such as epsilon-greedy over context features, to test the infrastructure before adding more sophisticated exploration
Recommendation 2: Consider Thompson Sampling Where Stochastic Decisions Are Acceptable
It is a reasonable default for consumer-facing applications with fast reward feedback, where exploration should fade naturally and decisions do not need to be individually explained.
Candidate uses: content recommendation, personalized search, ad optimization, product ranking.
Implementation considerations: Plan for sampling infrastructure, prior specification, calibration under delayed rewards, and a diagonal covariance approximation if the feature dimension is large. Budget for the added complexity of offline evaluation with a stochastic policy.
Recommendation 3: Consider LinUCB Where Decisions Must Be Explained or Reproduced
When decisions need explanations, audit trails, or regulatory scrutiny, LinUCB's deterministic rule and explicit confidence bonus are operational advantages.
Candidate uses: credit and fraud decisions, healthcare treatment assignment, high-stakes recommendations, and settings that require disparate impact analysis.
Implementation notes: Replay-based offline evaluation is simple, decisions can be tested end to end, and the confidence bound gives a natural explanation. The exploration parameter α still needs tuning for the application.
Recommendation 4: Plan Cold Start Explicitly
Avoid starting from uniform exploration by default. Initialize from historical data where it exists, widen the initial uncertainty so the policy does not become overconfident, and use cohort-level priors for new users.
Implementation pattern:
- Fit an offline supervised model (logistic regression, gradient boosting) on historical (context, action, reward) data
- Use its predictions to initialize the bandit's parameter estimates
- Inflate the initial uncertainty rather than inheriting the supervised model's confidence
- For new users, inherit priors from similar cohorts (for example nearest neighbors in feature space)
- Shift gradually from cohort priors to individual estimates as interaction data accumulates
Recommendation 5: Build Infrastructure for Delayed Rewards and Distribution Shift
The supporting systems decide whether a bandit works in production. Prioritize logging, delayed reward handling, monitoring, and offline evaluation.
Infrastructure checklist:
- Decision logging: capture (context, action, propensity, timestamp) for every decision with durable storage
- Reward feedback: observe rewards asynchronously and join them back to decisions
- Dual-reward design: immediate proxy rewards for online updates, delayed real outcomes for recalibration
- Distribution monitoring: track context and reward distributions per cohort and alert on drift
- Offline evaluation: build replay with counterfactual estimators so policy changes can be assessed before release
- Update pipelines: choose micro-batch or streaming updates to match traffic and reward latency
8. Conclusion
Contextual bandits move from learning one global policy to learning a context-dependent decision function. When the best action differs by context, that is what allows personalization. This paper has explained how that works, not measured how much it helps in any particular setting.
LinUCB and Thompson Sampling both explore in proportion to uncertainty, one through an explicit confidence bonus and the other through posterior sampling. The practical differences are mostly operational: determinism, explanation, offline replay, and the cost of calibration. The choice between them is best made on those criteria and then checked on your own data.
Feature engineering and reward design deserve attention before algorithm selection, because a bandit can only act on the signal it is given and optimizes the reward it is given. Cold start, delayed rewards, logging with propensities, drift monitoring, and offline evaluation are the supporting systems that determine whether the method is usable outside a notebook.
Contextual bandits treat uncertainty as information to act on, rather than as a problem to remove. That shift, from static prediction to sequential decision-making under uncertainty, is the core of the approach.
References & Further Reading
Core Literature
- Li, L., Chu, W., Langford, J., & Schapire, R. E. (2010). A contextual-bandit approach to personalized news article recommendation. Proceedings of the 19th International Conference on World Wide Web, 661-670.
- Agrawal, S., & Goyal, N. (2013). Thompson sampling for contextual bandits with linear payoffs. International Conference on Machine Learning, 127-135.
- Chu, W., Li, L., Reyzin, L., & Schapire, R. E. (2011). Contextual bandits with linear payoff functions. Proceedings of the 14th International Conference on Artificial Intelligence and Statistics, 208-214.
- Russo, D., Van Roy, B., Kazerouni, A., Osband, I., & Wen, Z. (2018). A tutorial on Thompson sampling. Foundations and Trends in Machine Learning, 11(1), 1-96.
- Zhou, D., Li, L., & Gu, Q. (2020). Neural contextual bandits with UCB-based exploration. International Conference on Machine Learning, 11492-11502.
Production Implementation Patterns
- Bottou, L., Peters, J., Quiñonero-Candela, J., Charles, D. X., Chickering, D. M., Portugaly, E., ... & Simard, P. (2013). Counterfactual reasoning and learning systems: The example of computational advertising. Journal of Machine Learning Research, 14(1), 3207-3260.
- Gilotte, A., Calauzènes, C., Nedelec, T., Abraham, A., & Dollé, S. (2018). Offline A/B testing for recommender systems. Proceedings of the Eleventh ACM International Conference on Web Search and Data Mining, 198-206.
- Riquelme, C., Tucker, G., & Snoek, J. (2018). Deep Bayesian bandits showdown: An empirical comparison of Bayesian deep networks for Thompson sampling. International Conference on Learning Representations.
Your turn
Bring your own data and the question you actually need answered.
CympleData Scientist Send me your data and question, I’ll send you the analytics. ds@mcpanalytics.ai
Frequently Asked Questions
What is the fundamental difference between contextual bandits and standard multi-armed bandits?
Standard multi-armed bandits learn a single global policy that applies to every user, while contextual bandits condition each decision on context features (user attributes, session data, environment state). This lets the policy personalize, because the best action can differ from one context to another.
When should I choose LinUCB over Thompson Sampling for contextual bandits?
LinUCB makes deterministic decisions from explicit confidence bounds, which suits settings that need explainable decisions and simple offline replay. Thompson Sampling explores by sampling from a posterior, which suits settings that can tolerate stochastic decisions and want exploration to taper off naturally as uncertainty shrinks. Neither dominates; the right choice depends on explainability needs, evaluation workflow, and how rewards arrive.
How do I solve the cold start problem with contextual bandits?
Common cold start approaches are: (1) context-based priors from similar user cohorts, (2) hybrid exploration that mixes a simple exploration rule with a contextual model, (3) transfer from a related domain, and (4) initializing the model with supervised learning on historical data before switching to bandit updates. Whatever the choice, keep initial uncertainty honest so the policy does not become overconfident too early.
What context features matter for contextual bandit performance?
Features that carry real signal about the reward matter most: behavioral history (past interactions, engagement patterns), temporal features (time of day, day of week, recency), and interactions between user and item attributes. Static demographics are often weaker proxies for behavior. Which features matter is specific to each application and should be checked on your own data.
How frequently should contextual bandit models be updated in production?
It depends on traffic volume and how quickly rewards arrive. Higher-traffic systems with fast feedback can update in small batches or continuously; lower-traffic systems gain little from very frequent updates. For delayed rewards, run separate cycles: online updates for immediate signals and periodic batch recalibration for delayed outcomes such as conversions.