Hypothesis Testing

Statistical Framework

The testing game

Goal.

Use a finite sample to confront a working assumption \(H_0\) (null hypothesis) against a competing one \(H_a\) (alternative), and decide which one the data support.

The engine.

A test statistic \(T(X_1, \dots, X_n)\) that you can always compute, and whose distribution you (approximately) know assuming \(H_0\) is true: the null distribution.

\(\Longrightarrow\) Extreme values of \(t_0\) under \(H_0\) argue in favour of \(H_a\).

Asymmetry

Warning.

The game is not symmetric in the two hypotheses.

  • The whole procedure rests on what happens when \(H_0\) is true (we know the null distribution);
  • It says nothing at all about \(H_a\), which is left completely unspecified.

You only ever calibrate the test under \(H_0\).

Test Statistic

What a statistic must do

A good test statistic \(T(X_1, \dots, X_n)\) should:

  • take real values, computable as soon as you observe the data;
  • be such that, under \(H_0\), small values comfort \(H_0\) while large values raise suspicion in favour of \(H_a\).

Optional but very useful: an accessible (asymptotic) distribution under \(H_0\) in classical assumptions, e.g. normality.

Example: testing a mean

Model.

\(X_1, \dots, X_n\) i.i.d. \(\sim \mathcal{N}(\mu, \sigma^2)\), with \(\sigma\) known. Test \(H_0: \mu = \mu_0\) vs \(H_a: \mu \ne \mu_0\).

Test statistic.

\[ T(X_1, \dots, X_n) := \sqrt{n} \frac{\overline X - \mu_0}{\sigma}, \qquad \overline X := \frac{1}{n}\sum_{i=1}^n X_i . \]

Why it works.

\(\overline X\) is unbiased for \(\mu\), so under \(H_0\) it concentrates near \(\mu_0\) and \(\;T\) stays small; departures push \(T\) to large positive or negative values.

Where the null distribution comes from

Four ways to learn the distribution of \(T\) under \(H_0\):

  • Parametric. A theoretical distribution, from distributional assumptions on the data.
  • Asymptotic. The theoretical distribution of \(T\) as \(n \to \infty\) (large sample).
  • Bootstrap. Approximate the distribution by resampling, in a large-sample regime.
  • Permutation. Approximate it by permuting labels, in a low-sample regime.

One-sided vs two-sided

What raises suspicion.

Skewing what large means changes which tail(s) of the null distribution matter:

  • one-sided: only the right tail (\(\mu > \mu_0\)) or the left tail (\(\mu < \mu_0\));
  • two-sided: both tails (\(\mu \ne \mu_0\)).

We always reject \(H_0\) only when \(t_0\) lands in the tail(s) that argue in favour of \(H_a\).

Right tail: \(H_a: \mu > \mu_0\)

\[ T(X_1, \dots, X_n) := \sqrt{n} \frac{\overline X - \mu_0}{\sigma} \]

Large positive \(T\) points to \(\mu\) truly above \(\mu_0\).

\(\Longrightarrow\) Reject only in the right tail.

Left tail: \(H_a: \mu < \mu_0\)

\[ T(X_1, \dots, X_n) := \sqrt{n} \frac{\overline X - \mu_0}{\sigma} \]

Large negative \(T\) points to \(\mu\) truly below \(\mu_0\).

\(\Longrightarrow\) Reject only in the left tail.

Two tails: \(H_a: \mu \ne \mu_0\)

\[ T(X_1, \dots, X_n) := \sqrt{n} \frac{\overline X - \mu_0}{\sigma} \]

Extreme \(T\) in either direction raises suspicion.

\(\Longrightarrow\) Reject in both tails.

Making a decision

You can be wrong

Every decision can miss, in two ways.

  • Rejecting \(H_0\) wrongly: Type I error;
  • Failing to reject a false \(H_0\): Type II error.
Decision \(H_0\) is true \(H_a\) is true
Do not reject \(H_0\) Well done Type II error
Reject \(H_0\) Type I error Well done

Remember.

Only the Type I rate is controllable, because it is a probability computed assuming \(H_0\) is true, the very frame we adopted.

Significance level \(\alpha\)

Control the Type I error.

Force \(\mathbb{P}_{H_0}(\text{reject } H_0) \le \alpha\), where the bound \(\alpha\) is the significance level.

For a right-tailed test, “reject \(H_0\)” is the event \(T > x\), so the rule becomes

\[ \mathbb{P}_{H_0}(T > x) \le \alpha \iff F_T^{(H_0)}(x) \ge 1 - \alpha . \]

\(\Longrightarrow\) Reject when \(t_0 \ge q_{1-\alpha}\), the order \((1-\alpha)\) quantile of the null distribution.

p-value

Definition.

The p-value is the probability, assuming \(H_0\) true, of observing the data we got or data even more in favour of \(H_a\) (here with \(t_0\) the observed statistic):

\[ p = \begin{cases} \mathbb{P}_{H_0}(T > t_0) & \text{right-tailed} \\ \mathbb{P}_{H_0}(T < t_0) & \text{left-tailed} \\ 2\min\!\big(\mathbb{P}_{H_0}(T > t_0),\, \mathbb{P}_{H_0}(T < t_0)\big) & \text{two-tailed} \end{cases} \]

Decision.

\(\Longrightarrow\) Reject \(H_0\) when \(p \le \alpha\).

The decision at a glance

Reject \(H_0\) when \(t_0\) is extreme enough that \(p \le \alpha\).

Statistical power

Definition.

The probability of correctly rejecting \(H_0\), i.e. of rejecting when the alternative is right:

\[ \gamma := \mathbb{P}_{H_a}(\text{Reject } H_0) = 1 - \beta . \]

Why it matters.

  • It compares competing testing procedures;
  • In trials (clinical, crash tests), it calibrates the sample size needed for a target power.

Caveat.

Computing \(\gamma\) forces you to step under \(H_a\), which is often only vaguely specified, so power studies explore how it behaves across plausible alternatives.

Compliance testing

Testing the mean, \(\sigma\) known

Model. \(X_1, \dots, X_n\) i.i.d. \(\sim \mathcal{N}(\mu, \sigma^2)\). Test. \(H_0: \mu = \mu_0\) vs \(H_a: \mu \ne \mu_0\) (or \(>\), or \(<\)).

\[ Z_n = \sqrt{n} \frac{\overline X_n - \mu_0}{\sigma} \stackrel{H_0}{\sim} \mathcal{N}(0, 1) \]

Check. \(\sigma\) must be known to compute \(Z_n\); the generic R helper is t.test().

Testing the mean, \(\sigma\) unknown

Workaround. Plug in the empirical standard deviation \(S_{n-1}\) for \(\sigma\).

\[ T_n = \frac{\overline X_n - \mu_0}{\sqrt{S_{n-1}^2 / n}} \stackrel{H_0}{\sim} \mathcal{S}tudent(n-1), \qquad S_{n-1}^2 := \frac{1}{n-1}\sum_{i=1}^n (X_i - \overline X)^2 \]

R function. t.test()

\(\Longrightarrow\) Knowing \(\mu\) vs \(\sigma\) swaps a \(\mathcal{N}(0,1)\) for a Student law with \(n-1\) degrees of freedom.

Testing the variance

Model. \(X_1, \dots, X_n\) i.i.d. \(\sim \mathcal{N}(\mu, \sigma^2)\). Test. \(H_0: \sigma^2 = \sigma_0^2\) vs \(H_a: \sigma^2 \ne \sigma_0^2\) (or \(>\), or \(<\)).

Candidate statistic.

\[ U_n = \sum_{i=1}^n \frac{(X_i - \mu)^2}{\sigma_0^2} \]

Hitch. \(\mu\) is unknown, so we cannot compute \(U_n\) as written.

Testing the variance, \(\mu\) known

Workaround. When \(\mu\) is known, the statistic is computable and has a \(\chi^2\) law:

\[ U_n \stackrel{H_0}{\sim} \chi^2(n) \]

R function. none (built from scratch).

Testing the variance, \(\mu\) unknown

Workaround. Plug in the empirical mean \(\overline X_n\) for \(\mu\).

\[ U_{n-1} = \sum_{i=1}^n \frac{(X_i - \overline X_n)^2}{\sigma_0^2} \stackrel{H_0}{\sim} \chi^2(n-1) \]

R function. none (built from scratch).

Testing a proportion

Model. \(X_1, \dots, X_n\) i.i.d. \(\sim \mathcal{B}e(p)\); \(X_i\) records whether individual \(i\) has the feature.

Test. \(H_0: p = p_0\) vs \(H_a: p \ne p_0\) (or \(>\), or \(<\)).

\[ B_n = \sum_{i=1}^n X_i \sim \mathcal{B}inom(n, p_0) \quad \text{under } H_0 \]

R function. binom.test()

Two-Sample Testing

Framework

Compare the distributions behind two independent samples:

\[ X_1, \dots, X_{n_X} \sim \mathcal{N}(\mu_X, \sigma_X^2), \qquad Y_1, \dots, Y_{n_Y} \sim \mathcal{N}(\mu_Y, \sigma_Y^2) \]

with the two samples statistically independent.

Two questions: equal variances? equal means?

Testing variance equality

Test. \(H_0: \sigma_X^2 = \sigma_Y^2\) vs \(H_a: \sigma_X^2 \ne \sigma_Y^2\) (or \(>\), or \(<\)).

