ArticleslgStudy

computer science

Thompson sampling

Thompson sampling is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Thompson sampling rather than just read about it. In short: Thompson sampling, named after William Rae Thompson, is a heuristic for choosing actions that address the exploration–exploitation dilemma in the multi-armed bandit problem. It consists of choosing the action that maximizes the expected reward with respect to a randomly drawn belief.

Thompson sampling — main illustration
Thompson sampling — illustration

Key takeaways

  • Thompson sampling belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Thompson sampling to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Thompson sampling from memory before moving on to harder problems.

Reference excerpt

Thompson sampling, named after William Rae Thompson, is a heuristic for choosing actions that address the exploration–exploitation dilemma in the multi-armed bandit problem. It consists of choosing the action that maximizes the expected reward with respect to a randomly drawn belief.

Description Consider a set of contexts 𝒳, a set of actions 𝒜, and rewards in ℝ. The aim of the player is to play actions under the various contexts, such as to maximize the cumulative rewards. Specifically, in each round, the player obtains a context x∈𝒳, plays an action a∈𝒜 and receives a reward r∈ℝ following a distribution that depends on the context and the issued action. The elements of Thompson sampling are as follows:

a likelihood function P(r|θ,a,x); a set Θ of parameters θ of the distribution of r; a prior distribution P(θ) on these parameters; past observations triplets 𝒟={(x;a;r)}; a posterior distribution P(θ|𝒟)∝P(𝒟|θ)P(θ), where P(𝒟|θ) is the likelihood function. Thompson sampling consists of playing the action a∗∈𝒜 according to the probability that it maximizes the expected reward; action a∗ is chosen with probability

∫𝕀[𝔼(r|a∗,x,θ)=maxa′𝔼(r|a′,x,θ)]P(θ|𝒟)dθ, where 𝕀 is the indicator function. In practice, the rule is implemented by sampling. In each round, parameters θ∗ are sampled from the posterior P(θ|𝒟), and an action a∗ chosen that maximizes 𝔼[r|θ∗,a∗,x], i.e. the expected reward given the sampled parameters, the action, and the current context. Conceptually, this means that the player instantiates their beliefs randomly in each round according to the posterior distribution, and then acts optimally according to them. In most practical applications, it is computationally onerous to maintain and sample from a posterior distribution over models. As such, Thompson sampling is often used in conjunction with approximate sampling techniques.

History Thompson sampling was originally described by Thompson in 1933. It was subsequently rediscovered numerous times independently in the context of multi-armed bandit problems. A first proof of convergence for the bandit case has been shown in 1997. The first application to Markov decision processes was in 2000. A related approach (see Bayesian control rule) was published in 2010. In 2010 it was also shown that Thompson sampling is instantaneously self-correcting. Asymptotic convergence results for contextual bandits were published in 2011. Thompson Sampling has been widely used in many online learning problems including A/B testing in website design and online advertising, and accelerated learning in decentralized decision making. A Double Thompson Sampling (D-TS) algorithm has been proposed for dueling bandits, a variant of traditional MAB, where feedback comes in the form of pairwise comparison.

Relationship to other approaches

Probability matching

Probability matching is a decision strategy in which predictions of class membership are proportional to the class base rates. Thus, if in the training set positive examples are observed 60% of the time, and negative examples are observed 40% of the time, the observer using a probability-matching strategy will predict (for unlabeled examples) a class label of "positive" on 60% of instances, and a class label of "negative" on 40% of instances.

Bayesian control rule A generalization of Thompson sampling to arbitrary dynamical environments and causal structures, known as Bayesian control rule, has been shown to be the optimal solution to the adaptive coding problem with actions and observations. In this formulation, an agent is conceptualized as a mixture over a set of behaviours. As the agent interacts with its environment, it learns the causal properties and adopts the behaviour that minimizes the relative entropy to the behaviour with the best prediction of the environment's behaviour. If these behaviours have been chosen according to the maximum expected utility principle, then the asymptotic behaviour of the Bayesian control rule matches the asymptotic behaviour of the perfectly rational agent. The setup is as follows. Let a1,a2,…,aT be the actions issued by an agent up to time T, and let o1,o2,…,oT be the observations gathered by the agent up to time T. Then, the agent issues the action aT+1 with probability:

P(aT+1|â1:T,o1:T), where the "hat"-notation ât denotes the fact that at is a causal intervention (see Causality), and not an ordinary observation. If the agent holds beliefs θ∈Θ over its behaviors, then the Bayesian control rule becomes

P(aT+1|â1:T,o1:T)=∫ΘP(aT+1|θ,â1:T,o1:T)P(θ|â1:T,o1:T)dθ, where P(θ|â1:T,o1:T) is the posterior distribution over the parameter θ given actions a1:T and observations o1:T. In practice, the Bayesian control amounts to sampling, at each time step, a parameter θ∗ from the posterior distribution P(θ|â1:T,o1:T), where the posterior distribution is computed using Bayes' rule by only considering the (causal) likelihoods of the observations o1,o2,…,oT and ignoring the (causal) likelihoods of the actions a1,a2,…,aT, and then by sampling the action aT+1∗ from the action distribution P(aT+1|θ∗,â1:T,o1:T).

Upper-confidence-bound (UCB) algorithms Thompson sampling and upper-confidence bound algorithms share a fundamental property that underlies many of their theoretical guarantees. Roughly speaking, both algorithms allocate exploratory effort to actions that might be optimal and are in this sense "optimistic". Leveraging this property, one can translate regret bounds established for UCB algorithms to Bayesian regret bounds for Thompson sampling or unify regret analysis across both these algorithms and many classes of problems.

References

Illustrations

Thompson sampling: Concrete example of Thompson sampling applied to simulate treatment efficacy evaluation
Concrete example of Thompson sampling applied to simulate treatment efficacy evaluation

Worked examples

Example 1 — a first encounter with Thompson sampling

Start with the simplest possible case. Write down what Thompson sampling claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Thompson sampling before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Thompson sampling ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Thompson sampling

In research
Thompson sampling appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Thompson sampling in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Thompson sampling is common in secondary-school and first-year university syllabi. It links to neighbouring topics Artificial intelligence engineering, Heuristic algorithms, Sequential experiments, so understanding it makes those chapters shorter.
In everyday life
Look for Thompson sampling outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Thompson sampling” →

How to study Thompson sampling in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Thompson sampling means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Thompson sampling out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Thompson sampling in simple terms?

Thompson sampling, named after William Rae Thompson, is a heuristic for choosing actions that address the exploration–exploitation dilemma in the multi-armed bandit problem. It consists of choosing the action that maximizes the expected reward with respect to a randomly drawn belief.

Why does Thompson sampling matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Thompson sampling?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Thompson sampling.

Tags

  • Artificial intelligence engineering
  • Heuristic algorithms
  • Sequential experiments
  • Sequential methods

Keep exploring