Hypothesis Testing

Concepts

Null Hypothesis H0 and Alternative Hypothesis H1

Tip

判决要么是"**reject 拒绝 H0 **",要么是"fail to reject 未能拒绝 H0 ",而不是"接受 H0"。

Example

Let X equal the breaking strength of a steel bar.
If the bar is manufactured by process I, X∼N(50,36).
It is hoped that if process II (a new process) is used, X∼N(55,36).
Given a large number of steel bars manufactured by process II, how could we test whether the five-unit increase in the mean breaking strength was realized?

Judgement: 5-unit increase in μ or not

H0 H1 Types 直觉解释
μ=50 μ=55 Simple vs Simple 只关心两个精准数值
μ=55 μ>55 单侧 One-sided(右尾) 只关心"有没有变大"
μ=55 μ<55 单侧 One-sided(左尾) 只关心"有没有变小"
μ=55 μ≠55 双侧 Two-sided(双尾) 变大变小都算异常
μ≤55 μ>55 Composite vs Composite 覆盖全部数值

Critical Region C and Test Statistic T

The space of the sample D:={(x1,x2,…,xn)∣xi∈SX, i=1,2,⋯,n}

Tip

D: 这次抽样所有可能出现的结果的 set

Hypothesis testing is based on critical region C, which is subset of D.

Tip

临界域 C 是 D 的一个子集: 只要实际抽到的样本落在 C 里,就拒绝原假设 H0(null hypothesis);没落在里面,就不拒绝。

样本∈C⇒拒绝 H0样本∉C⇒不拒绝 H0

Test statistic T is quantity derived from the sample for hypothesis testing.

Tip

T 是一个"摘要函数",把 n 个数据压缩成一个数。
类比: 想知道一个班学得好不好,不用逐一报 50 个人的分数,只看"平均分"这一个数就够了。平均分就是一个统计量 statistic。

The critical region C is usually specified in terms of test statistic T.

In the #Example,

Errors in Hypothesis Testing

H0 true H1 true
Reject H0 Type I error Correct
Fail to Reject H0 Correct Type II Error
α=P(Type I error)=P(T∈C;H0) β=P(Type II error)=P(T∉C;H1)
真的没病(H0) 真的有病(H1)
报告"阳性"(拒绝 H0) 误报(false positive)= 第一类错误 命中
报告"阴性"(不拒绝 H0) 命中 漏报(false negative)= 第二类错误

Example

X1,…,Xn∼i.i.d.N(μ,36),n=16
Take critical region C={x¯≥53}.
What is the probability of type I error and type II error in the following test

H0:μ=50againstH1:μ=55
X¯∼N(μ,σ2n)∼N(μ,3616)∼N(μ,2.25)α=P(X¯≥53;μ=50)=P(Z≥53−502.25)=P(Z≥2)≈0.0228
Tip

哪怕真实均值就是 50,单凭抽样波动,也有约 2.28% 的概率 x¯ 在 53 以上,被我们冤枉。

β=P(X¯<53;μ=55)=P(Z<53−552.25)=P(Z<−1.333)≈0.0912
Tip

即便真实均值是 55,仍有约 9.12% 的概率没跨过 53 而漏报

Error in Hypothesis Testing.png

Note

By changing critical region C, α and β change in different direction (i.e. α↑β↓).
Hence, α and β cannot be reduced at the same time

Significance Level

We usually fix the probability of type I error α, and then determine the corresponding critical region.
In this scenario, the value of α is called significance level of the test.

Tip

在先确定 type I error α 的值,再决定 critical region 的情景下,α 便称为 significance level

Example

X1,…,Xn∼i.i.d.N(μ,36),n=16
Take α=0.05
Find the critical region

Pr(Z≥c−502.25)=0.05c−502.25=z0.05c=52.47
Note

Pr(Z≥zα)=α

Note

注意权衡(Trade-off):α 调小 → 拒绝域变小 → 第一类错误减少,但第二类错误 β(放过坏人)会增大。α 和 β 像跷跷板,样本量 n 不变时无法同时减小。

