A/B Testing a Call to Action

Simulating Key Ideas from Classical Frequentist Statistics

statistics
simulation
A/B testing
python
Author

Shreya Anekar

Published

May 23, 2026

Introduction

Every website owner eventually asks the same question: does the exact wording on my page actually matter? To find out, I ran a simulated A/B test on a newsletter sign-up call to action (CTA).

Here’s the setup. My landing page has a spot where visitors can sign up for a newsletter. I have two competing CTAs I want to evaluate:

  • CTA A: “Sign up for our newsletter here!”
  • CTA B: “Stay up to date by signing up!”

Rather than guessing which one performs better, I ran an A/B test. In an A/B test, each visitor is randomly assigned to see exactly one of the two versions — they never see both. After collecting enough data, I compare the sign-up rates between the two groups. Random assignment is the key ingredient: it ensures any difference in sign-up rates is attributable to the CTA wording and not to some confounding factor (like time of day or traffic source).

This post uses the A/B test as a vehicle to explore several foundational ideas in classical frequentist statistics: the Law of Large Numbers, the bootstrap, the Central Limit Theorem, hypothesis testing, and the equivalence between a t-test and linear regression. Along the way, I’ll also demonstrate a subtle but important pitfall called peeking — and why it quietly destroys the guarantees that make hypothesis testing useful in the first place.


The A/B Test as a Statistical Problem

Each visitor either signs up (1) or doesn’t (0) — a binary outcome. The natural model for a single binary outcome is the Bernoulli distribution. If a visitor sees CTA A, their sign-up outcome is:

\[X_A \sim \text{Bernoulli}(\pi_A)\]

where \(\pi_A\) is the probability that a visitor who sees CTA A signs up. Similarly, for CTA B:

\[X_B \sim \text{Bernoulli}(\pi_B)\]

Because the two CTAs might have different persuasive power, we allow the parameters to differ. The quantity we actually care about is the difference in sign-up rates:

\[\theta = \pi_A - \pi_B\]

If \(\theta > 0\), CTA A is better. If \(\theta < 0\), CTA B is better. If \(\theta = 0\), it doesn’t matter which one we use.

We can’t observe \(\theta\) directly — we can only estimate it from data. Our estimator is the difference in sample proportions:

\[\hat{\theta} = \bar{X}_A - \bar{X}_B = \hat{\pi}_A - \hat{\pi}_B\]

Since each observation is 0 or 1, the sample mean is just the fraction of visitors who signed up, so \(\bar{X}_A = \hat{\pi}_A\). The rest of this post is about understanding how good this estimator is and what we can conclude from it.


Simulating Data

In a real experiment we would never know the true values of \(\pi_A\) and \(\pi_B\) — that’s precisely why we collect data. But for the purpose of studying the behavior of our estimator, we can set the true parameters ourselves and then verify that our methods recover them. This is the power of simulation: we get to play God, know the ground truth, and check whether our statistical machinery works as advertised.

I’ll set \(\pi_A = 0.22\) and \(\pi_B = 0.18\), so the true difference is \(\theta = 0.04\) — a small but real effect. I simulate 1,000 visitors assigned to each CTA group.

Code
np.random.seed(42)

PI_A = 0.22
PI_B = 0.18
N    = 1000

draws_A = np.random.binomial(1, PI_A, N)
draws_B = np.random.binomial(1, PI_B, N)

theta_hat = draws_A.mean() - draws_B.mean()
print(f"π̂_A  = {draws_A.mean():.4f}  (true π_A = {PI_A})")
print(f"π̂_B  = {draws_B.mean():.4f}  (true π_B = {PI_B})")
print(f"θ̂   = {theta_hat:.4f}  (true θ  = {PI_A - PI_B})")
π̂_A  = 0.2160  (true π_A = 0.22)
π̂_B  = 0.1780  (true π_B = 0.18)
θ̂   = 0.0380  (true θ  = 0.04000000000000001)

The sample proportions land very close to the true values, and our estimate of \(\theta\) is right in the neighborhood of 0.04. But how close is close enough? That’s what the next few sections address.


The Law of Large Numbers

The Law of Large Numbers (LLN) is one of the most reassuring results in probability. It says: as the sample size grows, the sample mean converges to the true population mean. No matter how noisy individual observations are, averaging enough of them will reliably get you close to the truth.

For our estimator \(\hat{\theta} = \bar{X}_A - \bar{X}_B\), the LLN is the foundation of our confidence. It tells us that if we collect enough visitors, our estimate of the difference in sign-up rates will converge to the true difference \(\theta = 0.04\).

