BPower & sample size · power.proportions
Power analysis — proportions
How many participants does a test of one or two proportions need?
Sample size, power or smallest detectable proportion for one proportion against a reference value or two independent proportions (any allocation ratio), with the normal approximation (pooled variance under H0, as power.prop.test) or Cohen's arcsine effect size h.
A real result
Example “Two sample normal”, recorded in both engines. In the app, “Try this example” loads it into Analyze.
n = 170 + 170 (N = 340), p1 = 0.500, p2 = 0.650, h = 0.30, α = 0.05, 1 − β = 0.802
- Group 1
- 170
- Group 2
- 170
- Total
- 340
- Power
- 0.8016
- Proportion 2
- 0.6500
- Difference p2 − p1
- 0.1500
- Cohen's h
- 0.3047
Plots
View data
| Power | Proportion 2 | Sample size (group 1) |
|---|---|---|
| 0.0544 | p2 = 0.613 | 2 |
| 0.0861 | p2 = 0.613 | 12.575 |
| 0.1186 | p2 = 0.613 | 23.150 |
| 0.1517 | p2 = 0.613 | 33.725 |
| 0.1852 | p2 = 0.613 | 44.300 |
| 0.2188 | p2 = 0.613 | 54.875 |
| 0.2523 | p2 = 0.613 | 65.450 |
| 0.2855 | p2 = 0.613 | 76.025 |
| 0.3184 | p2 = 0.613 | 86.600 |
| 0.3507 | p2 = 0.613 | 97.175 |
| 0.3823 | p2 = 0.613 | 107.75 |
| 0.4132 | p2 = 0.613 | 118.33 |
| 0.4432 | p2 = 0.613 | 128.90 |
| 0.4724 | p2 = 0.613 | 139.47 |
| 0.5006 | p2 = 0.613 | 150.05 |
| 0.5278 | p2 = 0.613 | 160.63 |
| 0.5541 | p2 = 0.613 | 171.20 |
| 0.5793 | p2 = 0.613 | 181.78 |
| 0.6035 | p2 = 0.613 | 192.35 |
| 0.6267 | p2 = 0.613 | 202.93 |
| 0.6488 | p2 = 0.613 | 213.50 |
| 0.6699 | p2 = 0.613 | 224.07 |
| 0.6900 | p2 = 0.613 | 234.65 |
| 0.7092 | p2 = 0.613 | 245.22 |
| 0.7273 | p2 = 0.613 | 255.80 |
| 0.7446 | p2 = 0.613 | 266.38 |
| 0.7609 | p2 = 0.613 | 276.95 |
| 0.7764 | p2 = 0.613 | 287.52 |
| 0.7910 | p2 = 0.613 | 298.10 |
| 0.8047 | p2 = 0.613 | 308.68 |
| 0.8177 | p2 = 0.613 | 319.25 |
| 0.8300 | p2 = 0.613 | 329.82 |
| 0.8415 | p2 = 0.613 | 340.40 |
| 0.8523 | p2 = 0.613 | 350.98 |
| 0.8625 | p2 = 0.613 | 361.55 |
| 0.8720 | p2 = 0.613 | 372.13 |
| 0.8810 | p2 = 0.613 | 382.70 |
| 0.8894 | p2 = 0.613 | 393.27 |
| 0.8972 | p2 = 0.613 | 403.85 |
| 0.9046 | p2 = 0.613 | 414.43 |
| 0.9114 | p2 = 0.613 | 425 |
| 0.0579 | p2 = 0.650 | 2 |
| 0.1155 | p2 = 0.650 | 12.575 |
| 0.1752 | p2 = 0.650 | 23.150 |
| 0.2356 | p2 = 0.650 | 33.725 |
| 0.2956 | p2 = 0.650 | 44.300 |
| 0.3540 | p2 = 0.650 | 54.875 |
| 0.4104 | p2 = 0.650 | 65.450 |
| 0.4641 | p2 = 0.650 | 76.025 |
| 0.5148 | p2 = 0.650 | 86.600 |
| 0.5624 | p2 = 0.650 | 97.175 |
| 0.6066 | p2 = 0.650 | 107.75 |
| 0.6474 | p2 = 0.650 | 118.33 |
| 0.6850 | p2 = 0.650 | 128.90 |
| 0.7193 | p2 = 0.650 | 139.47 |
| 0.7505 | p2 = 0.650 | 150.05 |
| 0.7788 | p2 = 0.650 | 160.63 |
| 0.8044 | p2 = 0.650 | 171.20 |
| 0.8273 | p2 = 0.650 | 181.78 |
| 0.8479 | p2 = 0.650 | 192.35 |
First 60 of 123 rows.
Required size of group 1 (or the one group) by power
| Proportion 2 | 70% power | 80% power | 90% power | 95% power |
|---|---|---|---|---|
| p2 = 0.613 | 241 | 305 | 408 | 504 |
| p2 = 0.650 | 134 | 170 | 227 | 280 |
| p2 = 0.688 | 85 | 107 | 143 | 176 |
- Normal approximation: pooled variance under H0, unpooled under H1.
- Two-sided power adds both rejection tails. Required sizes are rounded up and the power is recomputed at the rounded size.
n = 170 + 170 (N = 340), p1 = 0.500, p2 = 0.650, h = 0.30, α = 0.05, 1 − β = 0.802
- Group 1
- 170
- Group 2
- 170
- Total
- 340
- Power
- 0.8016
- Proportion 2
- 0.6500
- Difference p2 − p1
- 0.1500
- Cohen's h
- 0.3047
To keep this page light, the plots are shown once, under the R tab.
Required size of group 1 (or the one group) by power
| Proportion 2 | 70% power | 80% power | 90% power | 95% power |
|---|---|---|---|---|
| p2 = 0.613 | 241 | 305 | 408 | 504 |
| p2 = 0.650 | 134 | 170 | 227 | 280 |
| p2 = 0.688 | 85 | 107 | 143 | 176 |
- Normal approximation: pooled variance under H0, unpooled under H1.
- Two-sided power adds both rejection tails. Required sizes are rounded up and the power is recomputed at the rounded size.
Do it in CodeSplash Run it on your own data, in Python or R, with the code to prove it.
Worked examples in the app
- Two sample normalNo data needed
- Two sample arcsine ratioNo data needed
- One sample powerNo data needed
- Detectable differenceNo data needed
In the guides
Where it sits in the decision guides
- Sample-size planning · Statistics Guide · Step 0 — Prepare your data
- Two groups, binary outcome: two-proportion z, Fisher's exact · Statistics Guide · B — Compare groups
- One group vs a known proportion · Statistics Guide · E — Compare proportions / frequencies
- Two independent proportions · Statistics Guide · E — Compare proportions / frequencies
- Sample size and power · Statistics Guide · Additional considerations
- A/B testing (hypothesis testing) · DS & ML Decision Guide · Optimize / decide
Choices you make
hypothesis
- Solve fordefault n
Which quantity to compute from the others: the sample size, the power, or the smallest effect you could detect.
- Sample size(default) — Finds the sample size needed for your power and effect; the usual question before a study.
- Power — Finds the chance of detecting your effect at this sample; pick it when the sample size is fixed.
- Smallest detectable difference — Finds the smallest difference this sample can detect; pick it when the sample size is fixed.
- Designdefault two-sample
Two independent groups, or one group against a reference proportion.
- Two independent groups(default) — Compares two independent proportions; pick it for a treatment and a control group.
- One group vs a reference proportion — Compares one group's proportion with a fixed reference; pick it for a benchmark.
- Proportion 1 (control group, or the H0 reference value)default 0.5
Proportion in the control group, or the reference value under H0 for one group.
- Proportion 2 (treatment group, or the expected value)default 0.65
When solving for the detectable difference, only its side of proportion 1 (above or below) is used.
- Sample size (group 1, or the one group)default 100
Size of group 1, or of the single group (ignored when you solve for it).
- Allocation ratio (n₂ / n₁)default 1
Size of group 2 relative to group 1 (1 = equal groups).
- Target power (1 − β)default 0.8
Chance of detecting the effect if it is real. 0.8 is common; 0.9 is stricter and needs more data.
- Significance level (α)default 0.05
The false-alarm rate you accept: the chance of a significant result when there is truly no effect. 0.05 is conventional.
- Testdefault two-sided
Two-sided unless the direction was fixed before collecting data. One-sided needs fewer rows but ignores effects the other way.
- Two-sided(default) — Plans for a test in either direction; the usual choice, though it needs more data than one-sided.
- One-sided (difference in the expected direction) — Plans for a test in the expected direction only; needs less data, but can't claim an effect the other way.
options
- Approximationdefault normal
Formula used for power: the normal approximation (as R's power.prop.test) or Cohen's arcsine h.
- Normal (pooled under H0, as power.prop.test)(default) — Uses a pooled normal approximation, as R's power.prop.test does; the usual choice.
- Arcsine (Cohen's h) — Uses Cohen's h on arcsine-transformed proportions; matches Cohen's tables.
What it reports
- n
- How many independent units the analysis used; it sets precision and power.
- n2
- How many independent units the analysis used; it sets precision and power.
- n_total
- How many independent units the analysis used; it sets precision and power.
- n_exact
- How many independent units the analysis used; it sets precision and power.
- power
- The chance a test detects an effect of a given size, if that effect is real.
- target_power
- The chance a test detects an effect of a given size, if that effect is real.
- p1
- A value between 0 and 1 — a share, a rate or a probability.
- p2
- A value between 0 and 1 — a share, a rate or a probability.
- h
- The gap between two proportions on the arcsine scale, so equal values of h are about equally hard to detect.
- rd
- The difference in event probabilities between groups; the number that matters for decisions.
- alpha
- The false-positive rate you accept before looking at the data, usually 0.05.
Python and R
Python and R agree to 6 decimals (relative tolerance 1e-9). Python uses scipy, codesplash; R uses stats, codesplashr.
Default traps between the two languages
- Closed-form normal power; solved sizes and proportions come from Brent root finding (brentq ↔ uniroot, tol 1e-12) on identical brackets.
The exact script CodeSplash ran for example “Two sample normal” · Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# Power analysis — proportions
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method power.proportions 1.0.0 · spec d20b6e4d6fe1713d
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
import math
import codesplash as cs
from codesplash.stats import power as pw # normal-approximation power and Brent solvers (twin of R cs_power_*)
# %% Inputs
solve_for = "n" # n | power | effect
design = "two-sample" # two-sample | one-sample
p1 = 0.5 # control group, or the H0 reference value
p2 = 0.65 # treatment group, or the expected value
n_given = 100
ratio = 1 if design == "two-sample" else None
target = 0.8
alpha = 0.05
alternative = "two-sided"
method = "normal" # normal (pooled under H0; power.prop.test) | arcsine (Cohen's h)
warnings = []
two = design == "two-sample"
lo = max(2.0, 2.0 / ratio) if two else 2.0
def power_at(n1, q2, n2=None):
if two:
return pw.prop_power(p1, q2, n1, ratio * n1 if n2 is None else n2, alpha, alternative, method)
return pw.prop_power(p1, q2, n1, None, alpha, alternative, method)
# Equivalent idiom (equal groups, normal): power.prop.test in R; statsmodels NormalIndPower with
# proportion_effectsize(p2, p1) for the arcsine version.
# %% Solve
n_exact = None
target_power = None
if abs(p2 - p1) < 1e-12 and solve_for != "effect":
warnings.append("The two proportions are equal: there is no difference to detect.")
if solve_for == "n":
target_power = target
n_exact = pw.solve_increasing(lambda n: power_at(n, p2), target, lo, max(2.0 * lo, 10.0), 1e7)
if math.isnan(n_exact):
warnings.append("The target power is not reached below 10,000,000 participants.")
n1 = pw.ceil_n(n_exact)
elif solve_for == "power":
n1 = n_given
else:
target_power = target
n1 = n_given
if p2 >= p1: # search on the side of proportion 1 that proportion 2 is on
p2 = pw.solve_on(lambda x: power_at(n1, x), target, p1 + 1e-9, 1.0 - 1e-9)
else:
t = pw.solve_on(lambda x: power_at(n1, p1 - x), target, 1e-9, p1 - 1e-9)
p2 = p1 - t
if math.isnan(p2):
warnings.append("No proportion on that side of proportion 1 reaches the target power with this sample.")
n2 = pw.ceil_n(ratio * n1) if two else None
power = power_at(n1, p2, n2)
h = pw.cohens_h(p1, p2) if math.isfinite(p2) else math.nan
rd = p2 - p1
# %% Curves and sensitivity
n_ref = n1 if math.isfinite(n1) else 100.0
xs = pw.grid(lo, max(2.5 * n_ref, lo + 10.0))
alts = [p1 + s * rd for s in (0.75, 1.0, 1.25)] if math.isfinite(rd) and rd != 0 else [p2]
alts = [min(max(q, 1e-6), 1 - 1e-6) for q in alts]
curve = [(float(x), power_at(float(x), q), f"p2 = {q:.3f}") for q in alts for x in xs]
tables = []
if solve_for == "n" and math.isfinite(rd) and rd != 0:
rows = [{"effect": f"p2 = {q:.3f}", **{f"p{p}": pw.ceil_n(pw.solve_increasing(lambda m: power_at(m, q), p / 100, lo, max(2.0 * lo, 10.0), 1e7)) for p in (70, 80, 90, 95)}} for q in alts]
tables.append({
"key": "sensitivity", "title": "Required size of group 1 (or the one group) by power",
"columns": [{"key": "effect", "label": "Proportion 2", "type": "string"}] + [{"key": f"p{p}", "label": f"{p}% power", "type": "integer"} for p in (70, 80, 90, 95)],
"rows": rows,
})
# %% Result
n_total = n1 + n2 if two else n1
size_text = f"{n1:,.0f} + {n2:,.0f} (N = {n_total:,.0f})" if two else f"{n1:,.0f}"
cs.publish({
"method": "power.proportions",
"methodVersion": "1.0.0",
"title": "Power analysis: two proportions" if two else "Power analysis: one proportion",
"engine": cs.engine_info("scipy"),
"summary": f"n = {size_text}, p1 = {p1:.3f}, p2 = {p2:.3f}, power = {power:.3f} at {alternative} α = {alpha} ({method}).",
"apa": f"n = {size_text}, p1 = {p1:.3f}, p2 = {p2:.3f}, h = {cs.fmt_num(h)}, α = {alpha}, 1 − β = {power:.3f}",
"statistics": [
{"key": "n", "label": "Group 1" if two else "Participants", "value": n1, "format": "integer"},
*([{"key": "n2", "label": "Group 2", "value": n2, "format": "integer"}, {"key": "n_total", "label": "Total", "value": n_total, "format": "integer"}] if two else []),
{"key": "power", "label": "Power", "value": power},
{"key": "p2", "label": "Proportion 2", "value": p2},
{"key": "rd", "label": "Difference p2 − p1", "value": rd},
{"key": "h", "label": "Cohen's h", "value": h},
],
"tables": tables,
"warnings": warnings,
"notes": [
"Normal approximation: pooled variance under H0, unpooled under H1." if method == "normal" else "Arcsine approximation with Cohen's h = 2·asin√p2 − 2·asin√p1.",
"Two-sided power adds both rejection tails. Required sizes are rounded up and the power is recomputed at the rounded size.",
],
"charts": [cs.chart(
"power", "power-curve", "Power by sample size",
{"x": cs.col("Sample size (group 1)" if two else "Sample size", [c[0] for c in curve]),
"power": cs.col("Power", [c[1] for c in curve]),
"series": cs.col("Proportion 2", [c[2] for c in curve])},
controls={"target": target if target_power is not None else 0.8},
)],
"values": {
"n": n1, "n2": n2, "n_total": n_total, "n_exact": n_exact, "power": power, "target_power": target_power,
"p1": p1, "p2": p2, "h": h, "rd": rd, "alpha": alpha,
},
})
The exact script CodeSplash ran for example “Two sample normal” · R version 4.5.1 (2025-06-13)
# Power analysis — proportions
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method power.proportions 1.0.0 · spec d20b6e4d6fe1713d
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
library(codesplashr) # cs_power_prop() and the Brent solvers (twin of codesplash.stats.power)
# %% Inputs
solve_for <- "n" # n | power | effect
design <- "two-sample" # two-sample | one-sample
p1 <- 0.5 # control group, or the H0 reference value
p2 <- 0.65 # treatment group, or the expected value
n_given <- 100
two <- design == "two-sample"
ratio <- if (two) 1 else NA
target <- 0.8
alpha <- 0.05
alternative <- "two-sided"
method <- "normal" # normal (pooled under H0; power.prop.test) | arcsine (Cohen's h)
warnings <- list()
lo <- if (two) max(2, 2 / ratio) else 2
power_at <- function(n1, q2, n2 = NULL) {
if (two) cs_power_prop(p1, q2, n1, if (is.null(n2)) ratio * n1 else n2, alpha, alternative, method)
else cs_power_prop(p1, q2, n1, NULL, alpha, alternative, method)
}
# Equivalent idiom (equal groups, normal): power.prop.test(p1 = p1, p2 = p2, power = target, strict = TRUE)
# %% Solve
n_exact <- NA
target_power <- NA
if (abs(p2 - p1) < 1e-12 && solve_for != "effect") warnings <- c(warnings, "The two proportions are equal: there is no difference to detect.")
if (solve_for == "n") {
target_power <- target
n_exact <- cs_power_solve_increasing(function(n) power_at(n, p2), target, lo, max(2 * lo, 10), 1e7)
if (is.nan(n_exact)) warnings <- c(warnings, "The target power is not reached below 10,000,000 participants.")
n1 <- cs_power_ceil(n_exact)
} else if (solve_for == "power") {
n1 <- n_given
} else {
target_power <- target
n1 <- n_given
if (p2 >= p1) { # search on the side of proportion 1 that proportion 2 is on
p2 <- cs_power_solve_on(function(x) power_at(n1, x), target, p1 + 1e-9, 1 - 1e-9)
} else {
t <- cs_power_solve_on(function(x) power_at(n1, p1 - x), target, 1e-9, p1 - 1e-9)
p2 <- p1 - t
}
if (is.nan(p2)) warnings <- c(warnings, "No proportion on that side of proportion 1 reaches the target power with this sample.")
}
n2 <- if (two) cs_power_ceil(ratio * n1) else NA
power <- power_at(n1, p2, if (two) n2 else NULL)
h <- if (is.finite(p2)) cs_cohens_h(p1, p2) else NaN
rd <- p2 - p1
# %% Curves and sensitivity
n_ref <- if (is.finite(n1)) n1 else 100
xs <- cs_power_grid(lo, max(2.5 * n_ref, lo + 10))
alts <- if (is.finite(rd) && rd != 0) p1 + c(0.75, 1, 1.25) * rd else p2
alts <- pmin(pmax(alts, 1e-6), 1 - 1e-6)
curve <- do.call(rbind, lapply(alts, function(q) {
data.frame(x = xs, power = vapply(xs, function(x) power_at(x, q), numeric(1)), series = sprintf("p2 = %.3f", q))
}))
tables <- list()
if (solve_for == "n" && is.finite(rd) && rd != 0) {
rows <- lapply(alts, function(q) {
row <- list(effect = sprintf("p2 = %.3f", q))
for (p in c(70, 80, 90, 95)) row[[sprintf("p%d", p)]] <- cs_power_ceil(cs_power_solve_increasing(function(m) power_at(m, q), p / 100, lo, max(2 * lo, 10), 1e7))
row
})
tables <- list(list(
key = "sensitivity", title = "Required size of group 1 (or the one group) by power",
columns = c(list(list(key = "effect", label = "Proportion 2", type = "string")),
lapply(c(70, 80, 90, 95), function(p) list(key = sprintf("p%d", p), label = sprintf("%d%% power", p), type = "integer"))),
rows = rows
))
}
# %% Result
n_total <- if (two) n1 + n2 else n1
size_text <- if (two) sprintf("%s + %s (N = %s)", format(n1, big.mark = ","), format(n2, big.mark = ","), format(n_total, big.mark = ",")) else format(n1, big.mark = ",")
stats_list <- list(list(key = "n", label = if (two) "Group 1" else "Participants", value = n1, format = "integer"))
if (two) stats_list <- c(stats_list, list(list(key = "n2", label = "Group 2", value = n2, format = "integer"),
list(key = "n_total", label = "Total", value = n_total, format = "integer")))
stats_list <- c(stats_list, list(
list(key = "power", label = "Power", value = power),
list(key = "p2", label = "Proportion 2", value = p2),
list(key = "rd", label = "Difference p2 − p1", value = rd),
list(key = "h", label = "Cohen's h", value = h)
))
cs_publish(list(
method = "power.proportions",
methodVersion = "1.0.0",
title = if (two) "Power analysis: two proportions" else "Power analysis: one proportion",
engine = cs_engine_info("stats"),
summary = sprintf("n = %s, p1 = %.3f, p2 = %.3f, power = %.3f at %s alpha = %s (%s).", size_text, p1, p2, power, alternative, format(alpha), method),
apa = sprintf("n = %s, p1 = %.3f, p2 = %.3f, h = %s, α = %s, 1 − β = %.3f", size_text, p1, p2, cs_fmt_num(h), format(alpha), power),
statistics = stats_list,
tables = tables,
warnings = warnings,
notes = c(if (method == "normal") "Normal approximation: pooled variance under H0, unpooled under H1." else "Arcsine approximation with Cohen's h = 2·asin√p2 − 2·asin√p1.",
"Two-sided power adds both rejection tails. Required sizes are rounded up and the power is recomputed at the rounded size."),
charts = list(cs_chart(
"power", "power-curve", "Power by sample size",
list(x = cs_col(if (two) "Sample size (group 1)" else "Sample size", curve$x), power = cs_col("Power", curve$power),
series = cs_col("Proportion 2", curve$series)),
controls = list(target = if (is.na(target_power)) 0.8 else target)
)),
values = list(
n = n1, n2 = n2, n_total = n_total, n_exact = n_exact, power = power, target_power = target_power,
p1 = p1, p2 = p2, h = h, rd = rd, alpha = alpha
)
))
Sources
- Fleiss, J. L., Levin, B., & Paik, M. C. (2003). Statistical methods for rates and proportions (3rd ed.).
- Cohen, J. (1988). Statistical power analysis for the behavioral sciences (2nd ed.), chapter 6.
From the CodeSplash method registry · power.proportions v1.0.0.