Type I and II errors, and power
Two ways to be wrong, and how sample size trades them off. Plan a study so that it can actually answer your question.
What runs in CodeSplash
- Power analysis — t tests Verified in Python and R
You will be able to
- Define Type I error, Type II error and power
- Say how effect size, sample size and α change power
- Plan a sample size for a stated effect, and avoid "observed power"
Read first
Two ways to be wrong
ReadEvery yes-or-no test has four possible outcomes.
| No real effect | A real effect | |
|---|---|---|
| Test says "significant" | Type I errorType I errorRejecting H₀ when it is actually true — a false alarm. (rate α) | Correct |
| Test says "not significant" | Correct | Type II errorType II errorMissing a real effect — failing to reject H₀ when it is false. (rate β) |
PowerStatistical powerThe chance a test detects an effect of a given size, if that effect is real. is : the chance of detecting an effect of a given size, if it's really there. You choose α directly. Power you get by design.
What raises power most?
PredictPredict first
A two-group study has moderate power. Which change raises power the most?
Reveal
Doubling the effect. The signal-to-noise ratio grows in proportion to the effect, but only with the square root of . Doubling the effect is worth as much as four times the sample, which is why measuring well and choosing a strong contrast matter so much.
Explore power
ExploreGuess the power first. Then try the challenge: find the smallest n per group that gives 80% power for . Raise Outcomes tested (Bonferroni) and see what testing many outcomes costs.
BExplorable · Power explorer
How likely is my study to detect an effect that is really there?
Guess the power: the chance this study rejects H0 if the effect is real.
Standardized difference between the group means.
α
Alternative
Each outcome is tested at α ÷ this number.
Solid: t under H₀. Dashed: t if the effect is real. Shaded orange: power, the alternative's area inside the hatched rejection region.
Commit your prediction above to see the result
Make your prediction to see the result.
Plan the number
ReadWith d = 0.5, α = 0.05 and a target of 0.8, you need 64 per group, or 128 in total.
The hard input is . Use the smallest effect that matters to you, rather than an optimistic guess or a pilot's noisy estimate.
Many studies, one rule
ExploreLow power hurts more than missed effects. When few tested ideas are true, and power is low, many significant results are false alarms. Change Power and Share of studies with a real effect, and watch False discoveries.
BExplorable · Truth and decision: the four quadrants
Out of many studies, how many significant results are real?
Of the significant results, what share will be false alarms?
- Real effect, significant: 50
- No effect, significant (false alarm): 45
- Real effect, missed: 50
- No effect, not significant: 855
Commit your prediction above to see the result
Make your prediction to see the result.
Check your reading
CheckpointCheck yourself
A new drug truly works, but your trial finds p = 0.30. What kind of error is that?
Show the answer
Answer: b
A real effect went undetected. You can't tell this happened from the trial alone, which is why power is planned in advance.
Check yourself
How many subjects per group does a two-sided t test need for 80% power at d = 0.5 and α = .05?
Show the answer
Answer: 64
The exact answer is a little under 64, and you round up to whole subjects.
Check yourself
You planned for d = 0.5. Your budget changes, and you want to plan for d = 0.25 with the same power. About how many subjects will you need?
Show the answer
Answer: c
Sample size grows with one over the square of the effect. Half the effect takes four times the data.
Plan it in the app
In the appAnalyze has power analyses for t tests, proportions, ANOVA, correlations, regressions and survival. Use Solve for to get the sample size, the power or the detectable effect.
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).
The exact R script · R 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
)
))
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⌉.
The exact Python script · Python 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,
},
})
from statsmodels.stats.power import TTestIndPower
n = TTestIndPower().solve_power(effect_size=0.5, alpha=0.05, power=0.8)
print(n)power.t.test(delta = 0.5, sd = 1, sig.level = 0.05, power = 0.8)Recap
Recap- Type I: a false alarm, at rate α. Type II: a missed effect, at rate β. Power is .
- Power rises with the effect, the sample size and α, and falls as noise grows.
- Half the effect takes four times the data.
- Plan power before the study, for an effect that matters. "Observed power" adds nothing to the p-value.
Concepts in this lesson