Skip to content

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.

Verified in Python and R Python and R agree to 6 decimals

A real result

Example “Two sample normal”, recorded in both engines. In the app, “Try this example” loads it into Analyze.

Power analysis: two proportionsRecorded · Python and R agreeRecorded 2026-11-09 · 11 values compared
R

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

Power by sample sizeHow does power grow with sample size (or effect size), and where does it reach the target?
View data
PowerProportion 2Sample size (group 1)
0.0544p2 = 0.6132
0.0861p2 = 0.61312.575
0.1186p2 = 0.61323.150
0.1517p2 = 0.61333.725
0.1852p2 = 0.61344.300
0.2188p2 = 0.61354.875
0.2523p2 = 0.61365.450
0.2855p2 = 0.61376.025
0.3184p2 = 0.61386.600
0.3507p2 = 0.61397.175
0.3823p2 = 0.613107.75
0.4132p2 = 0.613118.33
0.4432p2 = 0.613128.90
0.4724p2 = 0.613139.47
0.5006p2 = 0.613150.05
0.5278p2 = 0.613160.63
0.5541p2 = 0.613171.20
0.5793p2 = 0.613181.78
0.6035p2 = 0.613192.35
0.6267p2 = 0.613202.93
0.6488p2 = 0.613213.50
0.6699p2 = 0.613224.07
0.6900p2 = 0.613234.65
0.7092p2 = 0.613245.22
0.7273p2 = 0.613255.80
0.7446p2 = 0.613266.38
0.7609p2 = 0.613276.95
0.7764p2 = 0.613287.52
0.7910p2 = 0.613298.10
0.8047p2 = 0.613308.68
0.8177p2 = 0.613319.25
0.8300p2 = 0.613329.82
0.8415p2 = 0.613340.40
0.8523p2 = 0.613350.98
0.8625p2 = 0.613361.55
0.8720p2 = 0.613372.13
0.8810p2 = 0.613382.70
0.8894p2 = 0.613393.27
0.8972p2 = 0.613403.85
0.9046p2 = 0.613414.43
0.9114p2 = 0.613425
0.0579p2 = 0.6502
0.1155p2 = 0.65012.575
0.1752p2 = 0.65023.150
0.2356p2 = 0.65033.725
0.2956p2 = 0.65044.300
0.3540p2 = 0.65054.875
0.4104p2 = 0.65065.450
0.4641p2 = 0.65076.025
0.5148p2 = 0.65086.600
0.5624p2 = 0.65097.175
0.6066p2 = 0.650107.75
0.6474p2 = 0.650118.33
0.6850p2 = 0.650128.90
0.7193p2 = 0.650139.47
0.7505p2 = 0.650150.05
0.7788p2 = 0.650160.63
0.8044p2 = 0.650171.20
0.8273p2 = 0.650181.78
0.8479p2 = 0.650192.35

First 60 of 123 rows.

Required size of group 1 (or the one group) by power

Proportion 270% power80% power90% power95% power
p2 = 0.613241305408504
p2 = 0.650134170227280
p2 = 0.68885107143176
  • 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.
Python

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 270% power80% power90% power95% power
p2 = 0.613241305408504
p2 = 0.650134170227280
p2 = 0.68885107143176
  • 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.
Python

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,
    },
})
R

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.