Bayes learning and multi-armed bandits

AII 600, from the book chapters on Bayesian learning, hypothesis testing and reinforcement learning

Vadim Sokolov

George Mason University

Exchangeability and the Bayesian view of probability models

  • Data: y=(y1,…,yT)y=(y_1,\ldots,y_T). Coin tosses: θ\theta is the probability that a toss is heads.
  • Tosses are not independent without conditioning on θ\theta: many heads tell us about θ\theta, which predicts future tosses.
  • Given θ\theta, tosses are independent: P(y2=1∣y1=1,θ)=P(y2=1∣θ)=θP(y_2=1\mid y_1=1,\theta)=P(y_2=1\mid\theta)=\theta.
  • Bayes: θ\theta is a random variable with prior p(θ)p(\theta). The joint distribution of the data is p(y1,…,yT)=∫∏t=1Tp(yt∣θ)p(θ)dθ. p(y_{1}, \ldots ,y_{T}) = \int\prod_{t=1}^Tp(y_t\mid \theta)\, p(\theta)\,d\theta .
  • The right-hand side does not depend on the order of the data: exchangeability.
  • Exchangeable: y1,…,yTy_1,\ldots,y_T has the same distribution as any permutation yπ1,…,yπTy_{\pi_1},\ldots,y_{\pi_T}. Independent and identically distributed variables are always exchangeable.
  • 8 tosses with 4 heads: the order (1,0,1,0,1,0,1,0)(1,0,1,0,1,0,1,0) or (0,1,1,0,1,0,0,1)(0,1,1,0,1,0,0,1) does not matter.

Coin toss: the question

  • Unfair coin, or a flipper trained to get the side he wants: estimate θ\theta, the probability of tails
  • Observed 10 flips: {H,T,H,H,H,T,H,T,H,H}\{H, T, H, H, H, T, H, T, H, H\}, three tails
  • Probability the next flip is tails: frequency answer 3/10=0.33/10 = 0.3
  • Bayes: prior belief that the coin is fair, but not sure
  • Discretize θ\theta on 0,0.1,…,10, 0.1, \dots, 1; largest mass at θ=0.5\theta = 0.5, zero at θ=0\theta = 0 and θ=1\theta = 1
theta <- seq(0, 1, by = 0.1)
prior <- c(0, 0.024, 0.077, 0.132, 0.173, 0.188, 0.173, 0.132, 0.077, 0.024, 0)

Prior

Coin toss: Bayes rule

  • Posterior: p(θ∣y)=p(y∣θ)p(θ)p(y)p(\theta \mid y) = \dfrac{p(y \mid \theta)\, p(\theta)}{p(y)}
  • Marginal likelihood: p(y)=∑θp(y∣θ)p(θ)p(y) = \sum_{\theta} p(y \mid \theta)\, p(\theta)
  • Binomial likelihood, 3 tails in 10: p(y∣θ)∝θ3(1−θ)7p(y \mid \theta) \propto \theta^3 (1 - \theta)^7
  • The posterior depends only on the counts of tails and heads: they are sufficient for θ\theta
likelihood <- function(theta, n, Y) {
  theta^Y * (1 - theta)^(n - Y)
}
posterior <- likelihood(theta, 10, 3) * prior
posterior <- posterior / sum(posterior) # normalize
pred10 <- sum(theta * posterior)        # posterior mean

Coin toss: 10 flips and 100 flips

Posterior for 10 flips

Posterior for 100 flips
posterior <- likelihood(theta, 100, 30) * prior
posterior <- posterior / sum(posterior) # normalize
  • 10 flips: posterior mean ∑θθp(θ∣y)=0.375\sum_\theta \theta\, p(\theta \mid y) = 0.375, against 0.30.3 by frequency
  • 100 flips with 30 tails: the posterior concentrates around θ=0.3\theta = 0.3
  • For large samples, the frequentist and Bayesian answers agree

Beta-Binomial model

  • Learning about a probability of success θ\theta, for example in A/B testing. Data yiy_i from a Binomial with mm trials; θ\theta is drawn from a Beta distribution. yi∣θ∼Binomial(m,θ)(likelihood),θ∼Beta(α,β)(prior). y_i \mid \theta \sim Binomial(m,\theta) ~\text{(likelihood)}, \quad \theta \sim Beta(\alpha,\beta) ~\text{(prior)}.
  • Beta density, on y∈[0,1]y\in[0,1] with α>0\alpha>0, β>0\beta>0 controlling the shape: Beta(y;α,β)=yα−1(1−y)β−1B(α,β),B(α,β)=∫01yα−1(1−y)β−1dy. Beta(y; \alpha, \beta) = \frac{y^{\alpha - 1}(1 - y)^{\beta - 1}}{B(\alpha, \beta)}, \qquad B\left( \alpha,\beta\right) =\int_{0}^{1}y^{\alpha-1}\left( 1-y\right)^{\beta-1}dy .
  • B(α,β)B(\alpha,\beta) is the beta function, a normalizing constant. Mean μ\mu and variance σ2\sigma^2: μ=αα+β,σ2=αβ(α+β)2(α+β+1) \mu = \frac{\alpha}{\alpha + \beta}, \qquad \sigma^2 = \frac{\alpha\beta}{(\alpha + \beta)^2(\alpha + \beta + 1)}

Beta distributions

Beta distributions

Conjugate prior and the Beta posterior

  • A prior is called a conjugate prior for a likelihood function if the posterior distribution is of the same family as the prior distribution. Beta is conjugate to the Binomial.
  • Bernoulli observations (m=1m=1), Y=∑i=1nyiY=\sum_{i=1}^n y_i the number of successes: p(θ∣Y)=Beta(Y+α,n−Y+β). p(\theta\mid Y) = Beta(Y+\alpha, n-Y+\beta).
  • YY is a sufficient statistic for θ\theta: the posterior depends on the data only through YY.
  • General case, nn observations each with mm trials, nmnm total Bernoulli trials: θ∣Y∼Beta(Y+α,nm−Y+β). \theta\mid Y \sim Beta(Y+\alpha, nm-Y+\beta).
  • Posterior mean and variance, with αn=α+Y\alpha_{n} = \alpha + Y and βn=β+nm−Y\beta_{n} = \beta + nm - Y: 𝔼[θ∣Y]=αnαn+βn and Var[θ∣Y]=αnβn(αn+βn)2(αn+βn+1) \mathbb{E}[\theta\mid Y] =\frac{\alpha_{n}}{\alpha_{n}+\beta_{n}} \;\text{ and }\; \operatorname{Var}[\theta\mid Y] =\frac{\alpha_{n}\beta_{n}}{\left( \alpha_{n}+\beta_{n}\right) ^{2}\left( \alpha_{n}+\beta_{n}+1\right) }

