RLS RL, Read as Statistics

Chapter 03 · Part 1: Estimating from experience

Thompson sampling as posterior inference

Thompson sampling is Bayesian updating plus one extra step, acting on a single posterior draw, and this chapter shows why that matches the best arm's posterior probability and how its regret behaves.

Chapter 2 treated a bandit as a sequential experiment and turned a confidence bound into a decision rule: UCB pulls the arm whose upper confidence limit is highest. That is a frequentist construction. It takes an interval, picks its upper end, and acts as if the arm were that good.

A Bayesian reader asks a different question. If the data produce a whole distribution over what each arm's mean could be, why not use that distribution directly? Thompson (1933), a pathologist, proposed this almost a century ago, in a Biometrika paper on comparing two unknown probabilities from two samples. The idea is to sample a plausible world from the posterior and act as though it were true. The method sat in the literature for decades, was shown to work well in practice by Chapelle and Li (2011), and received its first logarithmic-regret proof from Agrawal and Goyal (2012).

This chapter uses the notation of the notation page: KK arms, mean rewards μa\mu_a, μ∗=max⁡aμa\mu^* = \max_a \mu_a, gaps Δa=μ∗−μa\Delta_a = \mu^* - \mu_a, and regret Regret(T)=Tμ∗−E[∑t=1TRt]\mathrm{Regret}(T) = T\mu^* - \mathbb{E}[\sum_{t=1}^T R_t]. Rewards here are Bernoulli unless stated, and the arms are independent.

What does Thompson sampling do?

A Bayesian model has two parts. A prior p(μ1,…,μK)p(\mu_1, \dots, \mu_K) says what we believe about the arm means before seeing data. A likelihood says how rewards are generated given the means; for a Bernoulli bandit, a pull of arm aa gives R∼Bern⁡(μa)R \sim \operatorname{Bern}(\mu_a). Bayes' rule combines them into the posterior p(μ∣Ht)p(\mu \mid H_{t}) given the history HtH_t of arms pulled and rewards seen through round tt.

Thompson sampling, in each round tt:

  1. Compute (or update) the posterior of every arm given the history.
  2. Draw one sample μ~a\tilde\mu_a from each arm's posterior.
  3. Pull At=arg⁡max⁡aμ~aA_t = \arg\max_a \tilde\mu_a.
  4. Observe RtR_t and update the posterior of arm AtA_t.

There is no confidence level to tune and no exploration bonus. Exploration comes from the spread of the posterior: an arm we know little about has a wide posterior, so its draw is occasionally very high and it gets pulled. As data accumulate, the posterior narrows around the true mean and the draws stop beating the best arm.

Arms are independent under the prior, so only the pulled arm's posterior changes. And for the likelihoods we use, the posterior stays in the same family as the prior, which is worth a section.

Why is the beta distribution the right prior for Bernoulli rewards?

Take one arm with unknown mean θ\theta (we drop the arm subscript). A Beta⁡(a,b)\operatorname{Beta}(a, b) prior has density proportional to

θa−1(1−θ)b−1,θ∈[0,1].\theta^{a-1}(1-\theta)^{b-1}, \qquad \theta \in [0, 1].

Suppose we see nn pulls with ss successes and f=n−sf = n - s failures. The likelihood is θs(1−θ)f\theta^{s}(1-\theta)^{f}. Multiply:

p(θ∣data)  ∝  θa−1(1−θ)b−1⋅θs(1−θ)f  =  θa+s−1(1−θ)b+f−1.p(\theta \mid \text{data}) \;\propto\; \theta^{a-1}(1-\theta)^{b-1} \cdot \theta^{s}(1-\theta)^{f} \;=\; \theta^{a+s-1}(1-\theta)^{b+f-1}.

That is the kernel of Beta⁡(a+s, b+f)\operatorname{Beta}(a + s,\, b + f). A prior family that the likelihood maps to itself is called conjugate. The practical consequence is that the update is arithmetic on two counters: a success adds one to aa, a failure adds one to bb. The order of the observations does not matter, and updating one observation at a time gives the same answer as updating on all of them at once, because (s,n)(s, n) is a sufficient statistic.

