Testing a Proportion
This lesson applies the hypothesis testing recipe to a population proportion with a right-tailed test: we look for evidence that the true proportion is greater than a reference value p₀.
The scenario
A language-learning app knows from years of data that 62% of new users complete the first week of lessons. The team redesigns the onboarding and wants to know whether the completion rate has increased. Of 400 randomly selected users who saw the new onboarding, 268 completed the first week.
Step 1: Hypotheses
H₀: p = 0.62 (the redesign changed nothing)
H₁: p > 0.62 (completion improved) ← right-tailedp is the completion rate of all future users with the new onboarding. The direction comes from the question ("increased"), and is fixed before looking at the data.
Step 2: Significance level
We choose α = 0.05.
Step 3: Conditions
- The users are a random sample and behave independently.
- Under
H₀, the expected numbers of successes and failures are large enough:n × p₀ ≥ 5andn × (1 − p₀) ≥ 5(some books use 10).
n, p0 = 400, 0.62
print("expected successes n*p0 =", n * p0)
print("expected failures n*(1-p0) =", round(n * (1 - p0), 1))expected successes n*p0 = 248.0
expected failures n*(1-p0) = 152.0
Both are far above 5, so the normal approximation is fine.
Step 4: Test statistic
The test statistic measures how many standard errors the sample proportion lies from p₀. Note that the standard error uses p₀ (the value assumed under H₀), not p̂:
z = (p̂ − p₀) / √(p₀(1 − p₀) / n)import math
x, n, p0 = 268, 400, 0.62
p_hat = x / n
se0 = math.sqrt(p0 * (1 - p0) / n)
z = (p_hat - p0) / se0
print(f"p-hat = {p_hat:.3f}")
print(f"SE under H0 = {se0:.5f}")
print(f"z = {z:.3f}")p-hat = 0.670
SE under H0 = 0.02427
z = 2.060
The sample proportion is about 2.06 standard errors above 0.62.
Step 5: p-value
For a right-tailed test, the p-value is the area to the right of z under the standard normal curve: P(Z ≥ z).
from scipy import stats
z = 2.0602
p_value = stats.norm.sf(z) # area to the right
z_crit = stats.norm.ppf(1 - 0.05) # right-tailed critical value
print(f"p-value = {p_value:.4f}")
print(f"critical value z* = {z_crit:.3f} -> reject if z > {z_crit:.3f}")p-value = 0.0197
critical value z* = 1.645 -> reject if z > 1.645
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
z_obs, z_crit = 2.0602, stats.norm.ppf(0.95)
x = np.linspace(-4, 4, 400)
fig, ax = plt.subplots(figsize=(6.5, 3))
ax.plot(x, stats.norm.pdf(x), color="#2a78d6", linewidth=2)
tail = x >= z_crit
ax.fill_between(x[tail], stats.norm.pdf(x[tail]), color="#eb6834", alpha=0.5, label="rejection region (5%)")
ax.axvline(z_obs, color="#0b0b0b", linestyle="--", label=f"observed z = {z_obs:.2f}")
ax.legend(frameon=False)
ax.set_yticks([])
plt.tight_layout()
plt.show()
print("The observed z falls inside the right-tail rejection region")The observed z falls inside the right-tail rejection region
Step 6: Conclusion
p-value ≈ 0.020 < α = 0.05 (equivalently, z = 2.06 > 1.645), so we reject H₀.
In context: at the 5% significance level, the data provide evidence that the first-week completion rate with the new onboarding is higher than 62%.
The size of the improvement matters too: the estimate is 67%, about 5 percentage points more. Whether that is worth the cost of the redesign is a business question, not a statistical one.
The same test with statsmodels
proportions_ztest performs the whole test. Pass prop_var=p0 so that it uses p₀ in the standard error, as the textbook test does:
from statsmodels.stats.proportion import proportions_ztest
z, p_value = proportions_ztest(count=268, nobs=400, value=0.62,
alternative="larger", prop_var=0.62)
print(f"z = {z:.3f}, p-value = {p_value:.4f}")z = 2.060, p-value = 0.0197
An exact alternative
The exact binomial test needs no normal approximation, which makes it the safer choice for small samples:
from scipy import stats
result = stats.binomtest(268, n=400, p=0.62, alternative="greater")
print(f"exact p-value = {result.pvalue:.4f}")
ci = result.proportion_ci(confidence_level=0.95)
print(f"one-sided 95% bound: p > {ci.low:.4f}")exact p-value = 0.0215
one-sided 95% bound: p > 0.6292
The exact p-value is very close to the z-test result and leads to the same decision. The one-sided confidence bound says that, with 95% confidence, the true rate is above 0.62, which is exactly why the test rejected H₀.
Test yourself
In a right-tailed test of a proportion, which area is the p-value?
- The area to the left of z
- The area to the right of z
- Twice the area to the right of |z|
- The area between −z and z
Which standard error is used in the z statistic of a one-proportion test?
- √(p̂(1 − p̂)/n)
- √(p₀(1 − p₀)/n)
- s / √n
- p₀ / n
A right-tailed test at α = 0.05 gives z = 1.40. What is the decision?
- Reject H₀ because z > 0
- Fail to reject H₀ because z < 1.645
- Reject H₀ because z > 1.40
- Accept H₁