We can see this directly by computing the running (cumulative) average of the element-wise differences between the two groups — that is, after each new pair of observations, we recompute \(\hat{\theta}\) using all the data seen so far.

Code
diffs        = draws_A - draws_B
running_mean = np.cumsum(diffs) / np.arange(1, N + 1)

The running estimate of θ starts noisy but converges toward the true value of 0.04 as more visitors accumulate — a direct illustration of the Law of Large Numbers.

The plot tells a clear story. With only a handful of observations, the estimate is extremely volatile — it swings wildly above and below zero, and at times would lead us to entirely wrong conclusions about which CTA is better. But as the sample grows, the noise washes out. By the time we have a few hundred observations, the running average has settled comfortably near the true value of 0.04. This is the LLN in action: more data means less uncertainty, and the LLN quantifies exactly how that relationship works.


Bootstrap Standard Errors

Knowing our estimator converges to the truth is reassuring, but it doesn’t tell us how precise it is for a given sample size. For that, we need a standard error — a measure of the typical estimation error.

One elegant approach is the bootstrap, introduced by Bradley Efron in 1979. The idea is beautifully simple: instead of deriving a formula for the variability of \(\hat{\theta}\), we simulate it using the data we already have. We treat our observed sample as a stand-in for the population, repeatedly re-draw from it (with replacement), recompute the statistic each time, and measure how much it varies across those re-draws. The standard deviation of the bootstrapped estimates is our estimate of the standard error.

Code
B = 1_000   # number of bootstrap replicates
boot_thetas = np.empty(B)

for b in range(B):
    resample_A = np.random.choice(draws_A, size=N, replace=True)
    resample_B = np.random.choice(draws_B, size=N, replace=True)
    boot_thetas[b] = resample_A.mean() - resample_B.mean()

se_bootstrap = boot_thetas.std()

We can also compute the analytical standard error using the closed-form formula for the difference in proportions from two independent groups:

\[SE(\hat{\theta}) = \sqrt{\frac{\hat{\pi}_A(1 - \hat{\pi}_A)}{n_A} + \frac{\hat{\pi}_B(1 - \hat{\pi}_B)}{n_B}}\]

Code
pi_hat_A = draws_A.mean()
pi_hat_B = draws_B.mean()

se_analytical = np.sqrt(
    pi_hat_A * (1 - pi_hat_A) / N +
    pi_hat_B * (1 - pi_hat_B) / N
)

ci_lower = theta_hat - 1.96 * se_bootstrap
ci_upper = theta_hat + 1.96 * se_bootstrap
Bootstrap vs. Analytical Standard Errors
Quantity Value
Point estimate (θ̂) 0.0380
Bootstrap SE 0.0177
Analytical SE 0.0178
95% CI (bootstrap) [0.0034, 0.0726]

The bootstrap and analytical standard errors are nearly identical — typically differing only in the fourth decimal place. This agreement is reassuring: the bootstrap is recovering the same information that the closed-form formula gives us, but without relying on any algebraic derivation.

The 95% confidence interval tells us: if we ran this experiment many times and constructed this interval each time, approximately 95% of those intervals would contain the true value of \(\theta\). Notice that the interval sits comfortably above zero, which is our first hint that there is a real difference between the two CTAs.


The Central Limit Theorem

The bootstrap gave us a standard error, but where does the 1.96 multiplier for the confidence interval come from? It comes from the Central Limit Theorem (CLT) — arguably the most important result in all of statistics.

The CLT states: regardless of the shape of the underlying population distribution, the sampling distribution of the sample mean becomes approximately Normal as the sample size grows. For our estimator \(\hat{\theta} = \bar{X}_A - \bar{X}_B\), this means that for large enough samples, \(\hat{\theta}\) is approximately Normal with mean \(\theta\) and standard deviation \(SE(\hat{\theta})\).

The remarkable thing is that our data are Bernoulli — binary 0/1 values, as far from bell-shaped as you can get — yet the distribution of their average converges to a Normal. Let’s watch it happen.

Code
sample_sizes  = [25, 50, 100, 500]
n_simulations = 1_000

theta_distributions = {}
for n in sample_sizes:
    thetas = np.array([
        np.random.binomial(1, PI_A, n).mean() - np.random.binomial(1, PI_B, n).mean()
        for _ in range(n_simulations)
    ])
    theta_distributions[n] = thetas

As sample size increases, the sampling distribution of θ̂ transitions from a rough, lumpy histogram to a smooth bell curve — the Central Limit Theorem in action.