常见误区提醒

  • α=0.05 不是"H0 有 5% 的概率为真",而是"假如 H0 为真,我们错误拒绝它的概率是 5%"。
  • z0.05 的记号指右尾面积为 0.05,查表时别和 Φ(z)=0.05(左尾)搞混。因为 Φ(1.645)=0.95,所以右尾才是 0.05。
  • 拒绝域方向由 H1 决定:H1:μ>50 → 右尾;H1:μ<50 → 左尾;H1:μ≠50 → 双尾(α 平分两边)。
    Normal distribution in left and right tail.png

p-value: Observed Significance Level

p-value: If H0 is true, the probability of observing a more extreme test statistic (favour H1)
Smaller p-value indicates the data is more unlikely coming from H0 and H1 is perhaps better retained

Tip

想象一个场景:你抛一枚硬币 100 次,得到 90 次正面。你会怀疑硬币是不是被做手脚了(H0: 硬币公平 vs H1: 硬币不公平)。

  • p-value = 0.0001 的意思是:如果硬币真的公平,100 次里得到 90 次正面(或更极端)的概率只有 0.01%。
  • 这个概率太小了,所以我们有理由怀疑硬币不公平。

H0 is rejected if p-value ≤α

Example

Following the same example above (σ2=36,n=16,α=0.05).
Given observed test statistics x¯=56.

Pr(Z≥56−502.25)=Pr(Z≥4)=0.00003≤α∴H0 is rejected Pr(Z≥56−552.25)=0.2525≥α Pr(Z≤56−552.25)=0.7475≥α Pr(Z≥56−552.25)+Pr(Z≤54−552.25)=P(Z≥0.667)+P(Z≤−0.667)≈0.505≥α

Hypothesis Testing for Normal Mean

There are 3 approaches to hypothesis testing

Case 1: σ2 Known

Consider test statistic

T:=X¯−μ0σn

Under H0:μ=μ0, T∼N(0,1)

Tip

  1. μ0 只是一个已知的、假设的数值。
  2. 当 T 被定义为 X¯−μ0σn,由于 μ 并未假设为 μ0,因此 T≁N(0,1)。
  3. 直到题目给出 "Under H0:μ=μ0",假定 μ 为 μ0 时,T 才服从 N(0,1)。

Once the data x1,⋯,xn is observed, we can compute

z=x¯−μ0σn
Tip

z is observed value of T

Important

  • 大写 = 随机变量(random variable)
  • 小写 = 观测值(observed value / realization)

For the one-sided test H0:μ=μ0, H1:μ>μ0

p=Pr(T>z;H0)=Pr(Z>z)
Tip

这三种 approach 都是等价的

For one-sided test H0:μ=μ0, H1:μ<μ0

p=Pr(T<z;H0)=Pr(Z<z)

Reject H0 at significance level α when either

For two-sided test H0:μ=μ0, H1:μ≠μ0

p=Pr(|T|>|z|;H0)=Pr(Z>|z|)+Pr(Z<−|z|)=2Pr(Z>|z|)

Reject H0 at significance level α when either

Case 2: σ2 Unknown

When σ2 is unknown, replace standard normal distribution by t-distribution with degree of freedom (n−1) and σ2 by S2 in #Case 1 $ sigma 2$ Known

Consider test statistic

T:=X¯−μ0Sn

Under H0:μ=μ0, T∼t(n−1)
Once data x1,⋯,xn are observed, we can compute

t=x¯−μ0sn

For the one-sided test H0:μ=μ0, H1:μ>μ0

p=Pr(T>t;H0)=Pr(T>t;T∼t(n−1))

Reject H0 at significance level α when either

For the one-sided test H0:μ=μ0, H1:μ<μ0

p=Pr(T<t;T∼t(n−1))

Reject H0 at significance level α when either

For the two-sided test H0:μ=μ0, H1:μ≠μ0

p=Pr(|T|>|t|;H0)=Pr(T>|t|;T∼t(n−1))+Pr(T<−|t|;T∼t(n−1))=2Pr(T>|t|;T∼t(n−1))

Reject H0 at significance level α when either

Example

Two-sided Test

Let X (in millimeters) equal the growth in 15 days of a tumor induced in a mouse.
Assume that the distribution of X is N(μ,σ2)
Test the null hypothesis H0:μ=4.0 against the two-sided alternative hypothesis H1:μ≠4.0
Observe that n=9 observations with x¯=4.3,s=1.2 and set significance level of α=0.10

