Inferensys

Glossary

Thompson Sampling

A probabilistic algorithm for the multi-armed bandit problem that selects actions based on their probability of being optimal, efficiently balancing price 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.
PROBABILISTIC ALGORITHM

What is Thompson Sampling?

A probabilistic algorithm for the multi-armed bandit problem that selects actions based on their probability of being optimal, efficiently balancing price exploration and exploitation.

Thompson Sampling is a Bayesian algorithm that solves the exploration-exploitation dilemma by maintaining a probability distribution over the expected reward of each action. At each decision step, it samples a value from each action's posterior distribution and selects the action with the highest sample, naturally balancing the testing of uncertain price points against the exploitation of known high-performers.

In dynamic pricing, the algorithm models demand uncertainty using a prior distribution—often a Beta or Gaussian distribution—and updates it with observed purchase data. This approach converges to an optimal pricing policy faster than epsilon-greedy methods, making it ideal for real-time revenue optimization where the cost of a suboptimal price is immediate and measurable.

PROBABILISTIC EXPLORATION

Key Characteristics of Thompson Sampling

Thompson Sampling is a Bayesian algorithm for the multi-armed bandit problem that selects actions in proportion to their probability of being optimal, providing an elegant and efficient balance between exploring uncertain price points and exploiting known high-reward ones.

01

Bayesian Foundation

Thompson Sampling operates on a Bayesian framework, maintaining a probability distribution over the unknown reward of each action rather than a point estimate. For each pricing decision, the algorithm samples a value from each posterior distribution and selects the action with the highest sample. This intrinsic randomness ensures that actions with high uncertainty are explored proportionally to their chance of being optimal. As data accumulates, the posterior distributions narrow, naturally shifting behavior from exploration to exploitation without requiring an explicit exploration parameter like epsilon in epsilon-greedy strategies.

02

Probability Matching Mechanism

The core mechanism is probability matching: an action is selected with a frequency equal to the probability that it is the best action given current knowledge. This is fundamentally different from optimistic initialization or epsilon-greedy approaches. Key properties include:

  • Uncertainty-driven exploration: Actions with wide posterior distributions are selected more frequently
  • Automatic annealing: Exploration naturally decreases as confidence grows
  • No hyperparameter tuning: The exploration-exploitation trade-off emerges from the Bayesian update process itself
  • Regret minimization: Achieves logarithmic expected regret, matching the theoretical lower bound for stochastic bandits
03

Conjugate Priors for Efficient Updating

Thompson Sampling achieves computational efficiency through the use of conjugate priors, which allow posterior distributions to be updated analytically rather than through expensive numerical methods. Common pairings include:

  • Beta-Bernoulli: For binary outcomes like click/no-click or purchase/no-purchase at a given price point
  • Gaussian-Gaussian: For continuous rewards like revenue per transaction, assuming known variance
  • Gamma-Poisson: For count-based rewards like units sold per time period
  • Dirichlet-Multinomial: For categorical outcomes with more than two possible results This analytical tractability makes Thompson Sampling suitable for real-time pricing systems requiring sub-millisecond decision latency.
04

Contextual Extension with Linear Models

The basic Thompson Sampling framework extends naturally to contextual bandits, where pricing decisions depend on observable features like customer segment, time of day, or inventory level. In the linear contextual variant, the algorithm maintains a multivariate Gaussian posterior over the coefficient vector of a linear reward model. For each decision, it samples a coefficient vector from this posterior and selects the price that maximizes the predicted reward given the current context. This enables personalized dynamic pricing that adapts to individual customer characteristics while maintaining the elegant exploration properties of the base algorithm.

05

Regret and Convergence Guarantees

Thompson Sampling provides strong theoretical guarantees that make it attractive for revenue-critical pricing applications:

  • Logarithmic regret bound: The cumulative difference between the algorithm's rewards and the optimal policy grows only logarithmically with time, proven for Bernoulli and many other reward distributions
  • Asymptotic optimality: Converges to selecting the truly optimal action with probability approaching 1 as data accumulates
  • Finite-time bounds: Recent research provides explicit bounds on performance after a finite number of trials, enabling practical risk assessment
  • Robustness to non-stationarity: When combined with discounting or sliding windows, adapts to slowly changing market conditions while maintaining theoretical guarantees
06

Implementation in Production Pricing Systems

Deploying Thompson Sampling in production dynamic pricing requires careful engineering considerations:

  • Prior specification: Weakly informative priors (e.g., Beta(1,1) for Bernoulli) prevent strong initial bias while allowing rapid learning
  • Warm-starting: Historical transaction data can inform initial prior parameters, avoiding the cold-start period
  • Batch updating: In high-throughput systems, posterior updates can be batched to reduce computational load without significant regret increase
  • Non-stationarity handling: Exponential decay weighting or sliding window approaches allow adaptation to shifting demand curves
  • Constraint integration: Business rules like minimum margins or MAP compliance can be enforced as post-processing on sampled actions without breaking the Bayesian framework
THOMPSON SAMPLING EXPLAINED

Frequently Asked Questions

Clear, technical answers to the most common questions about the probabilistic algorithm that optimally balances exploration and exploitation in dynamic pricing and personalization systems.

Thompson Sampling is a probabilistic algorithm for the multi-armed bandit problem that selects actions based on their probability of being optimal given the observed data. It works by maintaining a posterior probability distribution over the expected reward of each action (e.g., a price point or product recommendation). At each decision step, the algorithm samples a value from each action's posterior distribution and selects the action with the highest sampled value. After observing the actual reward (e.g., a click or purchase), it updates the posterior distribution using Bayes' rule, refining its beliefs. This elegant mechanism naturally balances exploration—trying actions with high uncertainty—and exploitation—choosing actions with high estimated mean reward—without requiring a separate exploration schedule or epsilon parameter. In dynamic pricing, each price point is an 'arm,' and the reward is typically revenue or conversion, with the posterior often modeled as a Beta distribution for binary outcomes or a Gaussian distribution for continuous rewards.

ALGORITHM SELECTION GUIDE

Thompson Sampling vs. Other Bandit Algorithms

Comparative analysis of Thompson Sampling against alternative multi-armed bandit approaches for dynamic pricing and personalization use cases.

FeatureThompson SamplingEpsilon-GreedyUpper Confidence Bound (UCB)

Exploration mechanism

Probabilistic matching via posterior sampling from Beta/Gaussian distributions

Random uniform exploration with fixed probability epsilon

Deterministic optimism bonus based on confidence interval upper bound

Handles non-stationary rewards

Incorporates prior knowledge

Asymptotic regret bound

O(log T) logarithmic

O(T) linear

O(log T) logarithmic

Empirical regret on cold start

Low (efficient early exploration)

High (wastes exploration budget)

Medium (over-explores initially)

Computational complexity per round

O(1) with conjugate priors

O(1)

O(K) where K = number of arms

Supports contextual bandits

Natural uncertainty quantification

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.