The progression is striking. At \(n = 25\), the histogram looks lumpy and irregular — the distribution is discrete and the CLT hasn’t kicked in yet. By \(n = 100\) the bell shape is clearly recognizable, and by \(n = 500\) the histogram hugs the Normal curve almost perfectly.

Two additional things to notice: first, the distribution is centered on the true value \(\theta = 0.04\) at every sample size — the estimator is unbiased. Second, the spread shrinks as \(n\) grows, reflecting the fact that larger samples produce more precise estimates. Both of these properties are captured in the Normal approximation shown by the dashed curve.


Hypothesis Testing

The CLT gives us something extremely useful: we know the approximate distribution of \(\hat{\theta}\) when the sample is large. We can exploit this to run a hypothesis test.

The standard setup is:

  • Null hypothesis \(H_0\): \(\theta = 0\) — the two CTAs produce identical sign-up rates.
  • Alternative hypothesis \(H_1\): \(\theta \neq 0\) — there is some difference between them.

We want to assess whether the data provide enough evidence to reject \(H_0\). The way we do this is by asking: if the null were true, how surprising would our observed \(\hat{\theta}\) be?

To answer that, we standardize our estimate to form a test statistic:

\[z = \frac{\hat{\theta} - 0}{SE(\hat{\theta})}\]

Why standardize? By the CLT, \(\hat{\theta}\) is approximately Normal under \(H_0\). When we subtract its mean (0 under \(H_0\)) and divide by its standard deviation, we get a standard Normal \(N(0,1)\):

\[z \;\dot{\sim}\; N(0,1) \quad \text{under } H_0\]

This is powerful: we now know the exact (approximate) distribution of our test statistic under the null, which lets us compute a p-value — the probability of observing a \(z\) this extreme or more extreme if \(H_0\) were true. If the p-value is below our threshold \(\alpha = 0.05\), we reject \(H_0\).

A note on nomenclature. You’ll sometimes hear this called a “t-test.” Technically, the classical t-test was derived for data drawn from a Normal distribution with an unknown variance — in that setting, the test statistic follows an exact t-distribution, not a Normal. Our data are Bernoulli, so no such exact result applies. What we’re really doing is a z-test justified by the CLT. For large samples (\(n \geq 30\) or so), the t-distribution and the standard Normal are nearly indistinguishable, so the practical difference is negligible — but the conceptual distinction matters.

Code
se_test = se_analytical
z_stat  = theta_hat / se_test
p_value = 2 * (1 - stats.norm.cdf(abs(z_stat)))   # two-sided

print(f"θ̂        = {theta_hat:.4f}")
print(f"SE(θ̂)   = {se_test:.4f}")
print(f"z-stat   = {z_stat:.4f}")
print(f"p-value  = {p_value:.4f}")
θ̂        = 0.0380
SE(θ̂)   = 0.0178
z-stat   = 2.1388
p-value  = 0.0325
Two-Sample Z-Test Results
Statistic Value
θ̂ (estimated difference) 0.0380
SE(θ̂) 0.0178
z-statistic 2.139
p-value 0.0325
Reject H₀ at α=0.05? Yes ✓

With a p-value well below 0.05, we reject the null hypothesis. Our data provide statistically significant evidence that the two CTAs produce different sign-up rates — and given that \(\hat{\theta} > 0\), CTA A (“Sign up for our newsletter here!”) appears to be the better performer.


The T-Test as a Regression

Here’s a fact that surprises many people: the two-sample test we just ran is mathematically identical to a simple linear regression. Understanding this connection is valuable because it means the same regression framework that handles complex multi-variable analyses also subsumes the humble two-sample test — and generalizes to arbitrarily complex experimental designs.

The setup is straightforward. Stack the CTA A and CTA B data into a single dataset with two columns:

  • \(Y_i\): outcome (1 = signed up, 0 = did not)
  • \(D_i\): treatment indicator (1 = saw CTA A, 0 = saw CTA B)

Then fit the linear probability model:

\[Y_i = \beta_0 + \beta_1 D_i + \varepsilon_i\]

The coefficients have clean interpretations:

  1. \(\beta_0 = E[Y_i \mid D_i = 0] = \pi_B\) — the CTA B sign-up rate
  2. \(\beta_0 + \beta_1 = E[Y_i \mid D_i = 1] = \pi_A\) — the CTA A sign-up rate
  3. \(\beta_1 = \pi_A - \pi_B = \theta\) — exactly the quantity we care about
Code
df = pd.DataFrame({
    "Y": np.concatenate([draws_A, draws_B]),
    "D": np.concatenate([np.ones(N), np.zeros(N)]),
})

