The smallest effect you can see
When traffic is fixed, solve for the minimum detectable effect, and decide whether the test is worth running.
What runs in CodeSplash
- A/B test planning — sample size, MDE and duration Verified in Python and R
You will be able to
- Solve for the minimum detectable effect from fixed traffic
- Decide whether a test can detect anything worth acting on
- Explain why more arms make each comparison harder
Read first
Turn the question around
ReadOften traffic is fixed: you have three weeks and that's it. So ask the other way round: given this traffic, what's the smallest effect the test could reliably detect?
That number is the minimum detectable effectMinimum detectable effectThe smallest true effect a test of this size would detect with the planned power.. If it's larger than anything you could realistically achieve, the test can't help. It can only produce noise you'll be tempted to read.
Would you run this test?
PredictPredict first
You have 21 days, 3,000 users a day, 80% exposed, three arms, and a 20% baseline. Your team's past tests moved this metric by about 2% relative. Is the test worth running?
Reveal
The minimum detectable effect is 6.6% relative. That's about three times what past tests achieved, so this test would probably find nothing even if the change works.
Read the plan
In the app16,800 units per treatment arm, 16,800 control (50,400 total); MDE 6.64% relative, power 0.800.
- Units per treatment arm
- 16,800
- Control units
- 16,800
- Total units
- 50,400
- Relative MDE
- 6.6%
- Absolute MDE
- 0.0133
- Power
- 0.8000
- Days
- 21
Plots
View data
| Power | Effect | Units per treatment arm |
|---|---|---|
| 0.0667 | 5.0% lift | 1,680 |
| 0.0852 | 5.0% lift | 2363.39 |
| 0.1045 | 5.0% lift | 3046.78 |
| 0.1244 | 5.0% lift | 3730.17 |
| 0.1448 | 5.0% lift | 4413.56 |
| 0.1658 | 5.0% lift | 5096.95 |
| 0.1870 | 5.0% lift | 5780.34 |
| 0.2085 | 5.0% lift | 6463.73 |
| 0.2303 | 5.0% lift | 7147.12 |
| 0.2521 | 5.0% lift | 7830.51 |
| 0.2740 | 5.0% lift | 8513.90 |
| 0.2960 | 5.0% lift | 9197.29 |
| 0.3178 | 5.0% lift | 9880.68 |
| 0.3395 | 5.0% lift | 10564.07 |
| 0.3611 | 5.0% lift | 11247.46 |
| 0.3824 | 5.0% lift | 11930.85 |
| 0.4035 | 5.0% lift | 12614.24 |
| 0.4243 | 5.0% lift | 13297.63 |
| 0.4447 | 5.0% lift | 13981.02 |
| 0.4649 | 5.0% lift | 14664.41 |
| 0.4846 | 5.0% lift | 15347.80 |
| 0.5039 | 5.0% lift | 16031.19 |
| 0.5229 | 5.0% lift | 16714.58 |
| 0.5414 | 5.0% lift | 17397.97 |
| 0.5594 | 5.0% lift | 18081.36 |
| 0.5770 | 5.0% lift | 18764.75 |
| 0.5941 | 5.0% lift | 19448.14 |
| 0.6108 | 5.0% lift | 20131.53 |
| 0.6270 | 5.0% lift | 20814.92 |
| 0.6427 | 5.0% lift | 21498.31 |
| 0.6579 | 5.0% lift | 22181.69 |
| 0.6727 | 5.0% lift | 22865.08 |
| 0.6869 | 5.0% lift | 23548.47 |
| 0.7007 | 5.0% lift | 24231.86 |
| 0.7140 | 5.0% lift | 24915.25 |
| 0.7269 | 5.0% lift | 25598.64 |
| 0.7393 | 5.0% lift | 26282.03 |
| 0.7512 | 5.0% lift | 26965.42 |
| 0.7627 | 5.0% lift | 27648.81 |
| 0.7738 | 5.0% lift | 28332.20 |
| 0.7845 | 5.0% lift | 29015.59 |
| 0.7947 | 5.0% lift | 29698.98 |
| 0.8045 | 5.0% lift | 30382.37 |
| 0.8139 | 5.0% lift | 31065.76 |
| 0.8230 | 5.0% lift | 31749.15 |
| 0.8317 | 5.0% lift | 32432.54 |
| 0.8400 | 5.0% lift | 33115.93 |
| 0.8479 | 5.0% lift | 33799.32 |
| 0.8555 | 5.0% lift | 34482.71 |
| 0.8628 | 5.0% lift | 35166.10 |
| 0.8697 | 5.0% lift | 35849.49 |
| 0.8764 | 5.0% lift | 36532.88 |
| 0.8827 | 5.0% lift | 37216.27 |
| 0.8888 | 5.0% lift | 37899.66 |
| 0.8946 | 5.0% lift | 38583.05 |
| 0.9001 | 5.0% lift | 39266.44 |
| 0.9053 | 5.0% lift | 39949.83 |
| 0.9103 | 5.0% lift | 40633.22 |
| 0.9151 | 5.0% lift | 41316.61 |
| 0.9196 | 5.0% lift | 42,000 |
First 60 of 180 rows.
Minimum detectable effect by duration (80% power)
| Weeks | Units per treatment arm | Absolute MDE | Relative MDE |
|---|---|---|---|
| 1 | 5,600 | 0.0228 | 11.4% |
| 2 | 11,200 | 0.0162 | 8.1% |
| 3 | 16,800 | 0.0133 | 6.6% |
| 4 | 22,400 | 0.0115 | 5.8% |
| 5 | 28,000 | 0.0103 | 5.2% |
| 6 | 33,600 | 0.0094 | 4.7% |
| 7 | 39,200 | 0.0087 | 4.4% |
| 8 | 44,800 | 0.0082 | 4.1% |
- Normal approximation for a difference in means; conversion rates use unpooled per-arm variances, ratio metrics the delta method.
- Sizes are rounded up; the power is recomputed at the rounded size and includes both rejection tails for two-sided tests.
- 2 treatment arms vs the control: bonferroni correction, α = 0.025 per comparison.
The exact R script · R R version 4.5.1 (2025-06-13)
# A/B test planning — sample size, MDE and duration
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method experiment.power 1.1.0 · spec c75c5b62af1bb6e9
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
library(codesplashr) # closed-form A/B planning: cs_exp_* (twin of codesplash.experiment.design)
# %% Inputs
solve_for <- "mde" # n | mde | power
kind <- "binary" # binary | continuous | ratio
baseline <- 0.2
mde_type <- "relative" # relative | absolute
mde_in <- 0.05
direction <- "decrease"
alpha <- 0.05
target <- 0.8
alternative <- "two-sided"
arms <- as.integer(3)
allocation <- "equal"
rho <- 0
correction <- if (arms > 2) "bonferroni" else "none"
size_from <- if (solve_for != "n") "days" else "n"
n_given <- 10000
days_given <- 21
daily <- 3000 # eligible units per day (NULL = unknown)
exposure <- 0.8
whole_weeks <- FALSE
max_days <- NULL
warnings <- list()
if (kind == "binary" && !(baseline > 0 && baseline < 1)) stop("A conversion-rate baseline must be between 0 and 1.")
mv <- cs_exp_variance(kind, baseline, sd = 1, num_mean = 0.6, den_mean = 3,
num_sd = 0.9, den_sd = 2, corr = 0.6)
mean0 <- mv[1]
var0 <- mv[2]
if (var0 <= 0) stop("The metric's variance must be positive (check the ratio-metric inputs).")
shrink <- 1 - rho^2 # CUPED keeps 1 - rho^2 of the variance
v0 <- var0 * shrink
comparisons <- arms - 1L
ratio <- cs_exp_allocation(arms, allocation, 1) # control units per treatment unit
lam <- sqrt(1 / (1 + ratio)) # correlation root of two comparisons sharing the control (under H0)
cr <- cs_exp_crit(alpha, comparisons, correction, alternative, lam)
z_crit <- cr[1]
alpha_cmp <- cr[2]
z_pow <- stats::qnorm(target)
sgn <- if (direction == "decrease") -1 else 1
delta_planned <- sgn * (if (mde_type == "relative") mde_in * abs(mean0) else mde_in)
hypothesis <- "superiority" # superiority | noninferiority | equivalence | minimum_effect
margin_hyp <- hypothesis != "superiority"
m_abs <- e_true <- NULL
sds <- c(NA, NA, NA)
if (margin_hyp) {
# Margin tests are one-sided (equivalence: two one-sided tests, each at the per-comparison alpha).
if (solve_for == "mde") stop("With a margin-based hypothesis, solve for the sample size or the power.")
cr <- cs_exp_crit(alpha, comparisons, correction, "one-sided", lam)
z_crit <- cr[1]
alpha_cmp <- cr[2]
scl <- if (mde_type == "relative") abs(mean0) else 1
m_abs <- 0.02 * scl
e_true <- 0 * scl # in the better direction
margin_method <- "fm"
}
# Equivalent idiom (two arms, conversion): power.prop.test(p1 = baseline, p2 = baseline * (1 + mde), power = target)
# %% Solve
n_exact <- NA
if (size_from == "days" && is.null(daily)) stop("Sizes given in days need the eligible units per day.")
if (size_from == "days") {
n_t <- floor(days_given * daily * exposure / (comparisons + ratio))
if (n_t < 2) stop("Too little traffic: fewer than two units per arm in that many days.")
} else if (solve_for != "n") {
n_t <- n_given
}
if (margin_hyp) {
delta <- sgn * e_true
if (kind == "binary" && !(mean0 + delta > 0 && mean0 + delta < 1)) stop("The treatment conversion rate implied by the effect is outside (0, 1).")
v1 <- cs_exp_treatment_variance(kind, mean0, var0, delta) * shrink
sds <- cs_exp_margin_sds(kind, mean0, delta, v1, v0, ratio, m_abs, sgn, margin_method, shrink)
if (solve_for == "n") {
n_exact <- cs_exp_margin_n(hypothesis, e_true, m_abs, sds[1], sds[2], sds[3], z_crit, z_pow, target)
n_t <- as.integer(ceiling(n_exact - 1e-9))
}
power <- cs_exp_margin_power(hypothesis, e_true, m_abs, sds[1], sds[2], sds[3], n_t, z_crit)
} else if (solve_for == "n") {
delta <- delta_planned
v1 <- cs_exp_treatment_variance(kind, mean0, var0, delta) * shrink
if (kind == "binary" && !(mean0 + delta > 0 && mean0 + delta < 1)) stop("The treatment conversion rate implied by the effect is outside (0, 1).")
n_exact <- cs_exp_n(delta, v1, v0, ratio, z_crit, z_pow)
n_t <- as.integer(ceiling(n_exact - 1e-9))
power <- cs_exp_power(delta, v1, v0, n_t, ratio, z_crit, alternative)
} else if (solve_for == "mde") {
delta <- cs_exp_mde(kind, mean0, var0, n_t, ratio, z_crit, z_pow, shrink, direction)
v1 <- cs_exp_treatment_variance(kind, mean0, var0, delta) * shrink
power <- cs_exp_power(delta, v1, v0, n_t, ratio, z_crit, alternative)
} else {
delta <- delta_planned
v1 <- cs_exp_treatment_variance(kind, mean0, var0, delta) * shrink
power <- cs_exp_power(delta, v1, v0, n_t, ratio, z_crit, alternative)
}
n_c <- as.integer(ceiling(ratio * n_t - 1e-9))
n_total <- comparisons * n_t + n_c
mde_abs <- if (margin_hyp) NA else abs(delta)
mde_rel <- if (margin_hyp) NA else if (mean0 != 0) abs(delta / mean0) else NaN
# %% Duration
days <- weeks <- days_exact <- NULL
tables <- list()
if (!is.null(daily)) {
du <- cs_exp_duration(n_total, daily, exposure, whole_weeks)
days <- du$days
weeks <- du$weeks
days_exact <- du$exact
if (!is.null(max_days) && days > max_days) {
warnings <- c(warnings, sprintf("The test needs %d days, longer than the %d-day limit; consider a larger effect, more traffic or CUPED.", days, as.integer(max_days)))
}
rows <- list()
for (w in if (margin_hyp) integer(0) else 1:8) {
nt_w <- floor(7 * w * daily * exposure / (comparisons + ratio))
if (nt_w < 2) next
d_w <- cs_exp_mde(kind, mean0, var0, nt_w, ratio, z_crit, z_pow, shrink, direction)
rows[[length(rows) + 1]] <- list(weeks = w, n = nt_w, mde_abs = abs(d_w), mde_rel = if (mean0 != 0) abs(d_w / mean0) else NULL)
}
if (!margin_hyp) tables <- list(list(
key = "mde_by_duration", title = sprintf("Minimum detectable effect by duration (%.0f%% power)", 100 * target),
columns = list(list(key = "weeks", label = "Weeks", type = "integer"), list(key = "n", label = "Units per treatment arm", type = "integer"),
list(key = "mde_abs", label = "Absolute MDE", type = "number"), list(key = "mde_rel", label = "Relative MDE", type = "percent")),
rows = rows
))
}
if (comparisons > 1 && correction == "none") warnings <- c(warnings, "Several treatment arms without a correction inflate the chance of a false winner.")
# %% Power curve
grid <- pmax(2, n_t * (0.1 + 2.4 * (0:59) / 59))
curve <- NULL
if (margin_hyp) {
curve <- data.frame(x = grid, power = vapply(grid, function(x) cs_exp_margin_power(hypothesis, e_true, m_abs, sds[1], sds[2], sds[3], x, z_crit), numeric(1)),
series = sprintf("true effect %.4g", e_true))
}
for (s in if (margin_hyp) numeric(0) else c(0.75, 1, 1.25)) {
d <- delta * s
if (kind == "binary" && !(mean0 + d > 0 && mean0 + d < 1)) next
v1s <- cs_exp_treatment_variance(kind, mean0, var0, d) * shrink
label <- if (is.finite(mde_rel)) sprintf("%.1f%% lift", 100 * s * mde_rel) else sprintf("Δ = %.4g", d)
curve <- rbind(curve, data.frame(x = grid, power = vapply(grid, function(x) cs_exp_power(d, v1s, v0, x, ratio, z_crit, alternative), numeric(1)), series = label))
}
# %% Result
stats_list <- list(
list(key = "n_treatment", label = "Units per treatment arm", value = n_t, format = "integer"),
list(key = "n_control", label = "Control units", value = n_c, format = "integer"),
list(key = "n_total", label = "Total units", value = n_total, format = "integer"),
list(key = "power", label = "Power", value = power)
)
stats_list <- append(stats_list, if (margin_hyp) list(list(key = "margin_abs", label = "Margin", value = m_abs))
else list(list(key = "mde_rel", label = "Relative MDE", value = mde_rel, format = "percent"),
list(key = "mde_abs", label = "Absolute MDE", value = mde_abs)), after = 3)
if (!is.null(days)) stats_list <- c(stats_list, list(list(key = "days", label = "Days", value = days, format = "integer")))
cs_publish(list(
method = "experiment.power",
methodVersion = "1.1.0",
title = "A/B test plan",
engine = cs_engine_info("stats"),
summary = if (!margin_hyp) sprintf("%s units per treatment arm, %s control (%s total); MDE %.2f%% relative, power %.3f.",
format(n_t, big.mark = ","), format(n_c, big.mark = ","), format(n_total, big.mark = ","), 100 * mde_rel, power)
else sprintf("%s: %s units per treatment arm, %s control (%s total); margin %.4g, power %.3f.", gsub("_", " ", hypothesis),
format(n_t, big.mark = ","), format(n_c, big.mark = ","), format(n_total, big.mark = ","), m_abs, power),
statistics = stats_list,
tables = tables,
warnings = warnings,
notes = c("Normal approximation for a difference in means; conversion rates use unpooled per-arm variances, ratio metrics the delta method.",
"Sizes are rounded up; the power is recomputed at the rounded size and includes both rejection tails for two-sided tests.",
if (comparisons > 1) sprintf("%d treatment arms vs the control: %s correction, α = %.4g per comparison.", comparisons, correction, alpha_cmp),
if (rho > 0) sprintf("CUPED with ρ = %g keeps %.1f%% of the variance.", rho, 100 * shrink)),
charts = list(cs_chart(
"power", "power-curve", "Power by units per treatment arm",
list(x = cs_col("Units per treatment arm", curve$x), power = cs_col("Power", curve$power), series = cs_col("Effect", curve$series)),
controls = list(target = if (solve_for != "power") target else 0.8)
)),
values = list(
n_treatment = n_t, n_control = n_c, n_total = n_total, n_exact = n_exact, power = power,
target_power = if (solve_for != "power") target else NA, mde_abs = mde_abs, mde_rel = mde_rel,
alpha = alpha, alpha_comparison = alpha_cmp, z_crit = z_crit, baseline_mean = mean0,
variance_control = v0, variance_treatment = v1, cuped_factor = shrink, control_ratio = ratio,
comparisons = comparisons, days = if (is.null(days)) NA else days, weeks = if (is.null(days)) NA else weeks,
days_exact = if (is.null(days_exact)) NA else days_exact,
hypothesis = hypothesis, margin_abs = if (is.null(m_abs)) NA else m_abs, true_effect_abs = if (is.null(e_true)) NA else e_true,
sd_alternative = sds[1], sd_null_lower = sds[2], sd_null_upper = sds[3]
)
))
16,800 units per treatment arm, 16,800 control (50,400 total); MDE 6.64% relative, power 0.800.
- Units per treatment arm
- 16,800
- Control units
- 16,800
- Total units
- 50,400
- Relative MDE
- 6.6%
- Absolute MDE
- 0.0133
- Power
- 0.8000
- Days
- 21
To keep this page light, the plots are shown once, under the R tab.
Minimum detectable effect by duration (80% power)
| Weeks | Units per treatment arm | Absolute MDE | Relative MDE |
|---|---|---|---|
| 1 | 5,600 | 0.0228 | 11.4% |
| 2 | 11,200 | 0.0162 | 8.1% |
| 3 | 16,800 | 0.0133 | 6.6% |
| 4 | 22,400 | 0.0115 | 5.8% |
| 5 | 28,000 | 0.0103 | 5.2% |
| 6 | 33,600 | 0.0094 | 4.7% |
| 7 | 39,200 | 0.0087 | 4.4% |
| 8 | 44,800 | 0.0082 | 4.1% |
- Normal approximation for a difference in means; conversion rates use unpooled per-arm variances, ratio metrics the delta method.
- Sizes are rounded up; the power is recomputed at the rounded size and includes both rejection tails for two-sided tests.
- 2 treatment arms vs the control: bonferroni correction, α = 0.025 per comparison.
The exact Python script · Python Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# A/B test planning — sample size, MDE and duration
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method experiment.power 1.1.0 · spec c75c5b62af1bb6e9
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
import math
from scipy.special import ndtri
import codesplash as cs
from codesplash.experiment import design as xd # closed-form A/B planning (twin of R cs_exp_*)
# %% Inputs
solve_for = "mde" # n | mde | power
kind = "binary" # binary | continuous | ratio
baseline = 0.2
mde_type = "relative" # relative | absolute
mde_in = 0.05
direction = "decrease"
alpha = 0.05
target = 0.8
alternative = "two-sided"
arms = int(3)
allocation = "equal"
rho = 0
correction = "bonferroni" if arms > 2 else "none"
size_from = "days" if solve_for != "n" else "n"
n_given = 10000
days_given = 21
daily = 3000 # eligible units per day (None = unknown)
exposure = 0.8
whole_weeks = False
max_days = None
warnings = []
if kind == "binary" and not 0 < baseline < 1:
raise ValueError("A conversion-rate baseline must be between 0 and 1.")
mean0, var0 = xd.unit_variance(
kind, baseline, sd=1, num_mean=0.6, den_mean=3,
num_sd=0.9, den_sd=2, corr=0.6,
)
if var0 <= 0:
raise ValueError("The metric's variance must be positive (check the ratio-metric inputs).")
shrink = 1.0 - rho**2 # CUPED keeps 1 − ρ² of the variance
v0 = var0 * shrink
comparisons = arms - 1
ratio = xd.allocation(arms, allocation, 1) # control units per treatment unit
lam = math.sqrt(1.0 / (1.0 + ratio)) # correlation root of two comparisons sharing the control (under H0)
z_crit, alpha_cmp = xd.crit(alpha, comparisons, correction, alternative, lam)
z_pow = float(ndtri(target))
sign = -1.0 if direction == "decrease" else 1.0
delta_planned = sign * (mde_in * abs(mean0) if mde_type == "relative" else mde_in)
hypothesis = "superiority" # superiority | noninferiority | equivalence | minimum_effect
margin_hyp = hypothesis != "superiority"
m_abs = e_true = None
sds = (None, None, None)
if margin_hyp:
# Margin tests are one-sided (equivalence: two one-sided tests, each at the per-comparison α).
if solve_for == "mde":
raise ValueError("With a margin-based hypothesis, solve for the sample size or the power.")
z_crit, alpha_cmp = xd.crit(alpha, comparisons, correction, "one-sided", lam)
scale = abs(mean0) if mde_type == "relative" else 1.0
m_abs = 0.02 * scale
e_true = 0 * scale # in the better direction
margin_method = "fm"
# Equivalent idiom (two arms, conversion): power.prop.test in R / statsmodels NormalIndPower in Python.
# %% Solve
n_exact = None
if size_from == "days" and daily is None:
raise ValueError("Sizes given in days need the eligible units per day.")
if size_from == "days":
n_t = math.floor(days_given * daily * exposure / (comparisons + ratio))
if n_t < 2:
raise ValueError("Too little traffic: fewer than two units per arm in that many days.")
elif solve_for != "n":
n_t = n_given
if margin_hyp:
delta = sign * e_true
if kind == "binary" and not 0 < mean0 + delta < 1:
raise ValueError("The treatment conversion rate implied by the effect is outside (0, 1).")
v1 = xd.treatment_variance(kind, mean0, var0, delta) * shrink
sds = xd.margin_sds(kind, mean0, delta, v1, v0, ratio, m_abs, sign, margin_method, shrink)
if solve_for == "n":
n_exact = xd.margin_n(hypothesis, e_true, m_abs, *sds, z_crit, z_pow, target)
n_t = int(math.ceil(n_exact - 1e-9))
power = xd.margin_power(hypothesis, e_true, m_abs, *sds, n_t, z_crit)
elif solve_for == "n":
delta = delta_planned
v1 = xd.treatment_variance(kind, mean0, var0, delta) * shrink
if kind == "binary" and not 0 < mean0 + delta < 1:
raise ValueError("The treatment conversion rate implied by the effect is outside (0, 1).")
n_exact = xd.n_required(delta, v1, v0, ratio, z_crit, z_pow)
n_t = int(math.ceil(n_exact - 1e-9))
power = xd.power(delta, v1, v0, n_t, ratio, z_crit, alternative)
elif solve_for == "mde":
delta = xd.mde(kind, mean0, var0, n_t, ratio, z_crit, z_pow, shrink, direction)
v1 = xd.treatment_variance(kind, mean0, var0, delta) * shrink
power = xd.power(delta, v1, v0, n_t, ratio, z_crit, alternative)
else:
delta = delta_planned
v1 = xd.treatment_variance(kind, mean0, var0, delta) * shrink
power = xd.power(delta, v1, v0, n_t, ratio, z_crit, alternative)
n_c = int(math.ceil(ratio * n_t - 1e-9))
n_total = comparisons * n_t + n_c
mde_abs = abs(delta) if not margin_hyp else None
mde_rel = (abs(delta / mean0) if mean0 != 0 else math.nan) if not margin_hyp else None
# %% Duration
days = weeks = days_exact = None
tables = []
if daily is not None:
days, weeks, days_exact = xd.duration(n_total, daily, exposure, whole_weeks)
if max_days is not None and days > max_days:
warnings.append(f"The test needs {days} days, longer than the {int(max_days)}-day limit; consider a larger effect, more traffic or CUPED.")
rows = []
for w in (range(1, 9) if not margin_hyp else []):
nt_w = math.floor(7 * w * daily * exposure / (comparisons + ratio))
if nt_w < 2:
continue
d_w = xd.mde(kind, mean0, var0, nt_w, ratio, z_crit, z_pow, shrink, direction)
rows.append({"weeks": w, "n": nt_w, "mde_abs": abs(d_w), "mde_rel": abs(d_w / mean0) if mean0 else None})
tables.extend([] if margin_hyp else [{
"key": "mde_by_duration", "title": f"Minimum detectable effect by duration ({target:.0%} power)",
"columns": [{"key": "weeks", "label": "Weeks", "type": "integer"}, {"key": "n", "label": "Units per treatment arm", "type": "integer"},
{"key": "mde_abs", "label": "Absolute MDE", "type": "number"}, {"key": "mde_rel", "label": "Relative MDE", "type": "percent"}],
"rows": rows,
}])
if comparisons > 1 and correction == "none":
warnings.append("Several treatment arms without a correction inflate the chance of a false winner.")
# %% Power curve
grid = [max(2.0, n_t * (0.1 + 2.4 * i / 59)) for i in range(60)]
curve = []
if margin_hyp:
curve = [(x, xd.margin_power(hypothesis, e_true, m_abs, *sds, x, z_crit), f"true effect {e_true:.4g}") for x in grid]
for s in ((0.75, 1.0, 1.25) if not margin_hyp else ()):
d = delta * s
if kind == "binary" and not 0 < mean0 + d < 1:
continue
v1s = xd.treatment_variance(kind, mean0, var0, d) * shrink
label = f"{s * mde_rel:.1%} lift" if math.isfinite(mde_rel) else f"Δ = {d:.4g}"
curve += [(x, xd.power(d, v1s, v0, x, ratio, z_crit, alternative), label) for x in grid]
# %% Result
cs.publish({
"method": "experiment.power",
"methodVersion": "1.1.0",
"title": "A/B test plan",
"engine": cs.engine_info("scipy"),
"summary": (f"{n_t:,} units per treatment arm, {n_c:,} control ({n_total:,} total); MDE {mde_rel:.2%} relative, power {power:.3f}." if not margin_hyp
else f"{hypothesis.replace('_', ' ')}: {n_t:,} units per treatment arm, {n_c:,} control ({n_total:,} total); margin {m_abs:.4g}, power {power:.3f}."),
"statistics": [
{"key": "n_treatment", "label": "Units per treatment arm", "value": n_t, "format": "integer"},
{"key": "n_control", "label": "Control units", "value": n_c, "format": "integer"},
{"key": "n_total", "label": "Total units", "value": n_total, "format": "integer"},
*([{"key": "mde_rel", "label": "Relative MDE", "value": mde_rel, "format": "percent"},
{"key": "mde_abs", "label": "Absolute MDE", "value": mde_abs}] if not margin_hyp else
[{"key": "margin_abs", "label": "Margin", "value": m_abs}]),
{"key": "power", "label": "Power", "value": power},
*([{"key": "days", "label": "Days", "value": days, "format": "integer"}] if days is not None else []),
],
"tables": tables,
"warnings": warnings,
"notes": [
"Normal approximation for a difference in means; conversion rates use unpooled per-arm variances, ratio metrics the delta method.",
"Sizes are rounded up; the power is recomputed at the rounded size and includes both rejection tails for two-sided tests.",
*([f"{comparisons} treatment arms vs the control: {correction} correction, α = {alpha_cmp:.4g} per comparison."] if comparisons > 1 else []),
*([f"CUPED with ρ = {rho:g} keeps {shrink:.1%} of the variance."] if rho > 0 else []),
],
"charts": [cs.chart(
"power", "power-curve", "Power by units per treatment arm",
{"x": cs.col("Units per treatment arm", [c[0] for c in curve]), "power": cs.col("Power", [c[1] for c in curve]),
"series": cs.col("Effect", [c[2] for c in curve])},
controls={"target": target if solve_for != "power" else 0.8},
)],
"values": {
"n_treatment": n_t, "n_control": n_c, "n_total": n_total, "n_exact": n_exact, "power": power,
"target_power": target if solve_for != "power" else None, "mde_abs": mde_abs, "mde_rel": mde_rel,
"alpha": alpha, "alpha_comparison": alpha_cmp, "z_crit": z_crit, "baseline_mean": mean0,
"variance_control": v0, "variance_treatment": v1, "cuped_factor": shrink, "control_ratio": ratio,
"comparisons": comparisons, "days": days, "weeks": weeks if days is not None else None, "days_exact": days_exact,
"hypothesis": hypothesis, "margin_abs": m_abs, "true_effect_abs": e_true, "sd_alternative": sds[0],
"sd_null_lower": sds[1], "sd_null_upper": sds[2],
},
})
This plan splits 50,400 users across three arms. With two comparisons against control, each one gets 0.0250 after correction.
More arms, less power per arm
ReadEvery extra arm costs you twice: fewer users in each arm, and a stricter threshold for each comparison.
One partial remedy is to give control more users than each treatment, since control appears in every comparison. The usual rule makes the control arm times larger, where is the number of treatments.
47,543 units per treatment arm, 82,347 control (224,976 total); MDE 10.00% relative, power 0.800.
- Units per treatment arm
- 47,543
- Control units
- 82,347
- Total units
- 224,976
- Relative MDE
- 10.0%
- Absolute MDE
- 0.0035
- Power
- 0.8000
- Days
- 7
Plots
View data
| Power | Effect | Units per treatment arm |
|---|---|---|
| 0.0555 | 7.5% lift | 4754.30 |
| 0.0728 | 7.5% lift | 6688.25 |
| 0.0911 | 7.5% lift | 8622.21 |
| 0.1104 | 7.5% lift | 10556.16 |
| 0.1303 | 7.5% lift | 12490.11 |
| 0.1510 | 7.5% lift | 14424.06 |
| 0.1721 | 7.5% lift | 16358.02 |
| 0.1938 | 7.5% lift | 18291.97 |
| 0.2157 | 7.5% lift | 20225.92 |
| 0.2380 | 7.5% lift | 22159.87 |
| 0.2604 | 7.5% lift | 24093.83 |
| 0.2829 | 7.5% lift | 26027.78 |
| 0.3055 | 7.5% lift | 27961.73 |
| 0.3280 | 7.5% lift | 29895.68 |
| 0.3504 | 7.5% lift | 31829.64 |
| 0.3726 | 7.5% lift | 33763.59 |
| 0.3947 | 7.5% lift | 35697.54 |
| 0.4164 | 7.5% lift | 37631.49 |
| 0.4379 | 7.5% lift | 39565.45 |
| 0.4590 | 7.5% lift | 41499.40 |
| 0.4798 | 7.5% lift | 43433.35 |
| 0.5002 | 7.5% lift | 45367.30 |
| 0.5201 | 7.5% lift | 47301.26 |
| 0.5395 | 7.5% lift | 49235.21 |
| 0.5586 | 7.5% lift | 51169.16 |
| 0.5771 | 7.5% lift | 53103.11 |
| 0.5951 | 7.5% lift | 55037.07 |
| 0.6126 | 7.5% lift | 56971.02 |
| 0.6296 | 7.5% lift | 58904.97 |
| 0.6461 | 7.5% lift | 60838.92 |
| 0.6620 | 7.5% lift | 62772.88 |
| 0.6775 | 7.5% lift | 64706.83 |
| 0.6924 | 7.5% lift | 66640.78 |
| 0.7067 | 7.5% lift | 68574.73 |
| 0.7206 | 7.5% lift | 70508.69 |
| 0.7340 | 7.5% lift | 72442.64 |
| 0.7469 | 7.5% lift | 74376.59 |
| 0.7592 | 7.5% lift | 76310.54 |
| 0.7711 | 7.5% lift | 78244.50 |
| 0.7825 | 7.5% lift | 80178.45 |
| 0.7934 | 7.5% lift | 82112.40 |
| 0.8039 | 7.5% lift | 84046.35 |
| 0.8140 | 7.5% lift | 85980.31 |
| 0.8236 | 7.5% lift | 87914.26 |
| 0.8328 | 7.5% lift | 89848.21 |
| 0.8416 | 7.5% lift | 91782.16 |
| 0.8499 | 7.5% lift | 93716.12 |
| 0.8579 | 7.5% lift | 95650.07 |
| 0.8656 | 7.5% lift | 97584.02 |
| 0.8729 | 7.5% lift | 99517.97 |
| 0.8798 | 7.5% lift | 101451.93 |
| 0.8864 | 7.5% lift | 103385.88 |
| 0.8927 | 7.5% lift | 105319.83 |
| 0.8986 | 7.5% lift | 107253.78 |
| 0.9043 | 7.5% lift | 109187.74 |
| 0.9097 | 7.5% lift | 111121.69 |
| 0.9148 | 7.5% lift | 113055.64 |
| 0.9197 | 7.5% lift | 114989.59 |
| 0.9243 | 7.5% lift | 116923.55 |
| 0.9286 | 7.5% lift | 118857.50 |
First 60 of 180 rows.
Minimum detectable effect by duration (80% power)
| Weeks | Units per treatment arm | Absolute MDE | Relative MDE |
|---|---|---|---|
| 1 | 59,170 | 0.0031 | 8.9% |
| 2 | 118,341 | 0.0022 | 6.3% |
| 3 | 177,512 | 0.0018 | 5.1% |
| 4 | 236,683 | 0.0015 | 4.4% |
| 5 | 295,854 | 0.0014 | 3.9% |
| 6 | 355,025 | 0.0013 | 3.6% |
| 7 | 414,196 | 0.0012 | 3.3% |
| 8 | 473,367 | 0.0011 | 3.1% |
- Normal approximation for a difference in means; conversion rates use unpooled per-arm variances, ratio metrics the delta method.
- Sizes are rounded up; the power is recomputed at the rounded size and includes both rejection tails for two-sided tests.
- 3 treatment arms vs the control: dunnett correction, α = 0.01786 per comparison.
The exact R script · R R version 4.5.1 (2025-06-13)
# A/B test planning — sample size, MDE and duration
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method experiment.power 1.1.0 · spec d7b85c41b27320b5
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
library(codesplashr) # closed-form A/B planning: cs_exp_* (twin of codesplash.experiment.design)
# %% Inputs
solve_for <- "n" # n | mde | power
kind <- "binary" # binary | continuous | ratio
baseline <- 0.035
mde_type <- "relative" # relative | absolute
mde_in <- 0.1
direction <- "increase"
alpha <- 0.05
target <- 0.8
alternative <- "two-sided"
arms <- as.integer(4)
allocation <- "sqrt"
rho <- 0
correction <- if (arms > 2) "dunnett" else "none"
size_from <- if (solve_for != "n") "n" else "n"
n_given <- 10000
days_given <- 14
daily <- 40000 # eligible units per day (NULL = unknown)
exposure <- 1
whole_weeks <- TRUE
max_days <- 21
warnings <- list()
if (kind == "binary" && !(baseline > 0 && baseline < 1)) stop("A conversion-rate baseline must be between 0 and 1.")
mv <- cs_exp_variance(kind, baseline, sd = 1, num_mean = 0.6, den_mean = 3,
num_sd = 0.9, den_sd = 2, corr = 0.6)
mean0 <- mv[1]
var0 <- mv[2]
if (var0 <= 0) stop("The metric's variance must be positive (check the ratio-metric inputs).")
shrink <- 1 - rho^2 # CUPED keeps 1 - rho^2 of the variance
v0 <- var0 * shrink
comparisons <- arms - 1L
ratio <- cs_exp_allocation(arms, allocation, 1) # control units per treatment unit
lam <- sqrt(1 / (1 + ratio)) # correlation root of two comparisons sharing the control (under H0)
cr <- cs_exp_crit(alpha, comparisons, correction, alternative, lam)
z_crit <- cr[1]
alpha_cmp <- cr[2]
z_pow <- stats::qnorm(target)
sgn <- if (direction == "decrease") -1 else 1
delta_planned <- sgn * (if (mde_type == "relative") mde_in * abs(mean0) else mde_in)
hypothesis <- "superiority" # superiority | noninferiority | equivalence | minimum_effect
margin_hyp <- hypothesis != "superiority"
m_abs <- e_true <- NULL
sds <- c(NA, NA, NA)
if (margin_hyp) {
# Margin tests are one-sided (equivalence: two one-sided tests, each at the per-comparison alpha).
if (solve_for == "mde") stop("With a margin-based hypothesis, solve for the sample size or the power.")
cr <- cs_exp_crit(alpha, comparisons, correction, "one-sided", lam)
z_crit <- cr[1]
alpha_cmp <- cr[2]
scl <- if (mde_type == "relative") abs(mean0) else 1
m_abs <- 0.02 * scl
e_true <- 0 * scl # in the better direction
margin_method <- "fm"
}
# Equivalent idiom (two arms, conversion): power.prop.test(p1 = baseline, p2 = baseline * (1 + mde), power = target)
# %% Solve
n_exact <- NA
if (size_from == "days" && is.null(daily)) stop("Sizes given in days need the eligible units per day.")
if (size_from == "days") {
n_t <- floor(days_given * daily * exposure / (comparisons + ratio))
if (n_t < 2) stop("Too little traffic: fewer than two units per arm in that many days.")
} else if (solve_for != "n") {
n_t <- n_given
}
if (margin_hyp) {
delta <- sgn * e_true
if (kind == "binary" && !(mean0 + delta > 0 && mean0 + delta < 1)) stop("The treatment conversion rate implied by the effect is outside (0, 1).")
v1 <- cs_exp_treatment_variance(kind, mean0, var0, delta) * shrink
sds <- cs_exp_margin_sds(kind, mean0, delta, v1, v0, ratio, m_abs, sgn, margin_method, shrink)
if (solve_for == "n") {
n_exact <- cs_exp_margin_n(hypothesis, e_true, m_abs, sds[1], sds[2], sds[3], z_crit, z_pow, target)
n_t <- as.integer(ceiling(n_exact - 1e-9))
}
power <- cs_exp_margin_power(hypothesis, e_true, m_abs, sds[1], sds[2], sds[3], n_t, z_crit)
} else if (solve_for == "n") {
delta <- delta_planned
v1 <- cs_exp_treatment_variance(kind, mean0, var0, delta) * shrink
if (kind == "binary" && !(mean0 + delta > 0 && mean0 + delta < 1)) stop("The treatment conversion rate implied by the effect is outside (0, 1).")
n_exact <- cs_exp_n(delta, v1, v0, ratio, z_crit, z_pow)
n_t <- as.integer(ceiling(n_exact - 1e-9))
power <- cs_exp_power(delta, v1, v0, n_t, ratio, z_crit, alternative)
} else if (solve_for == "mde") {
delta <- cs_exp_mde(kind, mean0, var0, n_t, ratio, z_crit, z_pow, shrink, direction)
v1 <- cs_exp_treatment_variance(kind, mean0, var0, delta) * shrink
power <- cs_exp_power(delta, v1, v0, n_t, ratio, z_crit, alternative)
} else {
delta <- delta_planned
v1 <- cs_exp_treatment_variance(kind, mean0, var0, delta) * shrink
power <- cs_exp_power(delta, v1, v0, n_t, ratio, z_crit, alternative)
}
n_c <- as.integer(ceiling(ratio * n_t - 1e-9))
n_total <- comparisons * n_t + n_c
mde_abs <- if (margin_hyp) NA else abs(delta)
mde_rel <- if (margin_hyp) NA else if (mean0 != 0) abs(delta / mean0) else NaN
# %% Duration
days <- weeks <- days_exact <- NULL
tables <- list()
if (!is.null(daily)) {
du <- cs_exp_duration(n_total, daily, exposure, whole_weeks)
days <- du$days
weeks <- du$weeks
days_exact <- du$exact
if (!is.null(max_days) && days > max_days) {
warnings <- c(warnings, sprintf("The test needs %d days, longer than the %d-day limit; consider a larger effect, more traffic or CUPED.", days, as.integer(max_days)))
}
rows <- list()
for (w in if (margin_hyp) integer(0) else 1:8) {
nt_w <- floor(7 * w * daily * exposure / (comparisons + ratio))
if (nt_w < 2) next
d_w <- cs_exp_mde(kind, mean0, var0, nt_w, ratio, z_crit, z_pow, shrink, direction)
rows[[length(rows) + 1]] <- list(weeks = w, n = nt_w, mde_abs = abs(d_w), mde_rel = if (mean0 != 0) abs(d_w / mean0) else NULL)
}
if (!margin_hyp) tables <- list(list(
key = "mde_by_duration", title = sprintf("Minimum detectable effect by duration (%.0f%% power)", 100 * target),
columns = list(list(key = "weeks", label = "Weeks", type = "integer"), list(key = "n", label = "Units per treatment arm", type = "integer"),
list(key = "mde_abs", label = "Absolute MDE", type = "number"), list(key = "mde_rel", label = "Relative MDE", type = "percent")),
rows = rows
))
}
if (comparisons > 1 && correction == "none") warnings <- c(warnings, "Several treatment arms without a correction inflate the chance of a false winner.")
# %% Power curve
grid <- pmax(2, n_t * (0.1 + 2.4 * (0:59) / 59))
curve <- NULL
if (margin_hyp) {
curve <- data.frame(x = grid, power = vapply(grid, function(x) cs_exp_margin_power(hypothesis, e_true, m_abs, sds[1], sds[2], sds[3], x, z_crit), numeric(1)),
series = sprintf("true effect %.4g", e_true))
}
for (s in if (margin_hyp) numeric(0) else c(0.75, 1, 1.25)) {
d <- delta * s
if (kind == "binary" && !(mean0 + d > 0 && mean0 + d < 1)) next
v1s <- cs_exp_treatment_variance(kind, mean0, var0, d) * shrink
label <- if (is.finite(mde_rel)) sprintf("%.1f%% lift", 100 * s * mde_rel) else sprintf("Δ = %.4g", d)
curve <- rbind(curve, data.frame(x = grid, power = vapply(grid, function(x) cs_exp_power(d, v1s, v0, x, ratio, z_crit, alternative), numeric(1)), series = label))
}
# %% Result
stats_list <- list(
list(key = "n_treatment", label = "Units per treatment arm", value = n_t, format = "integer"),
list(key = "n_control", label = "Control units", value = n_c, format = "integer"),
list(key = "n_total", label = "Total units", value = n_total, format = "integer"),
list(key = "power", label = "Power", value = power)
)
stats_list <- append(stats_list, if (margin_hyp) list(list(key = "margin_abs", label = "Margin", value = m_abs))
else list(list(key = "mde_rel", label = "Relative MDE", value = mde_rel, format = "percent"),
list(key = "mde_abs", label = "Absolute MDE", value = mde_abs)), after = 3)
if (!is.null(days)) stats_list <- c(stats_list, list(list(key = "days", label = "Days", value = days, format = "integer")))
cs_publish(list(
method = "experiment.power",
methodVersion = "1.1.0",
title = "A/B test plan",
engine = cs_engine_info("stats"),
summary = if (!margin_hyp) sprintf("%s units per treatment arm, %s control (%s total); MDE %.2f%% relative, power %.3f.",
format(n_t, big.mark = ","), format(n_c, big.mark = ","), format(n_total, big.mark = ","), 100 * mde_rel, power)
else sprintf("%s: %s units per treatment arm, %s control (%s total); margin %.4g, power %.3f.", gsub("_", " ", hypothesis),
format(n_t, big.mark = ","), format(n_c, big.mark = ","), format(n_total, big.mark = ","), m_abs, power),
statistics = stats_list,
tables = tables,
warnings = warnings,
notes = c("Normal approximation for a difference in means; conversion rates use unpooled per-arm variances, ratio metrics the delta method.",
"Sizes are rounded up; the power is recomputed at the rounded size and includes both rejection tails for two-sided tests.",
if (comparisons > 1) sprintf("%d treatment arms vs the control: %s correction, α = %.4g per comparison.", comparisons, correction, alpha_cmp),
if (rho > 0) sprintf("CUPED with ρ = %g keeps %.1f%% of the variance.", rho, 100 * shrink)),
charts = list(cs_chart(
"power", "power-curve", "Power by units per treatment arm",
list(x = cs_col("Units per treatment arm", curve$x), power = cs_col("Power", curve$power), series = cs_col("Effect", curve$series)),
controls = list(target = if (solve_for != "power") target else 0.8)
)),
values = list(
n_treatment = n_t, n_control = n_c, n_total = n_total, n_exact = n_exact, power = power,
target_power = if (solve_for != "power") target else NA, mde_abs = mde_abs, mde_rel = mde_rel,
alpha = alpha, alpha_comparison = alpha_cmp, z_crit = z_crit, baseline_mean = mean0,
variance_control = v0, variance_treatment = v1, cuped_factor = shrink, control_ratio = ratio,
comparisons = comparisons, days = if (is.null(days)) NA else days, weeks = if (is.null(days)) NA else weeks,
days_exact = if (is.null(days_exact)) NA else days_exact,
hypothesis = hypothesis, margin_abs = if (is.null(m_abs)) NA else m_abs, true_effect_abs = if (is.null(e_true)) NA else e_true,
sd_alternative = sds[1], sd_null_lower = sds[2], sd_null_upper = sds[3]
)
))
47,543 units per treatment arm, 82,347 control (224,976 total); MDE 10.00% relative, power 0.800.
- Units per treatment arm
- 47,543
- Control units
- 82,347
- Total units
- 224,976
- Relative MDE
- 10.0%
- Absolute MDE
- 0.0035
- Power
- 0.8000
- Days
- 7
To keep this page light, the plots are shown once, under the R tab.
Minimum detectable effect by duration (80% power)
| Weeks | Units per treatment arm | Absolute MDE | Relative MDE |
|---|---|---|---|
| 1 | 59,170 | 0.0031 | 8.9% |
| 2 | 118,341 | 0.0022 | 6.3% |
| 3 | 177,512 | 0.0018 | 5.1% |
| 4 | 236,683 | 0.0015 | 4.4% |
| 5 | 295,854 | 0.0014 | 3.9% |
| 6 | 355,025 | 0.0013 | 3.6% |
| 7 | 414,196 | 0.0012 | 3.3% |
| 8 | 473,367 | 0.0011 | 3.1% |
- Normal approximation for a difference in means; conversion rates use unpooled per-arm variances, ratio metrics the delta method.
- Sizes are rounded up; the power is recomputed at the rounded size and includes both rejection tails for two-sided tests.
- 3 treatment arms vs the control: dunnett correction, α = 0.01786 per comparison.
The exact Python script · Python Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# A/B test planning — sample size, MDE and duration
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method experiment.power 1.1.0 · spec d7b85c41b27320b5
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
import math
from scipy.special import ndtri
import codesplash as cs
from codesplash.experiment import design as xd # closed-form A/B planning (twin of R cs_exp_*)
# %% Inputs
solve_for = "n" # n | mde | power
kind = "binary" # binary | continuous | ratio
baseline = 0.035
mde_type = "relative" # relative | absolute
mde_in = 0.1
direction = "increase"
alpha = 0.05
target = 0.8
alternative = "two-sided"
arms = int(4)
allocation = "sqrt"
rho = 0
correction = "dunnett" if arms > 2 else "none"
size_from = "n" if solve_for != "n" else "n"
n_given = 10000
days_given = 14
daily = 40000 # eligible units per day (None = unknown)
exposure = 1
whole_weeks = True
max_days = 21
warnings = []
if kind == "binary" and not 0 < baseline < 1:
raise ValueError("A conversion-rate baseline must be between 0 and 1.")
mean0, var0 = xd.unit_variance(
kind, baseline, sd=1, num_mean=0.6, den_mean=3,
num_sd=0.9, den_sd=2, corr=0.6,
)
if var0 <= 0:
raise ValueError("The metric's variance must be positive (check the ratio-metric inputs).")
shrink = 1.0 - rho**2 # CUPED keeps 1 − ρ² of the variance
v0 = var0 * shrink
comparisons = arms - 1
ratio = xd.allocation(arms, allocation, 1) # control units per treatment unit
lam = math.sqrt(1.0 / (1.0 + ratio)) # correlation root of two comparisons sharing the control (under H0)
z_crit, alpha_cmp = xd.crit(alpha, comparisons, correction, alternative, lam)
z_pow = float(ndtri(target))
sign = -1.0 if direction == "decrease" else 1.0
delta_planned = sign * (mde_in * abs(mean0) if mde_type == "relative" else mde_in)
hypothesis = "superiority" # superiority | noninferiority | equivalence | minimum_effect
margin_hyp = hypothesis != "superiority"
m_abs = e_true = None
sds = (None, None, None)
if margin_hyp:
# Margin tests are one-sided (equivalence: two one-sided tests, each at the per-comparison α).
if solve_for == "mde":
raise ValueError("With a margin-based hypothesis, solve for the sample size or the power.")
z_crit, alpha_cmp = xd.crit(alpha, comparisons, correction, "one-sided", lam)
scale = abs(mean0) if mde_type == "relative" else 1.0
m_abs = 0.02 * scale
e_true = 0 * scale # in the better direction
margin_method = "fm"
# Equivalent idiom (two arms, conversion): power.prop.test in R / statsmodels NormalIndPower in Python.
# %% Solve
n_exact = None
if size_from == "days" and daily is None:
raise ValueError("Sizes given in days need the eligible units per day.")
if size_from == "days":
n_t = math.floor(days_given * daily * exposure / (comparisons + ratio))
if n_t < 2:
raise ValueError("Too little traffic: fewer than two units per arm in that many days.")
elif solve_for != "n":
n_t = n_given
if margin_hyp:
delta = sign * e_true
if kind == "binary" and not 0 < mean0 + delta < 1:
raise ValueError("The treatment conversion rate implied by the effect is outside (0, 1).")
v1 = xd.treatment_variance(kind, mean0, var0, delta) * shrink
sds = xd.margin_sds(kind, mean0, delta, v1, v0, ratio, m_abs, sign, margin_method, shrink)
if solve_for == "n":
n_exact = xd.margin_n(hypothesis, e_true, m_abs, *sds, z_crit, z_pow, target)
n_t = int(math.ceil(n_exact - 1e-9))
power = xd.margin_power(hypothesis, e_true, m_abs, *sds, n_t, z_crit)
elif solve_for == "n":
delta = delta_planned
v1 = xd.treatment_variance(kind, mean0, var0, delta) * shrink
if kind == "binary" and not 0 < mean0 + delta < 1:
raise ValueError("The treatment conversion rate implied by the effect is outside (0, 1).")
n_exact = xd.n_required(delta, v1, v0, ratio, z_crit, z_pow)
n_t = int(math.ceil(n_exact - 1e-9))
power = xd.power(delta, v1, v0, n_t, ratio, z_crit, alternative)
elif solve_for == "mde":
delta = xd.mde(kind, mean0, var0, n_t, ratio, z_crit, z_pow, shrink, direction)
v1 = xd.treatment_variance(kind, mean0, var0, delta) * shrink
power = xd.power(delta, v1, v0, n_t, ratio, z_crit, alternative)
else:
delta = delta_planned
v1 = xd.treatment_variance(kind, mean0, var0, delta) * shrink
power = xd.power(delta, v1, v0, n_t, ratio, z_crit, alternative)
n_c = int(math.ceil(ratio * n_t - 1e-9))
n_total = comparisons * n_t + n_c
mde_abs = abs(delta) if not margin_hyp else None
mde_rel = (abs(delta / mean0) if mean0 != 0 else math.nan) if not margin_hyp else None
# %% Duration
days = weeks = days_exact = None
tables = []
if daily is not None:
days, weeks, days_exact = xd.duration(n_total, daily, exposure, whole_weeks)
if max_days is not None and days > max_days:
warnings.append(f"The test needs {days} days, longer than the {int(max_days)}-day limit; consider a larger effect, more traffic or CUPED.")
rows = []
for w in (range(1, 9) if not margin_hyp else []):
nt_w = math.floor(7 * w * daily * exposure / (comparisons + ratio))
if nt_w < 2:
continue
d_w = xd.mde(kind, mean0, var0, nt_w, ratio, z_crit, z_pow, shrink, direction)
rows.append({"weeks": w, "n": nt_w, "mde_abs": abs(d_w), "mde_rel": abs(d_w / mean0) if mean0 else None})
tables.extend([] if margin_hyp else [{
"key": "mde_by_duration", "title": f"Minimum detectable effect by duration ({target:.0%} power)",
"columns": [{"key": "weeks", "label": "Weeks", "type": "integer"}, {"key": "n", "label": "Units per treatment arm", "type": "integer"},
{"key": "mde_abs", "label": "Absolute MDE", "type": "number"}, {"key": "mde_rel", "label": "Relative MDE", "type": "percent"}],
"rows": rows,
}])
if comparisons > 1 and correction == "none":
warnings.append("Several treatment arms without a correction inflate the chance of a false winner.")
# %% Power curve
grid = [max(2.0, n_t * (0.1 + 2.4 * i / 59)) for i in range(60)]
curve = []
if margin_hyp:
curve = [(x, xd.margin_power(hypothesis, e_true, m_abs, *sds, x, z_crit), f"true effect {e_true:.4g}") for x in grid]
for s in ((0.75, 1.0, 1.25) if not margin_hyp else ()):
d = delta * s
if kind == "binary" and not 0 < mean0 + d < 1:
continue
v1s = xd.treatment_variance(kind, mean0, var0, d) * shrink
label = f"{s * mde_rel:.1%} lift" if math.isfinite(mde_rel) else f"Δ = {d:.4g}"
curve += [(x, xd.power(d, v1s, v0, x, ratio, z_crit, alternative), label) for x in grid]
# %% Result
cs.publish({
"method": "experiment.power",
"methodVersion": "1.1.0",
"title": "A/B test plan",
"engine": cs.engine_info("scipy"),
"summary": (f"{n_t:,} units per treatment arm, {n_c:,} control ({n_total:,} total); MDE {mde_rel:.2%} relative, power {power:.3f}." if not margin_hyp
else f"{hypothesis.replace('_', ' ')}: {n_t:,} units per treatment arm, {n_c:,} control ({n_total:,} total); margin {m_abs:.4g}, power {power:.3f}."),
"statistics": [
{"key": "n_treatment", "label": "Units per treatment arm", "value": n_t, "format": "integer"},
{"key": "n_control", "label": "Control units", "value": n_c, "format": "integer"},
{"key": "n_total", "label": "Total units", "value": n_total, "format": "integer"},
*([{"key": "mde_rel", "label": "Relative MDE", "value": mde_rel, "format": "percent"},
{"key": "mde_abs", "label": "Absolute MDE", "value": mde_abs}] if not margin_hyp else
[{"key": "margin_abs", "label": "Margin", "value": m_abs}]),
{"key": "power", "label": "Power", "value": power},
*([{"key": "days", "label": "Days", "value": days, "format": "integer"}] if days is not None else []),
],
"tables": tables,
"warnings": warnings,
"notes": [
"Normal approximation for a difference in means; conversion rates use unpooled per-arm variances, ratio metrics the delta method.",
"Sizes are rounded up; the power is recomputed at the rounded size and includes both rejection tails for two-sided tests.",
*([f"{comparisons} treatment arms vs the control: {correction} correction, α = {alpha_cmp:.4g} per comparison."] if comparisons > 1 else []),
*([f"CUPED with ρ = {rho:g} keeps {shrink:.1%} of the variance."] if rho > 0 else []),
],
"charts": [cs.chart(
"power", "power-curve", "Power by units per treatment arm",
{"x": cs.col("Units per treatment arm", [c[0] for c in curve]), "power": cs.col("Power", [c[1] for c in curve]),
"series": cs.col("Effect", [c[2] for c in curve])},
controls={"target": target if solve_for != "power" else 0.8},
)],
"values": {
"n_treatment": n_t, "n_control": n_c, "n_total": n_total, "n_exact": n_exact, "power": power,
"target_power": target if solve_for != "power" else None, "mde_abs": mde_abs, "mde_rel": mde_rel,
"alpha": alpha, "alpha_comparison": alpha_cmp, "z_crit": z_crit, "baseline_mean": mean0,
"variance_control": v0, "variance_treatment": v1, "cuped_factor": shrink, "control_ratio": ratio,
"comparisons": comparisons, "days": days, "weeks": weeks if days is not None else None, "days_exact": days_exact,
"hypothesis": hypothesis, "margin_abs": m_abs, "true_effect_abs": e_true, "sd_alternative": sds[0],
"sd_null_lower": sds[1], "sd_null_upper": sds[2],
},
})
Here the control arm has 1.73 times as many users as each treatment arm.
Check your understanding
CheckpointCheck yourself
How should you choose the effect a test is powered for?
Show the answer
Answer: b
Powering for a big effect makes the test short, but then a smaller effect that was still worth having can easily go undetected. Pilots are noisy, and their effects tend to run high.
Check yourself
An underpowered test comes back not significant. What have you learned?
Show the answer
Answer: b
Without enough power, a non-significant result is compatible both with no effect and with the effect you hoped for.
Reflect
ReflectTake a metric you care about. What's the smallest change that would make you act differently? Compare it with what your traffic can detect. If the gap is large, what would you change first: the metric, the treatment or the duration?
Recap
Recap- With fixed traffic, solve for the minimum detectable effect.
- If it's bigger than any plausible effect, don't run the test as designed.
- More arms mean fewer users per arm and a stricter threshold for each.
Concepts in this lesson