Latticework

Command Palette

Search for a command to run...

Statistics

Confidence Intervals

16 min

Explanation

A single sample mean is just an estimate — a confidence interval gives a range that's likely to contain the true population value, instead of one specific number pretending to be exact.

import math

def margin_of_error(std, n, z=1.96):
    return z * (std / math.sqrt(n))

z=1.96 corresponds to a 95% confidence level for a normal distribution — it's the number of standard errors on each side of the mean that captures the middle 95% of the distribution.

Try it

A tighter interval (smaller n gives a wider one) comes from either a bigger sample or a lower confidence level — there's no way to get 'more confident AND more precise' for free from the same data.

Loading editor…
Explanation

The correct interpretation of "95% confidence" is about the procedure, not this one specific interval: if you repeated the whole sampling process many times and built a new interval each time, about 95% of those intervals would contain the true population value. Once you have one actual interval in front of you, the true value either is or isn't in it — there's no probability left to talk about for that specific interval.

A wider interval (bigger z, like 2.58 for 99% confidence) trades precision for more confidence that you've captured the true value — you can always be "more confident" by being willing to say less precisely where the value is.

Exercise

Write `margin_of_error(std, n, z=1.96)`, returning the margin of error: z × (std / √n).

Exercise

Write `confidence_interval(sample_mean, std, n, z=1.96)`, returning a `(lower, upper)` tuple: the sample mean plus and minus the margin of error.

Quiz

What does a 95% confidence interval actually mean?

Checkpoint

You can compute a margin of error and build a confidence interval around a sample mean, and can state the correct (frequentist) interpretation of what "95% confidence" means.