Black swans

  • After TT trials, all successes: the probability of success next time is (T+1)/(T+2)(T+1)/(T+2). White swans seen, never a black swan.
  • Model yt=0y_t=0 or 11 with P(yt=1∣θ)=θP(y_t=1\mid\theta)=\theta. Y=∑t=1TytY=\sum_{t=1}^{T}y_t is the number of successes. p(y∣θ)=∏t=1Tp(yt∣θ)=θY(1−θ)T−Y. p\left( y\mid \theta\right) =\prod_{t=1}^{T}p\left( y_{t}\mid \theta\right) =\theta^{Y}\left( 1-\theta\right)^{T-Y}.
  • The MLE θ̂=T−1Y\widehat{\theta} = T^{-1}Y predicts θ̂=1\hat{\theta} = 1 and is shocked by a black swan. Bayes allows for “learning”.
  • Posterior is Beta(αn,βn)Beta(\alpha_n,\beta_n) with αn=α+Y\alpha_{n}=\alpha+Y, βn=β+T−Y\beta_{n}=\beta+T-Y.
  • T=100T=100 white swans, Y=100Y=100, uniform prior Beta(1,1)Beta(1,1): the posterior is Beta(101,1)Beta(101,1). P(yT+1=1∣Y=100)=𝔼[θ∣Y]=αnαn+βn=101102≈0.99. P(y_{T+1} = 1 \mid Y=100) = \mathbb{E}[\theta \mid Y] = \frac{\alpha_n}{\alpha_n + \beta_n} = \frac{101}{102} \approx 0.99.

Black swans: probability of a black swan

  • At least one black swan in the next 100 observations: P(at least one black swan in next 100∣Y)=∫01[1−θ100]p(θ∣Y)dθ. P(\text{at least one black swan in next } 100 \mid Y) = \int_0^1 \left[1 - \theta^{100}\right] p(\theta \mid Y) d\theta .
  • Monte Carlo: draw θ\theta from the Beta(101,1)Beta(101,1) posterior and average 1−θ1001-\theta^{100} over the draws.

Posterior distribution after observing 100 white swans.
  • Never observing a black swan, Bayes still keeps uncertainty about θ\theta through the posterior, so rare events keep positive probability.

Clinical trials

  • KK drugs a∈1,…,Ka\in 1,\dots,K to test. The outcome with drug aa is binary y(a)∈{0,1}y(a) \in \{0,1\}, Bernoulli with mean f(a)f(a). The model is w=f(1),…,f(K)w = f(1),\dots,f(K); the sample is D={y1,…,yn}D = \{y_1,\dots,y_n\}.
  • Beta prior and posterior, where na,1n_{a,1} and na,0n_{a,0} count the successes and failures of drug aa in DD: p(w∣α,β)=∏a=1KBeta(wa∣α,β),p(w∣D)=∏a=1KBeta(wa∣α+na,1,β+na,0) p(w\mid \alpha,\beta) = \prod_{a=1}^K Beta(w_a\mid \alpha,\beta), \qquad p(w\mid D) = \prod_{a=1}^K Beta(w_a\mid \alpha + n_{a,1},\beta + n_{a,0})
  • This setup allows sequential design of experiments. The simplest version is Thompson sampling: after nn patients, draw a single sample w̃\tilde w from the posterior and maximize the resulting surrogate an+1=argmaxaw̃a,w̃∼p(w∣D) a_{n+1} = \arg\max_{a} \tilde w_a, ~~~ \tilde{w} \sim p(w\mid D)

Clinical trial: ulcer treatments (Novick and Grizzle, 1965)

Treatment Excellent Fair Death Total
A 76 17 7 100
B 89 10 1 100
C 86 13 1 100
D 88 9 3 100
  • pip_i: death rate under treatment ii. Prior pi∼Beta(1,1)p_i \sim \text{Beta}(1,1) (uniform)
  • pA∣data∼Beta(1+7,1+93)=Beta(8,94)p_A \mid \text{data} \sim \text{Beta}(1+7,\,1+93) = \text{Beta}(8, 94)
  • pB∣data∼Beta(1+1,1+99)=Beta(2,100)p_B \mid \text{data} \sim \text{Beta}(1+1,\,1+99) = \text{Beta}(2, 100)

Ulcer trial: is A worse than B?

Posterior death rates. Dashed: posterior means

Posterior of pA−pBp_A - p_B, 100,000 draws. Dashed: 0
  • Draw pA∼Beta(8,94)p_A \sim \text{Beta}(8,94) and pB∼Beta(2,100)p_B \sim \text{Beta}(2,100); take the difference
  • P(pA>pB∣data)≈0.98P(p_A > p_B \mid \text{data}) \approx 0.98: share of draws right of 0
  • A near 7 to 8%, B near 1 to 2%. Mean of pA−pBp_A - p_B: 0.059; 95% interval 0.004 to 0.122

Ulcer trial: p-values against the posterior

  • Chi-square test of homogeneity: 12.4 on 6 degrees of freedom, p=0.053p = 0.053: fails to reject at 5%
  • Fisher’s exact test on A and B only: deaths 7 of 100 (A) against 1 of 100 (B)
    • H0H_0: pA=pBp_A = p_B. One-sided alternative: pA>pBp_A > p_B
    • Fix the total of 8 deaths. Under H0H_0 the 8 deaths fall at random among the 200 patients, 100 per arm (hypergeometric)
    • p=P(A gets 7 or 8 of the 8 deaths∣H0)=0.032p = P(\text{A gets 7 or 8 of the 8 deaths} \mid H_0) = 0.032: rejects at 5%
    • Two-sided alternative pA≠pBp_A \ne p_B: p=0.065p = 0.065, does not reject. The one-sided direction was chosen after seeing A had more deaths
    • A and B are the worst and best of 6 possible pairs. Bonferroni cutoff: 0.05/6=0.00830.05/6 = 0.0083; 0.0320.032 does not pass
  • Chi-square tests the whole table; the 98% answers one question: is A’s death rate higher than B’s
  • Posterior probability that A is not worse than B: 0.018
  • Bayesian inference is relative: which hypothesis explains the data better
Prior Prior mean P(pA>pB∣data)P(p_A > p_B \mid \text{data})
Uniform Beta(1,1) 0.50 0.98
Jeffreys Beta(0.5,0.5) 0.50 0.99
Skeptical Beta(2,20) 0.09 0.97
Optimistic Beta(1,10) 0.09 0.98
Strong Beta(5,50) 0.09 0.94
  • 100 patients per arm: the likelihood dominates the prior

Normal with unknown mean

  • Y∼N(μ,σ2)Y \sim N(\mu, \sigma^2), mean μ\mu unknown, variance σ2\sigma^2 known. The MLE is μ̂=y‾\hat{\mu} = \bar{y}, the sample mean. Normal prior μ∼N(μ0,σ02)\mu \sim N(\mu_0, \sigma_0^2) is conjugate; the posterior is N(μn,σn2)N(\mu_n, \sigma_n^2): μn=σ2nσ02+σ2μ0+nσ02nσ02+σ2y‾,σn2=σ2σ02nσ02+σ2 \mu_n = \frac{\sigma^2}{n\sigma_0^2 + \sigma^2}\mu_0 + \frac{n\sigma_0^2}{n\sigma_0^2 + \sigma^2}\bar{y}, \qquad \sigma_n^2 = \frac{\sigma^2\sigma_0^2}{n\sigma_0^2 + \sigma^2}
  • The posterior mean is a weighted average of the prior mean and the sample mean, with weights proportional to the precision of the prior and the likelihood. The posterior variance is smaller than the prior variance.
  • As n→∞n \to \infty: σ2nσ02+σ2→0,nσ02nσ02+σ2→1,σ2σ02nσ02+σ2→0. \frac{\sigma^2}{n\sigma_0^2 + \sigma^2} \rightarrow 0, \quad \frac{n\sigma_0^2}{n\sigma_0^2 + \sigma^2}\rightarrow 1, \quad \frac{\sigma^2\sigma_0^2}{n\sigma_0^2 + \sigma^2} \rightarrow 0 .
  • y‾=Ave(y1,…,yn)\bar y = \mathrm{Ave}(y_1,\ldots,y_n) is the sufficient statistic: p(y∣μ)=N(y‾∣μ,σ2/n)p(y \mid \mu) = N(\bar y\mid \mu,\sigma^2/n).

