Confidence Intervals and Sample Size, Worked Through

Where the margin of error comes from, when to use t instead of z, why the Wilson interval beats Wald for proportions, and how 385 became the default survey size.

· 9 min read

Illustration for the Confidence Interval Calculator guide

A sample gives you an estimate: 31% of customers would recommend you, the average part is 12.14 mm long. The question that follows is always "how sure are you?" A confidence interval answers it after the data are in, and a sample size calculation answers it before you start. Both rest on the same small formula, and this guide works through it with the Confidence Interval Calculator and the Sample Size Calculator so every number can be checked.

The one formula behind both tools

Almost every interval in introductory statistics has the same shape:

estimate ± critical value x standard error

The standard error is how much the estimate would wobble from sample to sample. For a mean it is s / √n; for a proportion it is √(p(1 - p) / n). The critical value sets how many standard errors wide the interval must be for the confidence level you want: 1.96 for 95% with the normal distribution, more with the t distribution. Their product is the margin of error.

Diagram of a confidence interval as an estimate of 50 with a margin of error of plus or minus 3.30 on each side, built from a t critical value of 2.064 times a standard error of 1.6, with the resulting interval from 46.70 to 53.30
Diagram of a confidence interval as an estimate of 50 with a margin of error of plus or minus 3.30 on each side, built from a t critical value of 2.064 times a standard error of 1.6, with the resulting interval from 46.70 to 53.30

Turn the formula around and you get sample size. If you decide in advance that the margin must be at most E, then E = z x √(p(1 - p) / n), and solving for n gives n = z² p(1 - p) / E². Everything in this guide is a variation on those two lines.

Intervals for a mean: t or z?

Suppose 25 measurements have mean 50 and standard deviation 8. The standard error is 8 / √25 = 1.6.

If you somehow knew the population standard deviation was 8, you would use the normal critical value 1.96 and get 50 ± 3.14. But you almost never know σ; you estimated it from the same 25 values, and that estimate has its own error. William Gosset, writing as "Student" in 1908 while working on small samples at the Guinness brewery, worked out the distribution that accounts for this. The t distribution looks like the normal but with heavier tails, and how heavy depends on the degrees of freedom, n - 1.

With 24 degrees of freedom the 95% critical value is 2.064, so the interval is 50 ± 3.30, from 46.70 to 53.30. The difference from z grows quickly as samples shrink:

Sample sizeDegrees of freedom95% t critical value
2112.706
542.776
11102.228
31302.042
very largevery large1.960

The practical rule is simple: if the standard deviation came from your sample, use t. It is never wrong, and for large samples it gives the same answer as z anyway. Reserve z for the rare case where σ is genuinely known, for example from a long-running, stable manufacturing process.

The t interval also assumes the sample mean is close to normally distributed. With 30 or more values the central limit theorem usually takes care of that. With 8 values, glance at the data first: one wild value or a strongly skewed shape makes the interval less trustworthy.

Intervals for a proportion: why Wilson beats Wald

The interval most textbooks teach for a proportion is the Wald interval, p̂ ± z√(p̂(1 - p̂) / n). It is the general formula with the sample proportion plugged into the standard error. It is easy, and it is unreliable.

Two problems stand out. First, at the edges it breaks completely. With 0 successes out of 30, p̂ = 0, the standard error is 0 and the Wald interval is "0% to 0%", a claim of perfect certainty from 30 observations. Second, even away from the edges its actual coverage is often well below the stated 95%. Lawrence Brown, Tony Cai and Anirban DasGupta showed in a 2001 paper that the coverage swings erratically as n and p change, and can fall well short of the nominal level even with samples in the hundreds when p is near 0 or 1.

The Wilson score interval, published by Edwin Bidwell Wilson in 1927, fixes both. Instead of plugging p̂ into the standard error, it asks which values of the true proportion would not be rejected by a test given the data, and solves for them. The result:

  • centre = (p̂ + z²/2n) / (1 + z²/n)
  • half-width = z √(p̂(1 - p̂)/n + z²/4n²) / (1 + z²/n)

The centre is pulled slightly toward 50%, the interval is asymmetric near the edges (as it should be, since a proportion cannot go below 0), and its coverage stays close to 95%.

