Day 9. Confidence Intervals & the Bootstrap
Turning a standard error into an interval with a known coverage rate, analytically or by resampling.
- ●Build normal and t confidence intervals for means, proportions and differences in means.
- ●Explain what "95% coverage" does and does not mean.
- ●Describe the nonparametric bootstrap and compute a percentile interval.
- ●Read standard errors and significance off a published interval.
Theory
The pivot
If , then , whose distribution does not depend on (a pivot). So
Common critical values: (90%), (95%), (99%).
With small samples from a normal population and an estimated SD, replace by the Student quantile with degrees of freedom.
Interpretation
The interval is random; is fixed. "95% confidence" means that the procedure captures in 95% of repeated samples. It does not mean there is a 95% probability that lies in the interval you computed (that is a Bayesian credible interval, Day 13). The CI coverage simulator in the Lab makes this concrete.
Standard errors you will use constantly
| Estimator | Standard error |
|---|---|
| Mean | |
| Proportion | |
| Difference in means (independent groups) |
The nonparametric bootstrap
When the SE formula is unknown or messy (medians, ratios, complex estimators):
- Draw observations with replacement from your sample.
- Compute the estimator on this resample, .
- Repeat times (say ).
- Use the standard deviation of the as the SE, or their 2.5th and 97.5th percentiles as a percentile interval.
The bootstrap treats the sample as a stand-in for the population. Resample in the way the data were generated: with clustered data, resample whole clusters.
Part 1 · Warm-up
Exercise 1 · A 95% interval for a mean
, , . Build a 95% CI.
Show solution
, so .
Exercise 2 · Raising the confidence level
Build the 99% CI for the same data. Why is it wider?
Show solution
. More confidence requires a wider net.
Exercise 3 · A proportion
In a survey of 600, 42% support a policy. Build a 95% CI.
Show solution
Step 1. .
Step 2. Margin of error: .
Step 3. Interval: .
Answer: : support is between about 38% and 46%.
Exercise 4 · A treatment effect
Treated: mean 78, sd 12, . Control: mean 74, sd 15, . Build a 95% CI for the difference.
Show solution
Step 1. Point estimate: .
Step 2. Standard error with independent groups:
Step 3. Margin: .
Answer: ; zero is excluded, so the effect is significant at 5%.
Exercise 5 · A small sample
, , . Build a 95% t interval ().
Show solution
, so , slightly wider than the normal interval .
Part 2 · Proofs
Exercise 6 · Coverage of the known-σ interval
If iid with known , prove that contains with probability exactly 0.95.
Show solution
Step 1. exactly.
Step 2. The event is the same event as , whose probability is .
Exercise 7 · Width and sample size
Show that the width of a CI is proportional to , and find the sample size needed to halve it.
Show solution
Width . Replacing by multiplies it by ; halving needs .
Exercise 8 · Left out of the bootstrap
Show that the probability a given observation does not appear in a bootstrap resample is , and find its limit.
Show solution
Step 1. Each of the independent draws misses the observation with probability , so it is never drawn with probability .
Step 2. Since , the limit is .
Step 3. Each resample contains about 63.2% of the distinct observations.
Exercise 9 · Duality of tests and intervals
Show that a two-sided 5% test of rejects exactly when lies outside the 95% CI.
Show solution
Step 1. The test rejects when , i.e. when or , which is exactly "outside the CI".
Step 2. A CI is the set of null values you would not reject.
Exercise 10 · Reverse-engineering a paper
A paper reports an effect with 95% CI . Recover the estimate, its SE and its z-statistic.
Show solution
Step 1. A 95% CI is symmetric around the estimate: .
Step 2. Its half-width is : .
Step 3. , so .
Answer: estimate 3.4, SE 1.12, , .
Part 3 · Applied
Exercise 11 · Clustered data
Using Day 7's design effect of 1.95, by how much should a naive CI be widened?
Show solution
Step 1. Clustering multiplies the variance by the design effect, 1.95, so the SE must be multiplied by .
Step 2. The naive interval uses naive SEs, which is only correct SEs.
Step 3. Its true coverage is .
Answer: widen it by 40%; otherwise the "95%" interval covers only about 84% of the time.
Exercise 12 · A percentile interval for median rent
A housing agency bootstraps the median monthly rent in a city from a sample of 300 listings, computes 1,000 bootstrap medians and sorts them. Which sorted values form the 95% percentile interval, and how would you report it?
Show solution
Step 1. A 95% interval leaves 2.5% in each tail.
Step 2. 2.5% of 1,000 is 25 and 97.5% is 975, so take roughly the 25th and 975th sorted bootstrap medians (numpy.percentile interpolates between neighbours).
Answer: report "median rent $X (95% bootstrap CI: [25th value, 975th value])". The interval need not be symmetric around $X, which is useful for skewed rents.
Exercise 13 · Bootstrapping a median
Health costs are heavily right-skewed. Why bootstrap the median rather than use a formula, and what are the steps?
Show solution
Step 1. Why not a formula: the median's analytic SE depends on the density of costs at the median, which is hard to estimate with skewed data.
Step 2. Draw 300 patients with replacement from the sample and compute the median cost.
Step 3. Repeat 2,000 times.
Step 4. Report the SD of the 2,000 medians as the SE, or their 2.5th and 97.5th percentiles as a 95% interval.
Answer: the bootstrap needs no formula, only the ability to recompute the median.
Exercise 14 · Overlapping intervals for two regions
A labour-force survey estimates mean hourly wages of $10 (SE 1) in region A and $13 (SE 1) in region B, from independent samples. Their 95% CIs overlap. Is the difference significant?
Show solution
Step 1. The CIs: A ; B . They overlap on .
Step 2. Test the difference directly: with .
Step 3. , .
Answer: significant at 5% despite the overlap. Overlapping intervals are not a test; always test the difference.
Exercise 15 · What the bootstrap SE of mean income estimates
You bootstrap the mean income of surveyed households. Show that, if you could average over all possible resamples, the bootstrap variance of the mean equals , where .
Show solution
Step 1. Each draw in a resample picks one of the households with equal probability .
Step 2. So one draw has mean and variance .
Step 3. A resample mean averages independent such draws, so its variance is .
Answer: the bootstrap variance is , essentially the textbook (smaller by the factor ).
Verify in Python
import numpy as np
from scipy import stats
rng = np.random.default_rng(9)
# Coverage of the 95% t interval with skewed data (log-normal, true mean known)
mu_true = np.exp(0.5)
reps, n = 20_000, 30
x = rng.lognormal(0, 1, (reps, n))
m, s = x.mean(1), x.std(1, ddof=1)
t = stats.t.ppf(0.975, n - 1)
cover = np.mean((m - t * s / np.sqrt(n) <= mu_true) & (mu_true <= m + t * s / np.sqrt(n)))
print(f"coverage with skewed data, n=30: {cover:.3f} (nominal 0.95)")
# With strongly skewed data and n = 30 the CLT has not fully kicked in: coverage falls short.
# Bootstrap the median of one sample
sample = rng.lognormal(8, 1, 200) # health costs
boots = np.array([np.median(rng.choice(sample, sample.size)) for _ in range(2000)])
print("median:", round(np.median(sample)), " bootstrap SE:", round(boots.std(ddof=1)),
" 95% percentile CI:", np.percentile(boots, [2.5, 97.5]).round())
print("P(left out):", round((1 - 1/200)**200, 4), " vs e^-1 =", round(np.exp(-1), 4))
Key takeaways
- covers in about 95% of repeated samples.
- The bootstrap estimates a sampling distribution by resampling your data the way they were collected.
- Compare estimates by testing their difference, not by checking whether intervals overlap.
Simulation Lab: Confidence-Interval Coverage
Open full screen →One hundred intervals from one hundred samples: count how many catch the true mean.
Try n = 5 with the z value: coverage falls short. Then try skewed data with small n.
Each horizontal line is a confidence interval from a new sample. The true mean (dashed) never moves; the intervals do. Orange intervals miss it.