The posterior mean is

E[θ∣data]=a+sa+b+n=a+ba+b+n⋅aa+b+na+b+n⋅sn.\mathbb{E}[\theta \mid \text{data}] = \frac{a + s}{a + b + n} = \frac{a+b}{a+b+n}\cdot\frac{a}{a+b} + \frac{n}{a+b+n}\cdot\frac{s}{n}.

It is a weighted average of the prior mean a/(a+b)a/(a+b) and the sample mean s/ns/n, with weights proportional to a+ba + b and nn. So a+ba + b acts as the prior sample size, the number of pseudo-observations the prior is worth. Beta⁡(1,1)\operatorname{Beta}(1,1) is the uniform distribution and is worth two pseudo-observations. Beta⁡(40,10)\operatorname{Beta}(40, 10) is worth fifty, and it believes the mean is 0.80.8. The posterior variance shrinks like 1/n1/n.

A worked example by hand

Two arms, both with a Beta⁡(1,1)\operatorname{Beta}(1,1) prior. Arm A is pulled once and pays 11. Arm B has not been pulled.

  • Arm A posterior: a=1+1=2a = 1 + 1 = 2, b=1+0=1b = 1 + 0 = 1, so Beta⁡(2,1)\operatorname{Beta}(2, 1), with density 2θ2\theta on [0,1][0,1] and mean 2/32/3.
  • Arm B posterior: still Beta⁡(1,1)\operatorname{Beta}(1, 1), uniform, mean 1/21/2.

Thompson sampling plays A when μ~A>μ~B\tilde\mu_A > \tilde\mu_B. This is Thompson's original quantity, "the probability that one unknown probability exceeds another". Condition on the draw μ~A=x\tilde\mu_A = x; then B's uniform draw is below xx with probability xx:

P(μ~A>μ~B)=∫012x⋅x dx=23.\mathbb{P}(\tilde\mu_A > \tilde\mu_B) = \int_0^1 2x \cdot x \, dx = \frac{2}{3}.

So A is played with probability 2/32/3 and B with probability 1/31/3. A greedy rule that plays the larger posterior mean would play A with probability one, forever, if A keeps winning. Thompson sampling still gives B a one-in-three chance now, and this chance falls as evidence accumulates against B.

Now suppose B is played and fails. Its posterior becomes Beta⁡(1,2)\operatorname{Beta}(1, 2) with density 2(1−θ)2(1-\theta) and mean 1/31/3. The new play probability is P(μ~A>μ~B)=∫012x⋅FB(x) dx\mathbb{P}(\tilde\mu_A > \tilde\mu_B) = \int_0^1 2x \cdot F_B(x)\,dx with FB(x)=1−(1−x)2=2x−x2F_B(x) = 1 - (1-x)^2 = 2x - x^2, so

∫012x(2x−x2) dx=∫01(4x2−2x3) dx=43−12=56.\int_0^1 2x(2x - x^2)\,dx = \int_0^1 (4x^2 - 2x^3)\,dx = \frac{4}{3} - \frac{1}{2} = \frac{5}{6}.

One failure moved the probability of exploring B from 1/31/3 to 1/61/6.

Why is this "probability matching"?

Let A∗=arg⁡max⁡aμaA^* = \arg\max_a \mu_a be the optimal arm. Under the Bayesian model A∗A^* is a random variable, a function of the unknown μ\mu. Write Pt(⋅)=P(⋅∣Ht−1)\mathbb{P}_t(\cdot) = \mathbb{P}(\cdot \mid H_{t-1}) for the posterior given the history before round tt. Thompson sampling draws μ~\tilde\mu from the posterior Pt(μ∈⋅)\mathbb{P}_t(\mu \in \cdot) and plays At=arg⁡max⁡aμ~aA_t = \arg\max_a \tilde\mu_a. The true μ\mu and the draw μ~\tilde\mu have the same distribution given Ht−1H_{t-1}, so arg⁡max⁡aμ~a\arg\max_a \tilde\mu_a and arg⁡max⁡aμa\arg\max_a \mu_a do too:

