Confidence interval builder
Build a confidence interval for a mean: a z interval when σ is known, a t interval when it is estimated by the sample's s. Then run the coverage simulation: it draws 100 samples from a coffee machine whose true mean it knows, builds an interval from each, and counts how many contain the mean.
Goes with chapter 11. Student's t and confidence intervals
Build an interval
Method
Confidence level
95 % t interval: from 111.1906 to 115.1694
113.18 ± 2.0096 × 7/√50 = 113.18 ± 1.9894
| Critical value t*, 49 degrees of freedom | 2.0096 |
|---|---|
| Standard error | 0.9899 |
| Margin of error | 1.9894 |
| Width | 3.9788 |
- Python (scipy.stats)
t.interval(0.95, df=49, loc=113.18, scale=7/np.sqrt(50))- Excel
=113.18-CONFIDENCE.T(0.05, 7, 50)
Coverage simulation
The simulation's coffee machine has a true mean of μ = 112 mg and σ = 7 mg. It draws 100 samples of n = 50 cups and builds a 95 % interval from each, using each sample's own s. An analyst never sees μ; the simulation does, and marks the intervals that miss it.
- Contains μ
- Misses μ: dashed, with a cross
- The true mean, μ = 112
95 of the 100 intervals contain μ = 112, and 5 miss it. The intervals that miss it are numbers 32, 37, 48, 51, 86. Over 10,000 samples, intervals built this way contain μ 95.1 % of the time.
What to look for
- About 95 of the 100 intervals contain μ at 95 % confidence, but not exactly 95: the count itself varies from run to run. Press New run a few times and watch it move between about 90 and 99.
- The 95 % is a property of the procedure, not of any one interval. Each interval either contains μ or misses it; there is no 95 % chance about a single one once it is drawn.
- With the t method each interval has its own width, because each sample has its own s. With z and a known σ every interval is equally wide and only its centre moves.
- Set n = 5 and compare: t covers about 95 %, while z with the sample's s would cover only about 88 %. That is why a small sample needs t.
- Raise the confidence to 99 %: fewer intervals miss, and every interval is wider. Raise n instead: the intervals narrow and still about 95 % contain μ.