Session 11: Beta--Bernoulli and Bandits

Movement III: Two Random Variables

EE 520: Random Processes (Fall 2026). Instructor: John Lipor.

Launch

Load a news site, and in the few milliseconds before the page appears an algorithm decides which of several ads to put in front of you. It is running a live experiment. Each candidate ad kk has a click-through rate θk\theta_k, the probability that a viewer clicks it, and nobody knows these rates in advance: a new ad might be brilliant or dead on arrival. The only way to learn an ad’s rate is to show it and count clicks, and every impression spent on a weak ad is revenue a strong ad would have earned. The system must learn and earn at the same time. This is the multi-armed bandit problem, named for a row of slot machines (“one-armed bandits”) with unknown payout rates, and the same structure decides which headline a news site leads with and which thumbnail a video gets.

The textbook answer is the A/B test: split traffic evenly for two weeks, then declare a winner. It is clean, and it is wasteful. Half the traffic goes to the loser for the whole test, even after the data are overwhelming. Large platforms instead let traffic drift toward the winner as the evidence arrives, and one of the most widely used ways to do that, Thompson sampling, owes its modern revival to exactly this setting (display advertising and news recommendation at Yahoo, website experiments at Google). It is built from nothing beyond the machinery of this course: a prior, a likelihood, and a single random draw. In practice it crushes the obvious “always show the best ad so far.”

A note on scale before we start: real click rates are a few percent. Our examples use larger rates so the arithmetic stays readable. Nothing in the mathematics depends on the scale; small rates only make learning slower, and we will say exactly how much slower.

Today we build Thompson sampling. The road there has three stages: first, Bayes’ rule when the unknown is a probability living in [0,1][0, 1] (the posterior becomes a density); second, the one distribution, Beta, that makes such updating a two-integer bookkeeping exercise; third, Thompson sampling, which turns that posterior directly into a sequential decision. Then we ask how good these decisions are. We will prove that “best so far” can fail forever, prove why Thompson sampling cannot, and meet the classical answer to what the best possible strategy looks like.

Development

Bayes’ rule with a density prior

Until now the unknown θ\theta took finitely many values and the prior was a list of numbers. Let θ\theta instead be a continuous quantity with a prior density f(θ)f(\theta), and let the data be discrete (clicks and non-clicks on an ad). Bayes’ rule reads exactly as before, with the density in place of the prior list: f(θ|data)=f(θ)L(data|θ)∫01f(ϑ)L(data|ϑ)dϑ∝f(θ)L(data|θ).\begin{equation} f(\theta \,|\,\text{data}) = \frac{f(\theta)\, L(\text{data} \,|\,\theta)} {\int_0^1 f(\vartheta)\, L(\text{data} \,|\,\vartheta)\, d\vartheta} \;\propto\; f(\theta)\, L(\text{data} \,|\,\theta). \label{eq:densitybayes} \end{equation} This is Session 2’s Bayes rule, now with densities: posterior ∝\propto prior ×\times likelihood, unchanged. Only two things are new. The evidence in the denominator is now an integral rather than a finite sum, and because that denominator is just the constant that makes the posterior integrate to one, the working style is ∝\propto-then-normalize: carry the shape f(θ)L(data|θ)f(\theta) L(\text{data} \,|\,\theta), drop every factor that does not depend on θ\theta, and fix the constant at the end by recognizing the result. The next two subsections show a family for which that recognition is immediate.

The Beta family

Definition 1 (Beta distribution). For shape parameters a,b>0a, b > 0, θ∼Beta(a,b)\theta \sim \mathrm{Beta}(a, b) has density on [0,1][0, 1] f(θ)=1B(a,b)θa−1(1−θ)b−1,B(a,b)=∫01ϑa−1(1−ϑ)b−1dϑ,\begin{equation} f(\theta) = \frac{1}{B(a, b)}\, \theta^{\,a-1}(1 - \theta)^{\,b-1}, \qquad B(a, b) = \int_0^1 \vartheta^{\,a-1}(1 - \vartheta)^{\,b-1}\, d\vartheta, \end{equation} with mean 𝔼[θ]=a/(a+b)\mathbb{E}[\theta] = a/(a + b). The constant B(a,b)B(a,b) is whatever it must be to integrate to one; we will almost never need its value.

The two parameters trade off shape and location. Figure 1 shows the range: Beta(1,1)\mathrm{Beta}(1,1) is flat (the uniform prior, “I know nothing”); equal large parameters concentrate the mass near 1/21/2; unequal parameters skew toward the larger one. Read aa as “how much the prior leans toward success” and bb as “toward failure.”

The Beta family spans priors from flat ignorance to sharp belief; the mean sits at a/(a+b)a/(a+b).

Conjugacy: updating is counting

Now feed Bernoulli data through [eq:densitybayes]. Suppose each impression of an ad is clicked independently with probability θ\theta, and we observe ss clicks (successes) in nn impressions.

Proposition 1 (Beta–Bernoulli conjugacy). If the prior is θ∼Beta(a,b)\theta \sim \mathrm{Beta}(a, b) and the data are ss successes in nn independent Bernoulli(θ)(\theta) trials, then the posterior is θ|data∼Beta(a+s,b+n−s).\begin{equation} \theta \,|\,\text{data} \;\sim\; \mathrm{Beta}(a + s,\ b + n - s). \end{equation}