t=4.3−41.29=0.75t0.102(9−1)=t0.05(8)=1.86p-value approach: p=2Pr(T>0.75;T∼t(8))=0.4747>0.1Critical region approach: |t|=0.75≤1.86Confidence interval approach: 4.3±1.86(1.29)=[3.556,5.044]4∈[3.556,5.044]

Hence, we fail to reject H0 at 10% significance level

One-sided Test

Suppose that we have n=25,x¯=308.8,s=115.15
Test H0:μ=500 against H1:μ<500, given significance level α=0.01

t=308.8−500115.1525=−8.30t0.01(24)=2.492p-value approach: p=Pr(T<−8.30;T∼t(24))=8.17×10−9<0.01Critical region approach: t=−8.30<−2.492Confidence interval approach: The 99% one-sided CI is(−∞,308.8+2.492×115.1525)=(−∞,366.191)

Hypothesis testing for one mean.png

Test Equality of Two Normal Means

Settings

Pooled t-test

Consider the pooled t-test statistic

T:=X¯−Y¯Sp1n+1m

The pooled variance Sp2 is

Sp2=(n−1)SX2+(m−1)SY2n+m−2

Under H0:μX=μY, T∼t(n+m−2)

Once data x1,⋯,xn and y1,⋯,ym is observed, we can compute

t=x¯−y¯sp(1n+1m)

For one-sided test H0:μX=μY versus H1:μX>μY
p-value is

p=Pr(T>t;H0)=Pr(T>t;T∼t(n+m−2))

Decision rules: Reject H0 at significance level α when either

For one-sided test H0:μX=μY versus H1:μX<μY
p-value is

p=Pr(T<t;H0)=Pr(T<t;T∼t(n+m−2))

Decision rules: Reject H0 at significance level α when either

For two-sided test H0:μX=μY versus H1:μX≠μY
p-value is

p=Pr(|T|>|t|;H0)=2Pr(T>|t|;T∼t(n+m−2))

Decision rules: Reject H0 at significance level α when either

Note

(x¯−y¯−tα2sp1n+1m,x¯−y¯+tα2sp1n+1m)≡(x¯−y¯)±tα2sp1n+1m

