BPower & sample size · power.ttest
Power analysis — t tests
How many participants does a t-test need, or how much power does it have?
Sample size, power or smallest detectable effect (Cohen's d) for one-sample, paired and two-sample t-tests with any allocation ratio, from the noncentral t distribution. Required sizes are rounded up and the achieved power at that size is reported.
A real result
Example “Two sample n”, recorded in both engines. In the app, “Try this example” loads it into Analyze.
n = 64 + 64 (N = 128), d = 0.50, α = 0.05, 1 − β = 0.801
- Group 1 / pairs / participants
- 64
- Group 2
- 64
- Total
- 128
- Power
- 0.8015
- Effect size d
- 0.5000
- df
- 126
- Noncentrality δ
- 2.828
Plots
View data
| Power | Effect | Sample size (group 1) |
|---|---|---|
| 0.0565 | d = 0.38 | 2 |
| 0.0902 | d = 0.38 | 5.950 |
| 0.1240 | d = 0.38 | 9.900 |
| 0.1581 | d = 0.38 | 13.850 |
| 0.1925 | d = 0.38 | 17.800 |
| 0.2268 | d = 0.38 | 21.750 |
| 0.2610 | d = 0.38 | 25.700 |
| 0.2949 | d = 0.38 | 29.650 |
| 0.3282 | d = 0.38 | 33.600 |
| 0.3609 | d = 0.38 | 37.550 |
| 0.3929 | d = 0.38 | 41.500 |
| 0.4241 | d = 0.38 | 45.450 |
| 0.4544 | d = 0.38 | 49.400 |
| 0.4837 | d = 0.38 | 53.350 |
| 0.5121 | d = 0.38 | 57.300 |
| 0.5394 | d = 0.38 | 61.250 |
| 0.5656 | d = 0.38 | 65.200 |
| 0.5908 | d = 0.38 | 69.150 |
| 0.6149 | d = 0.38 | 73.100 |
| 0.6379 | d = 0.38 | 77.050 |
| 0.6599 | d = 0.38 | 81 |
| 0.6808 | d = 0.38 | 84.950 |
| 0.7007 | d = 0.38 | 88.900 |
| 0.7196 | d = 0.38 | 92.850 |
| 0.7376 | d = 0.38 | 96.800 |
| 0.7545 | d = 0.38 | 100.75 |
| 0.7706 | d = 0.38 | 104.70 |
| 0.7857 | d = 0.38 | 108.65 |
| 0.8000 | d = 0.38 | 112.60 |
| 0.8135 | d = 0.38 | 116.55 |
| 0.8262 | d = 0.38 | 120.50 |
| 0.8381 | d = 0.38 | 124.45 |
| 0.8493 | d = 0.38 | 128.40 |
| 0.8598 | d = 0.38 | 132.35 |
| 0.8697 | d = 0.38 | 136.30 |
| 0.8789 | d = 0.38 | 140.25 |
| 0.8875 | d = 0.38 | 144.20 |
| 0.8956 | d = 0.38 | 148.15 |
| 0.9032 | d = 0.38 | 152.10 |
| 0.9102 | d = 0.38 | 156.05 |
| 0.9168 | d = 0.38 | 160 |
| 0.0615 | d = 0.50 | 2 |
| 0.1223 | d = 0.50 | 5.950 |
| 0.1835 | d = 0.50 | 9.900 |
| 0.2448 | d = 0.50 | 13.850 |
| 0.3051 | d = 0.50 | 17.800 |
| 0.3636 | d = 0.50 | 21.750 |
| 0.4199 | d = 0.50 | 25.700 |
| 0.4733 | d = 0.50 | 29.650 |
| 0.5236 | d = 0.50 | 33.600 |
| 0.5707 | d = 0.50 | 37.550 |
| 0.6143 | d = 0.50 | 41.500 |
| 0.6546 | d = 0.50 | 45.450 |
| 0.6916 | d = 0.50 | 49.400 |
| 0.7253 | d = 0.50 | 53.350 |
| 0.7560 | d = 0.50 | 57.300 |
| 0.7837 | d = 0.50 | 61.250 |
| 0.8088 | d = 0.50 | 65.200 |
| 0.8313 | d = 0.50 | 69.150 |
| 0.8515 | d = 0.50 | 73.100 |
First 60 of 123 rows.
Required size of group 1 (or pairs / participants) by power
| Effect | 70% power | 80% power | 90% power | 95% power |
|---|---|---|---|---|
| d = 0.38 | 89 | 113 | 151 | 186 |
| d = 0.50 | 51 | 64 | 86 | 105 |
| d = 0.62 | 33 | 42 | 55 | 68 |
- Noncentral t; two-sided power adds both rejection tails. Required sizes are rounded up and the power is recomputed at the rounded size.
- Group 2 = ceiling(1 × group 1).
n = 64 + 64 (N = 128), d = 0.50, α = 0.05, 1 − β = 0.801
- Group 1 / pairs / participants
- 64
- Group 2
- 64
- Total
- 128
- Power
- 0.8015
- Effect size d
- 0.5000
- df
- 126
- Noncentrality δ
- 2.828
To keep this page light, the plots are shown once, under the R tab.
Required size of group 1 (or pairs / participants) by power
| Effect | 70% power | 80% power | 90% power | 95% power |
|---|---|---|---|---|
| d = 0.38 | 89 | 113 | 151 | 186 |
| d = 0.50 | 51 | 64 | 86 | 105 |
| d = 0.62 | 33 | 42 | 55 | 68 |
- Noncentral t; two-sided power adds both rejection tails. Required sizes are rounded up and the power is recomputed at the rounded size.
- Group 2 = ⌈1 × group 1⌉.
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 nNo data needed
- Unequal one sidedNo data needed
- Paired powerNo data needed
- One sample effectNo data needed
- Tiny effect nNo data needed
- Two sample effectNo data needed
- Paired effectNo data needed
In the guides
Where it sits in the decision guides
- Sample-size planning · Statistics Guide · Step 0 — Prepare your data
- Two independent groups (continuous): Student / Welch t, Mann–Whitney U · Statistics Guide · B — Compare groups
- Paired (continuous): paired t, Wilcoxon signed-rank · Statistics Guide · B — Compare groups
- 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 effect — Finds the smallest effect this sample can detect; pick it when the sample size is fixed.
- Designdefault two-sample
Two independent groups, paired measurements, or one sample against a reference value.
- Two independent groups(default) — Compares the means of two independent groups; pick it for a treatment and a control group.
- Paired (two measurements per subject) — Compares two measurements on each subject; often needs far fewer subjects than two groups.
- One sample vs a reference value — Compares one group's mean with a fixed reference; pick it for a benchmark.
- Effect size (Cohen's d)default 0.5
Standardized mean difference; for paired designs d_z = mean difference / SD of differences. 0.2 small, 0.5 medium, 0.8 large (Cohen 1988).
- Sample size (group 1, pairs or participants)default 30
Size of group 1, or the number of pairs or participants (ignored when you solve for it).
- Allocation ratio (n₂ / n₁)default 1
Group 2 size 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 (effect 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.
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.
- d
- A difference in means expressed in standard deviations.
- df
- How much independent information is left to estimate variability after fitting the model.
- ncp
- How far a test statistic's distribution sits from its null. Larger values mean more power.
- crit
- The value a statistic must pass for the test to reject at the chosen significance level.
- 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-8). Python uses numpy, scipy, codesplash; R uses stats, codesplashr.
Default traps between the two languages
- Noncentral t: scipy uses Boost, R's pt(ncp =) uses AS 243 (error bound 1e-12), and R switches to a normal approximation when δ > 37.62 — only reached for power ≈ 1.
- Solved sizes and effects come from Brent root finding (scipy brentq ↔ R uniroot, tol 1e-12) on identical brackets; they agree to ~1e-9.
The exact script CodeSplash ran for example “Two sample n” · Python 3.13.5 · scipy 1.18.1 · pandas 3.1.0
# Power analysis — t tests
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method power.ttest 1.0.0 · spec 91cdd5784bea1eda
# Runs on its own: outside the app, cs.publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
import math
import numpy as np
from scipy import stats
import codesplash as cs
from codesplash.stats import power as pw # noncentral-t power and Brent solvers (twin of R cs_power_*)
# %% Inputs
solve_for = "n" # n | power | effect
design = "two-sample" # two-sample | paired | one-sample
alpha = 0.05
alternative = "two-sided" # two-sided adds both rejection tails (power.t.test(strict = TRUE))
ratio = 1 if design == "two-sample" else None # n2 / n1
d = 0.5
n_given = 30
target = 0.8
warnings = []
two = design == "two-sample"
lo = max(2.0, 2.0 / ratio) if two else 2.0 # smallest size with a defined test (n2 ≥ 2 too)
def power_at(n1, eff, n2=None):
"""Power with n1 in group 1 (n2 = ratio · n1 unless given) — noncentral t."""
if two:
return pw.t_power(eff, n1, ratio * n1 if n2 is None else n2, alpha, alternative)
return pw.t_power(eff, n1, None, alpha, alternative)
# Equivalent idiom: statsmodels.stats.power.TTestIndPower().solve_power(effect_size=d, alpha=alpha,
# power=target, ratio=ratio) — solved here with tighter tolerances so Python and R agree.
# %% Solve
n_exact = None
target_power = None
if solve_for == "n":
target_power = target
n_exact = pw.solve_increasing(lambda n: power_at(n, d), 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
d = pw.solve_increasing(lambda e: power_at(n1, e), target, 1e-6, 1.0, 1000.0)
if math.isnan(d):
warnings.append("No effect size up to d = 1000 reaches the target power with this sample.")
n2 = pw.ceil_n(ratio * n1) if two else None
power = power_at(n1, d, n2)
n_total = n1 + n2 if two else n1
df = n1 + n2 - 2.0 if two else n1 - 1.0
ncp = abs(d) * math.sqrt(n1 * n2 / (n1 + n2)) if two else abs(d) * math.sqrt(n1)
crit = float(stats.t.isf(alpha / (2 if alternative == "two-sided" else 1), df))
if n1 < 2 or (two and n2 < 2):
warnings.append("Each group needs at least two participants.")
# %% Curves and sensitivity
labels = {"two-sample": "two-sample t-test", "paired": "paired t-test", "one-sample": "one-sample t-test"}
n_ref = n1 if math.isfinite(n1) else 100.0
xs = pw.grid(lo, max(2.5 * n_ref, lo + 10.0))
effects = [0.75 * d, d, 1.25 * d] if math.isfinite(d) else [0.5]
curve_x, curve_power, curve_series = [], [], []
for e in effects:
for x in xs:
curve_x.append(float(x))
curve_power.append(power_at(float(x), e))
curve_series.append(f"d = {e:.2f}")
tables = []
if solve_for == "n" and math.isfinite(d):
rows = []
for e in effects:
row = {"effect": f"d = {e:.2f}"}
for tp in (0.7, 0.8, 0.9, 0.95):
row[f"p{round(tp * 100)}"] = pw.ceil_n(pw.solve_increasing(lambda n: power_at(n, e), tp, lo, max(2.0 * lo, 10.0), 1e7))
rows.append(row)
tables.append({
"key": "sensitivity", "title": "Required size of group 1 (or pairs / participants) by power",
"columns": [{"key": "effect", "label": "Effect", "type": "string"}]
+ [{"key": f"p{p}", "label": f"{p}% power", "type": "integer"} for p in (70, 80, 90, 95)],
"rows": rows,
})
# %% Result
size_text = f"{n1:,.0f} + {n2:,.0f} (N = {n_total:,.0f})" if two else f"{n1:,.0f}"
summary = f"{labels[design].capitalize()}: n = {size_text}, d = {d:.3f}, power = {power:.3f} at {alternative} α = {alpha}."
cs.publish({
"method": "power.ttest",
"methodVersion": "1.0.0",
"title": f"Power analysis: {labels[design]}",
"engine": cs.engine_info("numpy", "scipy"),
"summary": summary,
"apa": f"n = {size_text}, d = {cs.fmt_num(d)}, α = {alpha}, 1 − β = {power:.3f}",
"statistics": [
{"key": "n", "label": "Group 1 / pairs / participants" if two else ("Pairs" if design == "paired" 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": "d", "label": "Effect size d", "value": d},
{"key": "df", "label": "df", "value": df},
{"key": "ncp", "label": "Noncentrality δ", "value": ncp},
],
"tables": tables,
"warnings": warnings,
"notes": [
"Noncentral t; two-sided power adds both rejection tails. Required sizes are rounded up and the power is recomputed at the rounded size.",
*([f"Group 2 = ⌈{ratio:g} × group 1⌉."] if two else []),
],
"charts": [cs.chart(
"power", "power-curve", "Power by sample size",
{"x": cs.col("Sample size (group 1)" if two else "Sample size", curve_x),
"power": cs.col("Power", curve_power),
"series": cs.col("Effect", curve_series)},
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,
"d": d, "df": df, "ncp": ncp, "crit": crit, "alpha": alpha,
},
})
The exact script CodeSplash ran for example “Two sample n” · R version 4.5.1 (2025-06-13)
# Power analysis — t tests
# Generated by CodeSplash Analytics 1.0.0 on 2026-11-09 · method power.ttest 1.0.0 · spec 91cdd5784bea1eda
# Runs on its own: outside the app, cs_publish() writes $CS_RESULT_PATH or prints the result JSON.
# %% Setup
library(codesplashr) # cs_power_t() and the Brent solvers (twin of codesplash.stats.power)
# %% Inputs
solve_for <- "n" # n | power | effect
design <- "two-sample" # two-sample | paired | one-sample
alpha <- 0.05
alternative <- "two-sided" # two-sided adds both rejection tails (power.t.test(strict = TRUE))
two <- design == "two-sample"
ratio <- if (two) 1 else NA # n2 / n1
d <- 0.5
n_given <- 30
target <- 0.8
warnings <- list()
lo <- if (two) max(2, 2 / ratio) else 2 # smallest size with a defined test (n2 >= 2 too)
power_at <- function(n1, eff, n2 = NULL) {
# Power with n1 in group 1 (n2 = ratio * n1 unless given) - noncentral t
if (two) cs_power_t(eff, n1, if (is.null(n2)) ratio * n1 else n2, alpha, alternative)
else cs_power_t(eff, n1, NULL, alpha, alternative)
}
# Equivalent idiom for equal groups: power.t.test(delta = d, sd = 1, sig.level = alpha, power = target,
# type = "two.sample", strict = TRUE) - solved here with tighter tolerances so R and Python agree.
# %% Solve
n_exact <- NA
target_power <- NA
if (solve_for == "n") {
target_power <- target
n_exact <- cs_power_solve_increasing(function(n) power_at(n, d), 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
d <- cs_power_solve_increasing(function(e) power_at(n1, e), target, 1e-6, 1, 1000)
if (is.nan(d)) warnings <- c(warnings, "No effect size up to d = 1000 reaches the target power with this sample.")
}
n2 <- if (two) cs_power_ceil(ratio * n1) else NA
power <- power_at(n1, d, if (two) n2 else NULL)
n_total <- if (two) n1 + n2 else n1
df <- if (two) n1 + n2 - 2 else n1 - 1
ncp <- if (two) abs(d) * sqrt(n1 * n2 / (n1 + n2)) else abs(d) * sqrt(n1)
crit <- stats::qt(alpha / (if (alternative == "two-sided") 2 else 1), df, lower.tail = FALSE)
if (is.finite(n1) && (n1 < 2 || (two && n2 < 2))) warnings <- c(warnings, "Each group needs at least two participants.")
# %% Curves and sensitivity
labels <- c("two-sample" = "two-sample t-test", "paired" = "paired t-test", "one-sample" = "one-sample t-test")
n_ref <- if (is.finite(n1)) n1 else 100
xs <- cs_power_grid(lo, max(2.5 * n_ref, lo + 10))
effects <- if (is.finite(d)) c(0.75 * d, d, 1.25 * d) else 0.5
curve <- do.call(rbind, lapply(effects, function(e) {
data.frame(x = xs, power = vapply(xs, function(x) power_at(x, e), numeric(1)), series = sprintf("d = %.2f", e))
}))
tables <- list()
if (solve_for == "n" && is.finite(d)) {
rows <- lapply(effects, function(e) {
row <- list(effect = sprintf("d = %.2f", e))
for (tp in c(0.7, 0.8, 0.9, 0.95)) {
row[[sprintf("p%d", round(tp * 100))]] <-
cs_power_ceil(cs_power_solve_increasing(function(n) power_at(n, e), tp, lo, max(2 * lo, 10), 1e7))
}
row
})
tables <- list(list(
key = "sensitivity", title = "Required size of group 1 (or pairs / participants) by power",
columns = c(list(list(key = "effect", label = "Effect", 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
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 / pairs / participants" else if (design == "paired") "Pairs" 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 = "d", label = "Effect size d", value = d),
list(key = "df", label = "df", value = df),
list(key = "ncp", label = "Noncentrality δ", value = ncp)
))
cs_publish(list(
method = "power.ttest",
methodVersion = "1.0.0",
title = sprintf("Power analysis: %s", labels[[design]]),
engine = cs_engine_info("stats"),
summary = sprintf("%s: n = %s, d = %.3f, power = %.3f at %s alpha = %s.", labels[[design]], size_text, d, power, alternative, format(alpha)),
apa = sprintf("n = %s, d = %s, α = %s, 1 − β = %.3f", size_text, cs_fmt_num(d), format(alpha), power),
statistics = stats_list,
tables = tables,
warnings = warnings,
notes = c("Noncentral t; two-sided power adds both rejection tails. Required sizes are rounded up and the power is recomputed at the rounded size.",
if (two) sprintf("Group 2 = ceiling(%s × group 1).", format(ratio))),
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("Effect", 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,
d = d, df = df, ncp = ncp, crit = crit, alpha = alpha
)
))
Sources
- Cohen, J. (1988). Statistical power analysis for the behavioral sciences (2nd ed.). Erlbaum.
- Faul, F., Erdfelder, E., Lang, A.-G., & Buchner, A. (2007). G*Power 3. Behavior Research Methods, 39, 175–191.
From the CodeSplash method registry · power.ttest v1.0.0.