P(At=a∣Ht−1)  =  P(A∗=a∣Ht−1)for every arm a.\mathbb{P}(A_t = a \mid H_{t-1}) \;=\; \mathbb{P}(A^* = a \mid H_{t-1}) \quad\text{for every arm } a.

The algorithm plays each arm with the probability that the arm is best. That is why Chapelle and Li call it probability matching.

Two caveats a statistician should hold on to.

First, the identity is about the model's posterior. It is calibrated if the true μ\mu was in fact drawn from the prior. Under a fixed μ\mu (the frequentist view) the posterior probability that arm 1 is best can be 0.270.27 early on, while arm 1 is the best arm in every run. Matching is a statement about the algorithm's relationship to its own posterior, not a statement that the posterior is right.

Second, adaptive sampling does not break Bayesian updating. In chapter 2 we saw that the sample mean of an adaptively sampled arm is biased. The posterior is unaffected, because the rule for choosing arms depends only on observed data, and so it factors out of the likelihood. The posterior after nn pulls of an arm is the same Beta⁡(a+s,b+f)\operatorname{Beta}(a+s, b+f) regardless of how those pulls were scheduled. This is the likelihood principle at work, and it is one reason Bayesian bandit algorithms are comfortable with adaptive data.

What changes with Gaussian rewards?

Take rewards R∼N(θ,σ2)R \sim \mathcal{N}(\theta, \sigma^2) with σ2\sigma^2 known, and a prior θ∼N(m0,s02)\theta \sim \mathcal{N}(m_0, s_0^2). After nn pulls with sample mean xˉ\bar x, the posterior is again normal. Multiplying the prior density by the likelihood and completing the square in θ\theta gives

1sn2=1s02+nσ2,mn=sn2(m0s02+nxˉσ2).\frac{1}{s_n^2} = \frac{1}{s_0^2} + \frac{n}{\sigma^2}, \qquad m_n = s_n^2\left(\frac{m_0}{s_0^2} + \frac{n\bar x}{\sigma^2}\right).

Precisions (inverse variances) add, and the posterior mean is a precision-weighted average of m0m_0 and xˉ\bar x. For large nn, sn≈σ/ns_n \approx \sigma/\sqrt{n}, the standard error of the sample mean.

A Thompson draw for arm aa is then

μ~a=ma+saZa,Za∼N(0,1) independent.\tilde\mu_a = m_{a} + s_{a} Z_a, \qquad Z_a \sim \mathcal{N}(0, 1) \text{ independent}.

Compare UCB1 from chapter 2, which plays the arm maximising μ^a+2ln⁡t/Na\hat\mu_a + \sqrt{2\ln t / N_a}. Both are "estimate plus a multiple of the standard error". UCB uses a fixed, deliberately large multiple, chosen so that the bound holds with high probability for every arm and time. Thompson uses a random multiple ZaZ_a that is usually between −2-2 and 22, centred at zero. UCB is optimistic on purpose; Thompson is optimistic about half the time.

What is Bayesian regret, and how does it differ from the regret you know?

Fix a μ\mu. The frequentist regret of an algorithm on that instance is

Regret(T;μ)=∑aΔa(μ) Eμ[Na(T)],\mathrm{Regret}(T; \mu) = \sum_{a} \Delta_a(\mu)\, \mathbb{E}_\mu[N_a(T)],

the expectation taken over the rewards and the algorithm's own randomness, with μ\mu held fixed. This is what chapter 2's Lai–Robbins bound and UCB1's guarantee are about. The Bayesian regret averages that over the prior:

BR(T)=Eμ∼prior[Regret(T;μ)].\mathrm{BR}(T) = \mathbb{E}_{\mu \sim \text{prior}}\big[\mathrm{Regret}(T; \mu)\big].