Stylized example

  • Prior μ∼N(−1,1)\mu \sim N(-1,1), observe y=2y=2 with likelihood p(y∣μ)=N(μ,22)p(y \mid \mu) = N(\mu,2^2). The posterior is p(μ∣y)∝p(y∣μ)p(μ)=N(y∣μ,22)N(μ∣−1,1)=N(−0.4,0.8942). p(\mu \mid y) \propto p(y \mid \mu) p(\mu) = N(y\mid \mu,2^2) N(\mu\mid -1,1) = N(-0.4,0.894^2).

Norm-Norm Updating
  • The posterior mean is between those of prior and likelihood. The posterior variance is lower than the variance of both: the effect of combining information from data and prior.

Chicago Bears 2014 to 2015 season

  • Back-to-back 50-point defeats: Patriots-Bears 51−2351-23, Packers-Bears 55−1455-14. Next game at home against the Vikings, current line −3.5-3.5.
  • y‾\bar y is the average win/lose margin over n=9n =9 games, μ\mu its mean, σ\sigma its standard deviation, τ\tau the standard deviation of μ\mu. Observed y‾=−9.22\bar{y} = -9.22, s=18.34s = 18.34, pre-season prior mean μ0=0\mu_0 = 0, τ=4\tau = 4. y‾∣μ∼N(μ,σ2n)∼N(μ,18.3429),μ∼N(0,τ2) \bar{y} \mid \mu \sim N \left ( \mu , \frac{\sigma^2}{n} \right ) \sim N \left ( \mu , \frac{18.34^2}{9} \right ), \qquad \mu \sim N( 0 , \tau^2 )
  • Bayes shrinkage estimator, with shrinkage factor 0.30.3: 𝔼[μ∣τ,y‾]=τ2τ2+σ2ny‾. \mathbb{E}[\mu \mid \tau, \bar y] = \frac{ \tau^2 }{ \tau^2 + \frac{\sigma^2}{n} }\bar{y} .
  • Updated estimator 𝔼[μ∣y‾,τ]=−2.76>−3.5\mathbb{E}[\mu \mid \bar{y}, \tau] = -2.76 > -3.5: the line is an over-reaction. One point on the line is about 22% on a probability scale.

Bears: market-based τ\tau

  • Calculate a market-based τ\tau given line =−3.5=-3.5: w=3.59.22=0.38,τ2=w1−wσ2n≈22.9. w = \frac{3.5}{9.22} = 0.38, \quad \tau^2 = \frac{w}{1-w}\,\frac{\sigma^2}{n} \approx 22.9.
  • The market-based τ\tau is ≈4.8\approx 4.8 points.
  • Home advantage: about 33 points (NFL rule of thumb, an assumption)
  • μ\mu averages home and away games; the line −3.5-3.5 is for this home game, so home advantage is already in it
  • Model’s prediction for the home game: −2.76+3≈+0.2-2.76 + 3 \approx +0.2, a toss-up, against the market’s −3.5-3.5
  • Gap about 3.73.7 points: the market over-reacted to the two blowouts
  • Actual result: Bears 21, Vikings 13

Posterior predictive

  • The posterior predictive distribution is the distribution of a new observation yn+1y_{n+1} given the observed data y1,…,yny_1,\ldots,y_n: p(yn+1∣y1,…,yn)=∫p(yn+1∣μ)p(μ∣y1,…,yn)dμ=N(yn+1∣μn,σn2+σ2). p(y_{n+1} \mid y_1,\ldots,y_n) = \int p(y_{n+1} \mid \mu) p(\mu \mid y_1,\ldots,y_n) d\mu = N(y_{n+1} \mid \mu_n, \sigma_n^2 + \sigma^2).
  • Follows from the general properties of the Gaussian distribution.

Shrinkage and baseball batting averages

  • Jeter’s record against individual pitchers, 2006 season. pip_i is his ability against pitcher ii, yiy_i the hits, TiT_i the at-bats against pitcher ii. (yi∣pi)∼Bin(Ti,pi)withpi∼Be(α,β) (y_{i} \mid p_{i})\sim Bin(T_{i},p_{i})\;\;\mathrm{with}\;\;p_{i}\sim Be(\alpha,\beta)
  • Prior mean 𝔼[pi]=α/(α+β)=p‾\mathbb{E}[p_{i}]=\alpha/(\alpha+\beta)=\bar{p}. Prior variance Var[pi]=p‾(1−p‾)ϕ\operatorname{Var}[p_{i}]=\bar{p}(1-\bar{p})\phi with ϕ=(α+β+1)−1\phi=(\alpha+\beta+1)^{-1}.
  • ϕ\phi measures how concentrated the beta distribution is around its mean: ϕ=0\phi=0 highly concentrated, ϕ=1\phi=1 widely dispersed.
  • Hierarchical model: the abilities pip_i share a common distribution with parameters α\alpha and β\beta estimated from the data. Shrinkage: estimates of pip_i are shrunk towards p‾\bar{p}.
  • Estimated ϕ̂=0.002\hat{\phi} = 0.002 for Jeter. Extremes are shrunk the most, and most for the smallest sample sizes.
  • Jeter had a .316.316 average over these at-bats. His Bayes estimates vary from .311.311 to .326.326.

Hierarchy: population and individuals

flowchart TB
  POP["<b>Population</b><br/>Beta(α, β): mean p̄, spread φ<br/>estimated from all pitchers"]
  P1["<b>Mendoza</b><br/>ability p₁"]
  P2["<b>Martinez</b><br/>ability p₂"]
  P3["<b>Julio</b><br/>ability p₃"]
  PK["<b>...</b><br/>one p_i per pitcher"]
  Y1["<b>Data</b><br/>5 hits in 6"]
  Y2["<b>Data</b><br/>21 hits in 83"]
  Y3["<b>Data</b><br/>0 hits in 13"]
  YK["<b>Data</b><br/>y_i ~ Bin(T_i, p_i)"]
  POP --> P1 & P2 & P3 & PK
  P1 --> Y1
  P2 --> Y2
  P3 --> Y3
  PK --> YK

  • Top level: population of Jeter’s abilities against all pitchers, pi∼Be(α,β)p_i \sim \text{Be}(\alpha,\beta)
  • Middle level: ability pip_i against pitcher ii
  • Bottom level: observed hits yiy_i in TiT_i at-bats
  • Each pip_i is learned from its own data and from the population. Small ϕ̂=0.002\hat\phi = 0.002: tight population, strong pull to p‾\bar p (Mendoza .833→.322.833 \to .322)
  • Fewer at-bats, stronger pull. Both 00 hits: Kolb (5 at-bats) →.316\to .316, Julio (13 at-bats) →.312\to .312

Jeter against individual pitchers

