Advanced level

Hypothesis testing, the p-value and the two kinds of error

A new page variant converts at 4.8 per cent instead of 4.0 — a real improvement or chance? A statistical test turns that question into arithmetic: a null hypothesis, a test statistic, a rejection region and a p-value. Together with what a p-value does NOT say, because it is the most abused number in science.

Before you start

This topic builds on earlier ideas. Before you start, it's worth working through the lessons below — they'll make everything click:

Where this is used

Real situations where you count exactly the way this lesson teaches:

  • An A/B test
    Variant A converts at 4.0 per cent. Variant B gets 2500 views and converts 120 times, that is 4.8 per cent. Under the hypothesis that nothing changed, the standard error is √(0.04 · 0.96 / 2500) ≈ 0.0039, so z = (0.048 − 0.040)/0.0039 ≈ 2.04. That clears 1.96, so at α = 0.05 the null hypothesis is rejected — but only just, and the rollout decision should say so.
  • A clinical trial
    A drug is supposed to lower systolic pressure. Across 64 patients the mean drop was 5 mmHg with a deviation of 12 mmHg. The standard error is 12/√64 = 1.5, so z = 5/1.5 ≈ 3.33 — above 2.58, so the result is significant even at the 1 per cent level. Whether 5 mmHg is clinically important is a wholly separate question from whether it is statistically significant.
  • Monitoring a production line
    A filling machine is set for 500 g. An inspector weighs 100 packs: mean 499.4 g, deviation 2 g. The standard error is 0.2 g, so z = (499.4 − 500)/0.2 = −3. An absolute value of 3 clears 1.96, so the 0.6 g shortfall is not chance — across 20 000 packs a day that is 12 kg less product than the label promises.
  • Is the die fair
    In 600 throws a six came up 120 times instead of the expected 100. Under the fair-die hypothesis the standard deviation of the count of sixes is √(600 · 1/6 · 5/6) ≈ 9.13, so z = 20/9.13 ≈ 2.19. That clears 1.96, so at α = 0.05 the die is judged unfair — although at α = 0.01, where the boundary is 2.58, this data would not have been enough.

All formulas

  • Null and alternative hypothesis

    H0:μ=μ0H1:μμ0H_0: \mu = \mu_0 \qquad H_1: \mu \neq \mu_0

    H₀ always carries the equality — that is the computable one

  • Test statistic for a mean

    z=xˉμ0σ/nz = \frac{\bar{x} - \mu_0}{\sigma / \sqrt{n}}

    how many standard errors the sample sits from the hypothesis

  • Decision rule (two-sided test)

    z>zα/2    reject H0|z| > z_{\alpha/2} \;\Longrightarrow\; \text{reject } H_0

    z_{α/2} = 1.96 for α = 0.05

  • The p-value

    p=P(Zz    H0)p = P\big(|Z| \geq |z| \;\big|\; H_0\big)

    the probability of SUCH DATA given H₀ — not of H₀ given the data

  • Type I error

    α=P(reject H0H0 true)\alpha = P(\text{reject } H_0 \mid H_0 \text{ true})

    a false alarm; fixed in advance

  • Type II error

    β=P(fail to reject H0H1 true)\beta = P(\text{fail to reject } H_0 \mid H_1 \text{ true})

    missing an effect that is really there

  • Power of a test

    1β1 - \beta

    the chance of detecting an effect that exists

The previous lesson answered the question how much — what the mean is and how precisely we know it. This one answers the question whether: whether an observed difference is real, or within the reach of chance.

A new page variant converts at 4.8%4.8\% instead of 4.0%4.0\%. A filling machine dispenses 499.4499.4 g instead of 500500. A drug lowered blood pressure by 55 mmHg. Every time the question is the same: is this an effect, or is it noise.

Two hypotheses

A test begins with a pair of claims about the population:

H0:μ=μ0H1:μμ0H_0: \mu = \mu_0 \qquad H_1: \mu \neq \mu_0

The null hypothesis H0H_0 says nothing is happening: the mean equals the nominal value, the variants convert alike, the drug does not work. The alternative hypothesis H1H_1 says something is.

