Null and alternative hypotheses
A coin is tossed 100 times and shows 60 heads. Is it fair? A hypothesis test answers such a question by asking how surprising the data would be if a stated claim were true.
- The null hypothesis is the claim under test, usually "nothing special": here , with the chance of heads.
- The alternative hypothesis is what the test looks for instead: here . It is two-sided, because a bias either way counts.
- The test statistic is a number computed from the data whose distribution is known when is true. Here it is the number of heads , which is under (lesson 5.2.2).
Under , has mean 50 and standard deviation (lesson 5.3.2). Counts far from 50, in either direction, speak against .
The p-value
Rule: p-value
Small : data like these would be rare if were true, so they are evidence against it. The p-value is a probability computed assuming ; it is not the probability that is true.
Sixty heads is 10 away from 50, so "at least as extreme" means . Each tail has probability 0.0284, so .
If the coin is fair, the highlighted counts are at least as far from 50 as the 60 heads seen. Their total probability, 0.0569, is the p-value.
To decide, fix a significance level before looking at the data, often 0.05 or 0.01.
Rule: Significance level
The result is then statistically significant at level . Otherwise the data do not reject , which is not the same as showing it is true.
- 60 heads: , above 0.05, so the data do not reject fairness at the 5% level.
- 61 heads: , so they do. One toss changes the verdict, which is why a report should give the p-value, not only "reject" or "keep".
- Not the chance that is true. The p-value is . The reverse, , needs Bayes' rule and a prior (lesson 5.1.4).
A normal approximation, , gives instead of 0.0569. Near the cut-off it flips the verdict, so use the exact distribution when you have it.
Try it
binom.sf(59, 100, 0.5) is and binom.cdf(40, 100, 0.5) is . The loop prints one tail and the two-sided p-value for 60 and 61 heads, then SciPy's binomtest as a check.
The last lines give the normal approximation, then 100,000 seeded runs of 100 fair tosses. In 5.59% of them the count lands 10 or more from 50.
Two kinds of error
A test can go wrong in two ways.
| true | false | |
|---|---|---|
| reject | error of the first kind (Type I): a false alarm | correct |
| keep | correct | error of the second kind (Type II): a miss |
Rule: Error rates and power
is chosen; (beta) depends on how far the truth is from , on and on . The power is the chance of rejecting when it is false.
Take draws from and test against with . The statistic is standard normal under , so rejecting when gives .
If in fact , then . It lands beyond with probability 0.7054, the power, so .
The same rule judged under two truths. If , rejecting is a false alarm, with probability . If , keeping is a miss, with probability .
Power grows with the sample and with the size of the effect:
| power when | 0.3526 | 0.7054 | 0.9424 | 0.9988 |
At the power is 0.1701 when and 0.9988 when . Cutting to 0.01 (cut-off 2.576) lowers it from 0.7054 to 0.4697: fewer false alarms cost more misses.
How many draws? To detect a difference (delta) with 80% power at , the test needs about draws, with . For and that is 31.4, so 32 draws. Case study 5.8.5 asks the same question about years of returns.
Try it
power(mu, n) adds the two tails of beyond the cut-offs. The lines print the table, the effect sizes, the stricter and the sample size.
The loop simulates 100,000 seeded samples of 25 under each truth: the test rejects 5.05% of the time when and 70.34% when .
The t test for a mean
To test a claimed mean with unknown, standardise with , as lesson 5.8.2 did for the t interval.
Rule: One-sample t test
against . Under the statistic has the t distribution with degrees of freedom, exactly for normal draws and approximately for large samples otherwise. SciPy: ttest_1samp(x, mu0).
Five measurements. Lesson 5.8.2's values 10.2, 9.6, 10.9, 10.4 and 9.9 have and standard error 0.2214.
- Claimed value 10: and . No evidence against 10.
- Claimed value 9.5: and , so 9.5 is rejected at the 5% level.
Lesson 5.8.2's 95% interval, 9.5854 to 10.8146, contains 10 and leaves out 9.5. That is no coincidence.
Rule: Tests and intervals
For the two-sided t test and the t interval from the same data. The interval is the set of claimed values the test does not reject.
Try it
The loop computes and the p-value by the rule for both claimed values, then checks them with ttest_1samp.
The last line is the one-sided test of the next section.
A loaded die. Make a six half again as likely as each other face. Its probability is then 0.2308, and the die's mean is 3.6923, not 3.5. So is false.
Sixty seeded rolls give , and : no evidence at all. That is an error of the second kind.
That is the usual outcome, not bad luck. With 60 rolls the t test detects this die only 13.6% of the time, in a seeded simulation. For 80% power it needs about 666 rolls, by the sample-size formula with .
A test with little power says little when it keeps .
Try it
The first lines build the loaded die's probabilities, mean and standard deviation. Then one sample of 60 rolls and its t test.
The last lines are the power, as the share of 20,000 seeded samples of 60 with a p-value below 0.05. Then comes the formula's sample size for 80%.
One-sided tests
Sometimes only one direction matters: a new process should raise the mean, not lower it. Then , and only large values of count against .
Rule: One-sided p-value
For use . In SciPy, alternative="greater" or "less".
For the claimed value 9.5, the one-sided p-value is 0.0171, half of the two-sided 0.0341, because is positive. Had been negative, the one-sided p-value would be above 0.5.
Choose the side before seeing the data. Picking it afterwards, to match the sign of , halves every p-value and doubles the false alarms.
Many tests at once
Each test at raises a false alarm 5% of the time when its is true. Run many and some will.
With 20 independent tests of true nulls, the chance of at least one rejection is , and one false rejection is expected. In a seeded simulation of 10,000 batches of 20 t tests, 63.46% of batches had at least one. The smallest of the 20 p-values averaged 0.0478.
Rule: Bonferroni correction
Then the chance of any false alarm among the is at most . For 20 tests, each is run at 0.0025, and the chance of any false rejection is 0.0488. The price is power: each test now needs stronger evidence.
Significant is not the same as large. Take 10,000 draws with standard deviation 1. A sample mean of 0.02 gives and : significant, but tiny.
Report the estimate and its interval alongside the p-value. Case study 5.8.5 meets both problems with trading strategies.
Try it
The first line is the exact chance of a false alarm among 20 tests, the Bonferroni level, and its chance of any false alarm.
Then 10,000 seeded batches of 20 t tests, each on 30 standard normal draws, so every is true. The last line is the large-sample example.
Common mistakes
- Reading as the chance that is true. It is the chance of data this extreme if is true.
- Treating "not rejected" as "proved". The loaded die kept with ; the test had 13.6% power.
- Choosing or the side after seeing the data. Both make false alarms more common than says.
- Equating significant with important. A tiny effect is significant with enough data; give the estimate and its interval.
- Ignoring how many tests were run. Twenty tests of true nulls give at least one about 64% of the time.
- Trusting an approximation at the cut-off. For 60 heads the normal approximation gives 0.0455 and the exact test 0.0569.
Hypothesis tests
0 of 12 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Summary card
- A test states (the claim) and (two-sided or one-sided), and uses a statistic whose distribution under is known.
- The p-value is ; reject at level when . It is not the probability that is true.
- Type I error (false alarm) has probability ; Type II (a miss) has probability ; power grows with and the effect. For 80% power, .
- t test: against . It rejects exactly when is outside the matching interval.
- Many tests produce false alarms (); Bonferroni tests each at . Significant is not the same as large.