Pitcher At-bats Hits ObsAvg EstAvg 95% Int
R. Mendoza 6 5 .833 .322 (.282, .394)
H. Nomo 20 12 .600 .326 (.289, .407)
A.J.Burnett 5 3 .600 .320 (.275, .381)
E. Milton 28 14 .500 .324 (.291, .397)
D. Cone 8 4 .500 .320 (.218, .381)
R. Lopez 45 21 .467 .326 (.291, .401)
K. Escobar 39 16 .410 .322 (.281, .386)
J. Wetteland 5 2 .400 .318 (.275, .375)
T. Wakefield 81 26 .321 .318 (.279, .364)
P. Martinez 83 21 .253 .312 (.254, .347)
K. Benson 8 2 .250 .317 (.264, .368)
T. Hudson 24 6 .250 .315 (.260, .362)
J. Smoltz 5 1 .200 .314 (.253, .355)
F. Garcia 25 5 .200 .314 (.253, .355)
B. Radke 41 8 .195 .311 (.247, .347)
D. Kolb 5 0 .000 .316 (.258, .363)
J. Julio 13 0 .000 .312 (.243, .350 )
Total 6530 2061 .316

Shrinkage picture: 18 players, 1970 (Efron and Morris, 1977)

First 45 at-bats, season average and James-Stein estimate for each player.
  • yjy_j: average of player jj after 45 at-bats. y‾=.265\bar y = .265: grand mean, k=18k = 18 players
  • σ2=y‾(1−y‾)/45\sigma^2 = \bar y(1-\bar y)/45: sampling variance of one average
  • James-Stein estimate: y‾+c(yj−y‾)\bar y + c\,(y_j - \bar y)
  • c=1−(k−3)σ2∑j(yj−y‾)2=0.21c = 1 - \dfrac{(k-3)\sigma^2}{\sum_j (y_j - \bar y)^2} = 0.21: each estimate keeps a fifth of its distance to y‾\bar y
  • Clemente: .400→.294.400 \to .294
  • Error against the season average, MSE =1k∑j(estimatej−seasonj)2= \frac{1}{k}\sum_j(\text{estimate}_j - \text{season}_j)^2:
    • Observed: 0.00330.0033
    • James-Stein: 0.00100.0010
  • Same idea as the Jeter model: extremes are pulled to the mean

Lofton against Milton

  • Aug 29, 2006: Kenny Lofton (career .299.299, .308.308 in 2006) faced pitcher Milton (record 11 for 1919). He was rested and replaced by a .273.273 hitter.
  • P(≤1hitin19attempts∣p=0.3)=0.01P\left( \leq 1\;\mathrm{hit\;in}\ 19\;\mathrm{attempts} \mid p=0.3\right) =0.01, a 1-in-100 event. This ignores the multiplicity of batter-pitcher match-ups.
  • Hierarchical analysis of Lofton: ϕ=0.008\phi=0.008, four times larger than Jeter’s ϕ=0.002\phi=0.002.
  • His estimates vary from .265.265 to .340.340, the lowest against Milton.
  • The estimate against Milton is .265<.273.265<.273: resting Lofton is justified by this analysis.

Poisson-Gamma: learning about an intensity

  • NtN_t counts events up to time tt, with N0=0N_0=0. The probability of an event over a short interval Δt\Delta t is λΔt\lambda\Delta t, where λ\lambda is the intensity. Nt∼Poi(λt)N_t\sim Poi(\lambda t): P[Nt=k]=e−λt(λt)kk! for k=0,1,… P\left[ N_{t}=k\right] =\frac{e^{-\lambda t}\left( \lambda t\right) ^{k}}{k!}\text{ for }k=0,1,\ldots
  • Learn λ\lambda from observations up to time tt. The MLE is λ̂=Nt/t\widehat{\lambda}=N_{t}/t, and NtN_t with tt is sufficient for λ\lambda.
  • The MLE is 0 before the first event, although the model assumes events are possible (default risk in credit).
  • Gamma prior with hyperparameters aa, AA: p(λ∣a,A)=AaΓ(a)λa−1exp(−Aλ). p\left( \lambda \mid a,A\right) =\frac{A^{a}}{\Gamma(a) }\lambda^{a-1}\exp\left( -A\lambda\right) .

Poisson-Gamma posterior

  • The posterior is also Gamma, with at=Nt+aa_{t}=N_{t}+a and At=t+AA_{t}=t+A: p(λ∣Nt)∝λNt+a−1exp(−λ(t+A))∼𝒢(at,At). p\left( \lambda \mid N_{t}\right) \propto \lambda^{N_{t}+a-1}\exp\left( -\lambda\left( t+A\right) \right) \sim\mathcal{G}\left( a_{t},A_{t}\right).
  • Posterior mean in shrinkage form, a weighted average of the MLE and the prior mean, with wt=t/(t+A)w_{t}=t/(t+A): 𝔼[λ∣Nt]=atAt=Nt+at+A=wtNtt+(1−wt)aA. \mathbb{E}[\lambda \mid N_{t}] =\frac{a_{t}}{A_{t}}=\frac{N_{t}+a}{t+A}=w_{t}\frac{N_{t}}{t}+\left( 1-w_{t}\right) \frac{a}{A}.
  • In large samples wt→1w_{t}\rightarrow1 and 𝔼[λ∣Nt]≈Nt/t=λ̂\mathbb{E}[\lambda \mid N_{t}] \approx N_{t}/t=\widehat{\lambda}.
  • Generic properties: upward revision at event times and 1/(t+A)1/(t+A) decay during periods of no events.

Four Gamma priors

Prior densities p(λ∣a,A)p(\lambda \mid a, A). Vertical line: true λ=1\lambda = 1 used in the simulation.
  • Prior mean: a/Aa/A. Prior SD: a/A\sqrt{a}/A. Weight on the data: wt=t/(t+A)w_t = t/(t+A)
Prior aa AA Mean SD 90% interval Belief
High activity 4 1 4 2 1.37 to 7.75 many events, uncertain
Diffuse 1 1 1 1 0.05 to 3.00 little information
Low activity 1 5 0.2 0.2 0.01 to 0.60 few events
Dogmatic 30 10 3 0.55 2.16 to 3.95 wrong and confident

Sensitivity of the Gamma prior

High activity: a=4a = 4, A=1A = 1

Diffuse: a=1a = 1, A=1A = 1

Low activity: a=1a = 1, A=5A = 5

Dogmatic: a=30a = 30, A=10A = 10
  • Time in years, simulated intensity λ=1\lambda=1. Solid line: cumulative count NtN_t; thick dashed: posterior mean of λ\lambda; dotted: 5% and 95% posterior quantiles.
  • AA acts like AA years of prior data. Dogmatic, A=10A = 10: after t=10t = 10 years the data get only half the weight, w10=10/20w_{10} = 10/20