The two views give two separate bodies of theory for Thompson sampling.

The Bayesian bound. Russo and Van Roy (2014) analyse Thompson sampling through information theory. Let ItI_t be the mutual information, under the posterior at round tt, between the optimal arm A∗A^* and the pair (arm played, reward seen), and let Δt=Et[μ∗−μAt]\Delta_t = \mathbb{E}_t[\mu^* - \mu_{A_t}] be the expected instantaneous regret given the history. The information ratio is

Γt=Δt2It.\Gamma_t = \frac{\Delta_t^2}{I_t}.

It is small when a round either has little regret or teaches a lot about which arm is best. If Γt≤Γ\Gamma_t \le \Gamma always, then by Cauchy–Schwarz

E∑t=1TΔt  ≤  E∑t=1TΓIt  ≤  Γ T E∑tIt  ≤  Γ T H(A∗).\mathbb{E}\sum_{t=1}^T \Delta_t \;\le\; \mathbb{E}\sum_{t=1}^T \sqrt{\Gamma I_t} \;\le\; \sqrt{\Gamma\, T\, \mathbb{E}\textstyle\sum_{t} I_t} \;\le\; \sqrt{\Gamma\, T\, H(A^*)}.

The last step uses the chain rule: the information gained about A∗A^* over all rounds cannot exceed the prior entropy H(A∗)H(A^*) of the optimal arm. For Thompson sampling with rewards in [0,1][0,1], they prove Γt≤K/2\Gamma_t \le K/2 for any prior (their Proposition 3), which gives

BR(T)  ≤  12 K H(A∗) T  ≤  12 Kln⁡K⋅T.\mathrm{BR}(T) \;\le\; \sqrt{\tfrac{1}{2}\, K\, H(A^*)\, T} \;\le\; \sqrt{\tfrac{1}{2}\, K \ln K \cdot T}.

The bound scales with the entropy of the prior over the best arm. A prior that is already confident about which arm is best has small H(A∗)H(A^*) and a small bound. The tutorial by Russo, Van Roy, Kazerouni, Osband and Wen (2018) surveys this analysis alongside many extensions and applications, and is the best single reference for the method; it works through Bernoulli bandits, shortest-path problems, recommendation, and reinforcement learning in Markov decision processes.

The frequentist bound. Fix a Bernoulli instance and run Thompson sampling with Beta⁡(1,1)\operatorname{Beta}(1,1) priors. Agrawal and Goyal (2012) proved logarithmic expected regret, with a bound of order (∑a≠a∗1/Δa2)2ln⁡T\big(\sum_{a \ne a^*} 1/\Delta_a^2\big)^2 \ln T for KK arms, and O(ln⁡T/Δ+1/Δ3)O(\ln T/\Delta + 1/\Delta^3) for two arms. A sharper analysis by Kaufmann, Korda and Munos (2012) showed that the regret matches the Lai–Robbins lower bound asymptotically for Bernoulli rewards, so Thompson sampling is asymptotically optimal and not merely logarithmic. Agrawal and Goyal (2012, follow-up) gave a (1+ϵ)(1+\epsilon)-optimal problem-dependent bound and an O(KTln⁡T)O(\sqrt{KT\ln T}) problem-independent bound. Note the conditions: these are statements for Bernoulli rewards with a uniform prior, and they hold on every instance, whether or not the prior "describes" it.

Frequentist guarantees survive a wrong prior as long as the prior puts positive mass near the truth; this is the same reason Bayesian estimators are consistent under mild conditions. What the prior changes is the constant. The next section looks at how much.

What does a wrong prior cost?

The prior is worth a+ba + b pseudo-observations. If the data are informative, a prior that is wrong but weak is overruled quickly. A prior that is wrong and strong is not: you need roughly as many real observations as pseudo-observations before the data win.