Proof. The likelihood of ss successes and n−sn - s failures is L(data|θ)=θs(1−θ)n−sL(\text{data} \,|\,\theta) = \theta^{\,s}(1 - \theta)^{\,n - s} (the binomial coefficient does not depend on θ\theta, so ∝\propto-then-normalize discards it). Multiply by the prior and collect exponents: f(θ|data)∝θa−1(1−θ)b−1⏟prior⋅θs(1−θ)n−s⏟likelihood=θ(a+s)−1(1−θ)(b+n−s)−1.\begin{equation} f(\theta \,|\,\text{data}) \;\propto\; \underbrace{\theta^{\,a-1}(1-\theta)^{\,b-1}}_{\text{prior}} \cdot \underbrace{\theta^{\,s}(1-\theta)^{\,n-s}}_{\text{likelihood}} \;=\; \theta^{\,(a+s)-1}(1 - \theta)^{\,(b+n-s)-1}. \end{equation} That is the shape of a Beta(a+s,b+n−s)\mathrm{Beta}(a+s,\ b+n-s) density, and the normalizing constant is forced. No integral was evaluated. ◻

This is remarkable and worth naming: the posterior lands back in the prior’s own family (the family is conjugate to Bernoulli data), so the entire effect of nn observations is to advance two counters, a↦a+sa \mapsto a + s and b↦b+(n−s)b \mapsto b + (n - s). Learning has become bookkeeping. Whether the nn observations arrive all at once or one at a time makes no difference: incrementing the counters after each trial gives the same posterior, which is exactly the sequential updating a live decision-maker needs.

The posterior mean is a data–prior compromise

The posterior mean is the single number we will hand to a decision, so it is worth dissecting.

Proposition 2 (Shrinkage form of the posterior mean). With posterior Beta(a+s,b+n−s)\mathrm{Beta}(a + s,\ b + n - s), 𝔼[θ|data]=w⋅sn+(1−w)⋅aa+b,w=nn+a+b.\begin{equation} \mathbb{E}[\theta \,|\,\text{data}] = w \cdot \frac{s}{n} + (1 - w) \cdot \frac{a}{a + b}, \qquad w = \frac{n}{n + a + b}. \label{eq:shrinkage} \end{equation}

Proof. The Beta mean gives 𝔼[θ|data]=(a+s)/(a+b+n)\mathbb{E}[\theta \,|\,\text{data}] = (a + s)/(a + b + n). Split the numerator and multiply each piece by one: a+sa+b+n=na+b+n⋅sn+a+ba+b+n⋅aa+b,\begin{equation} \frac{a + s}{a + b + n} = \frac{n}{a + b + n}\cdot\frac{s}{n} + \frac{a + b}{a + b + n}\cdot\frac{a}{a + b}, \end{equation} which is [eq:shrinkage] with w=n/(n+a+b)w = n/(n + a + b) and 1−w=(a+b)/(n+a+b)1 - w = (a + b)/(n + a + b). ◻

Read [eq:shrinkage] as a weighted average of two estimates: the raw data frequency s/ns/n and the prior mean a/(a+b)a/(a + b). The weight ww on the data climbs from 00 (no data, pure prior) toward 11 as nn grows, so with enough evidence the prior washes out. Equivalently, aa and bb act as pseudo-counts: the prior behaves exactly as if we had already seen aa imagined successes and bb imagined failures before the real trials began, and the posterior mean is just the overall success fraction including those imagined trials. A concrete instance is worked in the next section.

How wide is the posterior?

The mean says where belief sits. Every bandit strategy below also needs to know how spread out it is, because spread is what is left to learn.

Proposition 3 (Beta variance). If θ∼Beta(a,b)\theta \sim \mathrm{Beta}(a, b) with mean m=a/(a+b)m = a/(a + b), then Var(θ)=m(1−m)a+b+1.\begin{equation} \mathrm{Var}(\theta) = \frac{m(1 - m)}{a + b + 1}. \label{eq:betavar} \end{equation}

Proof. The mean in the definition is a ratio of normalizing constants: 𝔼[θ]=1B(a,b)∫01θ⋅θa−1(1−θ)b−1dθ=B(a+1,b)/B(a,b)\mathbb{E}[\theta] = \frac{1}{B(a,b)}\int_0^1 \theta \cdot \theta^{\,a-1}(1-\theta)^{\,b-1}\, d\theta = B(a + 1, b)/B(a, b), so B(a+1,b)/B(a,b)=a/(a+b)B(a + 1, b)/B(a, b) = a/(a + b) for every a,b>0a, b > 0. Use that identity twice: 𝔼[θ2]=B(a+2,b)B(a,b)=B(a+2,b)B(a+1,b)⋅B(a+1,b)B(a,b)=a+1a+b+1⋅aa+b.\begin{equation} \mathbb{E}[\theta^2] = \frac{B(a + 2, b)}{B(a, b)} = \frac{B(a + 2, b)}{B(a + 1, b)}\cdot\frac{B(a + 1, b)}{B(a, b)} = \frac{a + 1}{a + b + 1}\cdot\frac{a}{a + b}. \end{equation} Then Var(θ)=𝔼[θ2]−m2=m(a+1a+b+1−aa+b)=m⋅b(a+b)(a+b+1)\mathrm{Var}(\theta) = \mathbb{E}[\theta^2] - m^2 = m\left(\frac{a + 1}{a + b + 1} - \frac{a}{a + b}\right) = m \cdot \frac{b}{(a + b)(a + b + 1)}, which is [eq:betavar] because b/(a+b)=1−mb/(a + b) = 1 - m. ◻

After nn trials the posterior has a+b+1=n+(prior pseudo-counts)+1a + b + 1 = n + (\text{prior pseudo-counts}) + 1 in the denominator, so for large nn the posterior standard deviation is about θ̂(1−θ̂)/n\sqrt{\hat\theta(1 - \hat\theta)/n}, with θ̂=s/n\hat\theta = s/n. Uncertainty shrinks like 1/n1/\sqrt{n}: four times the data buys half the width.