Normal with unknown variance, and the Normal-Gamma model

  • Known mean μ\mu, unknown variance. Conjugate prior: the inverse Gamma, with posterior IG(αposterior,βposterior)IG(\alpha_{\mathrm{posterior}},\beta_{\mathrm{posterior}}) for one observation yy: αposterior=α+12,βposterior=β+(y−μ)22. \alpha_{\mathrm{posterior}} = \alpha + \dfrac{1}{2}, ~~\beta_{\mathrm{posterior}} = \beta + \dfrac{(y-\mu)^2}{2}.
  • The predictive distribution of ynewy_{new} is a tt-distribution with 2αposterior2\alpha_{\mathrm{posterior}} degrees of freedom, mean μ\mu and variance βposterior/(αposterior−1)\beta_{\mathrm{posterior}}/(\alpha_{\mathrm{posterior}}-1).
  • Both unknown, with precision τ=1/σ2\tau = 1/\sigma^2: yi∣θ,τ∼N(θ,1/τ)y_i \mid \theta, \tau \sim N(\theta, 1/\tau) with Normal-Gamma prior, ν\nu scaling the precision of θ\theta: θ∣μ,τ,ν∼N(μ,1/(τν)),τ∣α,β∼Gamma(α,β). \theta\mid \mu,\tau,\nu \sim N(\mu, 1/(\tau \nu)), \quad \tau \mid \alpha, \beta \sim \text{Gamma}(\alpha, \beta).
  • The posterior is Normal-Gamma with μn=νμ+ny‾ν+n\mu_n = \dfrac{\nu\mu + n\bar{y}}{\nu+n}, νn=ν+n\nu_n = \nu+n, αn=α+n2\alpha_n = \alpha + \dfrac{n}{2} and βn=β+12∑i=1n(yi−y‾)2+nν2(ν+n)(y‾−μ)2. \beta_n = \beta + \dfrac{1}{2}\sum_{i=1}^n(y_i-\bar{y})^2 + \dfrac{n\nu}{2(\nu+n)}(\bar{y}-\mu)^2.
  • The marginal of θ\theta is a Student’s tt centered at μn\mu_n with squared scale βn/(νnαn)\beta_n/(\nu_n\alpha_n) and 2αn2\alpha_n degrees of freedom.

Bayesian computation: when conjugacy breaks

  • Most realistic models are not conjugate: the posterior exists but has no closed form. The remedy is simulation: draws θ(1),…,θ(N)∼p(θ∣y)\theta^{(1)},\ldots,\theta^{(N)} \sim p(\theta\mid y) turn posterior summaries into Monte Carlo averages: 𝔼[g(θ)∣y]≈1N∑n=1Ng(θ(n)). \mathbb{E}[g(\theta)\mid y] \approx \frac{1}{N}\sum_{n=1}^N g(\theta^{(n)}).
  • Posterior consistency: the posterior concentrates around the true value μ*\mu^\ast as data accumulate. For xi∼N(μ*,σ2)x_i \sim N(\mu^\ast, \sigma^2) and prior μ∼N(μ0,τ02)\mu \sim N(\mu_0, \tau_0^2): 𝔼[μ∣x1,…,xn]=τ0−2μ0+nσ−2x‾nτ0−2+nσ−2→μ*almost surely. \mathbb{E}[\mu \mid x_1, \ldots, x_n] = \frac{\tau_0^{-2}\mu_0 + n\sigma^{-2}\bar{x}_n}{\tau_0^{-2} + n\sigma^{-2}} \to \mu^\ast \quad \text{almost surely}.
  • The prior matters for small samples and becomes negligible for large samples.
  • Monte Carlo: μ̂n=1n∑i=1nf(xi)→𝔼[f(X)]\hat{\mu}_n = \frac{1}{n}\sum_{i=1}^n f(x_i) \to \mathbb{E}[f(X)] by the strong law of large numbers. The error decreases at rate Op(n−1/2)O_p(n^{-1/2}).
  • Posterior predictive simulation: θ(n)∼p(θ∣y)\theta^{(n)} \sim p(\theta\mid y), then ỹ(n)∼p(ỹ∣θ(n))\tilde y^{(n)} \sim p(\tilde y\mid \theta^{(n)}).

Markov chain Monte Carlo

  • Build a Markov chain with transition kernel P(x,y)P(x, y) whose stationary distribution is the posterior π\pi: π(y)=∫π(x)P(x,y)dx,1n∑i=1nf(x(i))→𝔼π[f(x)]almost surely. \pi(y) = \int \pi(x) P(x, y) dx, \qquad \frac{1}{n}\sum_{i=1}^n f(x^{(i)}) \to \mathbb{E}_\pi[f(x)] \quad \text{almost surely}.
  • Only the ratio of densities π(y)/π(x)\pi(y)/\pi(x) is needed, so intractable normalizing constants do not matter.
  • Metropolis-Hastings: propose yy from a symmetric proposal q(x,y)q(x, y) and accept with probability α(x,y)=min(π(y)π(x),1). \alpha(x, y) = \min\left(\frac{\pi(y)}{\pi(x)}, 1\right).
  • Gibbs sampler: cycle through the coordinates of x=(x1,…,xp)x = (x_1, \ldots, x_p), sampling each from its full conditional; the acceptance probability is always one: xj(t+1)∼π(xj∣x1(t+1),…,xj−1(t+1),xj+1(t),…,xp(t)) x_j^{(t+1)} \sim \pi(x_j \mid x_1^{(t+1)}, \ldots, x_{j-1}^{(t+1)}, x_{j+1}^{(t)}, \ldots, x_p^{(t)})

SAT coaching in eight schools: the data

  • Randomized experiments of coaching programs for the verbal SAT (scored 200 to 800) in eight high schools (Rubin, 1981)
  • yjy_j: estimated effect of coaching in school jj, in SAT points. σj\sigma_j: its standard error
  • 95% interval: yj±1.96σjy_j \pm 1.96\,\sigma_j
School yjy_j σj\sigma_j 95% interval
A 28 15 -1 to 57
B 8 10 -12 to 28
C -3 16 -34 to 28
D 7 11 -15 to 29
E -1 9 -19 to 17
F 1 11 -21 to 23
G 18 10 -2 to 38
H 12 18 -23 to 47
  • All intervals overlap and all contain 0 (A and G barely)

SAT coaching in eight schools: the model

  • θj\theta_j: true average effect of the program in school jj. Goal: estimate θj\theta_j from yjy_j.
  • Two programs appear to work, 28 and 18 points, but the standard errors are large. Classical test: ∑j(yj−θ̂)2/σj2=4.7\sum_j (y_j-\hat\theta)^2/\sigma_j^2 = 4.7 on 7 degrees of freedom, p=0.70p = 0.70. Pooled estimate: θ̂=∑jyj/σj2∑j1/σj2=7.7,standard error 4.1. \hat\theta = \frac{\sum_j y_j/\sigma_j^2}{\sum_j 1/\sigma_j^2} = 7.7, \quad \text{standard error } 4.1.
  • Taken separately, School A gains 28 points; pooled, it gains no more than School E.
  • Hierarchical model, effects exchangeable, σj2\sigma_j^2 known. μ\mu and τ\tau are the mean and standard deviation of the effects: yj∣θj∼N(θj,σj2),θj∣μ,τ∼N(μ,τ2),p(μ,τ2)∝1/τ. y_j \mid \theta_j \sim N(\theta_j, \sigma_j^2),\qquad \theta_j \mid \mu, \tau \sim N(\mu, \tau^2),\qquad p(\mu, \tau^2) \propto 1/\tau .
  • As τ→0\tau \to 0: complete pooling. As τ→∞\tau \to \infty: separate estimates.