Take an arm whose true mean is 0.350.35 and a Beta⁡(40,10)\operatorname{Beta}(40, 10) prior shared by all arms, so every arm is believed to pay about 0.80.8 and the prior is worth 5050 observations. After nn pulls of that arm the posterior mean is about (40+0.35n)/(50+n)(40 + 0.35n)/(50 + n). This falls below the best arm's true mean 0.60.6 when 10≤0.25 n10 \le 0.25\,n, that is, at about n=40n = 40. Every inferior arm must be pulled around forty times before the sampler stops being fooled, and each of those pulls costs regret. The posterior is also narrow (≈0.056\approx 0.056 standard deviation at the start), so the sampling noise that normally drives exploration is small and the fix is slow.

The experiment below also tries a prior with the wrong ordering: it believes the worst arm is best and the best arm is worst, each prior worth 20 observations.

Experiment: Thompson sampling against UCB1 and greedy

The code in ch03_thompson.py runs a five-arm Bernoulli bandit with means (0.60,0.50,0.45,0.40,0.35)(0.60, 0.50, 0.45, 0.40, 0.35), over 400400 independent runs and T=3000T = 3000 rounds. UCB1 is the index μ^a+2ln⁡t/Na\hat\mu_a + \sqrt{2\ln t/N_a} of Auer, Cesa-Bianchi and Fischer (2002). Greedy pulls each arm once and then plays the highest sample mean. We report pseudo-regret, which sums the gaps ΔAt\Delta_{A_t} of the arms played, so there is no reward noise in the regret itself; the only randomness is in which arms were played. Standard errors are across runs. A second experiment checks probability matching, and a third averages over instances drawn from the prior to estimate Bayesian regret.

Output
Experiment 1: fixed instance mu = [0.6, 0.5, 0.45, 0.4, 0.35], 400 runs, T = 3000
Cumulative pseudo-regret, mean (standard error) over runs
algorithm                                   T=100          T=500         T=1000         T=3000   locked-on-opt
greedy                                 9.3 ( 0.4)    40.8 ( 2.1)    79.5 ( 4.2)   232.7 (12.6)     49.2%
UCB1                                  11.9 ( 0.1)    47.7 ( 0.3)    80.0 ( 0.5)   158.0 ( 0.9)     38.0%
Thompson, Beta(1,1)                    9.8 ( 0.2)    26.8 ( 0.6)    34.9 ( 0.9)    45.5 ( 1.3)     98.0%
Thompson, optimistic Beta(40,10)      12.4 ( 0.1)    48.2 ( 0.3)    73.3 ( 0.6)   106.5 ( 0.8)     85.8%
Thompson, reversed prior (n0=20)      17.2 ( 0.1)    44.9 ( 0.8)    53.9 ( 1.3)    62.2 ( 2.0)     98.5%
Lai-Robbins asymptotic lower-bound scale, sum(gap/KL) * ln T at T=3000: 101.0
locked-on-opt = share of runs pulling the best arm in >90% of the last 100 rounds

Experiment 2: probability matching, Thompson Beta(1,1), same 400 runs
selection frequency of each arm at round t  vs  mean posterior P(optimal)
    t            arm0    arm1    arm2    arm3    arm4  
   10  chosen    0.268   0.223   0.188   0.155   0.168 
       P(opt)    0.269   0.213   0.199   0.164   0.156 
   50  chosen    0.430   0.212   0.160   0.102   0.095 
       P(opt)    0.409   0.207   0.171   0.119   0.094 
  200  chosen    0.635   0.177   0.102   0.050   0.035 
       P(opt)    0.635   0.173   0.094   0.061   0.037 
 1000  chosen    0.910   0.050   0.028   0.005   0.007 
       P(opt)    0.919   0.045   0.021   0.010   0.006 

Calibration: pool (run, arm, checkpoint); bin by posterior P(optimal)
        bin  count  mean P(opt)  chosen freq      se
[0.00,0.05)   3171        0.016        0.016   0.002
[0.05,0.20)   2346        0.106        0.104   0.006
[0.20,0.40)   1075        0.284        0.281   0.014
[0.40,0.60)    517        0.487        0.491   0.022
[0.60,0.80)    345        0.697        0.719   0.024
[0.80,1.00)    546        0.920        0.919   0.012

