Hypothesis testing and the p-value

Blood pressure dropped 4 mmHg more in the group that took a new drug than in the group that took a placebo. Is that the drug, or just people being different by chance? Shuffle the labels yourself and see what a p-value measures, and what it does not.

Short answer. A hypothesis test assumes there is no difference (the null hypothesis), then asks how rarely a result as extreme as your data would show up under that assumption. That rareness is the p-value. A p-value is not "the chance that the null hypothesis is true". It is "the chance of data this extreme, if the null hypothesis were true".

Made-up data: how much systolic blood pressure dropped after 4 weeks (mmHg). 25 people on placebo, 25 on the new drug.

Each dot in the top picture is one person. Green dots started on placebo, blue dots started on the new drug. "Shuffle once" keeps all 50 values as they are and only deals the "placebo" and "new drug" labels out again at random, 25 each. That is why the two colors mix in each row after a shuffle. Each shuffle adds one mean difference (new drug label − placebo label) to the bar chart below. The orange vertical line is the real difference, 4, and the orange parts are differences at least that far out. The shuffles come from a fixed-seed random number generator, so they are the same every time you open the page. The data and the drug are made up. This page only explains statistical ideas.

Definitions

The text below uses a difference between two group means. These are the textbook definitions. We test the difference between two population means μ₁ and μ₂; d is the difference observed in the sample, and d₀ is the value the null hypothesis states (usually 0).

null hypothesis, alternative hypothesis
Two claims about a parameter. The null hypothesis H₀ says the parameter equals a set value, such as "no difference, no effect". The alternative hypothesis H₁ is the claim you accept when the null hypothesis is wrong. For a two-sided test and a one-sided test, the alternatives are:

H0:μ1−μ2=d0,H1:μ1−μ2≠d0orH1:μ1−μ2>d0

test statistic
A statistic computed from the sample to measure how far the data are from the null hypothesis. You need its distribution when the null hypothesis is true. On this page, z is the observed difference minus d₀, divided by the standard error. If the null hypothesis is true, d is normally distributed and SE is known, z follows the standard normal distribution N(0, 1).

z=d−d0SE

p-value
The probability, when the null hypothesis is true, that the test statistic comes out as extreme as the observed value or more extreme. It is not the probability that the null hypothesis is true. For a two-sided z test, with Z a random variable that follows the standard normal distribution:

p=P(|Z|≥|z|)

significance level
A cutoff α set before you look at the data. If the p-value is smaller than α, you reject the null hypothesis and say the result is "statistically significant at level α". For a two-sided z test this is the same as the test statistic lying beyond the critical value zα/2 (the value with right tail area α/2 under the standard normal distribution).

p<α⟺|z|>zα/2⟹reject H0

type I error, type II error
Rejecting the null hypothesis when it is true is a type I error. Failing to reject it when it is false is a type II error. The probability of a type I error is the significance level α. The probability of a type II error is β, and 1 − β is the power.

α=P(reject H0∣H0 true),β=P(fail to reject H0∣H0 false)

1. What a hypothesis test is: assume "no difference" and check

A new drug was tested. 50 people were split into two groups at random. 25 took the new drug, and 25 took a placebo that looks the same but has no active ingredient. After 4 weeks, blood pressure dropped by 6 mmHg on average with the new drug and by 2 mmHg with the placebo. The difference is 4 mmHg.

But even if the drug did nothing, the two group means would still come out a little different. Each person's blood pressure moves differently, and who ended up in which group was chance. So the real question is not "is there a difference?" but "could chance alone easily produce a difference this big?"

A hypothesis test answers this backwards. First you assume "the drug has no effect". This is the null hypothesis (H₀). The claim "it has an effect" is the alternative hypothesis (H₁). Then you work out how often a difference as far out as 4 mmHg would show up if the null hypothesis were true. If it would show up only very rarely, the data do not fit "no effect" well, so you drop that assumption. This is called rejecting the null hypothesis.

A trial in court is a good comparison. You start by assuming the defendant is innocent (the null hypothesis). If the evidence is strong enough that "this evidence would be hard to get if they were innocent", you find them guilty (reject). If the evidence is not enough, the verdict is "not guilty", but that does not prove innocence. In the same way, failing to reject the null hypothesis does not mean "no effect has been proven". It means "there is not enough evidence to say there is an effect".