Eight schools: Gibbs sampler

  • The joint posterior is not a familiar family, but every full conditional is: θj∣μ,τ,y∼N(vj(yjσj2+μτ2),vj),vj=(1σj2+1τ2)−1,μ∣θ,τ∼N(θ‾,τ2/8),τ2∣θ,μ∼Inv-Gamma(72,12∑j(θj−μ)2). \begin{aligned} \theta_j \mid \mu, \tau, y &\sim N\left(v_j\left(\frac{y_j}{\sigma_j^2} + \frac{\mu}{\tau^2}\right), v_j\right),\qquad v_j = \left(\frac{1}{\sigma_j^2} + \frac{1}{\tau^2}\right)^{-1},\\ \mu \mid \theta, \tau &\sim N(\bar\theta, \tau^2/8),\\ \tau^2 \mid \theta, \mu &\sim \text{Inv-Gamma}\left(\frac{7}{2}, \frac{1}{2}\sum_j (\theta_j-\mu)^2\right). \end{aligned}
  • The variances are unequal, so the shrinkage differs: the larger σj\sigma_j, the more weight the conditional mean of θj\theta_j puts on μ\mu.

Eight schools: posterior quantiles

School yjy_j σj\sigma_j 2.5% 25% 50% 75% 97.5%
A 28 15 -2.2 6.0 10.2 15.5 31.5
B 8 10 -4.6 4.0 7.9 11.7 20.6
C -3 16 -11.6 2.0 6.6 10.8 20.5
D 7 11 -5.7 3.7 7.6 11.6 20.9
E -1 9 -9.0 1.4 5.6 9.4 16.3
F 1 11 -8.6 2.3 6.5 10.4 18.5
G 18 10 -1.2 6.1 10.0 14.5 26.0
H 12 18 -7.1 3.9 8.2 12.7 25.6
μ\mu -2.2 4.6 7.9 11.1 18.2
τ\tau 0.2 2.4 5.2 9.1 20.7
  • School A falls from 28 to a median of about 10, the same as School G, which reported 18. Every 95% interval contains zero.
  • Posterior of τ\tau: median near 5, interval from near 0 to about 21. The data do not settle how much to pool.
  • Draws of τ\tau have autocorrelation 0.78 at lag 1 and 0.04 at lag 50.

MAP estimation and regularization

  • Data (xi,yi)(x_i, y_i), i=1,…,Ni = 1,\dots,N. MAP learns ff by minimizing loss plus penalty: minimizef∑i=1Nℒ(yi,f(xi))+λϕ(f) \underset{f}{\mathrm{minimize}} \quad \sum_{i=1}^N \mathcal{L}(y_i,f(x_i)) + \lambda \phi(f)
  • ℒ\mathcal{L}: loss, the negative log-likelihood. Normal yi∣xi∼N(f(xi),σ2)y_i \mid x_i \sim N(f(x_i),\sigma^2) gives squared loss (y−f(x))2(y-f(x))^2
  • ϕ(f)\phi(f): penalty on complex ff. λ\lambda: trade-off between fit and simplicity
  • Negative log posterior: −logp(f∣y,x)=−∑ilogL(yi,f(xi))−logp(f)+logp(y∣x)-\log p(f \mid y, x) = -\sum_i \log L(y_i,f(x_i)) - \log p(f) + \log p(y \mid x)
  • So the penalty is a prior: λϕ(f)=−logp(f) \lambda \phi(f) = -\log p(f)
  • MAP: the posterior mode, a “poor man’s Bayesian”

Ridge: a Normal prior

  • Model f(x)=x⊤βf(x) = x^\top\beta. Prior β∼N(0,σβ2I)\beta \sim N(0, \sigma_\beta^2 I), so −logp(β)=12σβ2∥β∥22+const-\log p(\beta) = \frac{1}{2\sigma_\beta^2}\|\beta\|_2^2 + \text{const}
  • ∥β∥22=∑j=1pβj2\|\beta\|_2^2 = \sum_{j=1}^p \beta_j^2. σ2\sigma^2: noise variance. σβ2\sigma_\beta^2: prior variance minimizeβ∥y−Xβ∥22+λ∥β∥22,λ=σ2/σβ2 \underset{\beta}{\mathrm{minimize}}\quad \|y- X\beta\|_2^2 + \lambda \|\beta\|_2^2, \qquad \lambda = \sigma^2/\sigma_\beta^2 β̂ridge=(X⊤X+λI)−1X⊤y,β̂OLS=(X⊤X)−1X⊤y(λ=0) \hat{\beta}_{\text{ridge}} = ( X^\top X + \lambda I )^{-1} X^\top y, \qquad \hat{\beta}_{\text{OLS}} = ( X^\top X )^{-1} X^\top y \;\; (\lambda = 0)
  • λ=0\lambda = 0: maximum likelihood. Large λ\lambda: the prior mean, 0
  • λI\lambda I shifts the eigenvalues of X⊤XX^\top X away from zero: stable when XX is ill-conditioned (Hoerl and Kennard, 1970)
  • Same as: minimize ∥y−Xβ∥22\|y - X\beta\|_2^2 subject to ∥β∥22≤t\|\beta\|_2^2 \le t (Tikhonov, 1943). Choose λ\lambda by cross-validation

Ridge: shrinkage example

Prediction error against λ\lambda

Bias-variance decomposition
  • Simulated: n=50n = 50, p=30p = 30, σ2=1\sigma^2 = 1; 10 large coefficients between 0.5 and 1
  • OLS: squared bias 0.006, variance 0.627, prediction error 1+0.006+0.627=1.6331 + 0.006 + 0.627 = 1.633
  • Ridge at λ≈6\lambda \approx 6: squared bias 0.077, variance 0.402, error 1+0.077+0.402=1.481 + 0.077 + 0.402 = 1.48, about 9% better
  • More bias, much less variance: the prior says coefficients are small

Lasso: a Laplace prior

  • Prior βj∼Laplace(0,b)\beta_j \sim \text{Laplace}(0, b) independently; b>0b > 0: the scale p(βj∣b)=12bexp(−|βj|b) p(\beta_j \mid b) = \frac{1}{2b}\exp\left(-\frac{|\beta_j|}{b}\right) −logp(β)=1b∥β∥1+const,∥β∥1=∑j=1p|βj| -\log p(\beta) = \frac{1}{b} \|\beta\|_1 + \text{const}, \qquad \|\beta\|_1 = \sum_{j=1}^p |\beta_j|

Laplace density, b=1b = 1

minimizeβ∥y−Xβ∥22+λ∥β∥1,λ=2σ2/b \underset{\beta}{\mathrm{minimize}}\quad \|y- X\beta\|_2^2 + \lambda \|\beta\|_1, \qquad \lambda = 2\sigma^2/b

  • No closed form; coordinate descent with soft thresholding: soft(z,γ)=sign(z)(|z|−γ)+\text{soft}(z, \gamma) = \text{sign}(z)(|z| - \gamma)_+
  • Sparsity: many β̂j\hat\beta_j exactly 0. The constraint ∥β∥1≤t\|\beta\|_1 \le t is a diamond with corners on the axes
  • Large prior scale bb means small penalty λ\lambda

Lasso: sparsity and variable selection

# n = 100, p = 20 correlated predictors, sigma = 1
beta_true <- c(3, -2, 1.5, 0, 0, 2, 0, 0, 0, -1, rep(0, 10))
y <- X %*% beta_true + sigma * rnorm(n)
lasso_fit <- glmnet(X, y, alpha = 1)            # path over lambda
cv_lasso  <- cv.glmnet(X, y, alpha = 1, nfolds = 10)