Experiment 3: Bayesian regret, mu_a ~ U(0,1) iid, K=5, 1000 draws of mu, T=1000
algorithm                             Bayes regret   max over runs
greedy                                 40.1 ( 2.9)           576.9
UCB1                                   68.2 ( 0.4)           119.2
Thompson, Beta(1,1)                    17.2 ( 0.3)            94.4
Russo-Van Roy bound sqrt(0.5 * K * ln K * T) = 63.4

Regret

At T=3000T = 3000, Thompson sampling with the uniform prior has regret 45.5±1.345.5 \pm 1.3, UCB1 has 158.0±0.9158.0 \pm 0.9, and greedy has 232.7±12.6232.7 \pm 12.6. Three things stand out.

Thompson sampling beats UCB1 by a wide margin here, mostly by exploring less. UCB1 at T=100T = 100 is still worse than Thompson sampling and the gap widens. UCB1's index uses a confidence level calibrated for a worst-case guarantee, so its exploration bonus is larger than this instance needs; the share of runs "locked on" the best arm is only 38%38\% for UCB1 after 3000 rounds. The constant 22 in the bonus is chosen for a worst-case guarantee, and a tuned bonus would do better. The comparison shows what an untuned textbook UCB costs on this instance, not a law that Bayesian methods always win. Chapelle and Li also report, in simulations of delayed feedback, that Thompson sampling is more robust than UCB when feedback arrives in batches, because randomising over arms keeps a batch from repeating one choice.

Greedy has the largest and the least stable regret. Its standard error is more than ten times UCB1's. Greedy locks onto the best arm in 49%49\% of runs; in the others early noise ranked a bad arm first and the best arm was starved of samples. Its mean regret hides a bimodal distribution. In the Bayesian experiment the worst single run has regret 576.9576.9 against 94.494.4 for Thompson sampling. This is the same adaptive-sampling trap as chapter 2: an arm that looks bad stops being sampled, so its estimate is never corrected.

The Lai–Robbins line is a scale, not a ceiling. The quantity ∑aΔaln⁡T/kl(μa,μ∗)\sum_a \Delta_a \ln T / \mathrm{kl}(\mu_a, \mu^*), with kl\mathrm{kl} the Bernoulli Kullback–Leibler divergence, is 101.0101.0 at T=3000T = 3000. Thompson sampling's 45.545.5 is below it. That is not a contradiction: the lower bound is an asymptotic statement about lim inf⁡T→∞Regret(T)/ln⁡T\liminf_{T\to\infty}\mathrm{Regret}(T)/\ln T for algorithms that are consistent on every instance, and says nothing about the value at a particular finite TT, where lower-order terms are negative or not yet visible.

Misspecified priors

The two bad priors cost real regret. The optimistic Beta⁡(40,10)\operatorname{Beta}(40,10) prior roughly doubles Thompson sampling's regret at T=3000T = 3000 (106.5106.5 against 45.545.5), and 85.8%85.8\% of runs have locked on the best arm against 98%98\% for the uniform prior. This is the same order of magnitude as the back-of-envelope count above: 4040 forced pulls on each of four inferior arms, at an average gap of 0.1750.175, is about 2828 units of regret, and the observed excess is 6161. The rest comes from the slower resolution of near-ties when the posteriors are narrow. The reversed prior is worse early (17.217.2 at T=100T = 100 against 9.89.8) and then recovers, ending at 62.262.2 with 98.5%98.5\% of runs locked on the best arm. Both effects come from the same place: prior pseudo-observations are real evidence until enough data outweighs them. Both priors have positive density everywhere on [0,1][0,1], so the frequentist guarantees still apply and regret is still growing slowly, but the constant is paid in full.

Probability matching

