Thompson Sampling Explained Simply: A Complete Guide to Bayesian Decision Making
Imagine opening a brand-new ice cream shop with three exciting flavors:
- Vanilla
- Mango
- Mint Chocolate
You want to maximize profits and customer satisfaction, but there’s a problem:
This creates uncertainty. If you only sell Vanilla immediately, you might miss out on Mango becoming a massive success. But if you keep rotating flavors randomly forever, you may lose revenue by frequently promoting unpopular options.
This exact problem sits at the heart of one of the most important ideas in reinforcement learning and online optimization:
Thompson Sampling
Thompson Sampling is one of the most elegant and practical algorithms for making decisions under uncertainty. It helps machines, businesses, recommendation systems, and AI agents learn the best choices while continuously improving from experience.
Table of Contents
- 1. Introduction to Thompson Sampling
- 2. Exploration vs Exploitation
- 3. Multi-Armed Bandit Problem
- 4. Bayesian Thinking Explained
- 5. How Thompson Sampling Works
- 6. Mathematical Foundation
- 7. Beta Distribution
- 8. Bernoulli Rewards
- 9. Thompson Sampling Algorithm
- 10. Ice Cream Shop Example
- 11. Python Code Example
- 12. CLI Output Samples
- 13. Advantages
- 14. Limitations
- 15. Real World Applications
- 16. Comparison with Other Methods
- 17. Deep Reinforcement Learning Connection
- 18. Final Thoughts
1. Introduction to Thompson Sampling
Thompson Sampling is a probabilistic algorithm used to solve decision-making problems under uncertainty.
It belongs to a broader family of problems called:
The central challenge is:
How do we balance:
- Exploration → Trying new options to learn more
- Exploitation → Using the best-known option to maximize reward
Thompson Sampling solves this by combining:
- Probability theory
- Bayesian inference
- Continuous learning
2. Exploration vs Exploitation
Every intelligent system faces this dilemma.
Exploration
Trying unknown actions to collect information.
Examples:
- Testing a new ice cream flavor
- Showing users a new advertisement
- Recommending a new movie genre
Exploitation
Using current knowledge to maximize reward.
Examples:
- Selling the best-performing flavor more often
- Displaying the highest-converting advertisement
- Recommending popular content
3. Multi-Armed Bandit Problem
The term "multi-armed bandit" comes from casino slot machines.
Imagine a row of slot machines:
- Each machine gives different rewards
- You do not know which machine pays best
- You have limited attempts
Each machine is called an "arm."
Where:
- \(A\) represents all possible actions
- \(a_i\) represents one arm or decision
Your goal:
This means maximizing total reward over time.
4. Bayesian Thinking Explained
Thompson Sampling uses Bayesian probability.
Instead of saying:
“This flavor IS the best.”
Bayesian thinking says:
“This flavor PROBABLY has a certain chance of being the best.”
Prior Belief
Initial assumptions before seeing data.
Posterior Belief
Updated belief after observing data.
Where:
- \(P(\theta | D)\) = posterior probability
- \(P(D|\theta)\) = likelihood
- \(P(\theta)\) = prior probability
- \(P(D)\) = evidence
5. How Thompson Sampling Works
Step 1: Initialize Beliefs
Assume each option starts equally likely.
Step 2: Sample Probabilities
Generate random samples from probability distributions.
Step 3: Select Best Sample
Choose the action with highest sampled reward.
Step 4: Observe Outcome
Receive reward or feedback.
Step 5: Update Beliefs
Improve probability distributions using observed data.
Step 6: Repeat
Continue learning over time.
6. Mathematical Foundation
Suppose rewards are binary:
- 1 = success
- 0 = failure
Each arm has unknown probability:
This represents success probability for arm \(i\).
We estimate:
7. Beta Distribution
The Beta distribution models probabilities between 0 and 1.
Where:
- \(\alpha\) = successes + 1
- \(\beta\) = failures + 1
Why Beta Distribution?
Because it perfectly models uncertainty about probabilities.
Examples:
| Successes | Failures | Confidence |
|---|---|---|
| 1 | 1 | Very uncertain |
| 50 | 10 | High confidence |
| 500 | 5 | Extremely confident |
8. Bernoulli Rewards
Thompson Sampling often assumes Bernoulli rewards.
Meaning:
- Success probability = \(p\)
- Failure probability = \(1-p\)
Examples:
- User clicked ad → success
- User ignored ad → failure
- Customer bought product → success
9. Thompson Sampling Algorithm
Algorithm Steps
- Initialize Beta distributions
- Sample from each distribution
- Select arm with highest sample
- Observe reward
- Update distribution
- Repeat continuously
Choose:
10. Ice Cream Shop Example
Suppose:
- Vanilla gets 7 likes and 3 dislikes
- Mango gets 15 likes and 5 dislikes
- Mint gets 4 likes and 10 dislikes
Probability Distributions
Mango now has stronger probability of being selected.
11. Python Code Example
Basic Thompson Sampling Implementation
import random
import numpy as np
N = 1000
d = 3
successes = [1] * d
failures = [1] * d
for n in range(N):
sampled_theta = [
np.random.beta(successes[i], failures[i])
for i in range(d)
]
chosen_arm = np.argmax(sampled_theta)
reward = random.choice([0,1])
if reward == 1:
successes[chosen_arm] += 1
else:
failures[chosen_arm] += 1
print("Successes:", successes)
print("Failures:", failures)
12. CLI Output Samples
CLI Example 1
$ python thompson_sampling.py
Round 1000 Complete
Successes:
[42, 176, 18]
Failures:
[31, 52, 49]
Best Arm Selected:
Mango Flavor
CLI Example 2
$ python reinforcement_learning.py
Sampling probabilities...
Vanilla: 0.58
Mango: 0.81
Mint: 0.29
Selected Action:
Mango
Interactive Learning Section
Because the current best option may not actually be optimal. Exploration helps discover potentially better actions that initially appeared weaker due to limited data.
Random sampling encourages exploration naturally. It prevents the algorithm from getting stuck with suboptimal choices too early.
Over time, Thompson Sampling converges toward near-optimal behavior and achieves strong long-term performance in uncertain environments.
13. Advantages of Thompson Sampling
- Simple implementation
- Efficient exploration
- Strong empirical performance
- Probabilistic reasoning
- Adaptive learning
- Works well in online systems
- Scales effectively
14. Limitations
- Requires probabilistic modeling
- Can become computationally heavy
- Complex reward distributions need advanced mathematics
- Not always ideal for extremely dynamic environments
15. Real World Applications
1. Online Advertising
Choosing advertisements with highest click-through rates.
2. Recommendation Systems
Netflix, Spotify, YouTube recommendations.
3. Clinical Trials
Testing medical treatments while minimizing patient risk.
4. E-Commerce
Product recommendation optimization.
5. Robotics
Adaptive decision-making under uncertainty.
6. Search Engines
Ranking results dynamically.
16. Comparison with Other Methods
| Method | Exploration Style | Efficiency |
|---|---|---|
| Epsilon Greedy | Random exploration | Moderate |
| UCB | Confidence bounds | High |
| Thompson Sampling | Bayesian sampling | Very High |
17. Deep Reinforcement Learning Connection
Modern AI systems extend Thompson Sampling into:
- Contextual bandits
- Deep Bayesian networks
- Bayesian neural networks
- Probabilistic reinforcement learning
These systems power:
- Self-driving systems
- Adaptive recommendation engines
- Large-scale personalization
Advanced Mathematics Section
Expected Reward
Cumulative Regret
Where:
- \(\mu^*\) = optimal reward
- \(\mu_{a_t}\) = obtained reward
Posterior Update
18. Final Thoughts
Thompson Sampling represents one of the most elegant ideas in machine learning and decision theory.
Rather than blindly committing to one option or randomly exploring forever, it intelligently balances uncertainty, learning, and optimization.
Its Bayesian foundation allows systems to continuously improve with experience while still remaining flexible enough to explore new opportunities.
From online advertising to medical research and recommendation systems, Thompson Sampling powers many intelligent systems we use every day.
- Thompson Sampling solves uncertainty problems.
- It balances exploration and exploitation.
- It uses Bayesian probability.
- Beta distributions model uncertainty.
- The algorithm improves continuously with feedback.
- It is widely used in reinforcement learning and AI systems.
No comments:
Post a Comment