Chris Collins

Tools · math

Sampling and simulation

Watch probability and statistics happen: the central limit theorem from any distribution (even one you draw), the law of large numbers, confidence-interval coverage, A/B test planning and the cost of peeking, the bootstrap, permutation tests, Simpson's paradox, multiple comparisons, Monty Hall, two-dice events, balls into bins, coupons, inverse-transform and reservoir sampling — all on a seeded generator, so every picture can be linked.

Added

How to use it

Pick a tab. Every simulation runs on a seeded random-number generator: the same settings and the same seed always give the same picture, and New draws moves to the next seed. Under each tab, Link to exactly this gives a short URL that reopens what you see.

⛶ Full screen (or F) gives the open tab’s main plot the whole screen — the browser’s full screen where it is allowed, otherwise a full-window layer (iPhone). The plot is redrawn as tall as the screen allows, and the controls stay above it. Escape leaves.

  • Central limit — choose where one value comes from (a die, a coin, an exponential, the two-spike “0 or 100”, a uniform, the Cauchy, or a histogram you draw by hand by dragging over the left chart) and the sample size n. The tool draws thousands of samples, plots the histogram of their means against the predicted normal curve N(μ, σ²/n), and a tail meter gives the fraction beyond ±1.96 standard errors against the normal’s 5%. With the Cauchy the curve disappears: the spread of the means does not shrink however large n is.

  • Large numbers — running averages of several independent runs on a logarithmic axis, inside the funnel μ ± 1.96σ/√n.

  • Confidence intervals — “catch the mean”: each press adds an interval x̄ ± z*·s/√n (or t*) to a stack, blue if it covers the true mean 50 and red if it misses, with the running coverage. Tick biased sampling to draw from a population shifted by 4: the intervals narrow as n grows, and coverage collapses.

  • A/B planner — baseline rate, minimum detectable effect, α and power give the sample size per group; the plot shows the null and alternative distributions of z moving apart. Run A/B and Run A/A simulate an experiment day by day as a z-score random walk; tick peek to stop at the first crossing. The counter replays many A/A experiments: about 5% are significant at the end, but far more if you peek every day.

  • Bootstrap — resample the data with replacement (watch one resample at a time, stacked by repeats), build the distribution of the median, mean or a percentile, and read off the percentile interval. Paste your own numbers, or use the 200 stand-in response times.

  • Permutation test — shuffle the group labels, recompute Welch’s t each time, and compare the permutation p-value (simulated, and exact over every split when there are few enough) with the t-test’s.

  • Simpson — the kidney-stone data: slide how many small-stone patients each treatment got and watch the overall rates cross, while the rates within each group never change.

  • Multiple tests — m tests per experiment, some or none with a real effect: one experiment’s p-values against α, α/m and the Benjamini–Hochberg line, and the chance of at least one false positive over many experiments, with no correction, Bonferroni or BH.

  • Monty Hall — 3 to 100 doors, the host opening any number of them, and host knows versus host opens at random (games where a random host reveals the car are thrown away). Play one game slowly or thousands quickly; the exact answers are drawn as dashed lines.

  • Two dice — click cells to define events A and B (or pick them from the lists), see P(A), P(B), P(A ∩ B), P(A | B) and P(B | A) as exact fractions, whether A and B are independent, and condition on B fade every outcome outside B. Roll 1,000 times checks it by simulation.

  • Balls & bins — requests thrown at servers with one random choice or the best of two: the tallest stack, the idle fraction against (1 − 1/n)ⁿ ≈ 37%, and a histogram of loads.

  • Coupons — one collection drawn as distinct coupons against draws, the long wait for the last one, and the distribution of collection times against n·Hₙ.

  • Inverse transform — uniform heights u carried across to the CDF and down to x, for the exponential (x = −ln(1 − u)/λ) or a discrete distribution, with the histogram of many samples against the target.

  • Reservoir — Algorithm R step by step (keep item i with probability k/i), and the frequency with which each item ends in the sample over many runs: k/N for every item.

This tool simulates; to calculate with your own data use the Statistics tool (descriptive statistics, intervals, t-, z- and χ² tests, distribution tables) and Probability (exact dice distributions, counting, poker and lottery odds, the birthday problem, Bayes). Counting itself — Pascal’s triangle, stars and bars, derangements, birthday collisions — is in Counting, visually.