TermMeaningExample on this page
Null hypothesis H₀"No difference, no effect", set up on purpose so you can test itMean difference between new drug and placebo = 0
Alternative hypothesis H₁What you accept if the null hypothesis goesMean difference ≠ 0 (two-sided), or mean difference > 0 (one-sided)
Test statisticThe data boiled down to one number that measures how far you are from the null hypothesisThe mean difference, 4, or that divided by the standard error, z = 2.5
p-valueThe chance that the test statistic comes out as extreme as now, or more, when the null hypothesis is trueAbout 0.012 to 0.018 (a little different depending on the method)
Significance level αA line you set before seeing the data. If p is below it, you reject0.05
RejectDrop the null hypothesis. You say the result is "statistically significant"p < 0.05 → drop "no effect"

2. Differences that chance alone makes: shuffling labels

If the null hypothesis is true, meaning the drug did nothing to anyone, then each person's value (how much their blood pressure dropped) would have been the same whichever pill they got. The labels "new drug" and "placebo" are just name tags with nothing to do with the values. So data with the labels dealt out again at random is data that could just as well have happened.

The experiment at the top does exactly this. It keeps the 50 values, shuffles only the labels into two new groups of 25, and measures the difference between the group means. Do this many times and you get the distribution of "differences that chance alone makes when the drug has no effect". This is the null distribution. The differences pile up around 0 and get rarer further out on both sides.

Now place the real difference, 4, on this distribution. The share of shuffled differences that are as far out as 4, or further, is the p-value. Getting a p-value by shuffling labels like this is called a permutation test. It needs almost no assumptions such as a normal distribution, and the idea is right there to see. What allows you to swap the labels is that people were split into groups at random in the first place (randomization and control groups).

Try this. ① From the starting screen, press "Shuffle once". Green and blue mix in both rows, and the shuffled labels give a difference of 0.24, far smaller than the real 4. ② Then press "100 times". Out of 101 shuffles, a difference of size 4 or more came up once, so p ≈ 0.0099. ③ Press "Start over" and then "1,000 times". It comes up 18 times, so p ≈ 18 ÷ 1,000 = 0.018. The bar chart is a bell shape centered on 0, and beyond 4 there are only small orange bars at both ends. ④ Now pick "One-sided". Only the side with 4 or more is counted: 9 times, p = 0.009, about half. ⑤ Go back to "Two-sided" and press "1,000 times" nine more times to reach 10,000. It comes up 181 times, p ≈ 0.0181. With few shuffles p jumps around (the 0.0099 in ②). With more shuffles it settles near one value.

3. The procedure in five steps

  1. State the hypotheses. Null hypothesis "the mean difference between the two groups = 0", alternative "the mean difference ≠ 0". This is also when you decide whether to look at one side or both (section 5).
  2. Choose the significance level α. It is often 0.05. Decide it before you see the data. Also write down in advance how many people you will collect and which test you will use (section 9).
  3. Compute the test statistic. Boil the data down to one number. On this page that is the mean difference, 4, or that divided by the standard error, z = 2.5.
  4. Find the p-value. The chance that the test statistic comes out as extreme as now, or more, when the null hypothesis is true. You can count it by shuffling labels (section 2) or read it as an area under a curve (section 4).
  5. Decide, and report the rest too. If p < α, reject the null hypothesis and call the result "statistically significant". Otherwise you fail to reject it. Either way, report the size of the effect and its confidence interval as well (section 6).

4. How the formula is built: z = (d − d₀) ÷ SE

Instead of shuffling labels, you can also get a p-value from a formula. Here d is the difference you saw, d₀ is the value the null hypothesis claims (usually 0), and SE is the standard error.

z=d−d0SE

p=P(|Z|≥|z|)

Z stands for a value drawn from the standard normal distribution (a normal distribution with mean 0 and standard deviation 1). The second formula reads "the chance of drawing a value of size |z| or more from the standard normal distribution", which is the area in both outer tails of the curve. Press "Next" below to watch the formula grow one piece at a time. The picture uses the same data as the top experiment (difference 4, standard error 1.6).

