Choosing an Algorithm
All algorithms share identical per-arm state. Switching is a single field at campaign creation.
| Algorithm | Value | How it explores | When to use |
|---|---|---|---|
| LinUCB | "linucb" (default) |
Deterministic UCB bonus: θ·x + α·√(x·A⁻¹·x) |
Predictable, tunable. Sweep alpha offline to calibrate exploration. |
| Linear Thompson Sampling | "thompson_sampling" |
Samples θ̃ ~ N(θ, α²·A⁻¹), scores by θ̃·x | Natural Bayesian exploration. alpha=1.0 is the principled default. Concurrent users automatically diversify arm coverage. |
| NeuralLinUCB | {"neural_lin_ucb": {…}} |
Identical UCB formula to LinUCB, but applied to a learned embedding h(x; W) instead of raw context. MLP retrained in batch on its own cadence once retrain_every rewards accumulate. |
Non-linear or high-dimensional contexts where LinUCB has plateaued. Requires the neural feature flag at compile time. |
| NeuralThompsonSampling | {"neural_thompson_sampling": {…}} |
Same MLP embedding and retrain path as NeuralLinUCB, but draws w ~ N(θ, σ²A⁻¹) and scores by w·h(x;W) instead of a UCB bound. | Same non-linear/high-dimensional use case as NeuralLinUCB. Better long-run convergence, higher early regret. Requires the neural feature flag. |
| Progressive (Tournament) | {"progressive": {"base": "linucb", "challenger": {"neural_lin_ucb": {…}}}} |
Runs a linear base and a neural challenger side by side. Every reward updates both. Traffic moves 10 points at a time toward whichever wins repeated SNIPS evaluations, within a 10%–90% band. | You don't know yet whether the reward is non-linear. Starts safe with the linear model and hands over to the neural one only once the data shows it's better. |
LinUCB and Thompson Sampling
db.create_campaign("prices", ["10", "15", "20"], feature_dim=10) # LinUCB, alpha=1.0
db.create_campaign("banners", ["a", "b", "c"], feature_dim=10,
algorithm="thompson_sampling", alpha=1.0)
alpha scales exploration in both: the width of the UCB bonus, or the spread of the posterior draw. Both keep one ridge-regression posterior per arm (A⁻¹, b, θ = A⁻¹b). Each reward updates it in O(d²) with a Sherman–Morrison rank-one update. No matrix is ever inverted.
NeuralThompsonSampling
NeuralThompsonSampling uses the same MLP architecture, configuration and retrain procedure as NeuralLinUCB. Only the exploration step is different. At predict time each arm's weights are drawn from the posterior, w ~ N(θ, α²A⁻¹), in the embedding space, and the arm is scored by w·h(x;W). NeuralLinUCB adds a deterministic upper-confidence bonus instead. Because the scores are random, concurrent requests with near-identical contexts get different arms. A large, homogeneous segment is covered sooner than under a UCB rule, which keeps sending that whole segment to the same arm until its bonus shrinks.
from banditdb import Client, NeuralTSConfig
db = Client("http://localhost:8080", api_key="your-secret-key")
cfg = NeuralTSConfig( # same fields as NeuralLinUCBConfig
context_dim=128, # must equal feature_dim
embed_dim=16,
hidden_dim=64,
hidden_layers=2,
retrain_every=100,
retrain_steps=200,
)
db.create_campaign(
"finmedia_churn",
["no_action", "personalized_digest", "premium_trial",
"analyst_report_gift", "discount_offer", "account_manager_call"],
feature_dim=128,
algorithm=cfg,
)
Over raw HTTP, every field of the config is required:
"algorithm": {"neural_thompson_sampling": {
"context_dim": 128, "embed_dim": 16, "hidden_dim": 64, "hidden_layers": 2,
"retrain_every": 100, "retrain_steps": 200, "learning_rate": 0.001, "lambda": 1.0
}}
| NeuralLinUCB | NeuralThompsonSampling | |
|---|---|---|
| Exploration | Upper confidence bound (deterministic) | Posterior sample (stochastic) |
| Early regret | Lower | Higher |
| Long run (20K+ interactions) | Converges more slowly | Converges further |
| Choose when | Early mistakes are expensive | Long-run performance matters most, or you have large homogeneous segments |
| Based on | Xu et al., ICLR 2022 | Zhang et al., ICLR 2021 |
Progressive (tournament)
Progressive runs two algorithms on one campaign in shadow mode. Every reward updates both models, whichever one served the request. At each evaluation (on the checkpoint cadence) the engine estimates each model's value on the traffic actually served, using SNIPS (self-normalised inverse propensity scoring) and the propensities logged at predict time. If the challenger beats the base by more than 10% in required_wins evaluations in a row, the challenger gets step_bps more traffic. A run of base wins moves traffic back by the same step. Traffic stays between 10% and 90% in both directions, so the losing model keeps enough traffic to be evaluated, and to win its place back.
from banditdb import Client, NeuralLinUCBConfig, ProgressiveConfig
db = Client("http://localhost:8080", api_key="your-secret-key")
cfg = ProgressiveConfig(
base="linucb", # or "thompson_sampling"
challenger=NeuralLinUCBConfig(context_dim=10, embed_dim=32), # or NeuralTSConfig
min_obs=100, # per-arm observations before any traffic moves
required_wins=3, # consecutive wins needed for one step
step_bps=1000, # 1000 basis points = 10% per step
)
db.create_campaign("prices", ["10", "15", "20"], feature_dim=10, algorithm=cfg)
| Field | Default | Meaning |
|---|---|---|
base | "linucb" | Must be linear: "linucb" or "thompson_sampling". |
challenger | — | Must be neural: {"neural_lin_ucb": {…}} or {"neural_thompson_sampling": {…}}, with the full config. |
min_obs | 100 | No traffic shift until every arm has this many buffered observations. |
required_wins | 3 | Consecutive evaluations a side must win to earn one step. |
step_bps | 1000 | Traffic moved per confirmed win run, in basis points. |
Why the challenger must be neural. SNIPS compares the greedy policies the two models imply. A linear base and a linear challenger see the same features and the same rewards, so they learn identical θ. Every evaluation then ties, and the split stays at 90/10 forever. The base must be linear because a campaign has one network, and it belongs to the challenger.
Watch the tournament live:
curl -s http://localhost:8080/campaign/prices/diagnostics \
-H "X-Api-Key: your-secret-key" | jq '{challenger_traffic_pct, tournament_win_streak}'
# { "challenger_traffic_pct": 30.0, "tournament_win_streak": 2 }
See Model Architecture for why the tournament uses evidence before it moves traffic.
Normalise your context vectors. Every algorithm here scores
exploration with a term proportional to the context's magnitude, and the regret
bounds behind LinUCB assume ‖x‖ ≤ 1. Feed raw features on
mixed scales — age next to income next to a 0/1 flag — and the model still
converges, only much more slowly, with nothing in the output to indicate why.
On the UCB shuttle benchmark, unit-L2 scaling alone moved cumulative regret from
2,026 to 709. The Python SDK ships normalize_context() for this.
Retraining runs on a background cadence
(BANDITDB_RETRAIN_POLL_SECS), not as part of checkpointing. Before
2.0.0 the two were coupled, so a retrain held up the checkpoint behind it.
Predictions read an immutable snapshot of the network weights and never wait on
a retrain in progress.
The algorithm field is stored in both the WAL and checkpoint files. Old WAL records and checkpoints without an algorithm field recover as "linucb" automatically.