This is the promised price of realistic click rates. To pin a 2%2\% click rate to within ±0.002\pm 0.002 (ten percent of its value) takes n≈0.02×0.98/0.0022=4,900n \approx 0.02 \times 0.98 / 0.002^2 = 4{,}900 impressions per ad, while a rate near 1/21/2 reaches the same relative precision in about 100100. Small rates are expensive to learn, which is exactly why wasted impressions matter to an ad platform.

Two things to carry forward. First, width is the fuel of exploration; each algorithm below spends it differently. Second, from Session 12 on, posterior variance becomes a main character, and in the Gaussian world of Session 13 it will turn out not to depend on the observed values at all. Here it does, through mm.

AI corner: calibration is a conditional probability. Corner 2 was about how a model speaks, how it draws a token from a distribution; this one is about whether to believe it. A predictor is calibrated if ℙ(correct|it reports confidence c)=c,\begin{equation*} \mathbb{P}(\text{correct} \,|\,\text{it reports confidence } c) = c, \end{equation*} a conditional probability, nothing more. Checking calibration is estimating a Bernoulli success rate within each confidence bucket, exactly this session’s machinery pointed at the model itself: collect all the claims it stamped “80%80\% sure,” count how many were right, and form the Beta posterior over that bucket’s true accuracy. Two honest caveats. First, a language model’s confidence score is not a posterior and is routinely miscalibrated, so treating “90%90\% sure” as 0.90.9 is a modeling decision you must audit, not a given. Second, even a perfectly calibrated flag obeys Session 2: when the base rate is extreme, the posterior can stay small no matter how confident the flag sounds.

Bandits, regret, and greedy

Return to the ads, now KK of them. In bandit language each ad is an arm, showing it is a pull, and a click is a reward: arm kk pays Bernoulli(θk)\mathrm{Bernoulli}(\theta_k) when pulled, the θk\theta_k are unknown, and each round (one impression) we pull one arm and see its reward. We measure a strategy by its regret: the shortfall against an oracle that always pulls the best arm, Regret(T)=∑t=1T(maxkθk−θat),\begin{equation} \mathrm{Regret}(T) = \sum_{t=1}^{T}\left(\max_k \theta_k - \theta_{a_t}\right), \end{equation} where ata_t is the arm pulled at round tt. Low regret means we found and stuck with the best arm quickly.

The tempting strategy, greedy, pulls each arm a few times and thereafter always pulls the one with the best empirical mean. It has a fatal failure mode: an early unlucky streak on the genuinely best arm can leave its empirical mean below a worse arm’s, after which greedy never pulls it again and so never corrects the verdict. Greedy commits before it has looked, and one bad break is permanent. That story can be made a theorem with nothing beyond Sessions 3 and 9.

Proposition 4 (Greedy can lock in forever). Take two arms with θ1<θ2\theta_1 < \theta_2 and gap Δ=θ2−θ1\Delta = \theta_2 - \theta_1. Greedy pulls each arm mm times, then at every later round pulls the arm with the larger empirical mean (ties broken any way at all). Then for every T≥2mT \ge 2m, 𝔼[Regret(T)]≥(1−θ2)m[1−(1−θ1)m]⋅Δ⋅(T−2m).\begin{equation} \mathbb{E}[\mathrm{Regret}(T)] \;\ge\; (1 - \theta_2)^m\,\bigl[1 - (1 - \theta_1)^m\bigr]\cdot \Delta \cdot (T - 2m). \label{eq:greedylock} \end{equation} In particular, for every fixed mm the expected regret grows linearly in TT.

Proof. Let EE be the event that all mm forced pulls of the better arm 2 fail while at least one forced pull of arm 1 succeeds. On EE, after the forced phase, arm 2’s empirical mean is 00 and arm 1’s is positive. Arm 1’s empirical mean S1/N1S_1/N_1 can never return to 00, because its success count S1≥1S_1 \ge 1 never decreases; arm 2’s stays at 00, because it is never pulled again. So on EE greedy pulls arm 1 at every round after 2m2m and pays Δ\Delta each time, which gives Regret(T)≥Δ(T−2m)\mathrm{Regret}(T) \ge \Delta(T - 2m) on every outcome in EE, and hence 𝔼[Regret(T)|E]≥Δ(T−2m)\mathbb{E}[\mathrm{Regret}(T) \,|\,E] \ge \Delta(T - 2m).

Now split the expectation over EE and E𝖼E^{\mathsf{c}} (the tower property of Session 9, conditioning on whether EE occurred): 𝔼[Regret(T)]=𝔼[Regret(T)|E]ℙ(E)+𝔼[Regret(T)|E𝖼]ℙ(E𝖼).\begin{equation} \mathbb{E}[\mathrm{Regret}(T)] = \mathbb{E}[\mathrm{Regret}(T) \,|\,E]\,\mathbb{P}(E) + \mathbb{E}[\mathrm{Regret}(T) \,|\,E^{\mathsf{c}}]\,\mathbb{P}(E^{\mathsf{c}}). \end{equation} Every term max⁡kθk−θat\max_k \theta_k - \theta_{a_t} in the regret is nonnegative, so Regret(T)≥0\mathrm{Regret}(T) \ge 0 and the second product is nonnegative. Dropping it, 𝔼[Regret(T)]≥𝔼[Regret(T)|E]ℙ(E)≥Δ(T−2m)ℙ(E).\begin{equation} \mathbb{E}[\mathrm{Regret}(T)] \;\ge\; \mathbb{E}[\mathrm{Regret}(T) \,|\,E]\,\mathbb{P}(E) \;\ge\; \Delta(T - 2m)\,\mathbb{P}(E). \end{equation} Finally, ℙ(E)=(1−θ2)m[1−(1−θ1)m]\mathbb{P}(E) = (1 - \theta_2)^m [1 - (1 - \theta_1)^m] because the pulls are independent (Session 3): the first factor is the chance that arm 2’s mm pulls all fail, the second that arm 1’s mm pulls do not all fail. ◻

