Hypothesis Tests
One-Proportion z Statistic
Compute the one-proportion z statistic: z = (p̂ − p0) / √(p0(1−p0)/n), where
p̂ = successes/n and SE is calculated under H0. Formula only — no loops. The
library half replicates the same formula with numpy and computes the two-sided
p-value via scipy.stats.norm.sf(|z|) × 2 (snapshot only). RESULT is the z
statistic, matching between naive and library.
By hand
With the library
The z formula is applied with numpy; scipy.stats.norm.sf(|z|) × 2 gives the
two-sided p-value (normal approximation). RESULT is the z statistic.
import math
successes = 30
n = 50
p0 = 0.5
phat = successes / n
se0 = math.sqrt(p0 * (1 - p0) / n)
z = (phat - p0) / se0
print('RESULT:', round(z, 4))
import numpy as np
from scipy import stats
from dalib.display import set_display
set_display()
successes = 30
n = 50
p0 = 0.5
phat = successes / n
se0 = float(np.sqrt(p0 * (1 - p0) / n))
z = float((phat - p0) / se0)
pvalue = float(stats.norm.sf(abs(z)) * 2)
print('phat:', round(phat, 4))
print('se0:', round(se0, 6))
print('z_stat:', round(z, 4))
print('pvalue:', round(pvalue, 4))
print('RESULT:', round(z, 4))
phat: 0.6
se0: 0.070711
z_stat: 1.4142
pvalue: 0.1573
RESULT: 1.4142
Honesty
This lesson shows the computation exactly, on a tiny pinned sample. The arithmetic is correct and reproducible, but with a sample this small the result is not a valid statistical finding — it demonstrates the mechanism, not evidence. Real inference needs an adequate sample size and assumption checks (e.g. random sampling, independence, n×p₀ ≥ 5, and n×(1−p₀) ≥ 5); the p-value / interval here should be read as "how the formula is computed," not as a conclusion about a population.
Implementation notes
- Design rule §7: RESULT = statistic only; p-value is snapshot-only.
- SE is computed under H0 (using p0, not p̂). This is the standard null standard error for the z test; using p̂ instead would give a confidence interval SE, not a test statistic SE.
- Valid when n×p0 ≥ 5 and n×(1−p0) ≥ 5 (normal approximation). Here 50×0.5=25 ✓.
- z=√2 here: phat−p0=0.1, SE₀=0.1/√2, so z=√2 exactly.
- Cross-reference:
normal-pdf-point(ch05) for the standard normal density used in the p-value computation.