Companion to chapters 29–31 of Mathematics, Reconnected: Probability, Random variables and distributions and Statistics.

Limits

  • The generator is mulberry32, seeded from the settings and the seed. It is reproducible and good enough for teaching, not for cryptography. The book’s numbers were produced with Python’s and NumPy’s generators, so the simulated numbers here differ a little from the printed ones (the book gets a permutation p of 0.0067; this tool’s exact count over all 12,870 splits is 86/12,870 ≈ 0.0067, and its simulations land within sampling noise of it). The exact values — n·Hₙ, 2/3, the 14,748 users per group — match exactly.

  • The bootstrap’s stand-in data are 200 lognormal values drawn with this tool’s generator, not the book’s NumPy sample, so its median and p99 differ slightly from the printed 121.8 and 462.8.

  • A/B daily counts above 200 users per arm are drawn from the normal (or, for rare events, Poisson) approximation to the binomial. Intervals in Confidence intervals use z* by default, as the book’s lab does; choose t* for small samples.

  • Simulations run in the page; the largest settings (50,000 experiments of n = 400) take a moment.

Linking to an example

Links name the tab in the hash (#clt, #lln, #ci, #ab, #boot, #perm, #simpson, #multi, #monty, #dice, #bins, #coupon, #inverse, #reservoir) and its settings as short parameters, for example ?dist=exp&n=30&seed=3#clt or ?doors=4&open=2#monty. Parameters apply to the tab in the hash; anything left out takes its default. ?example=<name> loads a book example (and its tab, if the hash names none); other parameters then override it, so ?example=ch30-clt-exponential&n=5 is that example with n = 5. Values are plain numbers or words, so they need no escaping, except lists: separate them with commas (a=212,198,225). Opening a link replaces this browser’s saved settings for the tool; Restore my settings undoes it.

Tab Parameter Meaning Example

all

seed

the random seed (integer); same seed, same picture

seed=7

clt

dist

source: die, coin, exp, spike (0 or 100), uniform, drawn, cauchy

dist=spike

clt

n

sample size, 1–400

n=30

clt

reps

number of sample means, 100–50,000

reps=20000

clt, lln

p

P(heads) for dist=coin

p=0.3

clt, lln

draw

hand-drawn histogram on [0, 1]: 2–60 bar heights, commas

draw=0,1,5,9,5,1,0,0,4,8,4

lln

dist, N, runs

source (as above, except drawn), draws per run, number of runs (1–10)

dist=cauchy&N=100000

ci

pop

population: normal, skewed, bimodal (two-humped); true mean 50

pop=skewed

ci

n, level, crit

sample size, confidence level in %, z or t critical value

n=5&crit=t

ci

bias

shift of the population sampled from (the book’s switch is 4); 0 is unbiased

bias=4

ci

k

number of intervals drawn

k=100

ab

base, mde

baseline rate and minimum detectable effect, as fractions (0.01 = 1 point)

base=0.1&mde=0.005

ab

alpha, power

significance level and power

alpha=0.01&power=0.9

ab

frac

users actually run, as a multiple of the required number

frac=0.5

ab

days, peek

number of daily looks; peek=1 stops at the first crossing

days=15&peek=1

ab

reps

number of A/A replays for the false-positive counter

reps=2000

boot

data

the sample, numbers separated by commas (blank: the stand-in latencies)

data=3,5,8,13,21

boot

stat, reps, level

mean, median, p90, p99; resamples; level in %

stat=p99

perm

a, b, reps

the two groups (commas) and the number of shuffles

a=1,2,3&b=4,5,6

simpson

asmall, bsmall

small-stone patients (of 350) given treatment A and B

asmall=175&bsmall=175

multi

m, alpha, method

tests per experiment, α, none, bonf or bh

m=20&method=bonf

multi

real, reps

how many tests have a real effect; number of experiments

real=10

monty

doors, open, host

doors (3–100), doors the host opens (0: all but one), knows or random

doors=4&open=2

monty

games

games played

games=10000

dice

A, B

events: sum8, sumge10, sumle4, sumgt9, sumlt5, doubles, redeven, redodd, blueeven, blueodd, red6, blue2, six (at least one), redgtblue, or cells as red-blue digit pairs 26.35.44

A=six&B=sumge10

dice

cond

1 fades the outcomes outside B

cond=1

bins

n, balls, d

servers, requests (0: one per server), 1 or 2 choices

n=100&d=2

coupon

n, runs

coupon types; runs for the histogram

n=50

inverse

target, rate, probs, n

exp or disc; λ; discrete probabilities (commas); samples

target=disc&probs=0.5,0.3,0.2

reservoir

N, k, runs

stream length, reservoir size, runs

N=10&k=3

Book examples (?example=):

Example Shows Book

ch29-dice-total-8

P(total 8 | red die even) = 3/18 = 1/6, the book’s conditioning example

ch. 29

ch29-dice-total-8-doubles

A = total 8, D = doubles: A ∩ D = {(4,4)}, A ∪ D has 10 outcomes

ch. 29

ch29-dice-independent

“total is 7” is independent of “red die is even”

ch. 29

ch29-dice-six-given-ten

Exercise: P(at least one six | total ≥ 10) = 5/6

ch. 29

ch29-monty-hall

host knows: switching wins 2/3

ch. 29

ch29-monty-random-host

host opens a door at random: 1/2 and 1/2

ch. 29

ch29-monty-4-doors

Exercise: 4 doors, host opens 2 goats, switching wins 3/4

ch. 29

ch29-monty-100-doors

100 doors, 98 goats opened: switching wins 99/100

ch. 29

ch30-lln-die

three runs of 10 000 die rolls pinned ever closer to 3.5

ch. 30

ch30-lln-coin

the proportion of heads converges to p

ch. 30

ch30-lln-cauchy

Cauchy running means never settle

ch. 30

ch30-clt-exponential

means of 30 exponential(1) values: SD 1/√30, tails 1.4% / 3.3%

ch. 30

ch30-clt-exponential-n1

n = 1: the skewed source itself

ch. 30

ch30-clt-die

means of 5 die rolls

ch. 30

ch30-clt-two-spike

the “0 or 100” distribution, n = 30

ch. 30

ch30-clt-drawn

a hand-drawn two-humped source

ch. 30

ch30-clt-cauchy

Cauchy: averages of 100 are as wild as one value

ch. 30

ch30-coupon-50

50 coupons: n·H_n ≈ 225 draws

ch. 30

ch30-balls-one-choice

one random choice: about 37% idle

ch. 30

ch30-balls-two-choices

best of two choices: a far lower tallest stack

ch. 30

ch30-inverse-exponential

x = −ln(1 − u)/0.5: mean ≈ 2, P(x > 3) ≈ 0.223

ch. 30

ch30-inverse-discrete

walking up the CDF staircase for a, b, c

ch. 30

ch30-reservoir

k = 3 from a stream of 10: every item kept with probability 0.3

ch. 30

ch31-catch-the-mean

fifty 95% intervals from samples of 25

ch. 31

ch31-ci-biased

biased sampling: narrow intervals that miss

ch. 31

ch31-ci-skewed-small-n

skewed population, n = 5, z*: coverage falls short

ch. 31

ch31-ab-plan

10% → 11% at α 0.05, power 80%: about 14 750 per group

ch. 31

ch31-ab-half-point

a 0.5-point lift needs about four times as many

ch. 31

ch31-ab-peeking

15 looks at 1 000 users per arm: false positives ≈ 23%

ch. 31

ch31-bootstrap-median

bootstrap CI for the median of 200 response times

ch. 31

ch31-bootstrap-p99

the p99 interval: wide and lopsided

ch. 31

ch31-permutation-builds

old vs new build: t = 3.23, permutation p ≈ 0.007

ch. 31

ch31-simpson-kidney

kidney stones: A wins in each group, B overall

ch. 31

ch31-simpson-balanced

same case mix for both: the paradox vanishes

ch. 31

ch31-multiple-20

20 tests of true nulls: 64% find “something”

ch. 31

ch31-bonferroni-20

Bonferroni: any false positive ≤ 5%

ch. 31

ch31-bh-screening

Benjamini–Hochberg on 100 tests, 10 real effects

ch. 31

Runs entirely in your browser; nothing you type leaves this page.