Inferensys

Glossary

Thompson Sampling

Thompson Sampling is a probabilistic algorithm for the exploration-exploitation dilemma that selects actions based on their probability of being optimal given a posterior distribution, naturally balancing uncertainty for new items.
Knowledge engineer constructing knowledge base on laptop, document hierarchy visible, casual office setup.
PROBABILISTIC ALGORITHM

What is Thompson Sampling?

A Bayesian approach to the exploration-exploitation dilemma that selects actions based on their probability of being optimal given a posterior distribution, naturally balancing uncertainty for new items.

Thompson Sampling is a probabilistic algorithm for sequential decision-making that addresses the exploration-exploitation trade-off by maintaining a posterior probability distribution over each action's reward. At each step, it samples a value from each action's posterior and selects the action with the highest sample, ensuring that actions with high uncertainty are explored proportionally to their chance of being optimal.

In cold start mitigation, Thompson Sampling excels by naturally allocating exploration budget to new items with wide posterior distributions, while exploiting known high-performers with narrow distributions. Unlike epsilon-greedy strategies that explore uniformly at random, this Bayesian approach directs exploration toward actions that are plausibly superior, making it highly efficient for bootstrapping recommendations with sparse interaction data in contextual bandit frameworks.

PROBABILISTIC DECISION-MAKING

Key Characteristics of Thompson Sampling

Thompson Sampling is a Bayesian algorithm for the exploration-exploitation dilemma that selects actions proportionally to their probability of being optimal given current knowledge. It naturally balances trying uncertain options against exploiting known winners.

01

Probability Matching Mechanism

Thompson Sampling operates by probability matching—the probability of selecting an action equals the probability that the action is optimal given the posterior distribution. For each decision point, the algorithm draws a single sample from the posterior distribution of each arm's expected reward and selects the arm with the highest sampled value. This stochastic selection ensures that arms with high uncertainty but promising potential are explored proportionally to their chance of being the best, while arms with high certainty of being suboptimal are naturally suppressed.

02

Bayesian Posterior Updating

The algorithm maintains a posterior distribution over each action's reward parameter, updated via Bayes' theorem after every observation. For binary rewards, a Beta distribution serves as the conjugate prior:

  • Prior: Beta(α, β) representing initial beliefs
  • Update: After observing a success, α → α + 1; after a failure, β → β + 1
  • Result: The distribution narrows with more data, reflecting increased certainty This Bayesian framework naturally encodes uncertainty, making it ideal for cold-start items where the posterior remains wide, encouraging exploration.
03

Regret Minimization Properties

Thompson Sampling achieves logarithmic regret in stationary environments, meaning the cumulative difference between the algorithm's performance and the optimal policy grows only logarithmically over time. Key regret characteristics:

  • Lai-Robbins lower bound: Thompson Sampling asymptotically matches the theoretical lower bound for regret in multi-armed bandits
  • Empirical performance: Often outperforms UCB and ε-greedy in practice, especially in the finite horizon
  • Non-stationary adaptation: With appropriate forgetting mechanisms, it can track changing reward distributions This makes it particularly effective for dynamic retail environments where consumer preferences shift.
04

Contextual Integration via Generalized Linear Models

Contextual Thompson Sampling extends the algorithm to incorporate side information about users and items. Instead of maintaining independent per-arm distributions, it models expected rewards as a function of context features:

  • Linear contextual bandits: Reward = θᵀx + ε, where θ has a multivariate Gaussian posterior
  • Logistic contextual bandits: For binary outcomes, using a Bayesian logistic regression with Laplace approximation
  • Neural contextual bandits: Using Bayesian neural networks with Monte Carlo dropout for uncertainty estimation This enables cold-start mitigation by allowing the model to generalize from similar items or users based on their feature representations.
05

Practical Implementation Considerations

Deploying Thompson Sampling in production requires addressing several engineering challenges:

  • Prior specification: Weakly informative priors (e.g., Beta(1,1)) for cold-start items to ensure initial exploration
  • Batch updating: Accumulating feedback and performing posterior updates in mini-batches to reduce computational overhead
  • Delayed feedback: Handling the gap between action selection and reward observation using partial feedback models
  • Scalability: For large action spaces, maintaining millions of Beta distributions is memory-efficient; each arm requires only two floating-point parameters (α, β)
  • Non-binary rewards: Extending to Gaussian rewards with Normal-Inverse-Gamma conjugate priors or to multinomial outcomes with Dirichlet distributions
06

Comparison with Alternative Bandit Algorithms

Thompson Sampling distinguishes itself from other exploration strategies:

  • vs. ε-greedy: ε-greedy explores uniformly at random with fixed probability ε, wasting exploration on clearly suboptimal arms. Thompson Sampling directs exploration toward arms with high uncertainty and potential
  • vs. UCB: Upper Confidence Bound algorithms use optimistic point estimates with deterministic confidence intervals. Thompson Sampling's stochastic sampling often provides better finite-sample performance and handles complex posterior shapes more naturally
  • vs. Boltzmann exploration: Softmax exploration uses a temperature parameter to control exploration, requiring careful tuning. Thompson Sampling is parameter-free beyond the prior specification
  • Key advantage: Thompson Sampling is the oldest bandit algorithm (proposed in 1933) yet remains state-of-the-art due to its elegant Bayesian foundation and strong empirical results
THOMPSON SAMPLING EXPLAINED

Frequently Asked Questions

Clear, technically precise answers to the most common questions about Thompson Sampling, the probabilistic algorithm that elegantly solves the exploration-exploitation dilemma in reinforcement learning and personalization systems.

Thompson Sampling is a probabilistic reinforcement learning algorithm that selects actions by randomly drawing from the posterior probability distribution of each action's expected reward, naturally balancing the exploration-exploitation trade-off. Instead of calculating a single point estimate of an action's value, it maintains a full probability distribution representing the uncertainty about that estimate. At each decision step, the algorithm samples a value from each action's distribution and selects the action with the highest sampled value. Actions with high uncertainty have wider distributions, giving them a non-zero probability of being selected even if their current mean estimate is lower. This stochastic selection mechanism ensures that promising but uncertain actions are explored proportionally to their probability of being optimal, while well-understood high-reward actions are exploited most of the time. The posterior distribution is updated using Bayes' rule as new observations arrive, causing the distribution to narrow as confidence increases. For binary reward scenarios, a Beta distribution is commonly used as the conjugate prior, while Gaussian distributions are employed for continuous rewards.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.