Press "Next" to move one step at a time. One piece is added to the formula (the orange background marks the new piece), the line under it fills in this data's numbers, the picture changes to match, and the reason appears at the bottom. The green curve is the distribution of differences if there is no effect, the blue bar is the standard error (one standard deviation out from the middle of the curve), the orange vertical line is the difference you saw, and the purple dashed lines mark α = 0.05. In the third step the whole picture shrinks by a factor of 1.6. The gray numbers just under the axis are the mmHg scale, so they shrink with the picture. The black numbers that appear from the third step on are the z scale.

Written only as formulas, the steps above are:

d=x¯1−x¯2

d−d0

z=d−d0SE

p=P(|Z|≥|z|)=2P(Z≥|z|)

p<α⟺|z|>zα/2⟹reject H0

  1. The difference you saw: d. New drug mean 6 − placebo mean 2 = 4 mmHg. This number alone does not tell you if it is big or small.
  2. Subtract the null value: d − d₀. If there is no effect, the difference wobbles around 0. 4 − 0 = 4. This is how far you landed from the assumption. Why: if the null hypothesis said "difference = 2" instead of 0, you would subtract 2. What you measure is always "how far from the assumption".
  3. Divide by the standard error: (d − d₀) ÷ SE. 4 ÷ 1.6 = 2.5. The difference is 2.5 times the amount it wobbles by chance. Why: the same 4 mmHg is ordinary if the wobble is 10 and very rare if the wobble is 0.5. Dividing by the wobble removes the units, so any data can be checked against the one standard normal curve (the same move as standardization).
  4. The outer area is the p-value: p = P(|Z| ≥ |z|). In the standard normal distribution, the chance of 2.5 or more is 0.0062, and of −2.5 or less is also 0.0062. Together they make 0.0124.
  5. Compare with α. 0.0124 < 0.05, so reject the null hypothesis. Said another way, |z| = 2.5 lies beyond 1.96, the line where the two outer areas add up to 0.05.

Where the standard error comes from

A mean changes a little each time you pick a different set of people. The size of that wobble is the standard error of the mean: the spread from person to person (the standard deviation s) divided by √n (samples and sampling distributions). A difference of two means carries both wobbles, so you add the variances and then take the square root.

SE=s12n1+s22n2

In the data above, both groups have a sample variance (variance divided by n − 1) of 32, and n is 25.

Work it out by hand

StepCalculationValue
Difference you saw, d6 − 24 mmHg
Standard error, SE√(32 ÷ 25 + 32 ÷ 25) = √2.561.6 mmHg
Test statistic, z(4 − 0) ÷ 1.62.5
Area beyond on one side2.5 or more in the standard normal0.0062
p-value (two-sided)2 × 0.00620.0124
Decision0.0124 < 0.05Reject the null hypothesis

The same work as formulas:

d=6−2=4

SE=3225+3225=2.56=1.6

z=4−01.6=2.5

p=2P(Z≥2.5)=2×0.0062=0.0124<0.05

Areas under the standard normal curve cannot be worked out exactly with just adding and multiplying, so they are computed with an approximation. The experiments on this page use an approximation of the complementary error function (erfc). Measured against Python's math.erfc, its largest relative error is about 0.00000011. The line 1.96 is found with the same function, by searching for "the z where the two outer areas add up to 0.05".

Permutation test, the z formula, and the t-test

On the same data, the permutation test (10,000 shuffles) gives p ≈ 0.018 and the z formula gives p = 0.0124. The three methods build the null distribution in different ways, so p comes out a little different. The z formula treats the standard error 1.6 as if it were known exactly. In fact 1.6 is an estimate from 50 people's data, so it wobbles too. Take that wobble into account and you get the t distribution, which has slightly thicker tails than the normal curve. Getting p from it is the t-test. On the same data the t-test (48 degrees of freedom) gives p ≈ 0.016. With small samples the z formula tends to give a p that is too small, so for comparing means people usually use the t-test. If the data lean to one side or have many outliers and you worry about the normal assumption, use a permutation test or a nonparametric test that works with ranks.

5. One-sided and two-sided tests

