Short answer
Thompson sampling picks a price per visitor: draw one plausible conversion rate per price from its Beta posterior, multiply by the price, show the highest. Each price then gets traffic in proportion to its chance of earning the most per visitor. Stop when one price is 95% likely best or expected loss drops under 0.5%.
- After s sales from n visitors, the belief about a conversion rate is Beta(1 + s, 1 + n − s); one draw times the price is one plausible revenue per visitor.
- Showing the price that wins each draw gives every price a share of traffic equal to its probability of being best.
- A Beta draw is X / (X + Y) for two Gamma draws, and Marsaglia and Tsang's method makes Gamma draws in about 20 lines of code.
- Refunds need three outcomes and a Dirichlet posterior: in the example below, ignoring them makes $49 look 88.5% likely to be best when it is a coin flip.
- Batched updates (hourly in the demo, every 5 minutes in production) work because the method is randomized; a 5% floor and a stop rule make it safe on real traffic.
Thompson sampling fits in one sentence: for each visitor, draw a plausible conversion rate for every price from what you have seen so far, multiply each by its price, and show the price that comes out on top. Prices that are probably best get most of the traffic, and the split corrects itself as sales come in.
This article builds it up from three prices to a complete TypeScript implementation with no dependencies, a NumPy version that simulates 30 days of traffic, and the refund-aware model a production engine needs. If you want the bigger picture first (why a bandit at all, and how it compares with epsilon-greedy and UCB), start with multi-armed bandits for pricing.
How does Thompson sampling pick a price?
By sampling from its beliefs instead of trusting its averages. Take three prices after 1,500 visitors each (an illustration):
| Price | Visitors | Sales | Conversion | Revenue per visitor |
|---|---|---|---|---|
| $19 | 1,500 | 41 | 2.73% | $0.52 |
| $29 | 1,500 | 30 | 2.00% | $0.58 |
| $49 | 1,500 | 22 | 1.47% | $0.72 |
$49 looks best, but 22 sales is not much. Its true conversion rate could plausibly sit anywhere from about 1.0% to 2.1%, which puts its revenue per visitor between $0.51 and $1.02 (a 90% interval). Thompson sampling uses that uncertainty directly. For each visitor:
- Draw one plausible conversion rate for each price from its posterior, the belief described in the next section.
- Multiply each draw by its price to get a plausible revenue per visitor.
- Show the visitor the price with the highest result.
Here are five real draws from these posteriors:
| Draw | $19 | $29 | $49 | Shown |
|---|---|---|---|---|
| 1 | 2.80% → $0.53 | 1.88% → $0.55 | 1.34% → $0.66 | $49 |
| 2 | 2.82% → $0.54 | 1.67% → $0.49 | 1.04% → $0.51 | $19 |
| 3 | 3.03% → $0.58 | 2.11% → $0.61 | 1.59% → $0.78 | $49 |
| 4 | 2.40% → $0.46 | 2.00% → $0.58 | 1.60% → $0.78 | $49 |
| 5 | 2.69% → $0.51 | 1.61% → $0.47 | 1.70% → $0.83 | $49 |
Across 4 million draws, $49 wins 75.6% of them, $29 wins 18.9% and $19 wins 5.4%. Those are the probabilities that each price is the best, and they are also the shares of traffic each price gets. That is the whole trick: picking each option with its probability of being optimal, which you get for free by drawing one random parameter per decision (Chapelle and Li, 2011). William R. Thompson proposed it in 1933 for clinical trials; it stayed obscure for decades until studies like Chapelle and Li's showed it matching or beating the popular alternatives.
Why is the belief a Beta distribution?
Because the Beta distribution is the exact posterior for a conversion rate. Start from a uniform prior (every rate from 0% to 100% equally plausible), observe s sales from n visitors, and Bayes' rule gives:
conversion rate ~ Beta(1 + s, 1 + n - s)
posterior mean = (1 + s) / (2 + n)The two 1s are the prior: one imaginary sale and one imaginary non-buyer before the test starts. When purchases are rare, the relative uncertainty is roughly one over the square root of the number of sales: 22 sales leave $49's conversion rate uncertain by about ±21% (one standard deviation), which is why the three curves overlap:
Revenue per visitor is price times conversion, so a draw of one is a draw of the other, scaled by the price. The two numbers you need for decisions come from the same draws:
P(best) of price i = share of draws in which price i has the highest revenue per visitor
expected loss of price i = average over draws of (highest revenue per visitor - price i's revenue per visitor)Scott (2010) computes allocation probabilities exactly this way, as the share of Monte Carlo draws in which each arm comes out on top. Expected loss is what you give up per visitor, on average, if you pick price i and another price is actually better. It drives the stop rule at the end of this article.
Thompson sampling in TypeScript, with no dependencies
The listing below is complete and needs no packages. Run it with npx tsx thompson.ts, bun thompson.ts, deno run thompson.ts, or node thompson.ts on a Node version that runs TypeScript files directly (22.18 and later, or 23.6 and later). It simulates 30 days at 480 visitors a day. The true conversion rates are 2.7%, 2.0% and 1.5%, which the algorithm never sees: $49 truly earns $0.735 per visitor, 27% more than $29's $0.58.
// Thompson sampling for three prices, with no dependencies.
// Run: npx tsx thompson.ts (or node thompson.ts on Node 22.18+, bun thompson.ts, deno run thompson.ts)
type Rng = () => number;
// Seeded generator (mulberry32), so every run prints the same numbers.
function mulberry32(seed: number): Rng {
let a = seed >>> 0;
return () => {
a = (a + 0x6d2b79f5) >>> 0;
let t = a;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
// Standard normal draw (Box-Muller).
function sampleNormal(rng: Rng): number {
let u = 0;
while (u === 0) u = rng();
return Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * rng());
}
// Gamma(shape, 1) draw, Marsaglia and Tsang (2000).
function sampleGamma(rng: Rng, shape: number): number {
if (shape < 1) {
// Gamma(a) = Gamma(a + 1) * U^(1/a)
return sampleGamma(rng, shape + 1) * Math.pow(rng(), 1 / shape);
}
const d = shape - 1 / 3;
const c = 1 / Math.sqrt(9 * d);
for (;;) {
let x: number;
let v: number;
do {
x = sampleNormal(rng);
v = 1 + c * x;
} while (v <= 0);
v = v * v * v;
const u = rng();
if (u < 1 - 0.0331 * x * x * x * x) return d * v;
if (Math.log(u) < 0.5 * x * x + d * (1 - v + Math.log(v))) return d * v;
}
}
// Beta(a, b) draw from two Gamma draws.
function sampleBeta(rng: Rng, a: number, b: number): number {
const x = sampleGamma(rng, a);
const y = sampleGamma(rng, b);
return x / (x + y);
}
interface Arm {
price: number; // dollars
visitors: number;
sales: number;
}
// One Thompson draw: a plausible conversion rate per price, times the price.
function drawRevenuePerVisitor(rng: Rng, arms: Arm[]): number[] {
return arms.map((arm) => arm.price * sampleBeta(rng, 1 + arm.sales, 1 + arm.visitors - arm.sales));
}
function argmax(values: number[]): number {
let best = 0;
for (let i = 1; i < values.length; i++) if (values[i]! > values[best]!) best = i;
return best;
}
// Thompson sampling: show the price that wins this draw.
function choosePrice(rng: Rng, arms: Arm[]): number {
return argmax(drawRevenuePerVisitor(rng, arms));
}
// P(best) and expected loss per price, from many draws.
function summarize(rng: Rng, arms: Arm[], draws = 20_000) {
const wins = arms.map(() => 0);
const loss = arms.map(() => 0);
for (let s = 0; s < draws; s++) {
const values = drawRevenuePerVisitor(rng, arms);
const best = argmax(values);
wins[best]!++;
values.forEach((value, i) => {
loss[i]! += values[best]! - value;
});
}
return arms.map((arm, i) => ({
price: arm.price,
probabilityBest: wins[i]! / draws,
expectedLoss: loss[i]! / draws,
expectedValue: (arm.price * (1 + arm.sales)) / (2 + arm.visitors),
}));
}
// The simulated market. The algorithm never sees TRUE_CONVERSION.
const PRICES = [19, 29, 49];
const TRUE_CONVERSION = [0.027, 0.02, 0.015];
const VISITORS_PER_HOUR = 20;
const DAYS = 30;
const rng = mulberry32(42);
const arms: Arm[] = PRICES.map((price) => ({ price, visitors: 0, sales: 0 }));
const pct = (x: number) => `${(100 * x).toFixed(1)}%`.padStart(7);
console.log(`day ${PRICES.map((p) => `$${p}`.padStart(7)).join("")}`);
for (let day = 1; day <= DAYS; day++) {
const shown = PRICES.map(() => 0);
for (let hour = 0; hour < 24; hour++) {
// Outcomes are folded in once per hour, like a scheduled job.
const batch = PRICES.map(() => ({ visitors: 0, sales: 0 }));
for (let v = 0; v < VISITORS_PER_HOUR; v++) {
const i = choosePrice(rng, arms);
shown[i]!++;
batch[i]!.visitors++;
if (rng() < TRUE_CONVERSION[i]!) batch[i]!.sales++;
}
batch.forEach((b, i) => {
arms[i]!.visitors += b.visitors;
arms[i]!.sales += b.sales;
});
}
if (day === 1 || day % 5 === 0) {
const total = shown.reduce((t, n) => t + n, 0);
console.log(`${String(day).padStart(3)} ${shown.map((n) => pct(n / total)).join("")}`);
}
}
console.log("\nprice visitors sales P(best) expected loss");
for (const r of summarize(rng, arms)) {
const arm = arms.find((a) => a.price === r.price)!;
console.log(
`$${r.price}`.padEnd(7) +
String(arm.visitors).padStart(8) +
String(arm.sales).padStart(7) +
pct(r.probabilityBest).padStart(9) +
`$${r.expectedLoss.toFixed(4)}`.padStart(15),
);
}How it works, top to bottom:
mulberry32is a small seeded random generator, so your output matches the one below. Swap inMath.randomwhen you don't need reproducible runs.sampleGammais Marsaglia and Tsang's method: draw a normalx, cube1 + c·x, and accept the result with a cheap squeeze test, falling back to a log test only when the squeeze fails. Shapes below 1 use the identity Gamma(a) = Gamma(a + 1) × U^(1/a). Posteriors here always have shape 1 or more, so that branch is there for completeness.sampleBetauses a standard identity: if X ~ Gamma(a) and Y ~ Gamma(b) are independent, X / (X + Y) ~ Beta(a, b).choosePriceis Thompson sampling itself: one draw per price, times the price, highest wins.- The loop folds in outcomes once an hour, not after every visitor, the way a scheduled job would.
summarizetakes 20,000 draws to estimate P(best) and expected loss.
The output, identical on Node 24, Bun and Deno:
day $19 $29 $49
1 26.7% 22.1% 51.2%
5 5.2% 15.6% 79.2%
10 2.7% 6.5% 90.8%
15 1.5% 3.8% 94.8%
20 3.1% 5.4% 91.5%
25 1.5% 3.5% 95.0%
30 0.4% 3.8% 95.8%
price visitors sales P(best) expected loss
$19 653 16 1.2% $0.3291
$29 1071 20 4.0% $0.2552
$49 12676 211 94.8% $0.0033Traffic moved to $49 quickly because the gap is large: 79% of day 5, 95% of day 25. The losing prices never hit zero; $19 still got 0.4% of day 30, because a small chance remained that it was best. And after 211 sales at $49, picking it costs a third of a cent per visitor in expectation.
The same simulation in Python with NumPy
NumPy has a Beta sampler built in, so the Python version is shorter. It handles an hour of visitors at once: one row of three draws per visitor, then argmax picks each visitor's price. It needs Python 3.9+ and NumPy.
# Thompson sampling for three prices, with NumPy.
# Run: python thompson.py
import numpy as np
PRICES = np.array([19.0, 29.0, 49.0])
TRUE_CONVERSION = np.array([0.027, 0.020, 0.015]) # hidden from the algorithm
VISITORS_PER_HOUR = 20
DAYS = 30
rng = np.random.default_rng(42)
visitors = np.zeros(3, dtype=np.int64)
sales = np.zeros(3, dtype=np.int64)
print("day" + "".join(f"{'$' + str(int(p)):>8}" for p in PRICES))
for day in range(1, DAYS + 1):
shown_today = np.zeros(3, dtype=np.int64)
for hour in range(24):
# One posterior draw per visitor and price, from the counts as of the last update.
draws = rng.beta(1 + sales, 1 + visitors - sales, size=(VISITORS_PER_HOUR, 3))
choice = np.argmax(draws * PRICES, axis=1)
bought = rng.random(VISITORS_PER_HOUR) < TRUE_CONVERSION[choice]
shown = np.bincount(choice, minlength=3)
# Fold the hour's outcomes in, like a scheduled job.
visitors += shown
sales += np.bincount(choice[bought], minlength=3)
shown_today += shown
if day == 1 or day % 5 == 0:
share = shown_today / shown_today.sum()
print(f"{day:3d}" + "".join(f"{s:8.1%}" for s in share))
# P(best) and expected loss from 20,000 draws.
draws = rng.beta(1 + sales, 1 + visitors - sales, size=(20_000, 3)) * PRICES
p_best = np.bincount(draws.argmax(axis=1), minlength=3) / len(draws)
expected_loss = (draws.max(axis=1, keepdims=True) - draws).mean(axis=0)
print("\nprice visitors sales P(best) expected loss")
for i, price in enumerate(PRICES):
print(f"${int(price):<6}{visitors[i]:8d}{sales[i]:7d}{p_best[i]:9.1%}{'$' + format(expected_loss[i], '.4f'):>15}")The output with NumPy 2.0 (other versions may draw different numbers from the same seed):
day $19 $29 $49
1 20.2% 39.2% 40.6%
5 2.1% 6.2% 91.7%
10 2.7% 2.7% 94.6%
15 26.0% 2.5% 71.5%
20 20.2% 3.5% 76.2%
25 7.7% 8.1% 84.2%
30 15.6% 3.5% 80.8%
price visitors sales P(best) expected loss
$19 1743 58 18.2% $0.0999
$29 899 14 3.6% $0.2605
$49 11758 174 78.2% $0.0132Different random numbers, different path. Here $19 had a lucky stretch (58 sales from 1,743 visitors, 3.3% against a true 2.7%) and won back about a quarter of the traffic around days 15 to 20. While a price might still be best, Thompson sampling keeps testing it. Over 2,000 seeded runs of each version, $49 averaged 70% of the 30 days' traffic in both, which is a good sign the two implementations agree.
How do refunds change the model?
They add a third outcome. A visitor keeps a purchase, refunds it, or never buys. Under a revenue goal, a refund is worth the same as no sale. Under a profit goal it is worth less than no sale if your provider keeps its fee on refunded charges, as this example assumes. So the model has to keep the three outcomes apart.
The three-outcome version of the Beta is the Dirichlet distribution. With a uniform prior:
(pKept, pRefunded, pNone) ~ Dirichlet(1 + kept, 1 + refunded, 1 + none)
value per visitor = pKept × vKept + pRefunded × vRefundedA Dirichlet draw is one Gamma draw per outcome, divided by their sum, so the sampler you already have does the work. Create refunds.ts, paste everything from thompson.ts above the line // The simulated market, then add:
// Dirichlet(alpha) draw: independent Gamma draws, normalized to sum to 1.
function sampleDirichlet(rng: Rng, alphas: number[]): number[] {
const g = alphas.map((a) => sampleGamma(rng, a));
const total = g.reduce((t, x) => t + x, 0);
return g.map((x) => x / total);
}
interface RefundArm {
price: number;
visitors: number;
kept: number; // purchases that were not refunded
refunded: number; // purchases refunded in full
}
type Goal = "revenue" | "profit";
// What a kept and a refunded purchase are worth, in dollars.
function outcomeValues(price: number, goal: Goal, feeRate: number, feeFixed: number) {
if (goal === "revenue") return { kept: price, refunded: 0 };
// Profit: the provider keeps its fee on a refunded charge.
const fee = price * feeRate + feeFixed;
return { kept: price - fee, refunded: -fee };
}
// One posterior draw of the value per visitor.
function drawValue(rng: Rng, arm: RefundArm, goal: Goal, feeRate = 0.05, feeFixed = 0.5): number {
const none = arm.visitors - arm.kept - arm.refunded;
const [pKept, pRefunded] = sampleDirichlet(rng, [1 + arm.kept, 1 + arm.refunded, 1 + none]);
const value = outcomeValues(arm.price, goal, feeRate, feeFixed);
return pKept! * value.kept + pRefunded! * value.refunded;
}
// Example: 6,000 visitors per price; the higher price brings more refunds.
const refundArms: RefundArm[] = [
{ price: 29, visitors: 6000, kept: 114, refunded: 6 },
{ price: 49, visitors: 6000, kept: 67, refunded: 17 },
];
const r = mulberry32(7);
const DRAWS = 20_000;
const pctOf = (x: number) => `${(100 * x).toFixed(1)}%`;
// What the Beta model from the first listing sees if refunds are ignored.
const gross: Arm[] = refundArms.map((a) => ({ price: a.price, visitors: a.visitors, sales: a.kept + a.refunded }));
const grossBest = summarize(r, gross, DRAWS).map((s) => pctOf(s.probabilityBest));
console.log(`ignoring refunds P(best): $29 ${grossBest[0]}, $49 ${grossBest[1]}`);
for (const goal of ["revenue", "profit"] as const) {
const wins = refundArms.map(() => 0);
for (let s = 0; s < DRAWS; s++) {
wins[argmax(refundArms.map((arm) => drawValue(r, arm, goal)))]!++;
}
console.log(`goal ${goal.padEnd(8)} P(best): $29 ${pctOf(wins[0]! / DRAWS)}, $49 ${pctOf(wins[1]! / DRAWS)}`);
}The example is an illustration: 6,000 visitors per price, $29 sold 120 and 6 were refunded, $49 sold 84 and 17 were refunded. The fee of 5% + $0.50 per charge is an example too. Output:
ignoring refunds P(best): $29 11.5%, $49 88.5%
goal revenue P(best): $29 50.8%, $49 49.2%
goal profit P(best): $29 51.8%, $49 48.2%Ignore refunds and $49 earns 18% more per visitor ($0.686 against $0.58) and looks 88.5% likely to be best. Count them and it's a coin flip ($0.547 against $0.551), and under the profit goal $29 edges ahead.
Leverprice's engine models every price this way (how Leverprice tests prices), with two refinements. A kept purchase is valued at the average amount actually kept (coupons and partial refunds included), shrunk toward the list price by 5 pseudo-purchases. And when refunds are worth zero, the kept share alone follows Beta(1 + kept, 2 + refunded + none), which saves one Gamma draw per sample.
What changes when you run it on real traffic?
Four things: how often you update, which prior you start from, how much traffic a losing price keeps, and when you stop.
Update in batches, every few minutes
You can't learn a visitor's outcome the instant they arrive: purchases take minutes, payment webhooks arrive after that, and recomputing on every request is wasteful. Thompson sampling tolerates this because it is randomized, so visitors within one batch still spread across prices. In Chapelle and Li's simulations, UCB's regret went from 2.65 to 3.82 times Thompson sampling's as feedback slowed from every step to every 1,000 steps. Scott (2010) notes that greedy rules suffer here, because they play the same arm for a whole update cycle.
A production system can also compute P(best) explicitly in each batch and turn it into traffic weights, which matches the allocation that per-visitor Thompson sampling gives on average. Our engine does this every 5 minutes with 4,000 draws per price and moves the split halfway toward P(best) to keep it smooth; the SDK on your server then assigns each new visitor by those weights. It also leaves out visitors who arrived less than 30 minutes ago, so people who haven't had time to buy don't drag down the price they saw. With 4,000 draws, a P(best) near 95% is estimated to within about 0.3 percentage points (one standard error).
Pick a prior, or keep the uniform one
Beta(1, 1) says you know nothing; its one imaginary sale matters only until real sales outnumber it. If you know your current price converts about 2%, Beta(2, 98) encodes that with the weight of about 100 visitors. Be careful with untested prices: $49 won't convert like $29, and a confident prior borrowed from the current price costs traffic until the data overrides it. The production engine keeps a uniform prior on the outcomes and spends its prior strength on the average purchase amount instead, as described above.
Keep a minimum share
Pure Thompson sampling can starve a price: $19 got 0.4% of day 30 in the TypeScript run. That is efficient if conversion rates never change and a problem if they do, because a price with no traffic can't prove it was underrated. A floor fixes it. Add this function and call it in the loop instead of choosePrice:
// Give every price at least `floor` of the traffic (0.05 = 5%). Needs floor * prices <= 1.
function choosePriceWithFloor(rng: Rng, arms: Arm[], floor = 0.05): number {
if (rng() < floor * arms.length) return Math.floor(rng() * arms.length);
return choosePrice(rng, arms);
}With three prices and a 5% floor, 15% of visitors are spread at random and the rest follow Thompson sampling, so every price gets at least 5% and the leader at most 90%. Leverprice keeps the same 5% floor by default but applies it to the weights: any price below 5% is raised to it, and the difference comes proportionally out of the other prices' share above 5%.
Decide when to stop
A bandit can run forever; a price test should end with a decision. Two rules work well together:
// Stop when one price is 95% likely to be best, or when picking the leader
// is expected to cost less than 0.5% of its revenue per visitor.
function decide(summary: ReturnType<typeof summarize>): string {
const leader = summary.reduce((a, b) => (b.probabilityBest > a.probabilityBest ? b : a));
const relativeLoss = leader.expectedLoss / leader.expectedValue;
if (leader.probabilityBest >= 0.95) return `clear winner: $${leader.price}`;
if (relativeLoss < 0.005) return `practically equivalent: pick $${leader.price}`;
return "keep testing";
}Call it as decide(summarize(rng, arms)). In the TypeScript run, $49 ends at 94.8%, just short of the first rule, but its expected loss of $0.0033 is 0.4% of its posterior mean of $0.82, so the second rule says stop. These are the thresholds Leverprice uses, and it also refuses to decide before every price has 100 visitors and the test has 10 purchases. In our simulations of the production engine, the calls made on very few sales were the ones most often wrong, so a full week of data before you lock in a winner costs little (bandit vs A/B test for pricing has the numbers).
Frequently asked questions
Why not show the price with the highest revenue per visitor so far?
Because early averages are noisy, and a greedy rule never revisits a price it has dropped. A price that truly converts 1.3% has a 25% chance of showing two or fewer sales in its first 300 visitors, which makes it look like a loser.
How many draws do I need?
One draw per price for each visitor's choice. For reporting P(best), the standard error is the square root of p(1 − p) / draws, so 4,000 draws give about 0.3 percentage points at 95% and 20,000 give about 0.15.
Is Thompson sampling the same as a Bayesian A/B test?
They share the posterior. A Bayesian A/B test keeps a fixed split and uses the posterior to report results; Thompson sampling uses the same posterior to set the split.
Does it work for subscription prices?
It optimizes whatever reward you feed it. Feed it first payments and it finds the price with the best first-payment revenue per visitor; churn that happens months later is invisible to a test that ends in weeks.
Can I test more than three prices?
Yes. Nothing in the code assumes three; add prices to PRICES and TRUE_CONVERSION. Each extra price needs its own evidence before it can be ruled out, so the test takes longer.