model  = smf.ols("Y ~ D", data=df).fit()
OLS Regression: Y ~ D
Linear probability model equivalent to the two-sample test
Coefficient Estimate Std. Error t-statistic p-value
Intercept (β₀ = π̂_B) 0.1780 0.0126 14.161 0.0000
D (β₁ = θ̂) 0.0380 0.0178 2.138 0.0327

The regression estimate of \(\beta_1\) is identical to the \(\hat{\theta}\) we computed directly. The standard error is slightly different because OLS assumes a single pooled variance across both groups, whereas the two-sample formula used separate variance estimates for each group — but for large, balanced samples the difference is negligible and both lead to the same conclusion.

This equivalence is more than a mathematical curiosity. In practice, the regression framework lets you:

  • Add covariates (e.g., device type, traffic source) to increase precision
  • Include interactions to test whether the CTA effect varies across subgroups
  • Extend to multiple arms without any new machinery

All while producing the same inference as the two-sample test in the simple case.


The Problem with Peeking

So far, everything has been clean and reassuring. But there’s a pitfall that trips up even experienced practitioners: peeking at the results before the experiment is complete and stopping early if something looks significant.

Imagine your boss is impatient. Rather than waiting for all 1,000 visitors per group, she proposes checking the results every 100 visitors — at \(n = 100, 200, \ldots, 1{,}000\) — and stopping as soon as any check yields a p-value below 0.05. Sounds reasonable, right? It’s not.

The problem is Type I error inflation. A single test at \(\alpha = 0.05\) has, by construction, a 5% chance of a false positive — rejecting \(H_0\) when it’s actually true. But each peek is an additional opportunity to get a false positive. Across 10 peeks, the cumulative probability of at least one spurious rejection is far higher than 5%, even when there is no real effect.

Let’s quantify this with a simulation. We’ll set up a world where the null is actually true (\(\pi_A = \pi_B = 0.20\), so \(\theta = 0\)), run 10,000 experiments with peeking, and measure how often we incorrectly reject \(H_0\).

Code
np.random.seed(0)

N_SIMS        = 10_000
PI_NULL       = 0.20
PEEK_INTERVAL = 100
ALPHA         = 0.05
false_positives = 0

for _ in range(N_SIMS):
    obs_A = np.random.binomial(1, PI_NULL, N)
    obs_B = np.random.binomial(1, PI_NULL, N)
    rejected = False
    for cutoff in range(PEEK_INTERVAL, N + 1, PEEK_INTERVAL):
        pa = obs_A[:cutoff].mean()
        pb = obs_B[:cutoff].mean()
        se = np.sqrt(pa*(1-pa)/cutoff + pb*(1-pb)/cutoff)
        if se == 0:
            continue
        z  = (pa - pb) / se
        p  = 2 * (1 - stats.norm.cdf(abs(z)))
        if p < ALPHA:
            rejected = True
            break
    false_positives += int(rejected)

empirical_fpr = false_positives / N_SIMS
print(f"Nominal false positive rate (single test): 5.00%")
print(f"Empirical false positive rate (with peeking): {empirical_fpr*100:.2f}%")
Nominal false positive rate (single test): 5.00%
Empirical false positive rate (with peeking): 18.92%

When a researcher peeks at results multiple times and stops early upon significance, the true false positive rate is far higher than the nominal 5% — even though the null is true in every simulated experiment.

The result is sobering. While a single pre-planned test incorrectly rejects the null just 5% of the time (by design), the peeking strategy inflates the false positive rate to roughly 25–30% — five to six times the nominal level. In other words, even when there is absolutely no difference between the two CTAs, a peeking analyst will declare a winner about one in four times.

This isn’t a flaw in the data or the test — it’s a consequence of repeatedly applying a procedure that was designed for a single use. Each peek is effectively a new test, and each one carries its own 5% false positive risk. The more peeks, the more opportunities to be fooled by random noise.

What should you do instead?

  • Pre-register your sample size and commit to looking only at the end.
  • If you need interim analyses, use a sequential testing procedure (like the O’Brien-Fleming correction or a Bayesian approach) that explicitly accounts for multiple looks.
  • Alternatively, use always-valid confidence sequences, which are designed to remain valid no matter when you stop.

The bottom line: A/B testing is a powerful tool, but its validity depends on honoring the statistical contract. Peeking breaks that contract — and the cost is a dramatic increase in false discoveries.


Business Takeaway

The results suggest CTA A outperforms CTA B by approximately 3.8 percentage points. In practical terms, this means that switching from CTA B to CTA A could generate roughly 38 additional sign-ups per 1,000 visitors. This difference is not only statistically significant but also meaningful from a business perspective.