The top block of experiment 2 compares, over the 400400 runs, the fraction of runs that pulled each arm at round tt with the average of that run's posterior probability of the arm being optimal. They agree to within the sampling noise of 400400 runs (a standard error of about 0.020.02 on a frequency near 0.50.5). At t=1000t = 1000, arm 00 is chosen 91.0%91.0\% of the time and has mean posterior probability 0.9190.919.

The calibration table is a sharper test. It pools every (run, arm, checkpoint) triple, bins it by that run's posterior P(optimal)\mathbb{P}(\text{optimal}), and compares the bin's mean posterior probability with how often the arm was actually chosen. The two columns agree in every bin to within about one standard error, from bins with probability around 0.0160.016 to bins around 0.920.92.

Note what the table does not say. In these runs arm 00 is the best arm every time, yet at t=10t = 10 its mean posterior probability of being best is only 0.270.27. The posterior is honest about the data seen so far; it is not told the truth about μ\mu.

Bayesian regret

When μ\mu is drawn from the uniform prior, Thompson sampling's Bayesian regret at T=1000T = 1000 is 17.2±0.317.2 \pm 0.3, UCB1's is 68.2±0.468.2 \pm 0.4, and greedy's is 40.1±2.940.1 \pm 2.9. The Russo–Van Roy bound for Thompson sampling is 63.463.4, loose by a factor of about 3.73.7. It is a worst-case guarantee over all priors with that entropy and all reward distributions in [0,1][0,1], so it is not expected to be tight. Here the Bayesian setting favours Thompson sampling more than the fixed-instance one, which makes sense: the prior used for the draw is exactly the prior the algorithm assumes, so there is no misspecification to pay for.

Where does this leave us?

Thompson sampling treats exploration as uncertainty and uses the statistician's native object for uncertainty, the posterior. The cost is that you need a prior and a conjugate (or at least samplable) posterior. That is easy for Bernoulli and Gaussian arms and harder for arbitrary models; the tutorial by Russo et al. (2018) discusses approximate posterior sampling for models that lack a conjugate posterior. We come back to posterior sampling at full scale in chapter 9, where it is applied to whole Markov decision processes.

The next chapter drops the decision part and keeps the estimation part. Given a fixed policy, how do you estimate the expected return of a state from episodes of experience? It turns out to be the sample mean again, with two ways to define the sample. See chapter 4.

Exercises

  1. Pen and paper. Two arms each have a Beta⁡(1,1)\operatorname{Beta}(1,1) prior. Arm A is pulled twice and succeeds both times. Arm B is pulled once and fails. Write both posteriors and compute the probability that Thompson sampling plays A.
  2. Pen and paper. Arm A has one success and no failures; arm B has no data; both started from Beta⁡(1,1)\operatorname{Beta}(1,1). Which arm does a greedy-on-posterior-mean rule play, and with what probability does Thompson sampling play the other one? What does this say about why greedy can get stuck?
  3. Pen and paper. Show that the Beta posterior mean is a weighted average of prior mean and sample mean, with weights (a+b)/(a+b+n)(a+b)/(a+b+n) and n/(a+b+n)n/(a+b+n). For the prior Beta⁡(40,10)\operatorname{Beta}(40,10) and an arm with true mean 0.350.35, find the nn at which the expected posterior mean falls to 0.60.6. (Use the sample mean =0.35= 0.35.)
  4. Change the code and predict. In ch03_thompson.py, change the reversed prior's strength from 2020 to 55 and then to 100100. Predict the order of the three regret values at T=3000T = 3000 (uniform prior, n0=5n_0 = 5, n0=100n_0 = 100) before you run it. Does the damage grow roughly linearly in the prior sample size?
  5. Change the code and predict. Replace the means with (0.52,0.50,0.48,0.46,0.44)(0.52, 0.50, 0.48, 0.46, 0.44). Which method's regret grows fastest early? Does Thompson sampling still lock onto the best arm in 98%98\% of runs by T=3000T = 3000? Think in terms of how many pulls are needed to separate arms with a gap of 0.020.02.

Sources