All studies
Online Learning

Bandit Algorithms: Methods & Implementation Review

Compare exploration strategies and inspect where the experimental implementations differ from standard UCB, Thompson sampling, and nonlinear bandit methods.

Bandits
UCB
Thompson Sampling

Yong-Hwan Lee

Published · Updated

Overview

This Oregon State University study explores how bandit algorithms trade off collecting information and choosing actions with high estimated rewards. It covers independent-arm methods, linear contextual methods, and a squared-reward experiment.

The tables and figures preserve the original recorded results. A source review found differences between several implementations and their standard algorithm names. Those differences are explained alongside the results so that the numbers are interpreted as observations of the implemented variants.

Experiment and metric

For expected reward μt(i)\mu_t(i) and selected arm AtA_t, cumulative pseudo-regret is

RT=∑t=1T[max⁡i∈Atμt(i)−μt(At)].R_T=\sum_{t=1}^{T}\left[\max_{i\in\mathcal A_t}\mu_t(i)-\mu_t(A_t)\right].

The simulator adds the same sampled noise to the selected reward and the comparator reward, so that noise cancels in the recorded difference. With multiple users, its plotted cumulative regret is averaged across users.

The linked simulation's defaults are 25 context dimensions, 25 arms, 10 users, 200,000 iterations, and Gaussian observation noise with standard deviation 0.1. The archive does not pin a seed and complete configuration for every figure, so these defaults should not be assumed to describe every historical run.

A further logging detail matters: the source records every 100 iterations starting at zero and prints the last recorded point. That point need not equal the full-horizon total. The tables below retain the reported cumulative regret, rather than claiming a newly verified final-horizon result.

Explore-then-commit and UCB

Explore-then-commit first gathers observations, then chooses the best empirical arm for the remaining rounds. Its exploration parameter mm controls the initial sampling budget. Too little exploration risks a poor commitment; more exploration also incurs a cost.

Explore-then-commit settingReported regret
m=10m=101001.40
m=20m=20214.90
m=30m=30334.02
View explore-then-commit curves
Explore-then-commit regret, m = 10
Explore-then-commit regret, m = 10
Explore-then-commit regret, m = 20
Explore-then-commit regret, m = 20
Explore-then-commit regret, m = 30
Explore-then-commit regret, m = 30

UCB keeps exploring through a confidence bonus. For an arm with Ni>0N_i>0 previous observations, the implementation uses

score⁡t(i)=μ^i+2αlog⁡tNi,\operatorname{score}_t(i)=\widehat\mu_i+ \sqrt{\frac{2\alpha\log t}{N_i}},

where tt is the number of completed updates. Unobserved arms are selected before this expression is evaluated. The factor α\alpha was missing from the previous explanation, even though it changes the experiment.

UCB settingReported regret
α=0.1\alpha=0.1256.50
α=0.5\alpha=0.5977.03
α=1.0\alpha=1.01906.65
View UCB curves
UCB regret, alpha = 0.1
UCB regret, alpha = 0.1
UCB regret, alpha = 0.5
UCB regret, alpha = 0.5
UCB regret, alpha = 1.0
UCB regret, alpha = 1.0

These runs show sensitivity to exploration settings. They do not establish that the smallest exploration bonus is generally best.

Gaussian reward sampling

The class named ThompsonSamplingGaussianMAB draws an independent score for each arm using

np.random.normal(empirical_mean, 1 / (observation_count + 1))

NumPy's second argument is the standard deviation, not the variance. Therefore, the implemented score distribution is

μ~i∼N(μ^i,1(Ni+1)2),\widetilde\mu_i\sim \mathcal N\left(\widehat\mu_i,\frac{1}{(N_i+1)^2}\right),

when the second parameter in N\mathcal N denotes variance.

The center is an empirical mean, and the shrinking scale is chosen directly in code. Without a corresponding prior and observation model, this is a Gaussian sampling heuristic rather than a derived Bayesian posterior update.

Archived method labelReported regret
Thompson Sampling100.00
View the Gaussian sampling curve
Gaussian sampling heuristic: cumulative regret
Gaussian sampling heuristic: cumulative regret

Linear contextual methods

With context vectors xsx_s, observed rewards rsr_s, and regularization λ>0\lambda>0, define

At=λI+∑s<txsxs⊤,bt=∑s<txsrs,θ^t=At−1bt.A_t=\lambda I+\sum_{s<t}x_sx_s^\top,\qquad b_t=\sum_{s<t}x_sr_s,\qquad \widehat\theta_t=A_t^{-1}b_t.

This is a regularized least-squares estimator for a linear reward model.

LinUCB

LinUCB evaluates an available context xx using

