Section 11.22

Hypothesis test

Definition

A hypothesis test is a rule that uses data to decide whether to reject a stated model of how the data arise, called the null hypothesis, in favor of an alternative hypothesis. The rule computes a test statistic, one number that summarizes the data, and rejects the null hypothesis when the statistic falls in a rejection region fixed in advance. The region is chosen so that, if the null hypothesis is true, rejection happens with probability at most α\alpha (alpha), a chosen significance level such as 0.05.

The idea

A developer runs a benchmark three times on build A and three times on build B. Build B’s runs are all faster, by 0.4 milliseconds (ms) on average. Two identical builds would also differ a little from run to run, because of caches, interrupts §6.11 and timing noise. The question is whether noise alone often produces a difference this large, or rarely.

A hypothesis test answers by assuming the dull explanation, the null hypothesis that the build makes no difference, and asking how often that world would produce a result at least as striking as the observed one. If rarely, the test rejects the null hypothesis. A criminal trial is a fair picture: the defendant is presumed innocent, and conviction needs strong evidence. An acquittal does not prove innocence, just as failing to reject does not prove the null hypothesis. The picture stops at the error rate. The significance level limits wrongful rejections over many uses of the rule in cases where the null hypothesis is true; it is not the probability that one particular verdict is wrong.

How it works

A test is fixed by six decisions, made before the outcome is seen.

  1. The null hypothesis H0H_0: a precise probability model for the data when the effect of interest is absent. Example: each run would have produced the same latency whichever build it received.

  2. The alternative hypothesis H1H_1: the effect the test is meant to detect, with its direction. A one-sided alternative (build B is faster) looks in one direction; a two-sided alternative (the builds differ) looks in both.

  3. The design: the experimental unit §11.3 (the thing that independently receives a condition, here a run), how many units there are, how conditions are assigned to them, whether measurements are paired, and when data collection stops.

  4. The test statistic TT and which of its values count as more extreme, meaning further in the direction of the alternative. Example: TT is the mean latency of A minus the mean latency of B, and larger is more extreme.

  5. The significance level α\alpha.

  6. The rejection region: the most extreme values of TT, chosen so that their total probability is at most α\alpha when H0H_0 is true.

The distribution of TT when H0H_0 is true is called the null distribution, or reference distribution. It follows from the null hypothesis together with the design, in one of three common ways. A randomization §11.8 test, also called a permutation test, takes it from the random assignment of conditions that the experimenter carried out. A model-based test such as Student’s t-test takes it from a mathematical model: independent units whose measurements follow a normal (bell-shaped) distribution, or enough units that their averages approximately do. A rank test such as the Wilcoxon–Mann–Whitney test replaces the values by their ranks and assumes independent units drawn from continuous distributions. The three kinds rest on different assumptions and test slightly different null hypotheses.

The experiment then runs, TT is computed, and the test rejects H0H_0 if and only if TT lies in the rejection region. Equivalently, the test computes the p-value §11.23, the probability under H0H_0 of a statistic at least as extreme as the one observed, and rejects when the p-value is at most α\alpha.

A test can err in two ways. A Type I error §1.22 rejects a true null hypothesis: a false positive, an effect reported that is not there. The design limits its probability to α\alpha. A Type II error fails to reject when a stated alternative is true: a false negative, a missed effect. Its probability is written β\beta (beta), and 1−β1 - \beta is the statistical power §11.25 of the test against that alternative. This decision-rule form with two kinds of error was developed by Jerzy Neyman and Egon Pearson in papers published from 1928 to 1933 [472, 473]. Ronald Fisher’s earlier significance tests used the p-value as a graded measure of evidence against the null hypothesis.

Worked example

Six benchmark runs are available. Three are chosen by lot to run build A, and the other three run build B; this random assignment is the design. The measured latencies, in ms, are 10.2, 10.3 and 10.4 for A, and 9.8, 9.9 and 10.0 for B. The null hypothesis is Fisher’s sharp null hypothesis: every run would have produced the same latency under either build. The alternative is one-sided: B is faster. The statistic is T=mean of A−mean of B=10.3−9.9=0.4T = \text{mean of A} - \text{mean of B} = 10.3 - 9.9 = 0.4 ms. The significance level is α=0.05\alpha = 0.05.

If H0H_0 is true, the six latencies are fixed and only the labels A and B depend on the lot. The lot could have chosen any 3 of the 6 runs for A: (63)=(6×5×4)/(3×2×1)=20\binom{6}{3} = (6 \times 5 \times 4)/(3 \times 2 \times 1) = 20 equally likely assignments, where (ab)\binom{a}{b} is the number of ways to choose bb items from aa. The six values sum to 60.6 ms, so T=(SA−(60.6−SA))/3T = (S_A - (60.6 - S_A))/3, where SAS_A is the sum of the three values labeled A. TT is largest when A holds the three largest values: SA=10.2+10.3+10.4=30.9S_A = 10.2 + 10.3 + 10.4 = 30.9 and T=(30.9−29.7)/3=0.4T = (30.9 - 29.7)/3 = 0.4 ms. The next largest sum, 10.0+10.3+10.4=30.710.0 + 10.3 + 10.4 = 30.7, gives only T=(30.7−29.9)/3≈0.27T = (30.7 - 29.9)/3 \approx 0.27 ms. So exactly 1 of the 20 assignments gives T≥0.4T \ge 0.4 ms.

