Population Proportion Estimation
A proportion is the share of a population that has some characteristic: the fraction of voters supporting a candidate, of customers who would recommend a shop, of parts that are defective. We estimate the unknown population proportion p from a sample.
The point estimate
If x of the n sampled individuals have the characteristic, the sample proportion is
p̂ = x / nA city surveyed 600 randomly chosen residents about a proposed tram line; 342 said they would use it.
x, n = 342, 600
p_hat = x / n
print(f"p-hat = {x}/{n} = {p_hat:.3f} ({p_hat:.1%})")p-hat = 342/600 = 0.570 (57.0%)
The confidence interval
The standard error of p̂ is √(p̂(1 − p̂) / n). For large samples, p̂ is approximately normally distributed, so the confidence interval uses a z critical value:
p̂ ± z* × √(p̂(1 − p̂) / n)This is called the Wald interval. Its conditions:
- The sample is random, and individuals are independent (if sampling without replacement,
nshould be less than about 10% of the population). - There are enough "successes" and "failures":
n × p̂ ≥ 5andn × (1 − p̂) ≥ 5(some books use 10).
Step by step for the tram survey at 95% confidence:
import math
from scipy import stats
x, n = 342, 600
p_hat = x / n
print("successes, failures:", x, n - x, "(both >= 5: OK)")
se = math.sqrt(p_hat * (1 - p_hat) / n)
z_star = stats.norm.ppf(0.975)
margin = z_star * se
print(f"standard error = {se:.4f}")
print(f"margin of error = {z_star:.3f} x {se:.4f} = {margin:.4f}")
print(f"95% CI: ({p_hat - margin:.4f}, {p_hat + margin:.4f})")successes, failures: 342 258 (both >= 5: OK)
standard error = 0.0202
margin of error = 1.960 x 0.0202 = 0.0396
95% CI: (0.5304, 0.6096)
We are 95% confident that between about 53% and 61% of all residents would use the tram. Since the whole interval lies above 50%, the data suggests a majority would use it.
Different confidence levels
import math
from scipy import stats
x, n = 342, 600
p_hat = x / n
se = math.sqrt(p_hat * (1 - p_hat) / n)
for conf in [0.90, 0.95, 0.99]:
z_star = stats.norm.ppf(1 - (1 - conf) / 2)
lo, hi = p_hat - z_star * se, p_hat + z_star * se
print(f"{conf:.0%} CI: ({lo:.3f}, {hi:.3f}) width {hi - lo:.3f}")90% CI: (0.537, 0.603) width 0.066
95% CI: (0.530, 0.610) width 0.079
99% CI: (0.518, 0.622) width 0.104
Better intervals for small samples: Wilson
The Wald interval performs poorly when n is small or p̂ is near 0 or 1: its real coverage can be far below the nominal 95%. The Wilson score interval fixes this and is recommended by most statisticians. statsmodels computes both:
from statsmodels.stats.proportion import proportion_confint
for x, n in [(342, 600), (3, 40)]:
wald = proportion_confint(x, n, alpha=0.05, method="normal")
wilson = proportion_confint(x, n, alpha=0.05, method="wilson")
print(f"x={x:3d}, n={n:3d}: Wald ({wald[0]:.4f}, {wald[1]:.4f}) Wilson ({wilson[0]:.4f}, {wilson[1]:.4f})")x=342, n=600: Wald (0.5304, 0.6096) Wilson (0.5301, 0.6090)
x= 3, n= 40: Wald (0.0000, 0.1566) Wilson (0.0258, 0.1986)
For the big tram survey the two methods agree almost exactly. For 3 defective parts out of 40, the Wald formula actually gives a lower limit of about −0.007, an impossible proportion, which statsmodels silently clips to 0. That is a sign the normal approximation has broken down. The Wilson interval stays inside 0–1 and is asymmetric around p̂ = 0.075, as it should be near a boundary.
How many people should we ask?
The margin of error is E = z* × √(p(1 − p) / n). Solving for n:
n = z*² × p(1 − p) / E²If you have no idea what p is, use p = 0.5, which gives the largest (safest) n:
import math
from scipy import stats
z_star = stats.norm.ppf(0.975)
for E in [0.05, 0.03, 0.02, 0.01]:
n = math.ceil(z_star**2 * 0.5 * 0.5 / E**2)
print(f"margin ±{E:.0%}: need n = {n}")margin ±5%: need n = 385
margin ±3%: need n = 1068
margin ±2%: need n = 2401
margin ±1%: need n = 9604
This is why national opinion polls typically interview about 1,000 people: that gives a margin of error of roughly ±3 percentage points. Notice that the population size does not appear in the formula: a poll of 1,000 works as well for a country of 80 million as for a city of 1 million (as long as the sample is random).
Checking the coverage by simulation
import numpy as np
from scipy import stats
rng = np.random.default_rng(600)
p_true, n, reps = 0.57, 600, 10_000
x = rng.binomial(n, p_true, size=reps)
p_hat = x / n
se = np.sqrt(p_hat * (1 - p_hat) / n)
z = stats.norm.ppf(0.975)
covered = (p_hat - z * se <= p_true) & (p_true <= p_hat + z * se)
print(f"share of 95% intervals containing p: {covered.mean():.4f}")share of 95% intervals containing p: 0.9476
With a large sample and a proportion far from 0 and 1, the Wald interval achieves its promised coverage.
Test yourself
In a sample of 250 customers, 40 used a coupon. What is p̂?
- 0.04
- 0.16
- 0.25
- 40
Why is p = 0.5 used for sample-size planning when nothing is known about p?
- It is the most common value
- p(1 − p) is largest at 0.5, so it gives the most conservative (largest) n
- It makes the math easier
- Proportions are always near 0.5
A Wald interval for 2 successes out of 30 runs from −0.03 to 0.16. What should you do?
- Report it as is
- Use a better method such as the Wilson interval
- Set the lower limit to the sample proportion
- Double the sample proportion