It is just a two-sided pooled t-interval (ref. to Confidence Interval#Case 1 Pooled t-interval)

Example

Suppose X∼N(μX,σ2) and Y∼N(μY,σ2).
Observe that

n=12,x¯=1076.75,sX2=29.30m=12,y¯=1072.33,sY2=26.24

We would like to test H0:μX=μY versus H1:μX≠μY at significance level α=0.10

Compute test statistic

t=1076.75−1072.3311(29.3)+11(26.24)12+12−2112+112=2.05t0.102(12+12−2)=t0.05(22)=1.717<|t|

Hence, reject H0 at 10% significant level.

Welch's t-test

Consider the Welch's t-test statistic

T=X¯−Y¯SX2n+SY2m

Under H0, T∼t(r) approximately with

r=⌊(SX2n+SY2m)21n−1(SX2n)2+1m−1(SY2m)2⌋

Once data x1,⋯,xn and y1,⋯,ym, we can compute

t=x¯−y¯sX2n+sY2m

For two-sided test H0:μX=μY versus H1:μX≠μY
p-value is

p=Pr(|T|>|t|;H0)=2Pr(T>|t|;T∼t(r))

Decision rules: Reject H0 at significance level α when either

Note

(x¯−y¯−tα2(r)sX2n+sY2m,x¯−y¯+tα2(r)sX2n+sY2m)≡x¯−y¯±tα2(r)sX2n+sY2m

It is just the Welch's t-interval.

Paired t-test

Consider the following two-sided test

H0:μX=μY,H1:μX≠μY

Let Di:=Xi−Yi, for i=1,2,⋯,n
Consider the paired statistic

T:=D¯SDn=X¯−Y¯SDn

Under H0:μX=μY, T∼t(n−1)

Note

Paired t-test(配对 t 检验)的核心思想只有一句话——“把两列数据相减,变成一列数据,然后对这一列差值做单样本 t 检验”

  1. 假设 H0:μX=μY
  2. 造一列差值 Di=Xi−Yi
  3. 用差值的均值 (mean)除以差值的标准误 SDn(standard error);其中 SD 是差值的样本标准差(standard deviation)。其中 SD 是差值的样本标准差。得到 T,它在 H0 下服从 t(n−1)。

Once data x1,⋯,xn and y1,⋯,yn are observed, we can compute

t=x¯−y¯sDn

p-value is

p=Pr(|T|>|t|;H0)=2Pr(T>|t|;T∼t(n−1))

Decision rules: Reject H0 at significance level α when either

Note

Same as previous interval,

(x¯−y¯−tα2(n−1)sDn,x¯−y¯+tα2(n−1)sDn)≡x¯−y¯±tα2(n−1)sDn

It is just the paired t-interval

Example

24 girls in the 9th and 10th grades were put on an ultraheavy rope-jumping program.
Someone thought that such a program would increase their speed in the 40-yard dash.
Let D equal the difference in time to run the 40-yard dash-the "before-program time" (X) minus the "after-program time" (Y).
Assume that the distribution of D is approximately N(μd,σd2).
We shall test H0:μD=0 against H1:μD>0 at significance level α=0.10.
We observe n=24, d¯=0.0788, sD=0.2549.

t=0.07880.254924≈1.514p-value approach: p=P(T≥1.514;T∼t(23))0.05<p<0.1<0.1∴ Reject H0 at 10% significance levelCritical region approach: t0.10(23)=1.319<|t|∴ Reject H0 at 10% significance levelCI approach: The 90% one-sided paired CI=(0.0788−t0.10(23)(0.254924),∞)=(0.0788−1.319(0.254924),∞)=(0.0101,∞)0∉(0.0101,∞)∴ Reject H0 at 10% significance level

Test Equality of Two Normal Variances

Suppose two independent populations with i.i.d. samples

To test H0:σX2=σY2 versus H1:σX2≠σY2 ,
we have

U=(n−1)SX2σX2∼χ2(n−1)V=(m−1)SY2σY2∼χ2(m−1)

Recall F-distribution definition,

F-distribution

If U∼χ2(u),V∼χ2(v) independent, then

UuVv∼F(u,v)

Define test statistic

F:=Un−1Vm−1=SX2σX2SY2σY2

Under H0:σX2=σY2, the ratio becomes

F=SX2SY2∼F(n−1,m−1)

Once data x1,⋯,xn and y1,⋯,ym is observed, we can compute

f=sX2sY2

Reject H0 at significance level α when

f≥Fα2(n−1,m−1) or f≤F1−α2(n−1,m−1)
Note

For F distribution,

F1−α2(n−1,m−1)=1Fα2(m−1,n−1)

Two-sided F-test.png

For two-sided test H0:σX2=σY2 versus H1:σX2≠σY2

p=2Pr(F>f;F∼F(n−1,m−1)) p=2Pr(F<f;F∼F(n−1,m−1))
Note

F∼F(n−1,m−1),

  • 左边的 F:是一个随机变量,就是 SX2SY2。代入数据后得到观测值 f=sX2sY2,它是一个数。
  • 右边的 F:是 F 分布(F-distribution)。

Test Equality of Two Normal Variances.png
Decision rules: Reject H0 at significance level α when either

数据关系 方差假设 统计量分母 自由度 何时用它
Pooled 独立 σX2=σY2 Sp1n+1m n+m−2 独立且方差相等
Welch 独立 σX2≠σY2 SX2n+SY2m r(修正) 独立且方差不等
Paired 配对 只看差值 D SDn n−1 同一批人测两次

t-test-one-table.png

Hypothesis Testing for Proportions

Let p be the proportion of population who has certain characteristic
To estimate p, draw n i.i.d. random samples

X1,⋯,Xn∼Bernoulli(p)

where each Xi is 1 when it has the characteristic and 0 if doesn't have

Denote Y=∑i=1nXi∼Bin(n,p), then

E(Y)=npVar(Y)=np(1−p)

If n is large enough, by CLT,

p^=Yn=∑i=1nXin∼approxN(p,p(1−p)n)

Given null hypothesis test H0:p=p0, construct test statistic as follow

T:=p^−p0p0(1−p0)n

Under H0, T∼N(0,1) approximately

Once data x1,⋯,xn, are observed, we can compute

t=p^−p0p0(1−p0)n

t is the realized value of T (即 t 是 T 的观测值)

For one-sided test: H0:p=p0,H1:p>p0
p-value is

Pr(T>t;H0)=Pr(Z>t)

where Z∼N(0,1)

Reject H0 at significance level α when either

For one-sided test: H0:p=p0,H1:p<p0
p-value is

Pr(T<t;H0)=Pr(Z<t)

Reject H0 at significance level α when either

For two-sided test H0:p=p0,H1:p≠p0
p-value is

Pr(|T|>|t|;H0)=2Pr(Z>|t|)

Reject H0 at significance level α when either

Example

It was claimed that many commercially manufactured dice are not fair because the “spots” are really indentations, so that, for example, the 6-side is lighter than the 1-side.
Let p equal the probability of rolling a 6 with one of these dice.
To test H0:p=16 against the alternative hypothesis H1:p≠16 with significance level α=5%, several such dice will be rolled to yield a total of n=8000 observations, and the number 6 appears 1389 times.

Let Y be the no. of times that 6 resulted in the 8000 trials.
Then, the test statistics is

Z=(Yn−16)(16)(56)n∼approxN(0,1)t=(13898000−16)(16)(56)8000=1.67zα2=z0.025=1.96 p=2Pr(Z>|t|)=2Pr(Z>1.67)=2(0.0475)=0.095≥0.05

- Hence, fail to reject H0 at 5% significance level
If set α=0.1 with z0.05=1.64, H0 will be rejected at 10% significance level

Note

显著性水平 α 是你自己事先选的门槛,门槛放松(从 5% 放宽到 10%),同样的数据就可能变成“拒绝 H0”。
结论会随 α 改变,所以 α 必须在做实验之前就定好。

这就是为什么统计上严禁“先看 p 值,再挑一个刚好能拒绝 H0 的 α”(这是 p-hacking,也就是操纵数据/分析方式)。
α 必须在看数据之前定死。而且报告时正确说法是“在 10% 显著性水平下拒绝”,而不是“骰子确实不公平”。

Hypothesis Testing for Equality of Two Proportions

Given there are 2 populations, namely population 1 and population 2.
Let p1 and p2 be the proportion of population 1 and 2 respectively, who has a certain characteristic.

Test on H0:p1=p2 against possible alternative hypothesis H1

Denote the samples to be

X1,1,⋯,X1,n∼Bernoulli(p1)X2,1,⋯,X2,m∼Bernoulli(p2)

Assume they're independent

Denote the sums to be

Y1=∑i=1nX1,i∼Bin(n,p1)Y2=∑i=1mX2,i∼Bin(m,p2)

MLE of p1,p2 are

p^1=Y1np^2=Y2m

If n,m are large enough, then by CLT,

p^1−p^2∼approxN(p1−p2,p1(1−p1)n+p2(1−p2)m)

For two-sided test H0:p1=p2,H1:p1≠p2
Under H0, p1=p2 and

p^1−p^2∼approxN(0,p1(1−p1)(1n+1m))

Approximate p1 by

p^=Y1+Y2n+m

Construct test statistic

T:=p^1−p^2p^(1−p^)(1n+1m)

Under H0, T∼approxN(0,1)

Once data y1,y2 are observed, we can compute

t=(y1n−y2m)p^(1−p^)(1n+1m)

p-value is

p=Pr(|T|>|t|;H0)=2Pr(Z>|t|)

Reject H0 at significance level α when either

Example

In a sample of n=200, we havey1=22 low birth weight babies in Country 1.
In a sample of m=100 we find y2=16 low birth weight babies in Country 2
Please test H0:p1=p2 versus H1:p1≠p2 at significance level 5%

p^=22+16200+100=38300=19150t=22200−1610019150(1−19150)(1200+1100)≈−1.227 z0.025=1.96>|t|=1.227

- Fail to reject H0 at 5% significance level

p=2Pr(Z>1.227)=2Pr(Z>1.23)=0.219>0.05

- Fail to reject H0 at 5% significance level
Summary of Hypothesis Tests for Proportions.png