Coefficient paths

Cross-validation

Estimates against truth
  • λmin=0.016\lambda_{\min} = 0.016 (minimum CV error), λ1se=0.1\lambda_{1se} = 0.1 (largest within one standard error)
  • In-sample MSE: 0.68 at λmin\lambda_{\min}, 0.85 at λ1se\lambda_{1se}
  • All true signals kept; some noise variables also kept, fewer at λ1se\lambda_{1se}

Multi-armed bandit experiments

  • Classical A/B tests fix the sample size and wait. A multi-armed bandit adaptively allocates traffic based on accumulating evidence (Google Analytics Content Experiments).
  • Two features: the goal is to find the best or most profitable action (arm), and the randomization distribution is updated as the experiment progresses.
  • Gambler facing slot machines with unknown payoff rates: exploiting arms that performed well versus exploring new or seemingly inferior arms.
  • Gittins: for the discounted problem, optimal policies have an index form. The Gittins index of an arm depends only on its own state (current posterior) and the discount factor; always play the arm with the highest index.
  • It can be computationally intensive. In practice, simpler heuristics like Thompson sampling are widely used and often near-optimal.

The Bayesian approach to bandits

  • KK arms, each with unknown success probability θk\theta_k, k=1,…,Kk = 1, \ldots, K. xkx_k is the number of successes and nkn_k the number of trials for arm kk. With a Beta prior θk∼Beta(α,β)\theta_k \sim \text{Beta}(\alpha, \beta): θk∣xk,nk∼Beta(α+xk,β+nk−xk). \theta_k \mid x_k, n_k \sim \text{Beta}(\alpha + x_k, \beta + n_k - x_k).
  • The probability that arm kk is optimal, computed by Monte Carlo (sample the posteriors and count how often arm kk produces the largest sample): P(θk>θj for all j≠k∣data). P(\theta_k > \theta_j \text{ for all } j \neq k \mid \text{data}).
  • Posterior probabilities of optimality serve directly as allocation weights: an arm that does well receives more traffic, an underperforming arm less.

Thompson sampling

  • One of the oldest heuristics for the bandit problem (1933). For binary outcomes with Beta priors:
  1. Initialize αk=βk=1\alpha_k = \beta_k = 1 for each arm (uniform prior).
  2. At each decision point, sample θ̃k\tilde{\theta}_k from the current posterior Beta(αk,βk)\text{Beta}(\alpha_k, \beta_k) for each arm.
  3. Select the arm with the highest sampled value: at=argmaxkθ̃ka_t = \arg\max_k \tilde{\theta}_k.
  4. Observe the outcome Yt∈{0,1}Y_t \in \{0, 1\} and update: αat←αat+Yt\alpha_{a_t} \leftarrow \alpha_{a_t} + Y_t, βat←βat+(1−Yt)\beta_{a_t} \leftarrow \beta_{a_t} + (1 - Y_t).
  • Arms with high posterior means are selected frequently (exploitation); arms with high posterior variance also have a chance of producing high samples (exploration).
  • As data accumulates, posteriors concentrate around true values and the algorithm increasingly exploits the best arm.

Power and sample size

  • Test H0H_0: no difference between the two pages. Two possible errors:
    • Type I, size α\alpha: declare a difference that is not there. Usual α=0.05\alpha = 0.05
    • Type II, β\beta: miss a difference that is there
  • Power =1−β= 1 - \beta: probability of detecting a difference that is really there
  • Power calculation: the sample size that gives the chosen power for a chosen difference
  • Two conversion rates p1,p2p_1, p_2, average p‾=(p1+p2)/2\bar p = (p_1+p_2)/2, nn visitors per arm: n=(z1−α/22p‾(1−p‾)+z1−βp1(1−p1)+p2(1−p2))2(p2−p1)2 n = \frac{\left(z_{1-\alpha/2}\sqrt{2\bar p(1-\bar p)} + z_{1-\beta}\sqrt{p_1(1-p_1)+p_2(1-p_2)}\right)^2}{(p_2-p_1)^2}
  • zqz_q: normal quantile. z0.975=1.96z_{0.975} = 1.96; z0.95=1.645z_{0.95} = 1.645 (95% power); z0.80=0.842z_{0.80} = 0.842 (80% power)
  • Scott’s example: p1=0.04p_1 = 0.04, p2=0.05p_2 = 0.05, p‾=0.045\bar p = 0.045 n=(1.96×0.293+1.645×0.293)20.012≈11,165 per arm,2n=22,330 n = \frac{(1.96 \times 0.293 + 1.645 \times 0.293)^2}{0.01^2} \approx 11{,}165 \text{ per arm}, \quad 2n = 22{,}330
  • At 100 visits per day: 22,330/100≈22322{,}330/100 \approx 223 days. At 80% power: about 6,700 per arm
  • Small differences are expensive: nn grows like 1/(p2−p1)21/(p_2-p_1)^2

Bandits against classical A/B testing

  • Bandits are just as statistically valid but produce answers far more quickly: traffic moves toward winners gradually, and samples that would go to inferior variations go to potential winners.
  • Example from Scott: a website has a 4% conversion rate, the new variation converts at 5%.
  • Power calculation (previous slide): 22,330 observations, 223 days at 100 visits per day.
  • Scott’s single run ended after 66 days, saving 157 days. Over 500 repetitions the bandit identified the correct arm in 482 (96.4%) and saved an average of 175 days.
  • With more arms the classical approach needs Bonferroni-type corrections; the bandit handles multiple arms through the posterior probabilities.

Reproducing Scott’s bandit experiment

  • Original page converts at 4%, variation at 5%, 100 visits per day. The classical power calculation requires 223 days.
  • Traffic is split by serving weights equal to the current posterior probability that each arm is best, held fixed for the day and refreshed overnight. Day 1 splits traffic evenly.
  • The stopping rule is tested on the posterior that already includes the day’s data.
  • An experiment cannot end before two weeks; without that floor about a fifth of runs declare a winner within five days on a few hundred visits.

Probability that each arm is optimal at the end of each day in a single run of the bandit experiment. The dotted lines mark the 5% and 95% thresholds.
  • The two curves sum to 1 and fluctuate until one crosses the 95% threshold. One run is one draw from a wide distribution, not a target.

Bandit simulation: 500 runs

Distribution of days saved and conversions saved.
Metric Value Scott’s value
Correct arm identified 479 / 500 (95.8%) 482 / 500 (96.4%)
Median days to finish 31 not reported
Average days to finish 55.8 48
Average days saved 167.2 175
Average conversions saved 95.2 97.5
  • Left: days saved. Right: conversions saved, compared to the classical 223-day experiment. The distribution is right-skewed: median 31 days against a mean of 55.8.

Stopping rules, value remaining, and regret

  • Scott describes two complementary stopping criteria. First: the probability that a variation beats the original; at 95% confidence a winner can be declared.
  • Second: the value remaining in the experiment, the expected improvement from switching away from the current champion. End when there is at least a 95% probability that the value remaining is less than 1% of the champion’s conversion rate.
  • Regret: the cumulative cost of not always selecting the optimal arm.
  • Response-adaptive randomization can reduce the number of patients receiving inferior treatments in clinical trials.