The bound is crude on purpose. It counts only the most extreme lock-in, an all-failure start on the good arm, so its constant is small. For the demo arms θ=(0.5,0.7)\theta = (0.5, 0.7) below with a single forced pull each, ℙ(E)=0.3×0.5=0.15\mathbb{P}(E) = 0.3 \times 0.5 = 0.15 and 𝔼[Regret(T)]≥0.03(T−2)\mathbb{E}[\mathrm{Regret}(T)] \ge 0.03\,(T - 2), about 1515 by round 500500; with m=10m = 10 the constant drops to 0.310(1−0.510)≈6×10−60.3^{10}(1 - 0.5^{10}) \approx 6 \times 10^{-6}. Milder lock-ins (arm 2 starting merely below arm 1, not at zero) are far more common and are ignored here, so the real damage is much larger, as the demo will show. The proposition’s job is the shape, not the constant: more forced pulls shrink the slope but never make it zero. No fixed amount of up-front exploration cures greedy.

Thompson sampling

Thompson sampling fixes this using only what this session already built.

Algorithm: Thompson sampling. Keep a Beta\mathrm{Beta} posterior on each arm’s θk\theta_k, starting from Beta(1,1)\mathrm{Beta}(1,1). Each round:

  1. draw one sample θ̂k∼Beta(ak,bk)\hat{\theta}_k \sim \mathrm{Beta}(a_k, b_k) from every arm’s current posterior;

  2. pull the arm with the largest sample, at=arg max⁡kθ̂ka_t = \operatorname*{arg\,max}_k \hat{\theta}_k;

  3. observe the reward and advance that arm’s counters (a success adds one to aka_k, a failure adds one to bkb_k); leave the other arms untouched.

Key idea. The algorithm’s entire intelligence is “sample from the posterior, then act as if the sample were the truth.” Exploration is not bolted on; it emerges from posterior width. An arm we know little about has a wide posterior, so its sample is sometimes high and it gets tried; as evidence accumulates the posterior narrows and the samples stop straying, so exploration dies exactly as fast as the uncertainty that justified it.

Posterior →\to Action. Thompson sampling is a posterior handed straight to an action, once per round, with one intervening random draw. The draw in step 1 is where belief becomes decision: had we passed the posterior mean to the arg max\operatorname*{arg\,max} instead of a sample, the algorithm would be greedy in disguise (see the pitfall below). The single sample is what buys exploration.

Demo: two ads, 500 impressions

Figure 2 runs the two-arm case with θ=(0.5,0.7)\theta = (0.5, 0.7) for 500500 rounds, averaging cumulative regret over 100100 seeds. Greedy (after ten forced pulls of each arm) accumulates regret roughly twice that of Thompson sampling, because on the seeds where it locks onto the weaker arm it pays the gap forever. The right panel is a film-strip of the better arm’s posterior at rounds 1010, 100100, and 500500: a wide Beta\mathrm{Beta} early, tightening onto the true θ2=0.7\theta_2 = 0.7 as the counters grow. Watching that posterior narrow is watching exploration switch itself off.

Thompson sampling accrues about half the regret of greedy; the winning arm’s posterior narrows onto its true rate, and exploration fades with it.

Pitfall. Do not replace the posterior sample in Thompson’s step 1 with the posterior mean. Acting on the mean is greedy in disguise: it discards the width that was the whole source of exploration, and it can lock onto a wrong arm after an unlucky start, exactly the failure [eq:greedylock] quantifies and Thompson was built to avoid. The randomness is the feature, not a nuisance to average away.

Why Thompson cannot lock in: probability matching

