Contextual Bandits
Context-dependent rewards, LinUCB, LinTS, offline evaluation, NeuralTS
Standard A/B testing treats all users identically — it asks "which variant is better on average?" But users are different, and the best variant for one user segment may be the worst for another. Contextual bandits extend MAB by observing a feature vector at each round and learning which arm is best as a function of that context, not on average. This is the difference between finding the best treatment on average and finding the best treatment for each patient.
The naive alternative — train a supervised reward model and select greedily — fails because arms underrepresented in the logging policy have poorly calibrated reward estimates and are either permanently avoided or over-trusted. LinUCB's uncertainty bonus √(x^T A^{-1} x) is the key mechanism: it is largest exactly when the current context is far from previously observed data, targeting exploration where knowledge is genuinely lacking.
Key points
- Contextual bandit formulation: at round t, observe context x_t ∈ R^d, choose arm a_t ∈ {1,...,K}, observe reward r_t = f(x_t, a_t) + noise. Goal: minimise Σ_t [f(x_t, a*_t) − f(x_t, a_t)] where a*_t = argmax_a f(x_t, a). Every round is potentially different because the context changes — the same arm may be optimal for one user and suboptimal for another.
- Contextual bandits subsume classical A/B testing. A standard A/B test has context (user features) but ignores it during allocation — both variants are shown randomly regardless of user. A contextual bandit learns which variant is better for which user, discovering heterogeneous treatment effects while running the experiment. The allocation becomes personalised and the learnt policy is more valuable.
- LinUCB (disjoint model): assumes r_t = θ_a^T x_t + ε_t, a separate θ_a per arm. Estimates θ_a via ridge regression over (context, reward) pairs. UCB for arm a at context x = θ̂_a^T x + α√(x^T A_a^{-1} x) where A_a = X_a^T X_a + λI. The exploration bonus x^T A_a^{-1} x measures how far the current context is from previously observed contexts — large for novel contexts, small for familiar ones. This is the right signal: explore when the context is unfamiliar, exploit when it is not. This per-arm model is the *disjoint* variant; a *hybrid* variant shares a parameter vector across arms for common context features when arms have similar response patterns (see LinUCB In Depth).
- LinTS: Gaussian posterior over θ_a with mean θ̂_a and covariance σ²A_a^{-1}. At each round sample θ̃_a ~ N(θ̂_a, σ²A_a^{-1}) and select argmax_a θ̃_a^T x_t. Same regret bounds as LinUCB but empirically better, especially with informative priors. Prior misspecification matters here: a flat prior is the wrong choice when historical data is available to initialise θ̂_a.
- Offline policy evaluation (OPE): logged (context, arm, reward) data from a behaviour policy can evaluate new policies without online deployment. OPE is essential because online deployment is expensive and risky. Key estimators: Direct Method (DM) trains a reward model and evaluates offline — low variance but biased when the reward model is wrong for contexts the behaviour policy did not cover. Importance Sampling (IS) reweights logged rewards by π_e/π_b — unbiased but high variance when policies diverge. Doubly Robust (DR) combines both — consistent if either the reward model or propensities are correct.
- Greedy supervised learning is the wrong baseline. Train a reward model on logs and select the highest-predicted-reward arm each round. This fails because arms underrepresented in the logging policy have reward estimates with high uncertainty — and the greedy policy either perpetually avoids them (if their logged reward is low) or over-trusts them (if they happened to have high reward on the few occasions they were logged). No uncertainty bonus means no correction for this.
- Neural models improve reward estimation but break exact uncertainty quantification. NeuralTS uses last-layer Thompson Sampling: train a neural feature extractor to embed (context, arm) pairs, fix it periodically, run LinTS on the linear head over the embeddings. The neural network provides expressive reward modelling; LinTS provides principled uncertainty quantification on the last layer.
- Regret bounds: LinUCB achieves O(√(dT) ln K) regret where d is the context dimension. Context adds a √d factor over pure MAB O(√(KT)) — learning a linear reward function in d dimensions requires more exploration than learning K scalar arm means. Higher-dimensional or more complex reward functions require proportionally more data before the algorithm can exploit reliably.
Contextual bandits learn which arm is best for which user, not just which arm is best on average — this is the gap between standard A/B testing and personalised allocation. The critical failure mode of greedy supervised learning is exploration bias: arms underrepresented in the logging policy remain poorly estimated forever and are either permanently avoided or over-trusted. LinUCB's uncertainty bonus √(x^T A^{-1} x) is largest for contexts far from previously observed data — it targets exploration exactly where knowledge is lacking.
Recap
- Formulation: observe context x_t, pick arm a_t, learn which arm is best as a function of context — not on average.
- Subsumes A/B testing: a standard A/B test has context but ignores it; contextual bandits learn heterogeneous treatment effects while running.
- LinUCB: $r_t = θ_a^T x_t + ε_t$; index = θ̂_a^T x + α√(x^T A_a^{-1} x); bonus large for novel contexts.
- LinTS: sample θ̃_a ~ N(θ̂_a, σ²A_a^{-1}); same bounds as LinUCB, empirically better with informative priors.
- Greedy supervised = wrong baseline: no uncertainty bonus → under-logged arms permanently avoided or over-trusted.
- OPE estimators: DM (low variance, biased), IS (unbiased, high variance), DR (consistent if either model is right).
- Regret O(√(dT) ln K): context adds √d over pure MAB — richer reward functions need more exploration.
Check your understanding
Q1. How does a contextual bandit differ from a supervised learning model + greedy selection? Select the two true statements about what goes wrong with the greedy approach and what a contextual bandit does differently to fix it.
- A) Greedy supervised learning has no exploration mechanism at all, so arms undersampled by the logging policy stay poorly calibrated forever
- B) A contextual bandit's exploration bonus specifically targets under-explored context regions, which is what gives it a real O(d√T) regret guarantee
- C) The two approaches are essentially equivalent — any supervised model with high enough accuracy already behaves exactly like a contextual bandit
- D) Greedy supervised learning is actually superior in general, since it converges faster by never wasting any traffic on deliberate exploration
Q2. You are building a contextual bandit for mobile push notification personalization. Context = 50-dim user features. K=20 notification types. How do you choose between LinUCB (disjoint), LinUCB (hybrid), and NeuralTS?
- A) Always default straight to NeuralTS, since neural networks have the highest raw capacity and will therefore outperform any linear model
- B) Use disjoint LinUCB when arms differ a lot with enough per-arm data; hybrid when arms share structure; NeuralTS only if non-linear
- C) Start every project with NeuralTS by default and only fall back to plain LinUCB later if its training turns out to be too slow
- D) Disjoint LinUCB is always the correct starting point in every case, regardless of how much data you have or how the features are structured
Q3. Describe a contextual bandit deployment pipeline for news article recommendation. What are the main engineering challenges?
- A) The main challenge is purely picking the right reward function — once that single choice is made, deployment itself is entirely straightforward
- B) Pipeline: context → candidates → UCB scoring → serving → feedback → updates. Challenges: delayed feedback, covariate shift, cold start, OPE
- C) The only genuinely significant engineering challenge here is raw serving latency — every other aspect is handled by standard ML infrastructure
- D) Contextual bandits simply cannot be deployed for news recommendation, since articles expire far too quickly for any model to learn from them
Try it interactively
ML Systems Lab is a free interview-prep platform for ML engineers — work through the full interactive module, quizzes, and drills.
Open ML Systems Lab →