Multi-armed bandits: formal setup

  • Multi-armed bandit (MAB): balance exploration (trying new things) and exploitation (sticking with what works), minimizing regret (the difference between our choice and the optimal one). Outcomes must be available immediately or with a small delay.
  • KK alternatives (arms); arm aa has reward distribution fa(y∣θ)f_a(y\mid \theta) and value va(θ)v_a(\theta), its expected reward. yy is the reward and θ\theta the set of unknown parameters to be learned.
  • Online ads: θ=(θ1,…,θK)\theta = (\theta_1,\ldots,\theta_K) are click probabilities of KK independent binomial models, va(θ)=θav_a(\theta) = \theta_a.
  • Website design: dummy variables xcx_c for color (red or blue) and xsx_s for size (27 or 40); each of the four combinations is an arm and va(θ)=P(click∣θ,xc,xs)v_a(\theta) = P(\text{click} \mid \theta, x_c, x_s): logitP(click∣θ)=θ0+θcxc+θsxs+θcsxcxs. \mathrm{logit} P(\text{click} \mid \theta) = \theta_0 + \theta_c x_c + \theta_s x_s + \theta_{cs}x_cx_s .
  • Acting greedily, â=argmaxava(θ̂)\hat a = \arg\max_a v_a(\hat \theta), may miss better alternatives. Thompson sampling samples from each arm’s posterior and picks the highest sample. For Bernoulli rewards its regret is of order logT\log T over TT steps.

When to end experiments

  • Replace the sampling step by the probability that arm aa is the best, then pick the arm from the discrete distribution w1t,…,wKtw_{1t},\ldots,w_{Kt}. Here yt=(y1,…,yt)y^t = (y_1,\ldots,y_t) is the history up to time tt: wat=P(a is optimal ∣yt)=∫P(a is optimal ∣θ)P(θ∣yt)dθ. w_{at} = P(a \text{ is optimal } \mid y^t) = \int P(a \text{ is optimal } \mid \theta) P(\theta \mid y^t) d\theta .
  • Monte Carlo with θ(1),…,θ(G)∼p(θ∣yt)\theta^{(1)},\ldots,\theta^{(G)}\sim p(\theta \mid y^t) and the indicator I(⋅)I(\cdot): the proportion of draws in which the arm was best, wat≈1G∑g=1GI(a=argmaxivi(θ(g))). w_{at}\approx \dfrac{1}{G}\sum_{g=1}^G I(a = \arg\max_i v_i(\theta^{(g)})).
  • Regret is unobservable, but its posterior can be simulated. The champion is the arm that is optimal most often: r(g)=maxava(θ(g))−vat*(θ(g)),at*=argmaxawat. r^{(g)} = \max_a v_a(\theta^{(g)}) - v_{a^*_t}(\theta^{(g)}), \qquad a^*_t = \arg\max_a w_{at}.
  • va(θ(g))v_a(\theta^{(g)}): value (expected reward) of arm aa if the true parameters were draw gg. Conversion rates: va(θ(g))=θa(g)v_a(\theta^{(g)}) = \theta_a^{(g)}. Example: arm 3 converts with probability 0.580.58 in draw 1, so v3(θ(1))=0.58v_3(\theta^{(1)}) = 0.58 conversions per session
  • maxava(θ(g))\max_a v_a(\theta^{(g)}): value of the best arm in draw gg. vat*(θ(g))v_{a^*_t}(\theta^{(g)}): value of the champion in draw gg
  • In percent scale: r(g)←r(g)/vat*(θ(g))r^{(g)} \leftarrow r^{(g)}/v_{a^*_t}(\theta^{(g)}). Regret is positive exactly on the draws in which some arm beats the champion.

Regret example: six posterior draws

  • Three arms with 20, 30 and 40 sessions and 12, 20 and 30 conversions. Uniform prior θi∼Beta(1,1)\theta_i \sim \text{Beta}(1,1).

Six posterior draws of the conversion rates of the three arms:

θ1\theta_1 θ2\theta_2 θ3\theta_3
1 0.60 0.63 0.58
2 0.62 0.62 0.74
3 0.69 0.53 0.67
4 0.49 0.59 0.73
5 0.61 0.51 0.69
6 0.47 0.64 0.69

Posterior probability that each arm is the best, from six draws:

1 2 3
0.17 0.17 0.67

Regret of each of the six draws:

1 2 3 4 5 6
0.09 0 0.03 0 0 0
  • Arm 3 is the champion. Row 1: (0.63−0.58)/0.58=0.09(0.63-0.58)/0.58 = 0.09.

Regret example: 10000 draws

The histogram of the value remaining in an experiment (regret). The vertical line is the 95th percentile, or the potential value remaining.

Posterior probability that each arm is the best, from 10000 draws:

1 2 3
0.08 0.2 0.72
  • Arm 3 has a 72% probability of being best; the value of switching is zero in 72% of cases.
  • The 95th percentile is the potential value remaining, about 17%: one of the other arms might beat arm 3 by as much as 17%.
  • Google Analytics ends the experiment when there is at least a 95% probability that the value remaining is less than 1% of the champion’s conversion rate. Here 17% is well above 1%, so the experiment keeps running.

Contextual bandits

  • The binomial model assumes independent observations with fixed reward probabilities. This can mislead with diverse users or fluctuating activity, for example Asia, Europe and the Americas active at different times of day, or weekday browsing against weekend purchases.
  • Arm A is better on weekdays, arm B on weekends. High traffic can declare A the winner before any weekend is seen; bandits are more vulnerable because their experiments are shorter.
  • Known sub-populations or a proxy: use a logistic regression with context xx. Each arm aa has its own intercept β0a\beta_{0a} (one set to zero as reference) and coefficients βa\beta_a on the context: logitP(clicka∣θ,x)=β0a+βa⊤x,va(θ,x)=logit−1(β0a+βa⊤x). \mathrm{logit} P(\text{click}_a \mid \theta, x) = \beta_{0a} + \beta_a^\top x, \qquad v_a(\theta, x) = \mathrm{logit}^{-1}(\beta_{0a} + \beta_a^\top x).
  • With one β\beta shared by all arms, the ranking of the arms could never change with the context.
  • Unknown contexts: assume they are generated randomly, with a hierarchical beta-binomial model, θ={αa,δa}\theta=\{\alpha_a,\delta_a\} and va(θ)=αa/(αa+δa)v_a(\theta) = \alpha_a/(\alpha_a + \delta_a): θat∼Beta(αa,δa),clicka∣θat∼Bernoulli(θat). \theta_{at} \sim \text{Beta}(\alpha_a,\delta_a), \qquad \text{click}_a \mid \theta_{at} \sim \text{Bernoulli}(\theta_{at}).

Summary of MAB experimentation

  • Design: define the arms (layouts, pricing, campaigns); choose a bandit algorithm: Epsilon-greedy, Thompson Sampling, Upper Confidence Bound (UCB); set the parameters (a higher exploration rate encourages trying new arms earlier).
  • Analysis: define and track the reward metric per arm (clicks, conversions, profit); monitor cumulative reward and arm selection probabilities.
  • Compare arms with confidence intervals or Bayesian methods; adapt by changing parameters, stopping arms with poor performance, or introducing new arms.
  • Set a clear stopping criterion: a predetermined budget, time limit, or desired level of confidence.