The pair is asymmetric, and that is the first thing to understand:

  • H0H_0 always carries the equality, because only then can the distribution of the test statistic be computed. Writing μ=500\mu = 500 fixes one particular distribution; writing μ500\mu \neq 500 fixes infinitely many.
  • H0H_0 stands until the data overturns it. A test never "proves" H0H_0; at best it rejects it or fails to. This is the same asymmetry as in a courtroom: innocence is assumed until guilt is shown, and an acquittal does not mean innocence was proved.

The alternative can also be one-sided (H1:μ>μ0H_1: \mu > \mu_0), when only one direction is of interest in advance — whether a drug lowers pressure, rather than whether it changes it. The direction must then be fixed before the data is seen; we come back to that in the section on abuses.

The test statistic

Since H0H_0 fixes a distribution, we can ask how unusual our sample is relative to that distribution. The measure of unusualness is the test statistic:

z=xˉμ0σ/nz = \frac{\bar{x} - \mu_0}{\sigma / \sqrt{n}}

The numerator is the sample's departure from the hypothesis; the denominator is the standard error of the previous lesson. The quotient therefore says how many standard errors the sample mean sits from what H0H_0 claims.

Dividing by n\sqrt{n} is the heart of it: the same 0.60.6 g shortfall is nothing at n=4n = 4 and an alarm at n=100n = 100. That step is exactly what separates a test statistic from the plain standardisation of a single observation in the normal-distribution lesson.

A machine should dispense 500 g. A sample of n = 100 gave x̄ = 499.4 g with σ = 2 g. Compute z.

The rejection region and the significance level

It remains to settle how unusual a sample has to be to overturn H0H_0. The boundary is set by the significance level α\alpha — the acceptable probability of a false alarm.

−4−3−2−10123400.10.20.30.4zdensityz = −1.96z = 1.96distribution of z under H₀
The shaded middle is the acceptance region: 95 per cent of the area. The two unshaded tails beyond the marks are the rejection region — 5 per cent in total, 2.5 per cent on each side.

The decision rule is a single comparison:

z>zα/2    reject H0|z| > z_{\alpha/2} \;\Longrightarrow\; \text{reject } H_0
α\alphatwo-sided testone-sided test
0.100.101.641.641.281.28
0.050.051.961.961.641.64
0.010.012.582.582.332.33

These are the same values the estimation lesson matched to confidence levels — and that is no coincidence. We come to the link between the two calculations in a moment.

For the machine in the example z=3>1.96|z| = 3 > 1.96, so we reject the hypothesis that it dispenses 500500 g.

The p-value

Comparing against a boundary gives a yes/no answer but loses the information about how extreme the result was. That is what the p-value carries:

p=P(Zz    H0)p = P\big(|Z| \geq |z| \;\big|\; H_0\big)

In words: the probability of observing data at least as extreme as ours, assuming H0H_0 is true. The smaller it is, the worse the data fits the null hypothesis.

−4−3−2−10123400.10.20.30.4zdensityz = 2distribution of z under H₀
The one-sided p-value for z = 2 is the area of the shaded tail: 2.5 per cent. The two-sided version adds the mirror tail on the left and comes to 5 per cent.

The decision rule can therefore be written a second, equivalent way: reject H0H_0 when p<αp < \alpha. Three numbers from the three-sigma rule are enough to read p-values without tables:

z\lvert z \rverttwo-sided ppone-sided pp
1132%32\%16%16\%
225%5\%2.5%2.5\%
330.3%0.3\%0.15%0.15\%

The direction of the conditioning in the definition is everything, and we return to it in the section on abuses: p=P(dataH0)p = P(\text{data} \mid H_0), not P(H0data)P(H_0 \mid \text{data}). That is the same difference as between P(AB)P(A|B) and P(BA)P(B|A) in the conditional probability lesson.

The two kinds of error

A decision made from a sample can be wrong in two ways — and they are entirely different errors:

H0H_0 trueH0H_0 false
reject H0H_0Type I error (α\alpha)correct (power)
fail to reject H0H_0correctType II error (β\beta)
  • Type I error — a false alarm: announcing an effect that is not there. Its probability is α\alpha and we set it ourselves, in advance.
  • Type II error — a miss: the effect exists and we do not detect it. Its probability β\beta depends on how large the effect is and how large the sample is.
  • Power is 1β1 - \beta — the chance of detecting an effect that really exists. The conventional floor of respectability is 0.800.80.
