Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 57 ยท Estimation

Confidence Intervals for Proportions

Estimate a population proportion with Wald and Wilson confidence intervals, understand poll margins of error, sample sizes and differences of two proportions, with Python.

Many questions are about shares: what percentage of voters support a candidate, what fraction of visitors click an ad, what proportion of parts are defective. The data are yes/no answers, and the target is the population proportion p. This lesson builds confidence intervals for one proportion and for the difference between two.

The sample proportion

If x of n people say yes, the sample proportion is p-hat = x / n. As we saw with the central limit theorem, for large n it is approximately normal with mean p and standard error sqrt(p (1 - p) / n). Since p is unknown, we plug in p-hat for the estimated standard error.

The Wald interval

The classic textbook interval is

TEXT
p-hat  +/-  z* x sqrt(p-hat (1 - p-hat) / n)

It needs n p-hat >= 10 and n (1 - p-hat) >= 10 (about 10 successes and 10 failures) to be trustworthy.

Python A poll with a Wald interval

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

News reports that say "plus or minus 3 points" are quoting exactly this margin of error. The conservative value uses p-hat = 0.5 (the worst case, maximizing p (1 - p)), which gives roughly 1 / sqrt(n) for 95% confidence.

The Wilson interval

The Wald interval can perform badly when p is near 0 or 1, or n is small: it may spill below 0, and its real coverage can fall well under 95%. The Wilson score interval fixes most of this and is the better default. It pulls the center slightly toward 0.5:

TEXT
center = (p-hat + z^2 / (2n)) / (1 + z^2 / n)
half   = z / (1 + z^2 / n) x sqrt(p-hat (1 - p-hat) / n + z^2 / (4 n^2))
Python Wald versus Wilson with few successes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With 0 successes out of 25 the Wald interval is the single point 0, which is silly; Wilson gives a sensible upper bound. (A quick approximation for zero events: the 95% upper bound is about 3 / n, the "rule of three".) Libraries such as statsmodels offer proportion_confint with methods normal, wilson, beta (Clopper-Pearson, which is conservative) and others.

Sample size for a poll

Solving E = z* sqrt(p (1 - p) / n) for n:

TEXT
n = z*^2 x p (1 - p) / E^2

With no prior idea of p, use p = 0.5 for the safest answer.

Python Sample size for a margin of error

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Going from a 3% to a 1% margin requires about nine times as many respondents. That is why most polls stop near 1,000 people.

Comparing two proportions

To compare two groups (for instance, conversion rates for page A and page B), estimate the difference p1 - p2:

TEXT
(p-hat1 - p-hat2)  +/-  z* x sqrt(p-hat1 (1 - p-hat1) / n1 + p-hat2 (1 - p-hat2) / n2)

If the interval for the difference excludes 0, the data suggest a real difference.

Python A/B test difference

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Margin of error covers only random sampling error. It says nothing about bias from poor frames, nonresponse or leading questions. A poll with a "plus or minus 3" margin can still miss by more.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In a poll of n = 400, p-hat = 0.5. What is the standard error?

    SE = sqrt(0.5 * 0.5 / 400) = sqrt(0.000625) = 0.025.
  2. 2. Which value of p-hat gives the largest margin of error for a given n?

    p (1 - p) is largest at 0.5.
  3. 3. Why prefer the Wilson interval to the Wald interval for small samples?

    Wilson stays within [0, 1] and gives coverage closer to the nominal level.
  4. 4. To go from a 4% to a 2% margin of error, the sample size must be about:

    n is proportional to 1 / E^2, so halving E multiplies n by four.

Summary

  • p-hat = x / n; the standard error is sqrt(p-hat (1 - p-hat) / n).
  • The Wald interval is simple but unreliable for small n or extreme p; Wilson is a safer default.
  • Sample size for a margin of error E: n = z*^2 p (1 - p) / E^2, worst case p = 0.5.
  • The difference of two proportions has its own interval; if it excludes 0, the groups likely differ.
  • Margin of error ignores bias.

Next: The Logic of Hypothesis Testing