TExcelTAD
Home › Statistics › Hypothesis Testing

Hypothesis Testing

A hypothesis test is a formal way to decide whether data provide enough evidence against a default assumption. It answers questions like "Does the new fertiliser increase yield?" or "Is this coin fair?" while controlling how often we fool ourselves.

The two hypotheses

  • The null hypothesis H₀ is the default, "nothing special" claim: no effect, no difference, the parameter equals a specific value. For example H₀: p = 0.5 (the coin is fair).
  • The alternative hypothesis H₁ (or Hₐ) is what we look for evidence of: H₁: p ≠ 0.5, p > 0.5 or p < 0.5.

Hypotheses are always about population parameters (μ, p), never about sample statistics: we can see x̄ directly, so there is nothing to test about it.

The direction of H₁ determines the type of test:

Alternative Test Lessons
H₁: parameter > value right-tailed proportion, mean
H₁: parameter < value left-tailed proportion, mean
H₁: parameter ≠ value two-tailed proportion, mean

Choose the direction before looking at the data, based on the research question.

The logic: proof by "that would be surprising"

We temporarily assume H₀ is true and ask: how likely would data at least as extreme as ours be? That probability is the p-value. If it is very small, either something very unusual happened, or H₀ is wrong. We then reject H₀ in favour of H₁.

A friend claims her coin is fair, but in 100 flips you see 61 heads. Let's estimate the p-value by simulating 100,000 experiments with a truly fair coin:

python · try it yourself
import numpy as np

rng = np.random.default_rng(61)
heads = rng.binomial(n=100, p=0.5, size=100_000)     # 100,000 runs of 100 fair flips

observed = 61
as_extreme = np.abs(heads - 50) >= abs(observed - 50)   # two-tailed: 61+ or 39-
print(f"share of fair-coin experiments at least as extreme: {as_extreme.mean():.4f}")
share of fair-coin experiments at least as extreme: 0.0358

The exact answer comes from the binomial distribution:

python · try it yourself
from scipy import stats

result = stats.binomtest(61, n=100, p=0.5, alternative="two-sided")
print(f"exact p-value: {result.pvalue:.4f}")
exact p-value: 0.0352

Only about 3.5% of fair coins would produce a result this lopsided. That is fairly strong evidence against fairness.

Significance level and decision

Before the test you choose a significance level α, the threshold for "surprising enough". The most common choice is α = 0.05; 0.01 and 0.10 are also used.

  • If p-value ≤ α: reject H₀. The result is statistically significant.
  • If p-value > α: fail to reject H₀. The data are compatible with H₀.

"Fail to reject" is not the same as "accept". Absence of evidence is not evidence of absence: maybe the effect exists but the sample was too small to detect it.

An equivalent approach compares the test statistic with a critical value (for example, reject if |z| > 1.96 at α = 0.05, two-tailed). Both approaches always lead to the same decision.

The six steps

  1. State H₀ and H₁.
  2. Choose α.
  3. Check the conditions (random sample, sample size, normality…).
  4. Compute the test statistic (z, t, χ², …).
  5. Find the p-value (or critical value).
  6. Decide and state the conclusion in context.

Two kinds of error

H₀ actually true H₀ actually false
Reject H₀ Type I error (false positive), probability α correct (probability = power, 1 − β)
Fail to reject correct Type II error (false negative), probability β

The significance level α is the Type I error rate: if H₀ is true, a test at α = 0.05 wrongly rejects it 5% of the time. The simulation below runs 10,000 tests on fair coins:

python · try it yourself
import numpy as np
from scipy import stats

rng = np.random.default_rng(5)
false_alarms = 0
for heads in rng.binomial(100, 0.5, size=10_000):
    if stats.binomtest(int(heads), 100, 0.5).pvalue <= 0.05:
        false_alarms += 1
print(f"fair coins declared unfair: {false_alarms / 10_000:.2%}")
fair coins declared unfair: 3.26%

The rate is below 5% (about 3.3%) because the binomial distribution is discrete, so the exact test can't hit 5% precisely and errs on the safe side.

Power

Power is the probability of correctly rejecting a false H₀. It grows with the true effect size, the sample size and α. Suppose the coin really lands heads 60% of the time:

python · try it yourself
import numpy as np
from scipy import stats

rng = np.random.default_rng(6)
for n in [50, 100, 200, 400]:
    heads = rng.binomial(n, 0.60, size=2000)
    rejected = [stats.binomtest(int(h), n, 0.5).pvalue <= 0.05 for h in heads]
    print(f"n={n:3d} flips: power = {np.mean(rejected):.2f}")
n= 50 flips: power = 0.23
n=100 flips: power = 0.47
n=200 flips: power = 0.80
n=400 flips: power = 0.97

With 50 flips we would miss this biased coin most of the time. With 400 flips we almost always catch it. Planning the sample size so that power is high (often 80% or more) is part of designing a good study.

Interpreting p-values correctly

  • The p-value is not the probability that H₀ is true.
  • A small p-value does not mean the effect is large or important. With huge samples, trivial differences become significant.
  • p = 0.049 and p = 0.051 are practically the same evidence. Treat α as a guideline, and report the actual p-value and an effect size or confidence interval.

See Statistical Pitfalls for what happens when these rules are ignored.

Test yourself

What is the p-value?

  1. The probability that H₀ is true
  2. The probability, assuming H₀ is true, of data at least as extreme as observed
  3. The probability that H₁ is true
  4. The chance of a Type II error

A test at α = 0.05 gives p = 0.21. What is the correct conclusion?

  1. Accept H₀: it is proven true
  2. Reject H₀
  3. Fail to reject H₀: the data don't provide enough evidence against it
  4. The test is invalid

Convicting an innocent person corresponds to which error, if H₀ is 'the defendant is innocent'?

  1. Type I error
  2. Type II error
  3. Sampling error
  4. No error