A two-sided test counts a result as rare whether it leans toward "the new drug lowers blood pressure more" or "the new drug lowers it less". That is why it adds the two outer areas. A one-sided test counts only one direction, chosen in advance. If the null distribution is symmetric around 0, the one-sided p is about half the two-sided p (0.018 and 0.009 in the top experiment, 0.0124 and 0.0062 from the formula).

So switching to a one-sided test after seeing the data cuts p in half. That is choosing after seeing the result, which is not fair. Use a one-sided test only when you can say in advance that a result in the other direction would mean nothing. Either way, decide before you look at the data.

6. Effect size and sample size

A p-value is not set by the size of the effect alone. In z = (d − 0) ÷ SE, the SE shrinks with √n, so the same difference gives a larger z and a smaller p when you collect more people. The experiment below fixes a "true effect", runs 20 brand-new studies and plots their 20 p-values.

Each row is one study. It draws n new people per group, runs a two-sided z-test on the difference of the two means, and puts a dot at its p-value. Each step to the left on the axis is 10 times smaller (a log scale). Dots left of the orange dashed line (p < 0.05) are orange, dots to the right are green. A p smaller than the picture can show is drawn as a triangle at the left edge. The spread from person to person (the standard deviation) is 5.66 mmHg, the same as the top data, and we pretend it is known. In real work you estimate the spread from the data too, so you use a t-test. To draw fast, each study only draws the two group means (this has the same distribution as drawing all n people and averaging). Changing the effect or n starts again from the same seed. All the data are made up.

Try this. ① At the start (effect 0, n 25), 1 of the 20 studies has p < 0.05. The drug has no effect at all, and yet one came out "significant". Press "20 new studies" nine more times to reach 200, and 10 of them, 5.0%, are below 0.05. With no effect, p is spread evenly between 0 and 1, and 5% of it falls below 0.05. This is a type I error, and its rate is α. ② With effect 0, raising n all the way to 5,000 leaves the dots where they were, and the rate stays at 5%. ③ Raise the effect to 4 (n 25) and 14 of 20 have p < 0.05. The rate from the formula (the power) is 70.5%. So if you rerun a study the size of the top data, about 3 in 10 reruns will come out "not significant". ④ Lower the effect to 0.5 (n 25) and you get 1 of 20 (6.1% from the formula), almost the same as no effect. Raise n to 2,000 and 16 have p < 0.05 (79.8%). At 5,000 all 20 do (99.3%), and 8 of them have p smaller than the left edge of the picture (0.000001). That is for a tiny difference of 0.5 mmHg. ⑤ Effect 2 with n 100 puts the dots in exactly the same places as effect 4 with n 25. Halve the effect and you need 4 times as many people for the same power.