x⊤θ^t+αx⊤At−1x.x^\top\widehat\theta_t+\alpha\sqrt{x^\top A_t^{-1}x}.
LinUCB settingReported regret
α=0.5\alpha=0.524.43
α=1.5\alpha=1.5177.89
α=2.5\alpha=2.5487.73
View LinUCB regret and parameter error
LinUCB regret, alpha = 0.5
LinUCB regret, alpha = 0.5
LinUCB regret, alpha = 1.5
LinUCB regret, alpha = 1.5
LinUCB regret, alpha = 2.5
LinUCB regret, alpha = 2.5
LinUCB parameter error, alpha = 0.5
LinUCB parameter error, alpha = 0.5
LinUCB parameter error, alpha = 1.5
LinUCB parameter error, alpha = 1.5
LinUCB parameter error, alpha = 2.5
LinUCB parameter error, alpha = 2.5

Linear Thompson sampling

In a shared-parameter linear model, a standard Thompson decision samples one parameter vector,

θ~t∼N(θ^t,v2At−1),\widetilde\theta_t\sim\mathcal N(\widehat\theta_t,v^2A_t^{-1}),

then scores every candidate with the same draw, x⊤θ~tx^\top\widetilde\theta_t. The scale vv depends on the chosen posterior or exploration model.

The archived implementation samples inside the candidate-arm loop using covariance At−1A_t^{-1}. It therefore draws a different parameter vector for each candidate. That changes the joint distribution of arm scores and does not implement the shared-draw rule above.

Archived method labelReported regret
Linear Thompson Sampling1098.24
View the archived linear sampling curves
Per-arm parameter sampling: cumulative regret
Per-arm parameter sampling: cumulative regret
Per-arm parameter sampling: parameter error
Per-arm parameter sampling: parameter error

The figure remains useful as a record of that implementation. Its score should not be treated as a benchmark of the standard shared-parameter algorithm.

A nonlinear squared-reward experiment

The nonlinear simulator generates rewards of the form

r=(x⊤θ)2+ϵ,ϵ∼N(0,σ2).r=(x^\top\theta)^2+\epsilon,\qquad \epsilon\sim\mathcal N(0,\sigma^2).

The archived GeneralizedLinearBandit class still estimates θ^=A−1b\widehat\theta=A^{-1}b from linear sufficient statistics, then squares the prediction and adds a linear confidence bonus:

(x⊤θ^)2+αx⊤A−1x.(x^\top\widehat\theta)^2+\alpha\sqrt{x^\top A^{-1}x}.

This is a squared-prediction heuristic, not a validated GLM-UCB implementation. In particular, A−1bA^{-1}b is not the maximum-likelihood estimator for this nonlinear observation model. Under the stated Gaussian noise model, an unregularized maximum-likelihood fit instead minimizes

∑s[rs−(xs⊤θ)2]2.\sum_s\left[r_s-(x_s^\top\theta)^2\right]^2.

That objective is generally nonlinear and nonconvex. The squared link is not globally monotone and cannot distinguish θ\theta from −θ-\theta, so parameter error against only one sign also needs care. Standard generalized-linear-bandit guarantees cannot simply be transferred to this experiment.

Squared-prediction heuristic settingReported regret
α=0.1\alpha=0.162.16
α=0.5\alpha=0.5727.63
α=1.5\alpha=1.55948.48
View the squared-reward experiment curves
Squared-reward heuristic regret, alpha = 0.1
Squared-reward heuristic regret, alpha = 0.1
Squared-reward heuristic regret, alpha = 0.5
Squared-reward heuristic regret, alpha = 0.5
Squared-reward heuristic regret, alpha = 1.5
Squared-reward heuristic regret, alpha = 1.5
Squared-reward heuristic parameter error, alpha = 0.1
Squared-reward heuristic parameter error, alpha = 0.1
Squared-reward heuristic parameter error, alpha = 0.5
Squared-reward heuristic parameter error, alpha = 0.5
Squared-reward heuristic parameter error, alpha = 1.5
Squared-reward heuristic parameter error, alpha = 1.5

The original plots retain their historical “GLM-UCB” labels for traceability; the label does not resolve the estimator mismatch.

Reproduction notes

Use the repository's main branch. Simulation.py selects independent-arm and linear methods; SimulationNonLinear.py selects the nonlinear experiment. Algorithms are enabled in each script's algorithms dictionary.

Before making a new cross-algorithm comparison:

  • Fix environment settings and use matched random seeds across repeated runs.
  • Specify the actual sampling distribution, estimator, and confidence rule.
  • Record the full-horizon total in addition to periodic checkpoints.
  • Report a mean and uncertainty across runs, separately for each reward model.

No training or simulation results were regenerated during this content review. The external repository's implementations remain unchanged.

Takeaways

  • Exploration settings can dominate a single run; one regret value does not determine an algorithm ranking.
  • Standard deviation, covariance, and shared parameter draws change the algorithm being evaluated.
  • A nonlinear prediction formula does not make a linear estimator a nonlinear maximum-likelihood estimator.
  • Reading the implementation is necessary to interpret the experiment's labels and metrics.

Sources

Keep exploring

Attention in Sequence-to-Sequence Models