Under H0H_0, the probability of T≥0.4T \ge 0.4 ms is therefore 1/20=0.051/20 = 0.05. The rejection region “T≥0.4T \ge 0.4 ms” has probability exactly α=0.05\alpha = 0.05, the observed TT lies in it, and the test rejects H0H_0. The justification is the lot itself: the 20 assignments were equally likely by construction, so no assumption about normal distributions or about the noise was needed.

The design also limits what the test can show. With 3 runs per build the smallest possible one-sided p-value is 1/20=0.051/20 = 0.05. A two-sided test also counts the mirror assignment, with A holding the three smallest values, as equally extreme, so its smallest p-value is 2/20=0.12/20 = 0.1: a two-sided test at α=0.05\alpha = 0.05 can never reject with this design, whatever the data. Four runs per build give (84)=70\binom{8}{4} = 70 assignments and a smallest two-sided p-value of 2/70≈0.0292/70 \approx 0.029.

The procedure around the test matters too. Suppose the developer repeats the six-run experiment with fresh runs until one repetition rejects, and reports only that one. If the builds are identical, each repetition rejects with probability 0.05, independently of the others, so at least one of five repetitions rejects with probability 1−0.955≈0.2261 - 0.95^5 \approx 0.226. The reported test looks like a test at level 0.05, but the procedure that produced it has a Type I error rate of about 23% after five tries. Stopping and reporting rules are part of the design.

Stated precisely

Let H0H_0 be a set of probability distributions for the data: one distribution for a simple null hypothesis, several for a composite one. Let TT be the test statistic and RR the rejection region. The test has level α\alpha when PrP(T∈R)≤αfor every distribution P in H0,\Pr_P(T \in R) \le \alpha \quad \text{for every distribution } P \text{ in } H_0, where PrP\Pr_P is probability computed as if PP generated the data. The largest of these probabilities (strictly, their least upper bound §19.2.6) is the size of the test; for a discrete statistic it can be smaller than α\alpha. For a distribution QQ in the alternative, PrQ(T∈R)\Pr_Q(T \in R) is the power of the test against QQ, and β(Q)=1−PrQ(T∈R)\beta(Q) = 1 - \Pr_Q(T \in R) is its Type II error probability.

The guarantee PrP(T∈R)≤α\Pr_P(T \in R) \le \alpha depends on three conditions.

  1. The null model matches the design. H0H_0 contains the distribution that actually generates the data when the effect is absent, including how units were sampled or assigned, which measurements are independent and which are paired.

  2. Everything fixed in advance. TT, RR, α\alpha and the stopping rule were fixed without looking at the outcome. A choice made after looking is part of the procedure, and then the error rate of the whole procedure is what counts.

  3. One test. The guarantee covers this one test. A study that runs many tests needs an error rate for the whole family, which the Multiple comparisons §11.26 entry treats.

The level α\alpha is a long-run rate: if the rule were applied many times to data for which H0H_0 is true, at most a fraction α\alpha of the applications would reject. It is not the probability that H0H_0 is true after a rejection, and not the probability that a particular rejection is wrong.

Limits and common mistakes

Failing to reject is not evidence of no effect; the design may simply have had little power. To claim that two systems are equivalent, fix a margin δ\delta (delta), the largest difference that does not matter, and use an equivalence test. One such test is the procedure of two one-sided tests: it must reject both “the difference is at most −δ-\delta” and “the difference is at least δ\delta”.

Rejecting is not evidence of a large or useful effect. With many units a test rejects for a difference far too small to matter. Report the effect size §11.24 and its confidence interval §11.18.

Rejecting does not show what caused the difference. Random assignment of conditions, as in the example, supports a causal reading. In a comparison without random assignment, confounding §11.10, a third factor that affects both the condition and the outcome, can produce the difference.

The wrong unit gives the wrong null distribution. Treating 10,000 events from 3 sessions as 10,000 independent units makes rejection far too easy; this error is called pseudoreplication §11.6.

Choosing the test after seeing the data breaks the guarantee. Running a one-sided test in whichever direction the data point turns a nominal level α\alpha into a real Type I error rate of 2α2\alpha. Trying several tests and keeping the smallest p-value raises it further.

Questions answered

What null hypothesis, alternative, and sampling design justify the test?

A test is justified when its null distribution, the distribution of the test statistic when there is no effect, follows from the stated null hypothesis together with the way the data were actually sampled or assigned. The justification must therefore name three things, all fixed before the data are seen. The null hypothesis is a precise model of the data without the effect, for example “each run would have the same latency under either build” or “the latencies of both builds come from one distribution”. The alternative names the effect to be detected and its direction, one-sided or two-sided, chosen from the scientific question. The sampling design names the experimental unit and how many units there are, how conditions were assigned to units (at random or not), whether measurements are paired, how measurements are grouped into sessions or devices, and when data collection stops.

The design then selects the test. Random assignment of conditions to units justifies a randomization test, whose null distribution comes from the assignment itself. Independent units whose measurements are normally distributed, or numerous enough that their averages are, justify a t-test. Independent units from continuous distributions justify a rank test such as the Wilcoxon–Mann–Whitney test. Many measurements inside a few sessions or devices justify only a test whose unit is the session or the device. The test is chosen from the question and the design, never by which test gives the smallest p-value.

Related entries: p-value (§11.23), Effect size (§11.24), Statistical power (§11.25), Multiple comparisons (§11.26), False positive and false negative (§1.22), Randomization (§11.8). Further reading: [171, 472, 473].