Inferensys

Glossary

Thompson Sampling

A Bayesian probabilistic algorithm for action selection that chooses arms according to their posterior probability of being optimal, naturally balancing exploration and exploitation.
Stylish WeWork-like workspace with hot desks and document wall, professional searching through enterprise knowledge base on a mounted ultrawide display, warm industrial pendants overhead.
BAYESIAN BANDIT ALGORITHM

What is Thompson Sampling?

A probabilistic algorithm for sequential decision-making that selects actions based on their posterior probability of being the optimal choice, providing a natural mechanism for balancing exploration and exploitation.

Thompson Sampling is a Bayesian algorithm that selects actions by randomly sampling from the posterior probability distribution of each action's expected reward, then choosing the action with the highest sampled value. This stochastic selection mechanism naturally balances exploration and exploitation: actions with high uncertainty have wider posterior distributions, giving them a chance to be sampled as optimal even when their estimated mean reward is lower.

The algorithm maintains a prior belief about each action's reward distribution and updates it using Bayes' theorem as feedback arrives. For binary rewards, a Beta-Bernoulli conjugate pair is commonly used, while Gaussian posteriors handle continuous outcomes. Thompson Sampling achieves strong empirical performance and theoretical regret bounds, often outperforming deterministic alternatives like Upper Confidence Bound (UCB) in practice, especially in non-stationary environments where uncertainty quantification is critical.

BAYESIAN BANDIT MECHANICS

Key Features of Thompson Sampling

Thompson Sampling is a probabilistic algorithm that selects actions by sampling from the posterior distribution of each arm's reward, naturally balancing exploration and exploitation without requiring a manually tuned exploration parameter.

01

Bayesian Posterior Sampling

The core mechanism of Thompson Sampling is maintaining a posterior probability distribution over each arm's unknown reward parameter. At each decision step, the algorithm draws a random sample from each arm's posterior and selects the arm with the highest sampled value. This means arms with high uncertainty have a chance to produce high samples, driving exploration. As more data is collected, the posterior distributions narrow, reducing exploration naturally. For Bernoulli bandits, this typically uses a Beta distribution as the conjugate prior, updated with observed successes and failures. The elegance lies in the fact that the probability an arm is selected exactly equals the probability it is optimal given current knowledge.

Probability Matching
Core Selection Principle
02

Automatic Exploration-Exploitation Balance

Unlike Epsilon-Greedy, which requires manual tuning of the exploration rate ε, or Upper Confidence Bound (UCB), which requires setting an exploration bonus coefficient, Thompson Sampling automatically calibrates exploration based on uncertainty. When an arm has been tried few times, its posterior is wide, generating highly variable samples that occasionally beat well-established arms. As data accumulates, the posterior concentrates around the true mean, and sampling naturally converges to exploitation. This self-tuning property makes Thompson Sampling particularly robust in non-stationary environments where reward distributions shift over time, as the posterior can widen again with a forgetting mechanism or sliding window.

No Manual Tuning
Exploration Parameter
03

Contextual Extension for Personalization

Thompson Sampling extends naturally to contextual bandits by modeling the expected reward as a function of context features with a Bayesian model. In the linear contextual case, a Gaussian prior is placed on the weight vector, and the posterior is updated via Bayesian linear regression. At decision time, a weight vector is sampled from the posterior, and the arm with the highest predicted reward given the current context is selected. This enables real-time personalization where the algorithm learns which actions work best for specific user segments, device types, or time-of-day patterns without requiring separate models for each context partition.

Linear & Neural
Contextual Variants
04

Regret Minimization Guarantees

Thompson Sampling provides strong theoretical guarantees on regret minimization. For the standard stochastic K-armed bandit, it achieves a logarithmic regret bound, meaning the cumulative regret grows only logarithmically with the number of rounds. This is asymptotically optimal, matching the theoretical lower bound. In practice, Thompson Sampling often outperforms UCB and Epsilon-Greedy in empirical studies, especially in the early rounds where efficient exploration is critical. The algorithm's regret performance stems from its ability to quickly eliminate clearly suboptimal arms while continuing to sample promising but uncertain arms proportionally to their probability of being best.

O(log T)
Asymptotic Regret Bound
05

Conjugate Prior Computational Efficiency

Thompson Sampling is computationally efficient when using conjugate priors, where the posterior distribution remains in the same family as the prior after observing data. Common pairings include:

  • Beta-Bernoulli for binary click/no-click rewards
  • Gaussian-Gaussian for continuous rewards like revenue or time-on-site
  • Dirichlet-Multinomial for categorical outcomes with more than two possibilities The update step requires only simple parameter additions rather than expensive Markov Chain Monte Carlo sampling. For non-conjugate models, Laplace approximation or bootstrapped Thompson Sampling can approximate the posterior efficiently, making the algorithm viable for high-throughput production systems serving millions of decisions per second.
O(1) Update
Per-Step Complexity
06

Cold Start and Prior Specification

Thompson Sampling handles the cold start problem through informative prior specification. Rather than starting with uniform uncertainty, practitioners can encode domain knowledge into the prior distributions. For example, a new product category can inherit the prior from a similar existing category, or a hierarchical Bayesian model can share statistical strength across related arms. This warm-start capability means the algorithm can begin with reasonable performance from the first interaction, avoiding the random exploration phase that plagues uninformed methods. The prior also acts as a natural regularization mechanism, preventing overfitting to early noisy observations.

Prior-Encoded
Domain Knowledge
ALGORITHM SELECTION GUIDE

Thompson Sampling vs. Other Bandit Algorithms

A comparative analysis of Thompson Sampling against Epsilon-Greedy, Upper Confidence Bound, and LinUCB across key operational dimensions for production personalization systems.

FeatureThompson SamplingEpsilon-GreedyUpper Confidence BoundLinUCB

Algorithm Family

Bayesian probabilistic

Semi-uniform random

Deterministic optimistic

Linear contextual

Exploration Mechanism

Posterior sampling from reward distribution

Random uniform selection with probability ε

Confidence interval bonus added to point estimate

Confidence ellipsoid around linear parameters

Handles Uncertainty Naturally

Contextual Awareness

Via Bayesian regression or neural posterior

Empirical Regret on Stationary Problems

Low

Moderate to high

Low

Low

Hyperparameter Sensitivity

Low (prior parameters only)

High (ε decay schedule critical)

Moderate (confidence radius tuning)

Moderate (regularization parameter α)

Computational Cost per Decision

Moderate (posterior sampling step)

Negligible

Low (closed-form update)

Low (ridge regression update)

Cold-Start Performance

Strong (principled uncertainty drives exploration)

Weak (random exploration wastes budget)

Strong (optimistic initialization)

Moderate (requires feature design)

THOMPSON SAMPLING EXPLAINED

Frequently Asked Questions

Clear, technical answers to the most common questions about the Bayesian algorithm that optimally balances exploration and exploitation in real-time decisioning systems.

Thompson Sampling is a Bayesian probabilistic algorithm for sequential decision-making that selects actions in proportion to their probability of being optimal given the observed data. The algorithm works by maintaining a posterior probability distribution over the unknown reward parameter for each arm, sampling a value from each distribution, and then selecting the arm with the highest sampled value. This natural stochasticity causes the algorithm to explore arms with high uncertainty and exploit arms with high expected rewards. As more data is collected, the posterior distributions narrow, reducing exploration and converging toward the truly optimal action. The mechanism elegantly solves the exploration-exploitation trade-off without requiring a manually tuned exploration parameter like epsilon in epsilon-greedy approaches.

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.