You can state the effect size as the difference itself (4 mmHg), or divide it by the person-to-person spread (4 ÷ 5.66 ≈ 0.71, called Cohen's d). Significance and effect size answer different questions. "Statistically significant" only means "hard to explain by chance alone". It does not mean "large" or "important". So do not report the p-value alone. Report the size of the difference and its confidence interval too. For the data above, the difference is 4 mmHg and the 95% confidence interval is 4 ± 1.96 × 1.6, about 0.9 to 7.1 mmHg. When both use the same standard error and the normal curve, "the 95% confidence interval does not contain 0" and "two-sided p < 0.05" say the same thing, and the interval also tells you roughly how big the effect might be.

7. What a p-value tells you, and what it does not

A p-value is a probability computed under the condition "if the null hypothesis were true". The most common mistake is to read it the other way around, swapping the condition and the result. For each sentence below, choose whether it is a right reading. Once you choose, the reason appears.

  1. ① "If p = 0.03, there is a 3% chance that the null hypothesis (no effect) is true."

  2. ② "If the drug had no effect, there would be a 3% chance of a difference as extreme as this one, or more."

  3. ③ "If p = 0.03, there is a 3% chance that this difference happened by chance."

  4. ④ "If p > 0.05, it has been proven that there is no effect."

  5. ⑤ "A drug with p = 0.001 has a bigger effect than a drug with p = 0.04."

  6. ⑥ "We got p < 0.05, so if we run the same experiment again we will almost surely get p < 0.05 again."

  7. ⑦ "If you test twenty drugs that have no effect at all, one at a time, it would not be strange for one of them to get p < 0.05."

Readings that are wrong

Wrong readingRight reading
p = 0.03 → 3% chance the null hypothesis is trueIf the null hypothesis were true, there would be a 3% chance of data this extreme. You cannot get the chance that the null hypothesis is true from p alone
p = 0.03 → 3% chance the result is due to chanceThe same mistake as above. "The chance it is due to chance" is another way of saying "the chance the null hypothesis is true"
p = 0.03 → 97% chance the alternative hypothesis is truep is not the chance of the alternative hypothesis either
p > 0.05 → there is no effectThere is not enough evidence to say there is an effect. Small studies often give p > 0.05 even when an effect exists
Smaller p → bigger effectp mixes effect size and sample size. Judge the effect size from the difference and the confidence interval
p < 0.05 → it will be significant again on a rerunThe chance of being significant on a rerun is the power. If p was just under 0.05, it is about half
p = 0.049 and p = 0.051 lead to opposite conclusionsThey are nearly the same evidence. 0.05 is only an agreed line

Saying that p-values are often misused, the American Statistical Association (ASA) released a statement with six principles in 2016 (Wasserstein and Lazar in the sources). They are:

  1. P-values can indicate how incompatible the data are with a specified statistical model.
  2. P-values do not measure the probability that the studied hypothesis is true, or the probability that the data were produced by random chance alone.
  3. Scientific conclusions and business or policy decisions should not be based only on whether a p-value passes a specific threshold.
  4. Proper inference requires full reporting and transparency.
  5. A p-value, or statistical significance, does not measure the size of an effect or the importance of a result.
  6. By itself, a p-value does not provide a good measure of evidence regarding a model or hypothesis.

8. 0.05 is only an agreement

α = 0.05 is an agreement: "when there is no effect, we accept wrongly rejecting up to 1 time in 20". It is not a value found in nature but a long-used convention, so depending on the field and the situation people set it stricter, such as 0.01. Studies that run thousands of tests at once use a far smaller line (multiple comparisons).

A line splits decisions into two, but evidence is continuous. p = 0.049 and p = 0.051 are nearly the same evidence. So instead of just "p < 0.05", report the p-value itself (for example p = 0.012) along with the effect size and the confidence interval.

Two kinds of mistakes: type I and type II errors

Really no effect (null hypothesis true)Really an effect
Reject (p < α)Type I error: you claim an effect that is not there. Probability αCorrect. This probability is the power (1 − β)
Fail to rejectCorrectType II error: you miss an effect that is there. Probability β

A smaller α means fewer type I errors but more type II errors, where you miss a real effect. To cut both at once you need more people. The "rate from the formula" in section 6 is the power. See type I and II errors and power for more, and sample size calculation for how many people you need.

9. p-hacking, and writing your plan down first

Even with no effect, 20 tests will usually give one p < 0.05 (① in section 6). Turn that around and you can make almost anything "significant". Peek at the data many times while collecting it and stop the moment p drops below 0.05. Measure many outcomes and report only the significant ones. Rerun the analysis while changing whether to drop outliers or how to split the groups. Switch to a one-sided test after seeing the result. Each step can look reasonable, but they are all "look many times and keep the best". A p-value picked this way no longer keeps the promise "5% when there is no effect". This is called p-hacking. A study used simulations to show that this kind of flexible analysis alone greatly raises false positives (Simmons and others in the sources).

Even if all 20 independent tests are on things with no effect, the chance that at least one gives p < 0.05 is 1 − 0.95²⁰ ≈ 0.64.

The fix is to write your plan down before you see the results. Before collecting data, record in a public place what your main outcome is, how many people you will collect, which test you will use and how you will handle outliers (preregistration). If you must run many tests, make the line stricter to match how many you run (multiple comparisons). How clinical trials do this is on clinical trial design.

10. Key properties

PropertyIn plain words
A p-value is a probability under "if the null hypothesis were true"It is not the chance that a hypothesis is true. Do not read the condition backwards
With no effect, p is spread evenly between 0 and 1 (when the test's assumptions hold)So deciding with α = 0.05 gives "significant" 5% of the time when there is no effect (type I error)
p carries effect size and sample size togetherA small effect can give a small p if n is large, and a big effect can give a large p if n is small
p jumps around a lot from one data set to the nextRerun the same study and p can change a lot (the dots in section 6)
p only means something when its assumptions holdRandom assignment, independent data, an analysis fixed in advance. Look many times and pick, and the meaning is gone

Where is it used?

# Python standard library only. The same made-up data as the experiment
import random
from statistics import NormalDist, mean

placebo = [-10, -6, -5, -5, -3, -2, -2, -1, -1, 0, 1, 1, 2, 3, 3, 4, 5, 6, 6, 6, 6, 8, 10, 11, 13]
drug = [-6, -3, -1, 1, 1, 2, 2, 2, 3, 5, 5, 5, 6, 7, 7, 8, 8, 8, 9, 11, 12, 13, 13, 15, 17]
observed = mean(drug) - mean(placebo)              # 4

# Permutation test (two-sided). The groups are the same size, so comparing sums works (no rounding error)
both = placebo + drug
rng = random.Random(1)
count = 0
for _ in range(10000):
    rng.shuffle(both)
    if abs(sum(both[:25]) - sum(both[25:])) >= abs(sum(drug) - sum(placebo)):
        count += 1
print(count / 10000)                               # 0.0173

# The z formula
z = (observed - 0) / 1.6
print(2 * (1 - NormalDist().cdf(abs(z))))          # 0.0124

The shuffles in this code use Python's random numbers, so it gives 0.0173, a little different from the experiment's 0.018. A different shuffle order gives a slightly different p.

Common questions

What is a p-value?

It is the chance of getting a result as extreme as yours, or more extreme, if the null hypothesis (for example "there is no effect") were true. The smaller it is, the worse the data fit that assumption. It is not the chance that the null hypothesis is true.

Does p > 0.05 mean there is no effect?

No. It only means there is not enough evidence to say there is an effect. With a small sample, p > 0.05 comes up often even when a real effect exists. The confidence interval tells you how large the effect could still be.

Why 0.05 of all numbers?

It is an old convention: when there is no effect, you accept wrongly rejecting the null hypothesis up to 1 time in 20. It is not a law of nature, so some fields use a stricter line. Do not just report whether p crossed the line. Report the p-value itself, the effect size and the confidence interval.

When do you use a one-sided or a two-sided test?

A two-sided test counts a difference in either direction. Use a one-sided test only when you can say in advance that a result in the other direction would mean nothing. Either way, decide before you look at the data. Switching to one-sided after seeing the result cuts p in half, which is not fair.

How is shuffling labels (a permutation test) different from a t-test?

Both place the real difference on top of "the differences you would see if there were no effect" to get a p-value. A permutation test builds that distribution by actually shuffling the labels. A t-test computes it from a formula (the t distribution) that assumes the data are close to normal. On this page's data they come out close: 0.018 for the permutation test and 0.016 for the t-test.

Words

Plain words used hereTextbook term
The assumption "no difference"Null hypothesis (H₀)
"There is a difference"Alternative hypothesis (H₁)
The distribution of values you would see with no effectNull distribution
The data boiled down to one numberTest statistic. Divided by the wobble, it is the z-statistic
The chance of a result this extremep-value
The line set in advanceSignificance level (α)
Below the lineStatistically significant
Drop the null hypothesisReject the null hypothesis
The line where the two outer areas add up to α (1.96)Critical value. Beyond it is the rejection region
How much a mean wobbles by chanceStandard error (SE)
Shuffling labelsPermutation test, randomization test
One direction only / both directionsOne-sided test / two-sided test
Claiming an effect that is not thereType I error, false positive
Missing an effect that is thereType II error, false negative
The chance of finding an effect that is thereStatistical power
How big the difference isEffect size. Divided by the spread, it is Cohen's d
A pill with no active ingredientPlacebo
Looking many times and keeping the bestp-hacking, data dredging
Writing the plan down firstPreregistration
The function used for outer areas of the normal curveComplementary error function (erfc)

Sources

← Confidence intervalsType I and type II errors, and statistical power →