Inference

Power and sample size

Before collecting data: how likely is the test to detect an effect of a given size, and how many observations does it need? For the z test and the one-proportion test the textbook answers are formulas in normal quantiles. Here the quantiles are certified, so the power is an interval certain to contain it, and a sample size is the smallest whole number proven to meet the target.

Power of the z test

Test H0:μ=μ0H_0: \mu = \mu_0 with known σ\sigma and nn observations. Under the alternative μ\mu the statistic Z=(xˉ−μ0)n/σZ = (\bar x - \mu_0)\sqrt n/\sigma is normal with mean δ=(μ−μ0)n/σ\delta = (\mu - \mu_0)\sqrt n/\sigma and variance 1, so for H1:μ>μ0H_1: \mu > \mu_0

power=P(Z>z1−α)=1−Φ(z1−α−δ), \text{power} = P(Z > z_{1-\alpha}) = 1 - \Phi(z_{1-\alpha} - \delta),

and for a two-sided test Φ(−z1−α/2+δ)+Φ(−z1−α/2−δ)\Phi(-z_{1-\alpha/2} + \delta) + \Phi(-z_{1-\alpha/2} - \delta), both tails counted. power returns that number as an enclosure.

import { power, sampleSize, show } from "@ratmath/stats";

const p = power({ mu0: 100, mu: 106, sigma: 15, n: 25, alternative: "greater" });
show(p.power);     // "0.638760": reject when x̄ > 104.935, and P(Z > −0.355) ≈ 0.6388
power({ test: "proportion", p0: "0.5", p: "0.6", n: 100, alternative: "greater" }).power;   // ≈ 0.6415

The one-proportion version uses the normal approximation, as textbooks do: the cut-offs come from the standard error under p0p_0, the spread of p^\hat p from the one under pp. For other tests, where no closed form is standard, powerCurve estimates power by simulation.

Sample size for a margin of error

A 95% interval for a proportion has half-width zp(1−p)/nz\sqrt{p(1-p)/n}, so a margin of at most mm needs n≥z2p(1−p)/m2n \ge z^2 p(1-p)/m^2, with p=12p = \tfrac12 when nothing is known (the largest p(1−p)p(1-p)). For a mean with known σ\sigma, n≥(zσ/m)2n \ge (z\sigma/m)^2. The bound is enclosed and rounded up only once its enclosure lies within one whole step, so the answer is never off by one.

sampleSize({ margin: "0.03" }).n;             // 1068: (1.96/0.03)²·¼ = 1067.1
sampleSize({ margin: 2, sigma: 10 }).n;       // 97: (1.96·10/2)² = 96.04

n is a JavaScript number while it is exact as one; above 253−12^{53} - 1 it is returned as a bigint, because a number would round it (for σ=1\sigma = 1 and margin 10−910^{-9} the smallest nn is 3841458820694125959, which as a number would read 121 higher).

Sample size for a target power

With power given, sampleSize finds the smallest nn whose power is proven to reach it: the power grows with nn, so it doubles nn until the target is met and then bisects. For a one-sided z test this agrees with the formula n≥((z1−α+z1−β)σ/δ)2n \ge \big((z_{1-\alpha} + z_{1-\beta})\sigma/\delta\big)^2; for a two-sided test it also counts the far tail, which the formula ignores. With no effect (μ=μ0\mu = \mu_0) the power is α\alpha for every nn, so a target up to α\alpha is met by n=1n = 1 and a larger one is refused.

sampleSize({ power: "0.8", mu0: 100, mu: 106, sigma: 15, alternative: "greater" }).n;   // 39
sampleSize({ power: "0.9", mu0: 0, mu: "0.5", sigma: 1 }).n;                          // 43
Try itEdit the code and run it with @ratmath/stats and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs