---
title: Hypothesis tests
heading: Deciding whether a difference is real or just noise
eyebrow: A11 · Lesson 38 · Hypothesis tests
xp: 40
---

::: hook
A new soldering process gives 12 defects in 1000 boards, against the old rate of 2%. Did the process improve, or did this batch just get lucky? An amplifier's offset reads 1.1 mV averaged over 16 readings with 2 mV of noise each. Is the offset really nonzero? A hypothesis test turns these into a calculation: how often would noise alone produce a result this far from "no change"? It's the same reasoning a radar or a receiver uses to decide whether a signal is there, with the same two ways to be wrong.
:::

::: play
- The left curve is the distribution of the average of $n$ readings if nothing has changed (the null hypothesis, mean 0). The right curve is the same if the true mean has shifted by $d$. Each reading has noise σ = 1.
- You declare "changed" when the average exceeds the threshold $c$. The shaded tail of the left curve is the false alarm rate; the part of the right curve below $c$ is what you miss.
- Raise $n$. Both curves narrow, so you can lower the false alarms and the misses at the same time.
:::
:::

```sim plot
view: graph
x: m = -1.5..2.5
y: p = 0..2.4
slider: n = 1..36 step 1 = 9
slider: c = 0..1.5 step 0.05 = 0.55
slider: d = 0..2 step 0.05 = 1
plot: exp(-m^2*n/2)*sqrt(n)/sqrt(2*pi) | label: no change | fill: c..2.5
plot: exp(-(m - d)^2*n/2)*sqrt(n)/sqrt(2*pi) | label: shifted by d | color: ch2 | fill: -1.5..c
marker: x = c | label: threshold
readout: false alarm rate (α) = Q(c*sqrt(n)) | digits: 3
readout: chance of detecting the shift (power) = Q((c - d)*sqrt(n)) | digits: 3
```

::: predict
## Sixteen readings with 2 mV of noise each average 1.1 mV. If the true offset were zero, how often would the average land at least this far from zero, either way?

- [ ] About 50%
- [ ] About 14%
- [x] About 3%
- [ ] Never

::: reveal
About 3%. The average has standard error $\frac{2}{\sqrt{16}} = 0.5$ mV, so 1.1 mV is $z = 2.2$ standard errors out, and $2Q(2.2) = 0.028$. Noise alone would rarely do this, so the offset is probably real. That 2.8% is the **p-value** of the test.
:::
:::

::: nameit
## The setup

- The **null hypothesis** $H_0$ is "no effect": the offset is zero, the defect rate is unchanged, there's no signal.
- The **alternative** $H_1$ is the effect you're looking for.
- A **test statistic** summarizes the data, usually as a $z$-score: how many standard errors the estimate sits from the value $H_0$ predicts.

For a mean with known noise σ and $n$ readings:

$$z = \frac{\bar x - \mu_0}{\sigma/\sqrt n}$$

For a proportion $\hat p = k/n$ against a claimed $p_0$, with the Gaussian approximation:

$$z = \frac{\hat p - p_0}{\sqrt{p_0(1 - p_0)/n}}$$

## p-values and significance

The **p-value** is the chance, if $H_0$ were true, of a result at least as extreme as the one seen. A **one-sided** test counts one tail, $Q(z)$; a **two-sided** test counts both, $2Q(|z|)$. You reject $H_0$ when the p-value is below a **significance level** $\alpha$ chosen in advance, often 5% or 1%. Equivalently, reject when $z$ passes a threshold: 1.645 one-sided or 1.96 two-sided at 5%.

The p-value is not the chance that $H_0$ is true. That would need a prior and Bayes' rule (lesson 5).

## Two kinds of error

| | $H_0$ true | $H_1$ true |
|---|---|---|
| reject $H_0$ | **type I error** (false alarm), rate $\alpha$ | correct; rate is the **power** $1 - \beta$ |
| keep $H_0$ | correct | **type II error** (miss), rate $\beta$ |

For a fixed amount of data, moving the threshold trades one error for the other. Only more data, or a bigger effect, reduces both. For a one-sided test of a mean shift $d$ at level $\alpha$:

$$\text{power} = Q\left(z_\alpha - \frac{d}{\sigma/\sqrt n}\right)$$

Lesson 42 uses exactly this tradeoff to set a receiver's decision threshold.
:::

::: example
## Is the amplifier's offset nonzero? Sixteen readings average 1.1 mV, with 2 mV of noise each.

::: solution
1. **Test statistic.**
   $$\begin{aligned} z &= \frac{1.1 - 0}{2/\sqrt{16}} \\ &= 2.2 \end{aligned}$$
2. **p-value.** Two-sided, since an offset could have either sign:
   $$\begin{aligned} p &= 2Q(2.2) \\ &= 0.028 \end{aligned}$$
3. **Decide.** That's below 5%, so reject "zero offset" at the 5% level, though not at 1%.

**Answer:** yes at the 5% level ($p = 0.028$), though not at the 1% level.
:::
:::

::: example
## The old process had a 2% defect rate. A new one gives 12 defects in 1000 boards. Is it better?

::: solution
1. **Estimate.**
   $$\hat p = 0.012$$
2. **Standard error under $H_0$.**
   $$\sqrt{0.02 \times 0.98/1000} = 0.00443$$
3. **Test statistic.**
   $$\begin{aligned} z &= \frac{0.012 - 0.02}{0.00443} \\ &= -1.81 \end{aligned}$$
4. **p-value.** The question is one-sided (better means lower), so
   $$\begin{aligned} p &= Q(1.81) \\ &= 0.035 \end{aligned}$$
5. **Decide.** Significant at 5% but not at 1%: promising, and worth a larger trial before retooling the line.

**Answer:** probably: $p = 0.035$ is significant at 5% but not at 1%, so it's worth a larger trial.
:::
:::

::: example
## A coin gives 60 heads in 100 flips. Is that evidence it's biased?

::: solution
1. **Under a fair coin.** The count has mean 50 and standard deviation
   $$\sqrt{100 \times 0.25} = 5$$
   so 60 is $z = 2$ standard deviations out.
2. **p-value.** Two-sided:
   $$2Q(2) = 0.046$$
   just under the conventional 5% line.
3. **Decide.** It suggests a bias without proving it; a few hundred more flips would settle it.

**Answer:** it's weak evidence of bias: $p = 0.046$, just under the 5% line.
:::
:::

::: check
given: xb = 0.8 | 1.5; s = 2 | 4; n = 16 | 25
answer: xb/(s/sqrt(n))
tolerance: 0.5%

## {n} readings with noise {s} mV each average {xb} mV. What is the $z$-statistic for "the true mean is zero"?

::: right
Right. $\frac{\bar x}{\sigma/\sqrt n}$ = {answer}.
:::

::: misses
- [xb/s] Use the standard error of the average, $\sigma/\sqrt n$.
- [any] Not yet. $z = (\bar x - \mu_0)/(\sigma/\sqrt n)$.
:::
:::

::: check
given: z = 1.8 | 2.4 | 2.9
answer: 2*Q(z)
tolerance: 1%

## A two-sided test gives $z$ = {z}. What is the p-value?

::: right
Right. Both tails: $2Q(z)$ = {answer}.
:::

::: misses
- [Q(z)] That's one tail. A two-sided test counts extremes in both directions.
- [any] Not yet. $2Q(|z|)$.
:::
:::

::: check
given: p0 = 0.02 | 0.05; n = 1000 | 2000; f = 0.6 | 0.7
answer: Q((p0 - f*p0)/sqrt(p0*(1 - p0)/n))
tolerance: 1%

## A defect rate was {p0}. A new process shows a rate of {f} times that in {n} boards. What is the one-sided p-value for "no improvement"?

::: right
Right. $z = \frac{p_0 - \hat p}{\sqrt{p_0(1 - p_0)/n}}$ and $p = Q(z)$ = {answer}.
:::

::: misses
- [2*Q((p0 - f*p0)/sqrt(p0*(1 - p0)/n))] The question asks only whether it got better: one tail.
- [any] Not yet. Find the standard error under $H_0$, then $z$, then $Q(z)$.
:::
:::

::: check
given: s = 2 | 5; n = 4 | 25
answer: 1.645*s/sqrt(n)
unit: mV
tolerance: 0.5%

## Each reading has {s} mV of noise. Above what average of {n} readings should you declare "positive offset", for a 5% one-sided false alarm rate? (The 5% point of the Gaussian is $z = 1.645$.)

::: right
Right. $1.645\frac{\sigma}{\sqrt n}$ = {answer}.
:::

::: misses
- [1.645*s] The threshold applies to the average, which has standard error $\sigma/\sqrt n$.
- [1.96*s/sqrt(n)] 1.96 is the two-sided 5% point.
- [any] Not yet. $\mu_0 + z_\alpha\sigma/\sqrt n$.
:::
:::

::: check
given: s = 2 | 4; n = 16 | 36; d = 1 | 1.5
answer: Q(1.645 - d/(s/sqrt(n)))
tolerance: 1%

## Same kind of test: noise {s} mV per reading, {n} readings, one-sided at 5%. The true offset is {d} mV. What is the chance the test detects it?

::: right
Right. The power, $Q(1.645 - \frac{d}{\sigma/\sqrt n})$ = {answer}.
:::

::: misses
- [Q(1.645)] That's the false alarm rate, when there's no offset.
- [any] Not yet. Where does the threshold sit relative to the shifted distribution?
:::
:::

::: check
kind: choice

## A test gives a p-value of 0.03. What does that mean?

- [ ] There's a 3% chance the null hypothesis is true
- [x] If the null hypothesis were true, results this extreme would happen 3% of the time
- [ ] There's a 97% chance the effect is real
- [ ] The effect is large

::: right
Right. It's a statement about the data assuming $H_0$, not about $H_0$ itself.
:::

::: misses
- [1] That's a Bayesian posterior, which needs a prior.
- [3] Same mistake turned around.
- [4] A tiny effect with lots of data can give a small p-value; the p-value says nothing about size.
:::
:::

::: check
kind: parts
given: h = 56 | 60 | 62

## A coin gives {h} heads in 100 flips.

::: part
answer: (h - 50)/5
tolerance: 0.5%

### How many standard deviations is that from 50?

::: misses
- [(h - 50)/50] The standard deviation is $\sqrt{100 \times 0.25} = 5$.
- [any] $z = \frac{h - 50}{5}$.
:::
:::

::: part
answer: 2*Q((h - 50)/5)
tolerance: 0.5%

### What is the two-sided p-value?

::: misses
- [Q((h - 50)/5)] Both tails count.
- [any] $2Q(z)$.
:::
:::
:::

::: check
kind: derivation
vars: m = -2..2, s = 0.5..5, n = 4..100

## $n$ readings with noise $s$ average $m$. Test whether the true mean is 0.

::: step
answer: m/(s/sqrt(n))

### What is the $z$ statistic?
:::

::: right
Right. The average divided by its standard error.
:::
:::

::: check
kind: python

## Write `p_value(mean, sigma, n)` returning the two-sided p-value for testing a zero true mean from `n` readings with noise `sigma` and average `mean`. Set `p1 = p_value(1.1, 2, 16)`.

```python starter
import math

def p_value(mean, sigma, n):
    return 1.0

p1 = p_value(1.1, 2, 16)
print(p1)
```

```python solution
import math

def p_value(mean, sigma, n):
    z = abs(mean) / (sigma / math.sqrt(n))
    return math.erfc(z / math.sqrt(2))

p1 = p_value(1.1, 2, 16)
print(p1)
```

```python tests
assert abs(p1 - 0.027807) < 1e-5, f"z = 2.2 gives a two-sided p of 0.0278; got {p1}."
assert abs(p_value(0, 1, 10) - 1) < 1e-12, "A zero average gives p = 1."
```

::: right
Right. $z = 2.2$, $p = 0.028$: an offset of 1.1 mV is unlikely to be noise alone.
:::
:::

::: explain
## Why can't you reduce both false alarms and misses just by choosing a better threshold?

::: model
The threshold divides the possible results into "declare a change" and "don't". Raising it pushes more of the no-change distribution below it, cutting false alarms, but also pushes more of the changed distribution below it, adding misses. Lowering it does the opposite. With fixed data the two distributions overlap by a fixed amount, and the threshold only decides where the overlap's errors go. To cut both you must shrink the overlap: average more readings so each distribution narrows, or look for a bigger effect.
:::
:::

::: practice

::: check
given: z = 1.5 | 2 | 2.5
answer: Q(z)
tolerance: 1%

## A one-sided test gives $z$ = {z}. What is the p-value?

::: misses
- [2*Q(z)] One tail only.
- [any] $Q(z)$.
:::
:::

::: check
given: s = 3 | 6; n = 9 | 36
answer: s/sqrt(n)
tolerance: 0.5%

## What is the standard error of an average of {n} readings with noise {s} each?

::: misses
- [any] $\sigma/\sqrt n$.
:::
:::

::: check
given: k = 2..10 int
answer: k^2
tolerance: 0.5%

## To make the smallest shift a test can detect {k} times smaller, at the same α and power, by what factor must the number of readings grow?

::: misses
- [k] The detectable shift scales as $1/\sqrt n$.
- [any] $n$ grows as the square.
:::
:::
:::
