Friday, October 25, 2024

Thompson Sampling Simplified: How to Make Smart Choices in Uncertain Situations


Thompson Sampling Explained Simply | Reinforcement Learning Guide

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:

You do not know which flavor customers will love the most.

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.



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:

Multi-Armed Bandit Problems

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
The challenge is finding the perfect balance between learning and earning.

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."

\[ A = \{a_1, a_2, a_3, ..., a_n\} \]

Where:

  • \(A\) represents all possible actions
  • \(a_i\) represents one arm or decision

Your goal:

\[ \max \sum_{t=1}^{T} r_t \]

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.

\[ P(\theta | D) = \frac{P(D|\theta)P(\theta)}{P(D)} \]

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:

\[ \theta_i \]

This represents success probability for arm \(i\).

We estimate:

\[ \theta_i \sim Beta(\alpha_i, \beta_i) \]

7. Beta Distribution

The Beta distribution models probabilities between 0 and 1.

\[ f(x;\alpha,\beta) = \frac{x^{\alpha-1}(1-x)^{\beta-1}} {B(\alpha,\beta)} \]

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.

\[ X \sim Bernoulli(p) \]

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

  1. Initialize Beta distributions
  2. Sample from each distribution
  3. Select arm with highest sample
  4. Observe reward
  5. Update distribution
  6. Repeat continuously
\[ \theta_i^{(t)} \sim Beta(\alpha_i,\beta_i) \]

Choose:

\[ a_t = \arg\max_i \theta_i^{(t)} \]

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

\[ Vanilla \sim Beta(8,4) \]
\[ Mango \sim Beta(16,6) \]
\[ Mint \sim Beta(5,11) \]

Mango now has stronger probability of being selected.

Thompson Sampling naturally shifts toward better-performing choices while still occasionally exploring others.

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
Thompson Sampling often outperforms epsilon-greedy methods in practical applications.

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

\[ E[R_t] = \sum_{i=1}^{k} P(a_i)r_i \]

Cumulative Regret

\[ Regret(T) = T\mu^* - \sum_{t=1}^{T}\mu_{a_t} \]

Where:

  • \(\mu^*\) = optimal reward
  • \(\mu_{a_t}\) = obtained reward

Posterior Update

\[ Beta(\alpha,\beta) \rightarrow Beta(\alpha + success,\beta + failure) \]

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.

Final Learning Summary:
  • 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

Featured Post

How HMT Watches Lost the Time: A Deep Dive into Disruptive Innovation Blindness in Indian Manufacturing

The Rise and Fall of HMT Watches: A Story of Brand Dominance and Disruptive Innovation Blindness The Rise and Fal...

Popular Posts