−4−3−2−10123456700.10.20.30.4zdensityboundary 1.96distribution under H₀distribution under H₁
Two curves: the left one holds when H₀ is true, the right one when the real effect is three standard errors. The shaded tail of the left curve is α. The part of the right curve lying TO THE LEFT of the mark is β — the cases in which a real effect goes undetected.

The picture shows what the table cannot: both errors are fighting over the same boundary. Moving the mark to the right (tightening α\alpha) shrinks the shaded tail and at the same time enlarges the part of the right curve to the left of the mark. The only way to shrink both at once is to pull the curves apart or narrow them — a bigger effect or a bigger sample.

A test and a confidence interval

The two calculations of this lesson and the previous one are the same calculation written from two sides:

A two-sided test at level α\alpha rejects H0:μ=μ0H_0: \mu = \mu_0 exactly when μ0\mu_0 lies outside the confidence interval at level 1α1 - \alpha.

The 95% confidence interval for the mean is (499.01; 499.79). What does a test of H₀: μ = 500 at α = 0.05 say?

That is why more and more journals require an interval alongside the p-value: the interval says how large the effect is and how precisely it was measured, the p-value only whether it cleared a threshold.

Where this mathematics gets abused

This is the most abused calculation in science, so there are five abuses.

1. The p-value read as the probability of the hypothesis

"p=0.03p = 0.03, so there is only a 3 per cent chance the null hypothesis is true." False, and it swaps the conditioning: pp is P(dataH0)P(\text{data} \mid H_0), while that sentence is about P(H0data)P(H_0 \mid \text{data}).

The correction: the right wording is "if H0H_0 were true, data as extreme as ours would turn up in 3 per cent of studies". To say anything about the probability of the hypothesis itself you need a prior and Bayes' formula — and the test does not take that road. For hypotheses that are rarely true a priori, P(H0data)P(H_0 \mid \text{data}) can sit in the tens of per cent even at p=0.03p = 0.03.

2. "Absence of evidence" read as "evidence of absence"

"p=0.31p = 0.31, so the drug does not work." No: p=0.31p = 0.31 means this data was not enough to reject H0H_0. Perhaps there is no effect — or perhaps the sample was too small to detect it, i.e. the test had low power.

The correction: the conclusion is "we do not reject H0H_0", never "we confirm H0H_0". To actually demonstrate the absence of an effect there is the equivalence test, or a confidence interval that excludes every practically important effect.

3. Statistical significance mistaken for importance

With a large enough sample every difference becomes statistically significant, because the standard error shrinks like 1/n1/\sqrt{n}. At a million observations a conversion difference of 0.010.01 percentage points gives p<0.001p < 0.001 — and means nothing whatsoever to the business.

The correction: significance says "this is not noise", not "this matters". The effect size and its confidence interval are a separate and equally necessary piece of information; the threshold of practical importance is set by the field, not by statistics.

4. p-hacking and multiple testing

Run twenty independent tests at α=0.05\alpha = 0.05 with every null hypothesis true, and on average one comes out significant. Not because something was discovered, but because that is how α\alpha was defined. The same arithmetic sits behind adding observations "until it works", dropping outliers after the fact, testing many variables and hunting for the subgroup where it came out.

The correction: hypothesis and analysis plan fixed before the data is collected, and with many tests a correction for multiplicity — the simplest is the Bonferroni rule, dividing α\alpha by the number of tests. Preregistering a study is the practical version of the same principle.

5. Choosing the hypothesis after seeing the data

Switching from a two-sided to a one-sided test after seeing which way the data leaned doubles the stated α\alpha: a result with p=0.08p = 0.08 two-sided becomes "significant" at p=0.04p = 0.04. The same trick, under the name HARKing, consists of phrasing a hypothesis to fit a pattern that was found and presenting it as though it had been stated in advance.

The correction: the direction of the test, and the hypothesis itself, are part of the plan, not of the result. An analysis that found a pattern cannot confirm it with the same data — that takes a fresh sample.

Exercises

