Statistics for analysis · lesson 6 of 13
Parts in this lesson
Arng.permutation(both)shuffle the values, to deal them into new groups
Bstats.permutation_test((a, b), statistic)SciPy's shuffling test
Cstats.ttest_ind(a, b, equal_var=False)Welch's t-test for two means

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.

rain_test.ipynb

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, H0: rain makes no difference. The true mean kilos are the same on rainy and dry days, μdry=μrainy, and the 7.5 kg is luck.
  • The alternative hypothesis, H1: rain makes a difference, μdry≠μrainy.

The test doesn't try to prove H1. It asks: if H0 were true, how surprising would a difference of 7.5 kg be? If it would be very surprising, that's evidence against H0.

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:

Shuffle the labelsBatangas, dry days against rainy days
dry daysrainy daysmean 63.0mean 55.540 kg50 kg60 kg70 kg80 kg90 kgreal: 7.5 kg−10−50+5+10shuffleddifferences

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:

rain_test.ipynb
  • 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 117/10,000≈0.012.
  • stats.permutation_test does 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, p<0.05 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:

t=x¯dry−x¯rainysdry2ndry+srainy2nrainy

It says how many standard errors apart the two means are. Under H0, t follows a t distribution, so the p-value is the chance of a t at least this far from 0. Run the cells.

rain_test.ipynb
  • stats.ttest_ind(dry, wet, equal_var=False) gives t=2.7 and p=0.011, close to the shuffle test's 0.012. The formula by hand gives the same t.
  • equal_var=False makes 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 p<0.05.

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:
d=x¯1−x¯2spooled

Here d=0.72 (second cell in step 3). Jacob Cohen called about 0.2 small, 0.5 medium and 0.8 large, as rough guides. d 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 t=2.7, p=0.011)."

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:

What p < 0.05 means among a thousand ideas. A thousand ideas get tested, each with 20 days of data per group. 100 of them really work (pink, the top two rows); the other 900 do nothing at all (grey).
1 of 4 A thousand ideas get tested, each with 20 days of data per group. 100 of them really work (pink, the top two rows); the other 900 do nothing at all (grey).

Then run the same experiment in Python:

p_meaning.ipynb

83 experiments came out with p<0.05: 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 1−p chance it's real", or "a p chance it's luck", rewrite it: "if there were no real effect, a difference this big would turn up in about p of experiments like this one." Then give the effect size and its interval.

Check what you learned

A test of a new price gives p = 0.30. What can you say?
In a shuffle test, 30 of 1,000 shuffles give a difference at least as big as the real one. What is the p-value?
Two studies find p = 0.001. Study A: 2 million customers, a difference of ₱0.40 a visit. Study B: 60 customers, a difference of ₱150. Which effect is bigger?

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.

All sources for Simple Data Analysis