Chapter 10: Confidence intervals and hypothesis tests
Previous: PS 09 Estimation and Maximum Likelihood | PS 00 Course Map | Next: PS 11 Bayesian Inference
A single estimate (“the success rate is 0.8”) is incomplete without a statement of how uncertain it is. A confidence interval gives a range of parameter values that are compatible with the data. A hypothesis test asks whether an observed effect could plausibly be produced by chance alone. Both rest on the standard error from the central limit theorem, and both are frequently misread, so this chapter spends as much time on what they do not mean as on how to compute them.
- Build and interpret confidence intervals for a mean and for a proportion.
- Use the distribution for small samples, and better intervals for proportions.
- Build intervals by the bootstrap when no formula exists.
- Run a hypothesis test, compute a p-value, and interpret it correctly.
- Understand Type I and II errors, power and sample size.
- Compare two groups (independent or paired) and avoid the multiple-comparisons trap.
You need the CLT and standard errors (PS 08 Law of Large Numbers and Central Limit Theorem) and z-scores with the table of (PS 05 Continuous Random Variables). Key values: (90%), (95%), (99%).
10.1 Confidence intervals for a mean
The construction
By the CLT, the sample mean is approximately . So 95% of the time,
Read the same inequality the other way round: 95% of the time, is within standard errors of . Replacing the unknown by the sample standard deviation :
Every confidence interval in this chapter has the same shape:
| Confidence level | 90% | 95% | 99% |
|---|---|---|---|
| critical value | 1.645 | 1.96 | 2.576 |
More confidence costs a wider interval. More data buys a narrower one (by ).
Worked example 10.1
100 cycle-time measurements give s and s.
- Standard error: s.
- Margin: s.
- 95% interval: , that is s.
10.2 What “95% confidence” means
It does not mean “there is a 95% probability that is in ”.
In this (frequentist) framework, is a fixed number. It is either in that particular interval or it is not. There is nothing random left about it.
What is random is the interval: a different sample would give a different one.
The correct meaning. The procedure produces intervals that contain the true value in 95% of repeated experiments. Picture 100 research teams each collecting their own data and computing their own interval: about 95 of the intervals contain the truth, and about 5 miss it. You do not know which kind yours is.
Practical reading. Values inside the interval are compatible with the data. Values far outside are not. The width tells you how precisely you have measured.
(If you want a statement like “95% probability that is here”, you need a prior and a credible interval, PS 11 Bayesian Inference.)
10.3 Small samples: the distribution
With small , replacing by adds extra uncertainty: itself might be too small or too large. To account for it, use the distribution with degrees of freedom, which looks like a normal but with heavier tails.
| 3 | 5 | 10 | 20 | 30 | large | |
|---|---|---|---|---|---|---|
| 95% | 4.303 | 2.776 | 2.262 | 2.093 | 2.045 | 1.96 |
With the interval is about 40% wider than the normal version would claim. With 3 data points it is more than twice as wide. Small samples carry much less information than intuition suggests.
This interval assumes the data are roughly normal. With small, skewed samples, be cautious.
Worked example 10.2. Five measurements of a gripper force: N, N. Standard error . Interval: , so N.
10.4 Intervals for a proportion
The simple (Wald) interval
With successes in trials, , and the CLT gives
Worked example 10.3. 40 successes in 50 trials: , SE , interval .
When it fails
With 10 successes in 10 trials, , the standard error is computed as , and the interval is : it claims certainty that the policy never fails, from only 10 tries.
Better: the Wilson interval
The Wilson interval fixes this and should be the default for success rates:
For 10 out of 10 it gives roughly : a perfect score on 10 trials is consistent with a true rate as low as about 72%.
The rule of three
If you observe zero failures in independent trials, an approximate 95% upper bound on the failure rate is
0 failures in 30 trials: the failure rate could still be as high as about 10%. 0 in 300: about 1%.
Why: the chance of 0 failures in trials at failure rate is . Setting it to 0.05 gives .
10.5 The bootstrap: intervals without formulas
For statistics with no convenient standard-error formula (a median, a ratio, a model’s accuracy, the difference in success rates on a benchmark), use the bootstrap:
- From your data points, draw points with replacement. This is a “bootstrap sample”: some points appear twice, some not at all.
- Compute the statistic on it.
- Repeat steps 1 and 2 a few thousand times.
- Take the 2.5th and 97.5th percentiles of the results as a 95% interval.
Why it works. You cannot repeat the real experiment, so you use your sample as a stand-in for the population and repeat the sampling process on it. The spread of the resampled statistics imitates the spread you would see across real repeated experiments.
If each robot episode contains many frames, resample whole episodes, not frames. Resampling correlated pieces separately makes the interval far too narrow (the same mistake as in PS 08 Law of Large Numbers and Central Limit Theorem).
10.6 Hypothesis tests
The question
You observe an effect: a coin gives 60 heads in 100 flips; a new policy scores higher than the old one. Could chance alone produce an effect this large?
The setup
- Null hypothesis : the “nothing interesting” explanation. The coin is fair; the policies are equally good.
- Alternative hypothesis : what you suspect instead. The coin is biased; the new policy is better.
The logic
It is proof by contradiction, made probabilistic:
- Assume is true.
- Compute how surprising the observed data would be under that assumption.
- If the data would be very surprising, doubt .
The p-value
The p-value is the probability, assuming is true, of getting data at least as extreme as what was actually observed.
A small p-value says: “if were true, results like these would be rare.”
The decision
Choose a significance level before looking at the data (commonly ). If , reject . Otherwise, do not reject it (which is not the same as accepting it).
Worked example 10.4: is the coin fair?
60 heads in 100 flips.
Step 1: . The coin is fair, .
Step 2: distribution under . Number of heads , so mean 50, sd 5.
Step 3: how extreme? .
Step 4: p-value. “At least as extreme” in either direction (a two-sided test, since a bias could go either way): .
Step 5: decide. : reject fairness at the 5% level, but only just.
With 55 heads instead: , . Entirely ordinary for a fair coin.
10.7 What a p-value is not
- It is not the probability that is true. It is , not . Confusing them is the same error as the prosecutor’s fallacy (PS 02 Conditional Probability and Bayes Rule).
- A large p-value does not show is true. It only says the data cannot rule it out. With small samples, real effects routinely produce large p-values. Absence of evidence is not evidence of absence.
- A small p-value does not mean the effect is large or important. With enough data, a trivially small difference becomes “statistically significant”. Always report the size of the effect with its confidence interval.
10.8 Errors and power
Every test can go wrong in two ways:
| actually true | actually false | |
|---|---|---|
| reject | Type I error (false alarm). Rate | correct. Rate power |
| do not reject | correct | Type II error (missed effect). Rate |
Power is the probability of detecting an effect that really exists. It increases with:
- a larger sample,
- a larger true effect,
- less noise,
- a larger (at the cost of more false alarms).
A study with low power usually misses real effects. Worse, the “significant” results it does produce tend to exaggerate the effect, because only lucky, unusually large estimates cross the threshold.
How many trials do you need?
To detect a difference between two success rates near , with about 80% power at , a rough rule is
To distinguish 70% from 80% (, ): trials per policy. Detecting a 10-point improvement is expensive.
10.9 Comparing two groups
Independent groups
For two independent groups with means , the standard error of the difference adds in variance (PS 04 Expectation and Variance):
Test statistic: . Interval: .
For proportions, use for each group.
Worked example 10.5. Policy A: 80/100. Policy B: 70/100.
- Difference .
- SE .
- , two-sided . Not significant at 5%.
- 95% interval for the difference: , so . It includes zero: A might be no better at all, or up to 22 points better.
Paired comparisons
If the same units are measured under both conditions (the same 50 object placements tried with policy A and with policy B), analyse the differences within each pair.
Pairing removes the variation between units (some placements are simply harder), which can be large. The test on differences is usually far more sensitive.
Test both policies on the same set of initial conditions. It costs nothing extra and can cut the required number of trials dramatically.
10.10 Tests and intervals agree
A two-sided test at level rejects exactly when lies outside the 95% confidence interval.
So the interval contains everything the test tells you, plus the size of the effect and the precision of the measurement. Prefer reporting intervals.
10.11 The multiple-comparisons trap
If you test 20 hypotheses that are all actually null, at each, you expect one false positive.
The same happens when you try many metrics, many random seeds, many checkpoints or many hyperparameter settings, and report the best one. Something will look significant by chance.
Remedies:
- Decide the main comparison before looking at the data.
- Correct for the number of tests. The simplest is Bonferroni: with tests, use for each.
- Report everything you tried, not just the winner.
- Confirm the finding on fresh data.
Summary
| Task | Formula |
|---|---|
| CI for a mean | (or for small ) |
| CI for a proportion | Wilson interval (Wald: ) |
| zero failures in | upper bound |
| test statistic | |
| difference of two groups | |
| sample size (two proportions) | per group |
Three facts to remember:
- A 95% interval comes from a procedure that is right 95% of the time; it is not a probability about .
- A p-value is : not the chance is true, and not the size of the effect.
- Pair your comparisons, plan your sample size, and report intervals.
Exercises
Part A: warm-up
A1. , , . Give a 95% confidence interval for the mean.
A2. A policy succeeds 45 times in 60 trials. Give a 95% Wald interval for its success rate.
A3. A new gripper has 0 failures in 50 trials. Give an approximate 95% upper bound on its failure rate.
A4. With , , , give a 95% interval using the distribution.
A1. .
A2. , SE , interval .
A3. , i.e. 6%.
A4. SE . : .
Part B: core
B1. A coin gives 530 heads in 1,000 flips. Test fairness at and give a 95% interval for .
B2. Policy A succeeds 85/100; policy B succeeds 75/100. Test whether they differ, and give a 95% interval for the difference.
B3. The same 20 object placements are tried with two policies. In 6 placements only A succeeds; in 1 only B succeeds; in the other 13 both succeed or both fail. Is this evidence that A is better? (Hint: under , each of the 7 “disagreeing” placements is equally likely to favour A or B. Compute with the binomial.)
B4. You want to detect an improvement from 60% to 70% success with 80% power. Roughly how many trials per policy are needed?
B5. A paper reports for an improvement of 0.2 percentage points on a benchmark with 100,000 test examples. What two questions should you ask?
B1. SE under : . , : not significant at 5%. Interval: , so , which just includes 0.5. Consistent with the test.
B2. Difference . SE . , . Interval . Not significant at 5%.
B3. one-sided; two-sided . Suggestive but not significant. (This is McNemar’s test. Note that only the disagreeing placements carry information.)
B4. , : per policy.
B5. Is a 0.2-point effect of any practical importance? And how many models, seeds or settings were tried before this one was reported (multiple comparisons)?
Part C: explain and prove
C1. Explain to a friend, without formulas, what a 95% confidence interval is.
C2. Explain why a test can be “significant” with a huge sample even when the effect is negligible, using the formula for the standard error.
C3. Why does pairing reduce the standard error of a comparison? Use the formula .
C4. Derive the rule of three from using for small .
C1. “I used a method that, if everyone repeated my experiment with fresh data, would capture the true value about 95 times out of 100. This particular range is what the method gave for my data.”
C2. SE shrinks without limit as grows, so any nonzero difference, however tiny, eventually becomes many standard errors from zero.
C3. Outcomes of the same unit under two conditions are positively correlated (hard placements are hard for both). The covariance term is subtracted, shrinking the variance of the difference.
C4. , and , so .
Part D: practise with code
D1. Simulate 100 experiments, each drawing 30 samples from and computing a 95% interval. Plot the intervals and count how many contain 10.
D2. Implement the Wilson interval and compare it with the Wald interval for successes out of .
D3. Implement a bootstrap interval for the median of a sample, and for the difference in success rates between two policies with per-episode results.
D4. Simulate the multiple-comparisons trap: 20 “metrics” that are pure noise for two identical policies, each tested at . Over 1,000 repetitions, how often is at least one metric “significant”?
D1. About 95 of 100 intervals contain 10, but the exact count varies from run to run.
D4. About of repetitions show at least one false positive.
Where this is used
This chapter is the statistical core of 16 Evaluation and Uncertainty. Before claiming one policy beats another, check: the interval on the difference, whether trials were independent and paired, whether the sample size gives enough power, and how many comparisons were made.
Next: PS 11 Bayesian Inference: keeping a full probability distribution over what you do not know.