The first set walks the three steps of one test. The z prompt gives xˉ\bar{x}, μ0\mu_0, σ\sigma and nn and asks for the test statistic — remember to divide σ\sigma by n\sqrt{n}, because without that step you are standardising a single observation rather than a mean.

The "p-value" prompt gives zz and asks for the area of the tail, as a percentage, one-sided in some draws and two-sided in others; both read off the three-sigma rule. The third prompt adds a critical value zz^{*} and asks for the verdict: the answer is the symbol of the hypothesis that stands, H₀ or H₁. You may type H0 and H1.

Practice

Work through a set of exercises — they get harder as you go. At the end you'll see your score and the mistakes worth reviewing.

Exercise 1 of 8Score: 0
z: x̄ = 84, μ₀ = 100, σ = 24, n = 9

The second set is authored and asks about what the generator does not compute: the link between α\alpha and the Type I error, between power and the Type II error, the verdict read straight off a p-value and off a confidence interval, and the arithmetic of multiple testing. Where the answer is a hypothesis, write H₀ or H₁.

Practice

Work through a set of exercises — they get harder as you go. At the end you'll see your score and the mistakes worth reviewing.

Exercise 1 of 6Score: 0
Significance level α = 0.05. Probability of a Type I error → % =

Naming which kind of error a particular situation risks has no number for an answer and stays content — the table above settles all four cases.

Common mistakes

  • Reading p as the probability of the null hypothesisp=P(dataH0)p = P(\text{data} \mid H_0), not the other way round; swapping the conditioning is the same slip as confusing P(AB)P(A|B) with P(BA)P(B|A).
  • Concluding "we proved there is no effect" — a test can only reject H0H_0 or fail to; absence of evidence is not evidence of absence.
  • Confusing significance with importance — on a large sample every difference becomes significant, including differences of no practical consequence.
  • Setting α\alpha, or the direction of the test, after seeing the data — either one invalidates the stated significance level; switching to a one-sided test after the fact doubles it.
  • Dropping the n\sqrt{n} from the denominator — without it you are standardising a single observation, and the test is about a sample mean.
  • Repeating a test until it succeeds — twenty tests at α=0.05\alpha = 0.05 produce one significant result on average even when there is nothing to find.
  • Tightening α\alpha without looking at power — a smaller α\alpha always raises β\beta; both fall together only on a larger sample.

Formula card

Topic: Hypothesis testing

  • Null and alternative hypothesis

    H0:μ=μ0H1:μμ0H_0: \mu = \mu_0 \qquad H_1: \mu \neq \mu_0

    H₀ always carries the equality — that is the computable one

  • Test statistic for a mean

    z=xˉμ0σ/nz = \frac{\bar{x} - \mu_0}{\sigma / \sqrt{n}}

    how many standard errors the sample sits from the hypothesis

  • Decision rule (two-sided test)

    z>zα/2    reject H0|z| > z_{\alpha/2} \;\Longrightarrow\; \text{reject } H_0

    z_{α/2} = 1.96 for α = 0.05

  • The p-value

    p=P(Zz    H0)p = P\big(|Z| \geq |z| \;\big|\; H_0\big)

    the probability of SUCH DATA given H₀ — not of H₀ given the data

  • Type I error

    α=P(reject H0H0 true)\alpha = P(\text{reject } H_0 \mid H_0 \text{ true})

    a false alarm; fixed in advance

  • Type II error

    β=P(fail to reject H0H1 true)\beta = P(\text{fail to reject } H_0 \mid H_1 \text{ true})

    missing an effect that is really there

  • Power of a test

    1β1 - \beta

    the chance of detecting an effect that exists

−4−3−2−10123400.10.20.30.4zdensityz = −1.96z = 1.96distribution of z under H₀
The distribution of the test statistic when the null hypothesis is true. The shaded middle is the acceptance region from −1.96 to 1.96; beyond it, in the two unshaded tails, lies 5 per cent of the area in total — and that is the significance level of a two-sided test.
−4−3−2−10123456700.10.20.30.4zdensityboundary 1.96distribution under H₀distribution under H₁
Two distributions of the statistic: the left one holds when H₀ is true, the right one when the real effect is three standard errors. The shaded right tail of the left curve is the Type I error; the part of the right curve lying before the boundary is the Type II error.

Frequently asked questions

Related articles