Comparison of Wald and Wilson 95 percent intervals for 2 successes out of 40: Wald runs from 0 to 11.75 percent and is cut off at zero, while Wilson runs from 1.38 to 16.50 percent and is skewed upward
Comparison of Wald and Wilson 95 percent intervals for 2 successes out of 40: Wald runs from 0 to 11.75 percent and is cut off at zero, while Wilson runs from 1.38 to 16.50 percent and is skewed upward

For 2 successes out of 40 (5%), Wald gives -1.75% to 11.75%, truncated to 0% to 11.75%. Wilson gives 1.38% to 16.50%. For 0 out of 30, Wilson gives 0% to 11.35% rather than a zero-width interval. For 81 out of 263, a well-behaved case, the two nearly agree: Wald 25.22% to 36.38%, Wilson 25.53% to 36.62%. Those last figures match Robert Newcombe's 1998 comparison of interval methods, which is one of the checks in the calculator's tests.

The calculator shows both and flags the Wald interval whenever there are fewer than 10 successes or 10 failures. Report Wilson.

What "95% confident" means

A 95% confidence interval comes from a procedure that, if you repeated the whole study many times, would capture the true value in 95% of the repetitions. Any single interval either contains the truth or does not; the 95% describes the method's track record.

Two consequences are worth remembering. First, a confidence interval only accounts for random sampling error. If your survey reached only people who answer phone calls from unknown numbers, the interval says nothing about that bias. Second, the width shrinks with the square root of n, so precision gets expensive: to halve the margin of error you need four times the data.

How many responses do you need?

The survey version of the formula is Cochran's:

n₀ = z² p(1 - p) / E²

At 95% confidence (z = 1.959964), a margin of ±5% (E = 0.05) and p = 0.5:

n₀ = 3.8415 x 0.25 / 0.0025 = 384.15, which rounds up to 385.

That is where the ubiquitous 385 comes from. The p = 0.5 is deliberate: p(1 - p) is largest at 50%, so using it gives the biggest, safest sample. If earlier research says the answer is about 20%, p(1 - p) = 0.16 and n falls to 246, but if the true value turns out to be nearer 50% you will miss your margin.

Some standard results for a large population:

Confidence±10%±5%±3%±1%
90%682717526,764
95%973851,0689,604
99%1666641,84416,588

Small populations

Cochran's formula assumes sampling from an effectively infinite population. When the population is small, each response removes a meaningful share of the uncertainty, and the finite population correction applies:

n = n₀ / (1 + (n₀ - 1) / N)

For a company of 500 employees, 385 becomes 218. For 1,000 it becomes 278, and for 100 it becomes 80. For a city of a million it is 384, essentially unchanged. This is why a national poll of about 1,000 people has the same ±3.1% margin whether the country has 5 million or 300 million people: once the population is much bigger than the sample, its size stops mattering.

Chart of required sample size against population size at 95 percent confidence and a 5 percent margin: 80 for a population of 100, 218 for 500, 278 for 1,000, 370 for 10,000 and levelling off near 385 for large populations
Chart of required sample size against population size at 95 percent confidence and a 5 percent margin: 80 for a population of 100, 218 for 500, 278 for 1,000, 370 for 10,000 and levelling off near 385 for large populations

Means instead of percentages

To estimate an average within ±E you need a guess at the standard deviation: n = (zσ / E)². If a pilot study suggests σ = 15 and you want the mean within ±2 at 95%, n = (1.96 x 15 / 2)² = 216.08, so 217. The answer is only as good as the σ guess, so round up generously.

From responses to invitations

The formula gives completed responses. If you expect 25% of invitations to come back, divide by 0.25: 385 responses means inviting 1,540 people. Low response rates also raise the risk that responders differ from non-responders, which no sample size can fix.

Judgment calls the formulas leave to you

  • Subgroups need their own sample. If you want ±5% for each of four regions, you need about 385 per region.
  • Comparing groups is a different calculation. Detecting a difference between two conversion rates needs a power analysis that also depends on the smallest difference worth detecting. The formulas here size a single estimate.
  • Complex designs need a design effect. Cluster sampling (whole schools, whole households) gives less information per person, so multiply the sample by a design effect, often between 1.5 and 2.
  • Pick the margin by the decision. If the result will only change what you do when it moves by 10 points, a ±3% survey is wasted effort.

Get those right and the arithmetic is the easy part, which is exactly what the calculators are for. For the descriptive statistics that feed these formulas, including why the sample standard deviation divides by n - 1, see the descriptive statistics guide.

Open the Confidence Interval Calculator