Sub topic 8
How many observations: sample size for a proportion
This chapter assumes you know what a proportion and a sample are, that you have met a 95% confidence level, and that you have already chosen an attribute metric with an operational definition, such as the invoice error rate. It covers how many items to inspect so the proportion you report is precise enough to act on.
Why the number matters
A proportion is the share of items that are defective: defective items divided by items inspected. If you inspect 20 invoices and find 3 defective, the estimate is 15%. A second sample of 20 might show 1 defective, or 5. A small sample gives an unstable estimate, and a team can chase a "problem" that is only sampling noise.
A very large sample costs time and money. It may also delay the project, or lower the quality of each check because collectors rush. The goal is the smallest sample that answers the question with a stated precision.
The working model
Sample size for a proportion rests on three choices.
- Confidence level. How sure you want to be that the true rate lies in your interval. In Lean Six Sigma work, 95% is the usual default. It corresponds to a z value of 1.96.
- Margin of error (E). The half-width of the interval you will accept, in proportion units. A margin of 0.05 means "plus or minus 5 percentage points".
- Expected proportion (p). Your best guess of the true defect rate before you collect data.
The standard formula is:
n = z² × p × (1 − p) / E²
The product p × (1 − p) is largest at p = 0.5, where it equals 0.25. When you have no prior estimate, use p = 0.5. The result is the largest sample you could need for that margin, so the answer is conservative.
With z = 1.96, p = 0.5 and E = 0.05, n = 3.8416 × 0.25 / 0.0025 = 384.16. Always round up, to 385. Rounding down would give a margin slightly wider than you promised.
import math
def sample_size(p, margin, z=1.96, population=None):
"""Observations needed to estimate a proportion within +/- margin."""
n = z**2 * p * (1 - p) / margin**2
if population is not None:
# Finite population correction (see below)
n = n / (1 + (n - 1) / population)
return math.ceil(n)
print(sample_size(0.5, 0.05)) # 385
print(sample_size(0.10, 0.05)) # 139
print(sample_size(0.5, 0.10)) # 97
print(sample_size(0.5, 0.05, z=2.576)) # 664 (99% confidence)
print(sample_size(0.5, 0.05, population=500)) # 218What drives the number
Margin of error has the strongest effect, because it is squared in the denominator. Halving the margin quadruples the sample.
| Margin of error | Observations (p = 0.5, 95% confidence) |
|---|---|
| ±10 points | 97 |
| ±5 points | 385 |
| ±3 points | 1,068 |
| ±2 points | 2,401 |
Moving from ±5 to ±2 points costs about six times the effort. Ask what decision the number supports. If the team needs to know whether the error rate is near 10% or near 30%, ±10 points is enough. If it must confirm a drop from 12% to 10%, you need a much tighter margin, and probably a different tool, such as a hypothesis test for two proportions, with its own sample size calculation.
Confidence level matters less than margin, but it still has a cost. Raising confidence from 95% to 99% raises the sample from 385 to 664 at the same margin.
The expected proportion matters when you have real knowledge of it. If past audits suggest about 10% of invoices are defective, using p = 0.10 gives 139 observations for ±5 points instead of 385. That saving is only valid if your guess is reasonable. If the true rate turns out to be 30%, your real margin will be wider than ±5 points.
Choosing p
- With no data, use 0.5.
- With a small pilot or old audit data, use that rate. Round toward 0.5 if you are unsure, because that gives a larger sample.
- Do not use a target or a hoped-for rate. You are estimating the current state, not the goal.
Finite populations
The formula assumes the population is very large compared with the sample. If you will inspect a large share of a small population, you need fewer items. The finite population correction is:
n_adjusted = n / (1 + (n − 1) / N)
where N is the population size. For 500 invoices in the period, the 385 from the basic formula drops to 218. The code above applies this. As a rule of thumb, apply the correction when the sample would exceed about 5% of the population. When N is small enough (a few hundred items, say), checking every item may be simpler than sampling.
Rare defects
The formula uses a normal approximation. It works poorly when the defect rate is very low, because the sample may contain few or no defects. Suppose the true rate is 1% and you inspect 100 items. The chance of seeing zero defects is 0.99^100, about 37%. You would report 0% and learn nothing about where the problem is.
For low rates, check two things.
- Expected defects. Multiply n by p. If you expect fewer than about 5 defects in the sample, the estimate is unreliable. Increase n, or use an exact or Wilson interval from a statistics package.
- Relative precision. A margin of ±5 points is meaningless when p is 2%. It would allow a true rate of 0% to 7%. Choose a margin that is a fraction of p. For p = 0.02 and E = 0.01, n = 753.
import math
p, E, z = 0.02, 0.01, 1.96
n = math.ceil(z**2 * p * (1 - p) / E**2)
print(n) # 753
print(round(n * p)) # 15 expected defects in the sampleStratified samples
The plan from the earlier stratification chapter may split the data by billing team, customer segment or week. The sample size calculation then applies to each stratum you want to report separately. If you want a ±5 point estimate for each of three billing teams, you need about 385 observations per team, or 1,155 in total. Teams of different sizes can use the finite population correction separately.
If you only need the overall rate, a stratified sample can be allocated in proportion to each stratum's size, and the total from the formula is enough. Decide which reporting level matters before you compute, because the total can differ by a factor of the number of strata.
Reading the result backward
After collecting data, compute the margin you actually achieved:
E = z × √(p̂ × (1 − p̂) / n)
where p̂ is the observed proportion.
import math
n = 400
defective = 36
p_hat = defective / n
margin = 1.96 * math.sqrt(p_hat * (1 - p_hat) / n)
print(f"{p_hat:.1%} +/- {margin:.1%}") # 9.0% +/- 2.8%Report the estimate with its margin: "9.0% defective, plus or minus 2.8 points at 95% confidence". A bare "9%" overstates how much you know.
Common errors
- Using the wrong data type. This formula is for attribute (pass/fail) data. A continuous metric such as cycle time uses a different formula, based on the standard deviation.
- Counting observations that are not independent. Ten lines on one invoice are not ten invoices. Sample the unit in the operational definition, here one invoice. Items that are clustered, such as many invoices from one clerk on one day, carry less information than the same number of random items.
- Sampling the convenient items. The formula assumes a random or otherwise representative sample. A large sample of the first 385 invoices of the month does not rescue a biased selection.
- Forgetting non-response and bad records. If you expect 10% of records to be unusable, collect about 10% more than the formula says.
- Treating the number as fixed. Recompute after the pilot, when you have a real estimate of p.
Choosing the sample size in practice
The diagram shows the order of decisions.
Write the result into the data collection plan's "how many" field, together with the assumptions: confidence level, margin, p, and whether the correction was applied. A reader should be able to reproduce the number.
Exercise
A billing team issues about 800 invoices a month. An earlier audit found roughly 12% defective. You want to estimate this month's defect rate to within ±4 percentage points at 95% confidence. Compute the sample size with p = 0.12, then apply the finite population correction for N = 800. Which sample size do you plan for, and why?
Show answer
Basic formula: n = 1.96² × 0.12 × 0.88 / 0.04² = 3.8416 × 0.1056 / 0.0016 = 253.55, which rounds up to 254. Correction: 253.55 / (1 + 252.55 / 800) = 253.55 / 1.3157 = 192.7, which rounds up to 193. The sample is about 24% of the population, well above 5%, so the correction applies and you plan for 193 invoices, plus a buffer for unusable records. The expected defects are about 193 × 0.12 = 23, which is enough for the normal approximation.
Exercise
A team must report separate defect rates, each within ±5 points at 95% confidence, for four customer segments. They know nothing about the true rates. How many observations do they need in total, and what is the cost of also wanting ±2.5 points?
Show answer
With p = 0.5, each segment needs 385 observations, so 4 × 385 = 1,540 in total. Halving the margin to ±2.5 points quadruples the sample per segment, since the margin is squared: 3.8416 × 0.25 / 0.025² = 1,536.64, rounded up to 1,537 per segment, or 6,148 in total.