\[ V_n = \frac{S_X^2}{S_Y^2} \stackrel{H_0}{\sim} \mathcal{F}isher(n_X - 1, \, n_Y - 1) \]

R function. var.test()

Validity. relies on normality and independence within and between samples.

Testing mean differences

Test. \(H_0: \mu_X = \mu_Y\) vs \(H_a: \mu_X \ne \mu_Y\) (or \(>\), or \(<\)), with \(\delta := \mu_X - \mu_Y\) and \(\delta_0\) the value under \(H_0\).

R function. t.test()

The form of the statistic depends on whether the variances are equal.

Mean differences, equal variances

With \(\sigma_X^2 = \sigma_Y^2\), pool the two sample variances:

\[ T_n = \frac{(\overline X_n - \overline Y_n) - \delta_0}{\sqrt{S_\mathrm{pooled}^2 \left(\tfrac{1}{n_X} + \tfrac{1}{n_Y}\right)}} \stackrel{H_0}{\sim} \mathcal{S}tudent(n_X + n_Y - 2), \qquad S_\mathrm{pooled}^2 := \frac{(n_X-1)S_X^2 + (n_Y-1)S_Y^2}{n_X + n_Y - 2} . \]

Mean differences, unequal variances (Welch)

With \(\sigma_X^2 \ne \sigma_Y^2\), keep the variances separate:

\[ T_n = \frac{(\overline X_n - \overline Y_n) - \delta_0}{\sqrt{\dfrac{S_X^2}{n_X} + \dfrac{S_Y^2}{n_Y}}} \]

The Welch degrees of freedom

The null distribution is a Student law with the Welch degrees of freedom:

\[ T_n \stackrel{H_0}{\sim} \mathcal{S}tudent(m), \qquad m = \frac{\left( \dfrac{S_X^2}{n_X} + \dfrac{S_Y^2}{n_Y} \right)^2}{ \dfrac{(S_X^2 / n_X)^2}{n_X - 1} + \dfrac{(S_Y^2 / n_Y)^2}{n_Y - 1} } \]

Adequacy testing

Goal

Adequacy tests check whether the observed data are coherent with a given probability distribution \(F_0\).

Shapiro-Wilk test

Test. \(H_0: (X_1, \dots, X_n) \stackrel{i.i.d.}{\sim} \mathcal{N}(\mu, \sigma^2)\) vs \(H_a:\ \text{not normal}\).

\[ T_n = \frac{\left( \sum_i a_i X_{(i)} \right)^2}{\sum_i (X_i - \overline X_n)^2} \stackrel{H_0}{\sim} \mathcal{W}ilks(n) \]

R function. shapiro.test() — needs \(3 \le n \le 5000\).

Kolmogorov-Smirnov test

Test. \(H_0: X \sim F_0\) vs \(H_a:\ \text{not } F_0\).

\[ T_n = \sup_{x \in \mathbb R} \left| F_n(x) - F(x) \right| \]

Asymptotic null distribution.

\[ \sqrt{n}\, T_n \xrightarrow{\mathcal{L}} \sup_{x \in \mathbb R} \left| B(F(x)) \right|, \qquad B\ \text{a Brownian bridge} \]

R function. ks.test() — asymptotic.

\(\chi^2\) adequacy for one categorical variable

Idea. Compare observed class frequencies with the theoretical ones under \(F_0\) (\(k\) classes, \(f_i = n_i/n\)).

\[ U_n = n \sum_{i=1}^k \frac{(f_i - f_{0i})^2}{f_{0i}} \xrightarrow{\mathcal{L}} \chi^2_{k-1-\ell} ,\qquad \ell = \#\text{parameters estimated for } F_0 \]

R function. chisq.test() — needs \(n_i \ge 5\) typically.

\(\chi^2\) independence between two categorical variables

Test. \(H_0: \nu = \mu \otimes \lambda\) (independence) vs \(H_a:\ \text{dependence}\), where \(\nu\) is the joint law and \(\mu, \lambda\) the marginals.

Statistic (with \(N_{\ell, \cdot}\), \(N_{\cdot, h}\), \(N_{\ell,h}\) the row/column/cell counts).

\[ U_n = n \sum_{\ell=1}^s \sum_{h=1}^r \frac{\left( \frac{N_{\ell,h}}{n} - \frac{N_{\ell,\cdot}}{n}\frac{N_{\cdot,h}}{n} \right)^2}{\frac{N_{\ell,\cdot}}{n}\frac{N_{\cdot,h}}{n}} \xrightarrow{\mathcal{L}} \chi^2\big((s-1)(r-1)\big) \]

R function. chisq.test() — asymptotic; typically \(n \gg 30\) and \(N_{\ell,h} \gg 5\).

Your turn

  • Download the lab 10-Hypothesis-Testing-Exercises.qmd from the course website.
  • Run the proposed tests with R and interpret each p-value in one line.