Run Thompson sampling on KK arms with independent Beta\mathrm{Beta} priors fkpriorf_k^{\mathrm{prior}}, and let HtH_t be the history before round tt: which arms were pulled and whether each impression was clicked. We first need one fact about the posterior: given HtH_t, the arms’ rates are independent, each with its own Beta\mathrm{Beta} posterior. Write the likelihood of the history one round at a time. Round ss contributes two factors: the probability of choosing arm asa_s, and the probability of the observed reward given θas\theta_{a_s}. The first depends only on earlier data and the algorithm’s own random draws, never on θ\theta directly, so it drops out under ∝\propto-then-normalize. The second involves only the pulled arm’s rate. Grouping the surviving factors by arm, with SkS_k clicks and FkF_k non-clicks on arm kk so far, f(θ1,…,θK|Ht)∝∏k=1Kfkprior(θk)⏟prior⋅θkSk(1−θk)Fk⏟arm k’s data.\begin{equation} f(\theta_1, \dots, \theta_K \,|\,H_t) \;\propto\; \prod_{k=1}^{K} \underbrace{f_k^{\mathrm{prior}}(\theta_k)}_{\text{prior}} \cdot \underbrace{\theta_k^{\,S_k}(1 - \theta_k)^{\,F_k}}_{\text{arm $k$'s data}}. \end{equation} By conjugacy each factor is the shape of a Beta\mathrm{Beta} density whose parameters are the prior’s plus (Sk,Fk)(S_k, F_k), which are exactly the counters (ak,bk)(a_k, b_k) that Thompson’s algorithm box maintains. So the posterior is a product of one Beta(ak,bk)\mathrm{Beta}(a_k, b_k) per arm. A joint density that factors this way is a joint density of independent variables. This is Session 3’s conditional independence, now given the data.

Proposition 5 (Probability matching). Under Thompson sampling, for every arm kk and round tt, ℙ(at=k|Ht)=ℙ(θk=maxjθj|Ht).\begin{equation} \mathbb{P}(a_t = k \,|\,H_t) = \mathbb{P}\left(\theta_k = \max_j \theta_j \,|\,H_t\right). \label{eq:probmatch} \end{equation} Each arm is pulled with exactly the posterior probability that it is the best arm.

Proof. Two different sources of randomness appear in [eq:probmatch], and the whole proof is noticing that they have the same distribution. On the right, the random object is the true rate vector θ\theta, random because we are uncertain about it, distributed according to the posterior. On the left, the random object is the algorithm’s own draw θ̂\hat\theta, random because Thompson flips coins.

Start with two arms, and write the posteriors as θ1∼Beta(a1,b1)\theta_1 \sim \mathrm{Beta}(a_1, b_1) and θ2∼Beta(a2,b2)\theta_2 \sim \mathrm{Beta}(a_2, b_2), independent given HtH_t by the factorization above.

Two pairs of independent variables with the same marginals have the same joint density (the product of the marginals), and two random vectors with the same joint density give every event the same probability. Both sides therefore equal the same integral, obtained by conditioning on the first coordinate: ℙ(θ̂2>θ̂1|Ht)=ℙ(θ2>θ1|Ht)=∫01f1(x)[1−F2(x)]dx,\begin{equation} \mathbb{P}(\hat\theta_2 > \hat\theta_1 \,|\,H_t) = \mathbb{P}(\theta_2 > \theta_1 \,|\,H_t) = \int_0^1 f_1(x)\,\bigl[1 - F_2(x)\bigr]\, dx, \end{equation} with f1f_1 the density of Beta(a1,b1)\mathrm{Beta}(a_1, b_1) and F2F_2 the CDF of Beta(a2,b2)\mathrm{Beta}(a_2, b_2).

For KK arms nothing changes except the event: “θ2>θ1\theta_2 > \theta_1” becomes “coordinate kk is the largest.” The draw vector θ̂\hat\theta and the true vector θ\theta (given HtH_t) are both KK independent variables with the same KK Beta\mathrm{Beta} marginals, so they have the same joint density and give that event the same probability. Ties have probability zero for continuous draws, so “the largest” is well defined. ◻

In one sentence: Thompson draws one plausible world from your belief and acts on the best arm in that world, and the share of plausible worlds in which arm kk is best is, by definition, the posterior probability that arm kk is best. The worked example of Section 3.3 is this proof with numbers.

Greedy, by contrast, gives probability 11 to the arm with the largest posterior mean and probability 00 to every other arm, including one that is 30%30\% likely to be the best. Thompson pays that arm 30%30\% of its attention.

Probability matching also explains why the lock-in of [eq:greedylock] cannot happen. Suppose Thompson stopped pulling arm 2 forever. Arm 2’s posterior would freeze at some Beta(a2,b2)\mathrm{Beta}(a_2, b_2), which has positive density all the way up to 11, while arm 1’s posterior narrows onto θ1<1\theta_1 < 1. The posterior probability that arm 2 is best would then settle near ℙ(Beta(a2,b2)>θ1)>0\mathbb{P}(\mathrm{Beta}(a_2, b_2) > \theta_1) > 0, and by [eq:probmatch] arm 2 would keep being pulled with at least that probability every round, contradicting the supposition. (Making this airtight takes more limit theory than we have; the mechanism is the point.) It is Session 2’s failed search in another costume: an arm you stop looking at keeps its uncertainty, and an uncertain arm keeps getting looked at.

How well does Thompson sampling do? Two results from the research literature, stated for reference and not proved here:

So greedy’s regret grows like TT and Thompson’s like log⁡T\log T. Over a million rounds, log⁡T≈13.8\log T \approx 13.8: one strategy keeps paying for an early mistake on a fixed fraction of all rounds, and the other pays for a number of experiments that grows only with the number of digits in TT.

The best possible strategy: Gittins’ index

Thompson sampling is good. Is anything best? To ask that we must say best at what, and the classical answer (Bellman, 1956; Gittins and Jones, 1974) changes the objective in one honest way: instead of regret, maximize the discounted total reward under the prior, 𝔼[∑t=1∞γt−1rt],0<γ<1,\begin{equation} \mathbb{E}\Bigl[\sum_{t=1}^{\infty} \gamma^{\,t-1} r_t\Bigr], \qquad 0 < \gamma < 1, \label{eq:discounted} \end{equation} where the expectation is over both the prior on the θk\theta_k and the rewards. The discount has a reading this course already owns. Suppose play stops after a random number of rounds H∼Geometric(1−γ)H \sim \mathrm{Geometric}(1 - \gamma), independent of everything else, so that ℙ(H≥t)=γt−1\mathbb{P}(H \ge t) = \gamma^{\,t-1}. Writing the total as ∑trt𝟙{H≥t}\sum_t r_t \mathbb{1}\left\{H \ge t\right\} and taking expectations term by term (the indicator method) gives exactly [eq:discounted]. Discounting is just an uncertain horizon (a campaign that might end after any impression) with mean 1/(1−γ)1/(1 - \gamma): 1010 rounds at γ=0.9\gamma = 0.9, 100100 at γ=0.99\gamma = 0.99.

Each arm’s state is its counter pair (ak,bk)(a_k, b_k). Pulling arm kk moves it to (ak+1,bk)(a_k + 1, b_k) with probability ak/(ak+bk)a_k/(a_k + b_k) and to (ak,bk+1)(a_k, b_k + 1) otherwise, because the predictive probability of a success is the posterior mean, ℙ(r=1|data)=𝔼[θ|data]\mathbb{P}(r = 1 \,|\,\text{data}) = \mathbb{E}[\theta \,|\,\text{data}] (the tower property of Session 9). Arms that are not pulled do not move. So the optimal strategy can in principle be computed by backward induction (Bellman’s dynamic programming): Session 3’s expected utility, applied recursively to every posterior the future might hold. The catch is that the state is the counters of all KK arms jointly, and the number of joint states grows exponentially in KK.

Theorem 1 (Gittins index theorem; Gittins and Jones, 1974; Gittins, 1979). For each arm there is a number G(a,b)G(a, b), depending only on that arm’s own posterior Beta(a,b)\mathrm{Beta}(a, b) and on γ\gamma, such that the strategy maximizing [eq:discounted] is: at every round, pull the arm with the largest G(ak,bk)G(a_k, b_k).

The coupled KK-arm planning problem falls apart into KK one-arm problems, a genuinely surprising fact whose proofs are beyond this course (the shortest are Whittle, 1980, and Weber, 1992; Mahajan and Teneketzis, 2008, survey the theory for sensor management). What we can do is say what GG means. Take one arm in state (a,b)(a, b) and offer it a retirement option: at any round you may stop for good and collect a fixed λ\lambda per round forever, worth λ/(1−γ)\lambda/(1 - \gamma). In ad terms: drop the ad for good in favor of a guaranteed sponsor who pays λ\lambda per impression. The index G(a,b)G(a, b) is the value of λ\lambda at which you are indifferent, at the start, between retiring now and pulling (keeping the option to retire later). It is the arm’s fair per-round price.

Proposition 6 (The index is at least the mean). G(a,b)≥a/(a+b)G(a, b) \ge a/(a + b).

Proof. Let m=a/(a+b)m = a/(a + b) and suppose λ<m\lambda < m. Retiring now is worth λ/(1−γ)\lambda/(1 - \gamma). Pulling once and then retiring is worth m+γλ/(1−γ)m + \gamma\lambda/(1 - \gamma), and the difference is m+γλ/(1−γ)−λ/(1−γ)=m−λ>0m + \gamma\lambda/(1 - \gamma) - \lambda/(1 - \gamma) = m - \lambda > 0. So for every λ<m\lambda < m some way of pulling strictly beats retiring, you are not indifferent, and the indifference point satisfies G≥mG \ge m. ◻

The gap G(a,b)−mG(a, b) - m is an exploration bonus, and it has a name from Session 7: it is the value of information. A pull does not just pay mm on average; it also reveals something about θ\theta, and you may act on what it reveals (retire after bad news, keep going after good news). The pull is a measurement that happens to pay. Three consequences, all visible in Figure 3:

Computing GG for a Beta\mathrm{Beta} arm is cheap: backward induction over one arm’s counter grid, with bisection on λ\lambda, takes a few lines of code and well under a second.

The Gittins index is the posterior mean plus a bonus that shrinks as the arm is learned and grows with the horizon. Mean plus one posterior standard deviation, a UCB-style score, has the same shape.

Pitfall. Gittins’ theorem is exact but fragile. It needs the discounted objective [eq:discounted], arms that stay frozen when not pulled, and no cost for switching between arms. Change any one (a fixed finite horizon, ads whose appeal drifts with the news cycle even while you are not showing them, a cost to switch) and the index strategy is in general no longer optimal. It also optimizes reward averaged over your prior, not regret on the true θ\theta. This is why practice usually runs Thompson sampling, which needs none of these assumptions.

One family: index strategies

Every strategy in this session has the same skeleton: compute a score for each arm from that arm’s own posterior, then pull the arg max\operatorname*{arg\,max}. They differ only in the score, which is to say in how they price uncertainty.

Table 1: Strategy; Score of arm kk; Exploration comes from; Guarantee
Strategy Score of arm kk Exploration comes from Guarantee
Greedy posterior mean mkm_k nowhere regret linear in TT [eq:greedylock]
UCB mk+β⋅sdkm_k + \beta \cdot \mathrm{sd}_k a bonus proportional to the posterior width regret ∝log⁡T\propto \log T
Thompson a random draw θ̂k∼Beta(ak,bk)\hat\theta_k \sim \mathrm{Beta}(a_k, b_k) random excursions as wide as the posterior regret ∝log⁡T\propto \log T, best constant
Gittins G(ak,bk)G(a_k, b_k) the exact value-of-information bonus optimal for discounted reward

The one new row is UCB (upper confidence bound), whose motto is optimism under uncertainty: score each arm by how good it could plausibly be. An arm we know little about has a large posterior standard deviation sdk\mathrm{sd}_k from [eq:betavar], so its optimistic score is high and it gets tried; every pull shrinks sdk\mathrm{sd}_k like 1/n1/\sqrt{n}, and the arm is abandoned once even its optimistic score falls below a rival’s. The classical version (Auer, Cesa-Bianchi, and Fischer, 2002) uses the empirical mean plus 2log⁡t/Nk\sqrt{2 \log t / N_k}, with NkN_k the number of pulls of arm kk so far: the same 1/n1/\sqrt{n} shape. Its log⁡T\log T regret guarantee needs a concentration inequality beyond this course. Figure 3 suggests one way to read the family: Gittins computes the bonus that UCB guesses, and Thompson draws it at random.

Keep the middle column in mind. From Session 12 on, the posterior variance becomes a main character, and Session 18 meets a rule whose score is the width alone: when the goal is learning rather than reward, the mean drops out.

Worked examples

One ad, updated

Before any data, we mildly doubt a new ad: prior θ∼Beta(2,3)\theta \sim \mathrm{Beta}(2, 3) (mean 2/5=0.42/5 = 0.4, leaning toward “not clicked”). We then observe s=8s = 8 clicks in n=10n = 10 impressions. By conjugacy the posterior is Beta(2+8,3+10−8)=Beta(10,5),𝔼[θ|data]=1015=23≈0.667.\begin{equation} \mathrm{Beta}(2 + 8,\ 3 + 10 - 8) = \mathrm{Beta}(10, 5), \qquad \mathbb{E}[\theta \,|\,\text{data}] = \frac{10}{15} = \frac{2}{3} \approx 0.667. \end{equation} The shrinkage decomposition [eq:shrinkage] explains where 2/32/3 comes from. The data weight is w=nn+a+b=1010+5=23,\begin{equation} w = \frac{n}{n + a + b} = \frac{10}{10 + 5} = \frac{2}{3}, \end{equation} so 𝔼[θ|data]=23⋅810⏟data 0.8+13⋅25⏟prior 0.4=23,\begin{equation} \mathbb{E}[\theta \,|\,\text{data}] = \tfrac{2}{3}\cdot\underbrace{\tfrac{8}{10}}_{\text{data } 0.8} + \tfrac{1}{3}\cdot\underbrace{\tfrac{2}{5}}_{\text{prior } 0.4} = \tfrac{2}{3}, \end{equation} the posterior mean sitting between the data frequency 0.80.8 and the prior mean 0.40.4, pulled two-thirds of the way toward the data because n=10n = 10 already outweighs the a+b=5a + b = 5 pseudo-counts of prior conviction.

Because the posterior is a full distribution, we can answer a decision-relevant question directly: is the ad’s click rate more likely than not above one half? With θ|data∼Beta(10,5)\theta \,|\,\text{data} \sim \mathrm{Beta}(10, 5), ℙ(θ>0.5|data)=∫0.51f(θ|data)dθ≈0.91\begin{equation} \mathbb{P}(\theta > 0.5 \,|\,\text{data}) = \int_{0.5}^{1} f(\theta \,|\,\text{data})\, d\theta \approx 0.91 \end{equation} (one line of scipy: beta.sf(0.5, 10, 5)). We would keep the ad, and we can say how sure we are, which is more than a point estimate can do.

How sure, in one number? By [eq:betavar] the posterior standard deviation is (2/3)(1/3)/16=1/72≈0.118\sqrt{(2/3)(1/3)/16} = \sqrt{1/72} \approx 0.118. Had we shown the ad 4040 times with the same click frequency (s=32s = 32), the posterior would be Beta(34,11)\mathrm{Beta}(34, 11) with standard deviation about 0.0630.063: four times the data, about half the width.

A strong prior is not harmless

Suppose instead we had begun overconfident that the ad was excellent: Beta(20,5)\mathrm{Beta}(20, 5) (mean 0.80.8, worth 2525 pseudo-trials of conviction). If the ad is in fact mediocre and we observe s=3s = 3 clicks in n=10n = 10 (frequency 0.30.3), the posterior is Beta(23,12)\mathrm{Beta}(23, 12) with mean 23/35≈0.6623/35 \approx 0.66, still far above the data, because ten trials cannot outvote twenty-five imagined ones. Had we started from the flat Beta(1,1)\mathrm{Beta}(1,1), the same data would give Beta(4,8)\mathrm{Beta}(4, 8) with mean 1/3≈0.331/3 \approx 0.33, right at the evidence. The lesson is in the pitfall.

Pitfall. A Beta prior is not a harmless formality. Its parameters are pseudo-counts, so a confident wrong prior (large a+ba + b pointed the wrong way) needs a comparably large amount of data to overcome, and until then it visibly biases every estimate and decision. Choose a+ba + b to honestly reflect how much you know, and treat a strong prior as a claim you must be prepared to defend.

Greedy says never; Thompson says 28%28\%

Two ads start from flat priors. Ad 1 has been clicked on 55 of 77 impressions, ad 2 on 11 of 22. By conjugacy the posteriors are θ1∼Beta(6,3)\theta_1 \sim \mathrm{Beta}(6, 3) (mean 2/32/3) and θ2∼Beta(2,2)\theta_2 \sim \mathrm{Beta}(2, 2) (mean 1/21/2), independent given the data. Greedy compares means and shows ad 1 next, with probability 11. By [eq:probmatch], Thompson sampling shows ad 2 with probability ℙ(θ2>θ1|data)\mathbb{P}(\theta_2 > \theta_1 \,|\,\text{data}). To compute it, condition on θ1\theta_1 (Session 9’s tower property, ℙ(A)=𝔼[ℙ(A|θ1)]\mathbb{P}(A) = \mathbb{E}[\mathbb{P}(A \,|\,\theta_1)]) and use independence: ℙ(θ2>θ1|data)=∫01f1(x)ℙ(θ2>x|data)dx=∫01f1(x)[1−F2(x)]dx≈0.279,\begin{equation} \mathbb{P}(\theta_2 > \theta_1 \,|\,\text{data}) = \int_0^1 f_1(x)\, \mathbb{P}(\theta_2 > x \,|\,\text{data})\, dx = \int_0^1 f_1(x)\, \bigl[1 - F_2(x)\bigr]\, dx \approx 0.279, \end{equation} with f1f_1 the Beta(6,3)\mathrm{Beta}(6,3) density and F2F_2 the Beta(2,2)\mathrm{Beta}(2,2) CDF (one numerical integral; a Monte Carlo check with a million paired draws agrees to three decimals). This is the integral from the proof of [eq:probmatch]: it is at once the posterior probability that ad 2 is better and the probability that Thompson’s two draws put ad 2 on top. Ad 2 has been shown only twice; the data cannot rule out that it is the better ad, and Thompson spends about 28%28\% of the next impression on that doubt. Greedy spends nothing on it, today or ever, unless ad 1 stumbles.

One look is worth a lot: bounding a Gittins index

A brand-new ad has a flat prior, Beta(1,1)\mathrm{Beta}(1, 1), so its mean is 1/21/2, and γ=0.9\gamma = 0.9. How large is its Gittins index? We do not need the optimal strategy to get a lower bound; any particular way of playing that beats retiring will do. Try the simplest strategy that uses information: show the ad once; after a click, keep showing it forever; after no click, retire it in favor of the sponsor.

Build its value. The first pull pays 1/21/2 on average. With probability 1/21/2 it succeeds, the posterior becomes Beta(2,1)\mathrm{Beta}(2, 1), and every later pull pays 𝔼[θ|success]=2/3\mathbb{E}[\theta \,|\,\text{success}] = 2/3 on average (tower property), worth γ⋅2/31−γ\gamma \cdot \frac{2/3}{1 - \gamma} from round 2 on. With probability 1/21/2 it fails and we retire from round 2 on, worth γλ/(1−γ)\gamma\lambda/(1 - \gamma). Indifference with retiring immediately means λ1−γ=(round-1 reward)+γ⋅(expected value of rounds 2,3,…)=12+γ[12⋅2/31−γ+12⋅λ1−γ]⇔λ=(1−γ)/2+γ/31−γ/2.\begin{equation} \begin{aligned} \frac{\lambda}{1 - \gamma} &= (\text{round-1 reward}) + \gamma \cdot (\text{expected value of rounds } 2, 3, \dots)\\ &= \frac12 + \gamma\Bigl[\frac12\cdot\frac{2/3}{1 - \gamma} + \frac12\cdot\frac{\lambda}{1 - \gamma}\Bigr] \quad\Longleftrightarrow\quad \lambda = \frac{(1 - \gamma)/2 + \gamma/3}{1 - \gamma/2}. \end{aligned} \end{equation} At γ=0.9\gamma = 0.9 this is λ=0.35/0.55=7/11≈0.636\lambda = 0.35/0.55 = 7/11 \approx 0.636. For any smaller λ\lambda this strategy beats retiring, so G(1,1)≥7/11G(1, 1) \ge 7/11. (Sanity check: at γ=0\gamma = 0 the formula returns 1/21/2, the mean. With no future, the look is worthless.)

The exact index, by computer, is G(1,1)≈0.703G(1, 1) \approx 0.703. An untested ad that pays 0.50.5 per impression on average is worth keeping over a sponsor paying up to 0.700.70 per impression, and a single look already accounts for about two-thirds of that 0.200.20 premium (0.1360.136 of 0.2030.203). The structure is Session 7’s buy-a-measurement problem: the first pull is the measurement, and its value is that you get to decide after seeing it.

Consolidation

  1. (Mechanical.) An ad starts from prior Beta(3,3)\mathrm{Beta}(3, 3) and is clicked on 66 of 88 impressions. Give the posterior, its mean, and its standard deviation. Roughly how many more impressions, at the same click frequency, would halve the standard deviation? (Answers: Beta(9,5)\mathrm{Beta}(9, 5); mean 9/14≈0.6439/14 \approx 0.643; variance 3/1963/196, standard deviation ≈0.124\approx 0.124. Halving needs a+b+1a + b + 1 to grow about fourfold, from 1515 to 6060: roughly 4545 more impressions.)

  2. (Conceptual.) A colleague accepts [eq:greedylock] but proposes greedy with m=50m = 50 forced pulls per arm “to be safe.” What does the proposition still say about the regret as T→∞T \to \infty, and what new cost has the colleague paid for certain? (Answer: the slope is tiny but positive, so regret is still linear in TT; and the forced phase alone costs 50Δ50\Delta of regret on every run, needed or not. Fixed up-front exploration trades one waste for another.)

  3. (Conceptual.) Two arms have the same posterior mean, one after 44 pulls and one after 400400. Which has the larger Gittins index, and why? How does your answer change as γ→0\gamma \to 0? (Answer: the 4-pull arm, whose wider posterior leaves more to learn, so its value-of-information bonus is larger. As γ→0\gamma \to 0 both indices fall to the common mean and the strategies tie: with no future, learning is worth nothing.)

Synthesis

A probability model answers three questions: what is uncertain, what is observed, and how should observation change belief and action? This session pushed all three into the hardest case yet, where the uncertain thing is itself a probability. The answer was the same Bayes’ rule, now producing a density. The Beta family made the update two counters, and its variance made the remaining doubt a number that shrinks like 1/n1/\sqrt{n}. Then the posterior went to work. Greedy hands the decision the posterior mean and, provably, can pay for one unlucky start forever. Thompson sampling hands it a sample, and so pulls every arm exactly as often as that arm is believed best. Gittins’ theorem shows that the truly optimal strategy for discounted reward is also a score per arm: the mean plus the value of what a pull would teach. The recurring lesson of Movement III holds once more: an estimate is a compression of a posterior, chosen for a purpose, and when the purpose is to keep deciding, the compression must keep the posterior’s width. Movement IV makes that width, the posterior variance, its main object.

Self-check questions

  1. After nn trials from a Beta(a,b)\mathrm{Beta}(a, b) prior, what happens to the data weight ww in [eq:shrinkage] as n→∞n \to \infty, and at what rate does the posterior standard deviation shrink?

  2. A colleague runs Thompson sampling but pulls the arm with the largest posterior mean each round. What have they built, which proposition says what can go wrong, and why would adding forced pulls not fix it?

  3. Why is an arm’s Gittins index never below its posterior mean, and when does the gap vanish?