Hypothesis tests and p-values
Last updated
In lesson 7.P, rainy days at the Batangas store sold about 12% fewer kilos. But there were only 18 rainy days. Could a difference that big turn up by luck, even if rain changed nothing? A hypothesis test answers exactly that question. In this lesson you'll run one by shuffling, then with Welch's t-test, say what the p-value means in plain words, measure how big the effect is, and learn the misreading of p-values that the American Statistical Association wrote a whole statement about.
By the end of this lesson you can
- Write a question as a null and an alternative hypothesis
- Run a permutation test by shuffling, and with SciPy
- Run Welch's t-test and report the difference with an interval
- Say what a p-value means, and measure effect size with Cohen's d
A question as two hypotheses
Run the cell: the Batangas store's daily kilos, from the clean Tindahan data, split by whether it rained that day.
42 dry days averaged 63.0 kg; 18 rainy days averaged 55.5 kg. A difference of 7.5 kg. Daily sales bounce around anyway, though: on dry days alone they ran from 48 to 92 kg. So the question is whether 7.5 kg is more than that bouncing around could produce.
A hypothesis test puts the question as two statements:
- The null hypothesis, : rain makes no difference. The true mean kilos are the same on rainy and dry days, , and the 7.5 kg is luck.
- The alternative hypothesis, : rain makes a difference, .
The test doesn't try to prove . It asks: if were true, how surprising would a difference of 7.5 kg be? If it would be very surprising, that's evidence against .
A test by shuffling
If rain made no difference, the labels "rainy" and "dry" would be meaningless: any 18 of the 60 days would do as the "rainy" ones. So deal the same 60 numbers into new piles of 42 and 18, at random, and see what differences that makes. Try it:
The real labels: dry days sold 7.5 kg more on average. Pink dots are the rainy days. Shuffle to deal the same 60 numbers into two new piles of 42 and 18, as if rain made no difference.
Most shuffles give differences between about −5 and +5 kg; the real 7.5 kg sits out in the tail. That's a permutation test (a shuffle test): the pile of shuffled differences shows what luck alone does, and the real difference is compared with it. Now in Python:
rng.permutation(both)shuffles the 60 values; the first 42 become "dry". 10,000 shuffles gave a difference at least as big as 7.5 kg, either way, 117 times.- So the p-value is .
stats.permutation_testdoes the same in one call; with its own random shuffles it gets 0.008.
The p-value is the probability of getting a difference at least as big as the one you saw, if the null hypothesis were true. Here, about 1%: if rain made no difference, a gap of 7.5 kg would turn up about once in a hundred weeks like this one.
By convention, is called statistically significant. That's a habit, not a law, and it says nothing about whether the difference matters. Step 4 is about that.
The t-test
Before computers, nobody could shuffle 10,000 times, so the CLT from 8.4 was used instead. The difference between two sample means is close to normal, with a standard error built from both samples. Dividing the difference by that standard error gives Welch's t statistic:
It says how many standard errors apart the two means are. Under , follows a t distribution, so the p-value is the chance of a at least this far from 0. Run the cells.
stats.ttest_ind(dry, wet, equal_var=False)gives and , close to the shuffle test's 0.012. The formula by hand gives the same .equal_var=Falsemakes it Welch's t-test, which doesn't assume the two groups have the same spread. Use it as the default: the older "Student" version (equal_var=True) can give wrong p-values when the spreads and group sizes differ.res.confidence_interval(0.95)gives a 95% interval for the difference: 1.9 to 13.1 kg. It doesn't include 0, which matches .
The shuffle test and the t-test usually agree. The shuffle test needs fewer assumptions; the t-test is quicker, and gives you an interval too.
How big is it?
A p-value says how surprising a difference would be by luck. It doesn't say how big the difference is: with enough data, a difference of a few grams would have a tiny p-value. So always report the size of the effect too.
- In the data's own units: rainy days sold 7.5 kg less, about 11.9% less, with a 95% interval of 1.9 to 13.1 kg. For the owners, that's the useful number: it's about how much less stock to order for a rainy day.
- In standard deviations: Cohen's d divides the difference by the pooled standard deviation of the two groups:
Here (second cell in step 3). Jacob Cohen called about 0.2 small, 0.5 medium and 0.8 large, as rough guides. is useful for comparing effects measured in different units, such as kilos and pesos.
Report all three: "Rainy days sold 7.5 kg less (95% CI 1.9 to 13.1 kg; Welch's , )."
Wrong way: “96% real”
A report says: "We tested the new shelf layout: p = 0.04. So there's a 96% chance the layout really works." It's the most common misreading of a p-value, and it's wrong. The p-value is calculated assuming the layout does nothing; it can't also be the chance that the layout does nothing.
The American Statistical Association was worried enough about this to publish a statement on p-values in 2016, the first time it had taken a position on a specific matter of statistical practice. One of its six principles: p-values do not measure the probability that the hypothesis studied is true, or the probability that the data were produced by chance alone.
What the chance really is depends on how many of the ideas you test are any good. Step through it:
Then run the same experiment in Python:
83 experiments came out with : 46 of the 100 that really work, and 37 of the 900 that do nothing. Only 55% of the "significant" results are real. If most ideas tested are duds, as is common, many significant results are flukes.
The check: read your sentence back. If it says "a chance it's real", or "a chance it's luck", rewrite it: "if there were no real effect, a difference this big would turn up in about of experiments like this one." Then give the effect size and its interval.
Check what you learned
Sources
Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.
- Wasserstein and Lazar, The ASA Statement on p-Values: Context, Process, and Purpose, The American Statistician 70(2), 2016: six principles for reading p-values, from the American Statistical Association
- SciPy: scipy.stats.permutation_test: permutation tests for any statistic
- SciPy: scipy.stats.ttest_ind: the two-sample t-test, with equal_var=False for Welch’s version
- Diez, Çetinkaya-Rundel and Barr, OpenIntro Statistics, 4th ed., chapters 5 and 7: hypothesis tests, p-values and the t-test for two means
- Greenland et al., Statistical tests, P values, confidence intervals, and power: a guide to misinterpretations, European Journal of Epidemiology 31, 2016: the common misreadings of p-values, one by one