forked from winterwang/LSHTMlearningnote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02-Inference.Rmd
3403 lines (2451 loc) · 171 KB
/
02-Inference.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# (PART) 統計推斷 Inference {-}
# 統計推斷的概念
> If people do not believe that mathematics is simple, it is only because they do not realize how complicated life is.
>
> --- [John von Neumann](https://zh.wikipedia.org/wiki/%E7%BA%A6%E7%BF%B0%C2%B7%E5%86%AF%C2%B7%E8%AF%BA%E4%BC%8A%E6%9B%BC)
## 人羣與樣本 (population and sample)
討論樣本時,需考慮下面幾個問題:
1. 樣本是否具有代表性?
2. 人羣被準確定義了嗎?
3. 我們感興趣的“人羣”是否可以是無限大 (多) 的?
4. 我們研究的樣本,是僅僅用來觀察,亦或是計劃對之進行某種干預呢?
5. 我們從所有可能的人羣中抽樣了嗎?
## 樣本和統計量 (sample and statistic)
通常我們在進行實驗或觀察時只是獲得了樣本的數據。而希望從樣本數據去推斷 (inference) 總體 (或人羣) 的一些特徵。我們也許只是想用樣本的平均值來估計整體人羣的某個特徵的平均值。不管是何種估計和推斷,都是基於對樣本數據的計算,從樣本中獲得想要推斷總體的**統計量 (statistics)**。我們用已知樣本去推斷未知總體的過程就叫做**估計 (estimate)**。這個想要被推斷的總體或人羣的值,被叫做**參數 (parameter)**,常常使用希臘字母來標記。用來估計總體或人羣的,從樣本數據計算得來的統計量,叫做**估計量 (estimator)**。
所有的統計量,都有**樣本分佈 (sampling distributions,意爲重複無限次取樣後獲得的無限次統計量的分佈)**。推斷的過程歸納如下:
1. 從總體或人羣中抽樣 (樣本量 $n$)
2. 計算這個樣本的合適統計量,從而用於估計它在整體或人羣中的值。
3. 我們還需要決定計算獲得的統計量的樣本分佈 (假定會抽樣無數次) 。
4. 一旦可以精確地確認樣本分佈,我們就可以定量地計算出使用步驟2中獲得的統計量估計總體或人羣的參數時的準確度。
## 估計 Estimation
從樣本的均值,推斷總體或人羣的均值是一種估計。我們的目的是,從已知樣本中計算一個儘可能接近那個未知的總體或人羣參數的值。一個估計量有兩個與生俱來的性質 (properties):1) 偏倚 (bias); 2) 精確度 (precision)。這兩個性質都可以從樣本分佈和估計量獲得。
1. 偏倚: 偏倚簡單說就是樣本分佈的均值,也就是我們從樣本中計算獲得的估計量,和我們想要拿它來估計的總體或人羣的參數之間的差距。(The bias is the difference between the mean of the sampling distribution -- the expected or average value of the estimator -- and the population parameter being estimated.) 一個小的偏倚,確保了我們從樣本中計算獲得的估計值 (假設我們抽樣無數次,計算無數個樣本估計值) **均勻地**分佈在總體或人羣參數的左右兩邊。偏倚本身並不是太大的問題,但是假如樣本量增加,偏倚依然存在 (估計量不一致, inconsistent) ,那常常意味着是抽樣過程出現了問題。例如:<br>用簡單隨機抽樣法獲得的樣本均值,就是總體或人羣均值的無偏估計 (unbiased estimator)。如果抽樣時由於某些主觀客觀的原因導致較小的樣本很少被抽樣 (抽樣過程出了問題,脫離了簡單隨機抽樣原則) ,那麼此時得到的樣本均值就會是一個過高的估計值 (upward biased estimator)。
2. 精確度:估計值的精確度可以通過樣本分佈的方差或標準差來評價 (簡單說是樣本分佈的方差越低,波動越小,精確度越高) 。樣本分佈的標準差被定義爲估計值的標準誤。假如估計量是樣本均值,那麼樣本分佈的標準差 (估計量的標準誤) 和樣本數據之間有如下的關係:
$$\text{true standard error of the mean} = \frac{\text{true standard deviation}}{\sqrt{\text{sample size}}}$$
在一些簡單的情況下,通常估計值的選用不言自明 (例如均值,或者百分比) 。但是在複雜的情況下,我們可能可以有多個不同類型的估計量可以選擇,他們也常常各有利弊,需要我們做出取捨。
## 信賴區間 confidence intervals
從樣本中計算估計量獲得的一個估計值,只是一個**點估計 (point estimate)**。對比之下,信賴區間就是一個對這個點估計的精確度的體現。信賴區間越窄,說明我們對於總體或人羣的參數的可能取值的範圍估計越精確。
信賴區間通常是成對成對的出現的,即有上限和下限。這樣的一對從樣本數據中計算得來的統計量,同樣也是有樣本分佈的。**每次我們重新從總體或人羣中抽樣,計算獲得的信賴區間都不同,這些信賴區間就組成了信賴區間的樣本分佈。總體和人羣的參數落在這些信賴區間範圍內的概率,就是我們常說的信賴區間的水平 ($95\%$) 。** 常用的這個概率值就是 $95\%, 90\%, 99\%$。
當從樣本數據計算獲得的估計量的信賴區間很寬,說明了這個收集來的數據提供了很少的參數信息,導致估計變得很不精確。
~~看到這裏的都是好漢一條啊! 我不知道你暈了麼有,反正我是已經暈了。。。~~
# 估計和精確度 Estimation and Precision
## 估計量和他們的樣本分佈 {#CI-for-sample-mean}
例子: 最大呼氣量 (Forced Expoiratory Volume in one second, FEV1) 用於測量一個人的肺功能,它的測量值是連續的。我們從前來門診的人中隨機抽取 $n$ 人作爲樣本,用這個樣本的 FEV1 平均值來估計這個診所的患者的平均肺功能。
**模型假設:** 在這個例子中,我們的假設有如下:每個隨機抽取的 FEV1 測量值都是從同一個總體 (人羣) 中抽取,每一個觀察值 $Y_i$ 都互相獨立互不影響。我們用縮寫 iid 表示這些隨機抽取的樣本是服從獨立同分佈 (independent and identically distributed)。另外,總體的分佈也假定爲正態分佈,且總體均值爲 $\mu$,總體方差爲 $\sigma^2$。那麼這個模型可以簡單的被寫成:
$$Y_i \stackrel{i.i.d}{\sim} N(\mu, \sigma^2), i=1,2,\dots,n$$
**總體均值 $\mu$ 的估計量:** 顯然算術平均值: $\bar{Y}=\frac{1}{n}\sum_{i=1}^ny_i$ 是我們用於估計總體均值的估計量。
**估計量的樣本分佈:**
$$\bar{Y}\stackrel{i.i.d}{\sim}N(\mu, \frac{\sigma^2}{n})$$
**證明**
$$
\begin{aligned}
E(\bar{Y}) &= E(\frac{1}{n}\sum Y_i) \\
&= \frac{1}{n}E(\sum Y_i) \\
&= \frac{1}{n}\sum E(Y_i) \\
&= \frac{1}{n}n\mu = \mu \\
Var(\bar{Y}) &= Var(\frac{1}{n}\sum Y_i) \\
\because Y_i \;\text{are} &\; \text{independent} \\
&= \frac{1}{n^2}\sum Var(Y_i) \\
&= \frac{1}{n^2} n Var(Y_i) \\
&= \frac{\sigma^2}{n}
\end{aligned}
$$
**證明當 $Z=\frac{\bar{Y}-\mu}{\sqrt{Var(\bar{Y})}}$ 時, $Z\sim N(0,1)$:**
由式子可知, $Z$ 只是由一組服從正態分佈的數據 $\bar{Y}$ 線性轉換 (linear transformation) 而來,所以 $Z$ 本身也服從正態分佈
$$
\begin{aligned}
E(Z) &= \frac{1}{\sqrt{Var(\bar{Y})}}E[\bar{Y}-\mu] \\
&= \frac{1}{\sqrt{Var(\bar{Y})}}[\mu-\mu] = 0 \\
Var(Z) &= \frac{1}{Var(\bar{Y})}Var[\bar{Y}-\mu] \\
&= \frac{1}{Var(\bar{Y})}Var(\bar{Y}) =1 \\
\therefore Z \;&\sim N(0,1)
\end{aligned}
$$
**均值 $\mu$ 的信賴區間:** 上節說道,
> 信賴區間通常是成對成對的出現的,即有上限和下限。這樣的一對從樣本數據中計算得來的統計量,同樣也是有樣本分佈的。**每次我們重新從總體或人羣中抽樣,計算獲得的信賴區間都不同,這些信賴區間就組成了信賴區間的樣本分佈。總體和人羣的參數落在這些信賴區間範圍內的概率,就是我們常說的信賴區間的水平($95\%$) 。** 常用的這個概率值就是 $95\%, 90\%, 99\%$。
假定我們用 $95\%$ 作爲信賴區間的水平。那麼下面我們嘗試推導一下信賴區間的計算公式。從長遠來說 (也就是假設我們從總體中抽樣無數次,每次都進行信賴區間的計算,也獲得無數個信賴區間) ,這些信賴區間中有 $95\%$ 是包含了總體的真實均值 (但是卻是未知) 的,而且這些信賴區間由於是從一個服從正態分佈的數據而來,它們也服從正態分佈 (對真實均值左右對稱) 。所以我們有理由相信,可以找到一個數值 $c$:
$$Prob(\bar{Y} > \mu+c) = 0.025 \\
Prob(\bar{Y} < \mu-c) = 0.025$$
因此,我們可以定義 $95\%$ 信賴區間的上限和下限分別是:
$$L=\bar{Y}-c \Rightarrow Prob(L>\mu)=0.025 \\
U=\bar{Y}+c \Rightarrow Prob(U<\mu)=0.025$$

接下來就是推倒 (故意的) $c$ 的過程啦:
$$
\begin{aligned}
Prob(\bar{Y}>\mu+c)=Prob(\bar{Y}-\mu>c) \;&= 0.025 \\
\Rightarrow Prob(\frac{\bar{Y}-\mu}{\sqrt{Var(\bar{Y})}} > \frac{c}{\sqrt{Var(\bar{Y})}}) \;&= 0.025 \\
\Rightarrow Prob(Z>\frac{c}{\sqrt{Var(\bar{Y})}}) \;&= 0.025 \\
we\;have\;proved\; Z\sim N(0,1) \\
we\;also\;know\; Prob(Z>1.96) \;&= 0.025 \\
so\;let\; \frac{c}{\sqrt{Var(\bar{Y})}} =1.96 \\
\Rightarrow c=1.96\sqrt{Var(\bar{Y})} \\
the\;95\%\;confidence\;interval \;of\; &the\;population\;mean\;is\\
\mu = \bar{Y}\pm1.96\sqrt{Var(\bar{Y})}=\bar{Y}\pm & 1.96\frac{\sigma}{\sqrt{n}}
\end{aligned}
$$
其中,$\sqrt{Var(\bar{Y})}$ 就是我們熟知的估計量 $\bar{Y}$ 的標準誤。
## 估計量的特質
考慮以下的問題:
1. 什麼因素決定了一個估計量 (estimator) 的好壞,是否實用?
2. 如果有其他的可選擇估計量,該如何取捨呢?
3. 當情況複雜的時候,我們該如何尋找合適的估計量?
### 偏倚 {#bias}
假設 $T$ 是我們估計總體參數 $\theta$ 的一個估計量。一般來說我們希望估計量的樣本分佈可以在 `“正確的位置”` 左右均勻分佈。換句話說我們希望:
$$E(T)=\theta$$
如果實現了這個條件,我們說這樣的估計量是無偏的 (`unbiased`)。然而,天下哪有這等好事,我們叫真實值和估計量之間的差距爲偏倚:
$$bias(T) = E(T)-\theta$$
其實偏倚完全等於零並不是最重要,許多常見的估計量都是有偏倚的。重要的是,這個偏倚會隨着樣本量的增加而逐漸趨近於零。所以我們就可以認爲這樣的估計量是漸進無偏的 (asymptotically unbiased):
$$T\;is\;an\;\textbf{unbiased}\;estimator\;for\;\theta\;if\;\\E(T)=\theta\\
T\;is\;an\;\textbf{asymptotically unbiased}\;estimator\;for\;\theta\;if\;\\lim_{n\rightarrow\infty}E(T)=\theta$$
### 估計量的效能 Efficiency
通常,我們希望一個估計量 (estimator) 的偏倚要小,同時,它的樣本分佈也希望能儘可能的不要波動太大。換句話說,我們還希望估計量的方差越小越好。
如果說,兩個估計量有相同的偏倚,均可以選擇來推斷總體,我們說,其中樣本分佈的方差小的那個 (波動幅度小) 的那個估計量是相對更好的。因爲樣本分佈方差越小,說明可以**更加精確的**估計總體參數。這兩個估計量的方差之比:$Var(S)/Var(T)$ 被叫做這兩個估計量的**相對效能 (relative efficiency)**。所以我們用估計量去推斷總體時,需要選用效能最高,精確度最好的估計量 **(the minimum variance unbiased estimator/an efficient estimator)**。
### 均值和中位數的相對效能
在一個服從 $N(\mu,\sigma^2)$ 正態分佈的數據中,中位數和均值是一樣的,也都同時等於總體均值參數 $\mu$。而且,樣本均數 $\bar{Y}$ 和樣本中位數 $\dot{Y}$ 都是對總體均值的無偏估計量。那麼應該選用中位數還是平均值呢?
之前證明過當 $Y_i \sim N(\mu,\sigma^2)$ 時, $Var(\bar{Y})=\sigma^2/n$。然而,當 $n$ 較大的時候,可以證明的是:
$$Var(\dot{Y})=\frac{\pi}{2}\frac{\sigma^2}{n}\approx1.571\frac{\sigma^2}{n}$$
因此,這兩個估計量的相對效能就是:
$$\frac{Var(\dot{Y})}{Var(\bar{Y})}\approx1.571$$
所以總體是正態分佈時,平均值就是較中位數更適合用來估計總體的估計量。
### 均方差 mean square error (MSE)
兩個估計量的偏倚不同時,可以比較他們和總體參數之間的差距,這被叫做均方差, Mean Square Error (MSE)。
$$MSE(T)=E[(T-\theta)^2]$$
這裏用一個數學技巧,將式子中的估計量和總體參數之間的差,分成兩個部分:一是估計量本身的方差 ($T-E(T)$),一是估計量的偏倚 ($E(T)-\theta$)。
$$
\begin{aligned}
MSE(T) &= E[(T-\theta)^2] \\
&= E\{[T-E(T)+E(T)-\theta]^2\} \\
&= E\{[T-E(T)]^2+[E(T)-\theta]^2 \\
& \;\;\;\;\; \;\;+2[T-E(T)][E(T)-\theta]\} \\
&= E\{[T-E(T)]^2\}+E\{[E(T)-\theta]^2\} + 0\\
&= Var(T) + [bias(T)^2]
\end{aligned}
$$
## 總體方差的估計,自由度 {#samplevarbias}
如果 $Y_i \sim (\mu, \sigma^2)$,並不需要默認或者假定它服從正態分佈或者任何分佈。那麼它的方差我們會用:
$$V_{\mu}=\frac{1}{n}\sum_{i=1}^n(Y_i-\mu)^2$$
**證明 $V_{\mu}$ 是 $\sigma^2$ 的無偏估計:**
$$
\begin{aligned}
V_{\mu} &= \frac{1}{n}\sum_{i=1}^n(Y_i-\mu)^2 \\
we\;need\;to\;prove &E(V_{\mu}) = \sigma^2 \\
\Rightarrow E(V_{\mu}) &= \frac{1}{n}\sum_{i=1}^nE(Y_i-\mu)^2 \\
&= \frac{1}{n}\sum_{i=1}^nVar(Y_i) \\
&= \frac{1}{n}\sum_{i=1}^n\sigma^2 \\
&= \sigma^2
\end{aligned}
$$
然而通常情況下,我們並不知道總體的均值 $\mu$。因此,只好用樣本的均值 $\bar{Y}$ 來估計 $\mu$。所以上面的方程就變成了:
$$V_{\mu}=\frac{1}{n}\sum_{i=1}^n(Y_i-\bar{Y})^2$$
你如果仔細觀察認真思考,就會發現,上面這個式子是`有問題的`。這個大問題就在於,$Y_i-\bar{Y}$ 中我們忽略掉了樣本均值 $\bar{Y}$ 和總體均值 $\mu$ 之間的差 ($\bar{Y}-\mu$)。因此上面的計算式來估計總體方差時,很顯然是會低估平均平方差,從而低估了總體方差。
這裏需要引入**自由度 (degree of freedom)** 在參數估計中的概念。
字面上可以理解爲:自由度是估計過程中使用了多少互相獨立的信息。所以在上面第一個公式中:$V_{\mu}=\frac{1}{n}\sum_{i=1}^n(Y_i-\mu)^2$。所有的 $n$ 個觀察值互相獨立,不僅如此,他們還對總體均值獨立。然而在第二個我們用 $\bar{Y}$ 取代了 $\mu$ 的公式中,樣本均數則與觀察值不互相獨立。因爲**樣本均數必然總是落在觀察值的中間**。然而總體均數並不一定就會落在觀察值中間。總體均數,和觀察值之間是自由,獨立的。因此,當我們觀察到 $n-1$ 個觀察值時,剩下的最後一個觀察值,決定了樣本均值的大小。所以說,樣本均值的自由度,是 $n-1$。
所以,加入了自由度的討論,我們可以相信,用樣本估計總體的方差時,使用下面的公式將會是總體方差的無偏估計:
$$V_{n-1}=\frac{1}{n-1}\sum_{i=1}^n(Y_i-\bar{Y})=\frac{n}{n-1}V_n$$
**證明**
利用上面也用到過的證明方法 -- 把樣本和總體均值之間的差分成兩部分:
$$
\begin{aligned}
V_{\mu} &= \frac{1}{n}\sum_{i=1}^n(Y_i-\mu)^2 \\
&= \frac{1}{n}\sum_{i=1}^n[(Y_i-\bar{Y})+(\bar{Y}-\mu)]^2 \\
&= \frac{1}{n}\sum_{i=1}^n[(Y_i-\bar{Y})^2+(\bar{Y}-\mu)^2\\
&\;\;\;\;\;\;\;\;\;\;\;\;+2(Y_i-\bar{Y})(\bar{Y}-\mu)]\\
&=\frac{1}{n}\sum_{i=1}^n(Y_i-\bar{Y})^2+\frac{1}{n}\sum_{i=1}^n(\bar{Y}-\mu)^2\\
&\;\;\;\;\;\;\;\;\;\;\;\;+\frac{2}{n}(\bar{Y}-\mu)\sum_{i=1}^n(Y_i-\bar{Y}) \\
&= V_n+(\bar{Y}-\mu)^2 \\ &\;\;\;\;\;\;\;\;\;\;\;\;(\text{note that}\;\sum_{i=1}^n(Y_i-\bar{Y})=0) \\
\Rightarrow V_n &= V_{\mu}-(\bar{Y}-\mu)^2 \\
\therefore E(V_n)&= E(V_{\mu}) - E[(\bar{Y}-\mu)^2] \\
&= Var(Y)-Var(\bar{Y}) \\
&= \sigma^2-\frac{\sigma^2}{n} \\
&= \sigma^2(\frac{n-1}{n})
\end{aligned}
$$
因此,我們看見 $V_n$ 正如上面討論的那樣,是低估了總體方差的。雖然當 $n\rightarrow\infty$ 時無限接近 $\sigma^2$ 但是依然是低估了的。所以,我們可以對之進行修正:
$$
\begin{aligned}
E[\frac{n}{n-1}V_n] &= \frac{n}{n-1}E[V_n] =\sigma^2 \\
\Rightarrow E[V_{n-1}] &= \sigma^2
\end{aligned}
$$
## 樣本方差的樣本分佈 {#samplevar}
$S^2$ 常用來標記樣本方差,取代上面我們用到的 $V_{n-1}$:
$$S^2=\frac{1}{n-1}\sum_{i=1}^n(Y_i-\bar{Y})^2$$
而且上面也證明了,$E(S^2)=\sigma^2$ 是總體方差的無偏估計。然而,要注意的是,樣本標準差 $\sqrt{S^2}$ 卻不是總體標準差 $\sigma$ 的無偏估計(因爲並不是線性變換,而是開了根號) 。
**證明樣本標準差 $S$ 不是總體標準差 $\sigma$ 的無偏估計**
$$
\begin{aligned}
Var(S) &=E(S^2)-[E(S)]^2 \\
\Rightarrow [E(S)]^2 &=E(S^2)-Var(S) \\
\because E(S^2) &=\sigma^2 \\
\therefore [E(S)]^2 &=\sigma^2-Var(S) \\
E(S) &=\sqrt{\sigma^2-Var(S)} \\
\end{aligned}$$
**可見樣本標準差是低估了總體標準差的。**
另外可以被證明的是:
$$\frac{n-1}{\sigma^2}S^2\sim \mathcal{X}_{n-1}^2\\
Var(S^2)=\frac{2\sigma^4}{n-1}$$
$\mathcal{X}^2_m$: 自由度爲 $m$ 的卡方分佈 (Section \@ref(chi-square-distribution))。是在圖形上向右歪曲的分佈。當自由度增加時,會越來越接近正態分佈。
# 卡方分佈 Chi-square distribution {#chi-square-distribution}
## 卡方分佈的期望和方差的證明
當 $X\sim N(0,1)$ 時, $X^2\sim \mathcal{X}_1^2$
如果 $X_1, \dots, X_n\stackrel{i.i.d}{\sim} N(0,1)$,
那麼 $\sum_{i=1}^nX_i^2\sim\mathcal{X}_n^2$
其中: $\mathcal{X}_n^2$ 表示自由度爲 $n$ 的卡方分佈。
且 $X_m^2+X_n^2=\mathcal{X}_{m+n}^2$
## 卡方分佈的期望
$$E(X_1^2)=Var(X)+[E(X)]^2=1+0=1$$
$$\Rightarrow E(X_n^2)=n$$
## 卡方分佈的方差
$$
\begin{aligned}
Var(X_1^2) &= E(X_1^{2^2}) - E(X_1^2)^2 \\
&= E(X_1^4)-1
\end{aligned}
$$
### 下面來求 $E(X_1^4)$
$$
\begin{aligned}
\because E(X_1) &= \int_{-\infty}^{+\infty} xf(x)dx \\
\therefore E(X_1^4) &= \int_{-\infty}^{+\infty} x^4f(x)dx
\end{aligned}$$
已知: $f(x)=\frac{1}{\sqrt{2\pi}}e^{(-\frac{x^2}{2})}$ 代入上式:
$$
\begin{aligned}
E(X_1^4) &= \int_{-\infty}^{+\infty} x^4f(x)dx \\
&= \int_{-\infty}^{+\infty} x^4\frac{1}{\sqrt{2\pi}}e^{(-\frac{x^2}{2})}dx\\
&=\frac{1}{\sqrt{2\pi}}\int_{-\infty}^{+\infty}x^4e^{(-\frac{x^2}{2})}dx\\
&=\frac{-1}{\sqrt{2\pi}}\int_{-\infty}^{+\infty}x^3(-x)e^{(-\frac{x^2}{2})}dx
\end{aligned}
$$
令 $u=x^3, v=e^{(-\frac{x^2}{2})},t=-\frac{x^2}{2}$
可以推導:
$$
\begin{aligned}
\frac{dv}{dx} &= \frac{dv}{dt}\frac{dt}{dx} \\
&= e^t(-\frac{1}{2}\times2x) \\
&= (-x)e^{(-\frac{x^2}{2})} \\
\Rightarrow dv &= (-x)e^{(-\frac{x^2}{2})}dx
\end{aligned}
$$
再代入上面的式子:
$$
\begin{aligned}
E(X_1^4) &= \frac{-1}{\sqrt{2\pi}}\int_{-\infty}^{+\infty}u\:dv \\
integrate\; &by\; parts:\\
E(X_1^4) &= \frac{-1}{\sqrt{2\pi}}\{[u\:v] \rvert_{-\infty}^{+\infty}-\int_{-\infty}^{+\infty}v\:du\} \\
&= \frac{-1}{\sqrt{2\pi}}\{[x^3e^{(-\frac{x^2}{2})}]\rvert_{-\infty}^{+\infty} -\int_{-\infty}^{+\infty}v\:du\} \\
&=\frac{-1}{\sqrt{2\pi}}\{0-0-\int_{-\infty}^{+\infty}e^{(-\frac{x^2}{2})}dx^3\} \\
&=\frac{-1}{\sqrt{2\pi}}[-3\int_{-\infty}^{+\infty}x^2e^{(-\frac{x^2}{2})}dx] \\
&=\frac{-3}{\sqrt{2\pi}}[\int_{-\infty}^{+\infty}x(-x)e^{(-\frac{x^2}{2})}dx] \\
\end{aligned}
$$
再來一次分部積分:
令 $a=x,b=e^{(-\frac{x^2}{2})},d\:b = (-x)e^{(-\frac{x^2}{2})}dx$
$$
\begin{aligned}
E(X_1^4) &= \frac{-3}{\sqrt{2\pi}}\{[a\:b] \rvert_{-\infty}^{+\infty} - \int_{-\infty}^{+\infty}b\:da\} \\
&=\frac{-3}{\sqrt{2\pi}}\{[xe^{(-\frac{x^2}{2})}]\rvert_{-\infty}^{+\infty} -\int_{-\infty}^{+\infty}b\:da\} \\
&=\frac{-3}{\sqrt{2\pi}}\{0-0-\int_{-\infty}^{+\infty}e^{(-\frac{x^2}{2})}dx\} \\
&=\frac{-3}{\sqrt{2\pi}}[-\int_{-\infty}^{+\infty}e^{(-\frac{x^2}{2})}dx] \\
&=\frac{3}{\sqrt{2\pi}}\int_{-\infty}^{+\infty}e^{(-\frac{x^2}{2})}dx
\end{aligned}
$$
下面令 $I=\int_{-\infty}^{+\infty}e^{(-\frac{x^2}{2})}dx\\
\Rightarrow I^2=\int_{-\infty}^{+\infty}\int_{-\infty}^{+\infty}e^{(-\frac{x^2+y^2}{2})}dxdy$
接下來需要用到 [座標轉換](https://www.youtube.com/watch?v=r0fv9V9GHdo)的知識,將 $x,y$ 表示的笛卡爾座標,轉換爲用角度 $\theta$ 和半徑 $r$ 表示的形式。之後的證明可以在[油管](https://www.youtube.com/watch?v=fWOGfzC3IeY)上看到,但是我還是繼續證明下去。
直角座標系 (cartesian coordinators) 和
極座標系 (polar coordinators) 之間轉換的關係如下:
$$
\begin{aligned}
x&=r\:cos\theta\\
y&=r\:sin\theta\\
r^2&=x^2+y^2\\
\end{aligned}
$$
座標轉換以後可以繼續求 $E(X_1^4)$。 在那之前我們先求 $I^2$。
注意轉換座標系統以後,$\theta\in[0,2\pi], r\in[0,+\infty]$
$$
\begin{aligned}
I^2 &= \int_{-\infty}^{+\infty}\int_{-\infty}^{+\infty}e^{(-\frac{x^2+y^2}{2})}dxdy \\
&= \int_{0}^{+\infty}\int_{0}^{2\pi}e^{(-\frac{r^2}{2})}rd\theta dr \\
\end{aligned}
$$
由於先從中間的 $\int_{0}^{2\pi}e^{(-\frac{r^2}{2})}rd\theta$ 開始積分,$\theta$ 以外都可以視爲常數,那麼這個 $[0,2\pi]$ 上的積分就的等於 $2\pi e^{(-\frac{r^2}{2})}r$。
因此上面的式子又變爲:
$$
\begin{aligned}
I^2 &= 2\pi\int_{0}^{+\infty}e^{(-\frac{r^2}{2})}r\:dr \\
\because \frac{d(e^{\frac{-r^2}{2}})}{dr} &= -e^{(-\frac{r^2}{2})}r \\
\therefore I^2 &= 2\pi(-e^{\frac{-r^2}{2}})\rvert_0^{+\infty} \\
&= 0-(2\pi\times(-1)) \\
&= 2\pi\\
\Rightarrow I &= \sqrt{2\pi}
\end{aligned}
$$
所以,
$$
\begin{aligned}
E(X_1^4) &= \frac{3}{\sqrt{2\pi}}\int_{-\infty}^{+\infty}e^{(-\frac{x^2}{2})}dx \\
&= \frac{3}{\sqrt{2\pi}}\times I \\
&= 3 \\
\Rightarrow Var(X_1^2) &= E(X_1^4) - 1 \\
&= 3-1 =2
\end{aligned}
$$
## 把上面的推導擴展
$$
\text{Suppose } \mathcal{X}^2_1, \cdots \mathcal{X}^2_k \stackrel{i.i.d}{\sim} \mathcal{X}^2_1 \\
\Rightarrow \sum_{i=1}^k \mathcal{X}^2_i \sim \mathcal{X}^2_k \\
\Rightarrow \text{E}(\sum_{i=1}^n\mathcal{X}^2_i)=\sum_{i-1}^n\text{E}(\mathcal{X}^2_i)=n\times1=n\\
\text{Var}(\sum_{i=1}^n\mathcal{X}^2_i)=\sum_{i=1}^n\text{Var}(\mathcal{X}^2_i) = n\times2=2n
$$
結論:$X_1, \dots, X_n\stackrel{i.i.d}{\sim} N(0,1)$ 時,$\sum_{i=1}^nX_i^2\sim\mathcal{X}_n^2$ 服從卡方分佈,其期望 $E(X_n^2)=n$,方差 $Var(X_n^2)=2n$。
根據**中心極限定理**(Section \@ref(CLT))
$$n\rightarrow \infty, X_n^2\sim N(n, 2n)$$
# 似然 Likelihood {#likelihood-definition}
## 概率 vs. 推斷 Probability vs. Inference
在概率論的環境下,我們常常被告知的前提是:某某事件發生的概率是多少。例如: 一枚硬幣正面朝上的概率是 $0.5\; Prob(coin\;landing\;heads)=0.5$。然後在這個前提下,我們又繼續去計算複雜的事件發生的概率(例如,10次投擲硬幣以後4次正面朝上的概率是多少?) 。
$$
\binom{10}{4}\times(0.5^4)\times(0.5^{10-4}) = 0.205
$$
```{r inference00, cache=TRUE}
dbinom(4, 10, 0.5)
# or you can calculate by hand:
factorial(10)*(0.5^10)/(factorial(4)*(factorial(6)))
```
在統計推斷的理論中,我們考慮實際的情況,這樣的實際情況就是,我們通過觀察獲得數據,然而我們並不知道某事件發生的概率到底是多少(神如果存在話,只有神知道) 。故這個 $Prob(coin\;landing\;heads)$ 的概率大小對於“人類”來說是未知的。我們可能觀察到投擲了10次硬幣,其中有4次是正面朝上的。那麼我們從這一次觀察實驗中,需要計算的是能夠符合觀察結果的“最佳”概率估計 (best estimate)。在這種情況下,**似然法 (likelihood)** 就是我們進行參數估計的最佳手段。
## 似然和極大似然估計 Likelihood and maximum likelihood estimators
此處用二項分佈的例子來理解似然法的概念:假設我們觀察到10個對象中有4個患~~中二~~病,我們假定這個患病的概率爲 $\pi$。於是我們就有了下面的模型:
**模型:** 我們假定患病與否是一個服從**二項分佈的隨機變量**,$X\sim Bin(10,\pi)$。同時也默認每個人之間是否患病是相互獨立的。
**數據:** 觀察到的數據是,10人中有4人患病。於是 $x=4$。
現在按照觀察到的數據,參數 $\pi$ 變成了未知數:
$$Prob(X=4|\pi)=\binom{10}{4}\pi^4(1-\pi)^{10-4}$$
此時我們會很自然的考慮,當 $\pi$ 是未知數的時候,**它取值爲多大的時候才能讓這個事件(即:10人中4人患病) 發生的概率最大?** 所以我們可以將不同的數值代入 $\pi$ 來計算該事件在不同概率的情況下發生的可能性到底是多少:
```{r inference01, echo=FALSE, eval=FALSE, cache=TRUE}
dt <- read.csv("/home/ccwang/Documents/full-website-content/static/files/pi.csv", header = T)
kable(dt, "html",align = "c",caption = "The probability of observing X=4") %>%
kable_styling(bootstrap_options = c("striped", "bordered"))
```
<table class="table table-striped table-bordered" style="margin-left: auto; margin-right: auto;">
<caption>Table 12.1: The probability of observing $X=4$</caption>
<thead><tr>
<th style="text-align:center;"> $\pi$ </th>
<th style="text-align:center;"> 事件 $X=4$ 發生的概率 </th>
</tr></thead>
<tbody>
<tr>
<td style="text-align:center;"> 0.0 </td>
<td style="text-align:center;"> 0.000 </td>
</tr>
<tr>
<td style="text-align:center;"> 0.2 </td>
<td style="text-align:center;"> 0.088 </td>
</tr>
<tr>
<td style="text-align:center;"> **0.4** </td>
<td style="text-align:center;"> **0.251** </td>
</tr>
<tr>
<td style="text-align:center;"> 0.5 </td>
<td style="text-align:center;"> 0.205 </td>
</tr>
<tr>
<td style="text-align:center;"> 0.6 </td>
<td style="text-align:center;"> 0.111 </td>
</tr>
<tr>
<td style="text-align:center;"> 0.8 </td>
<td style="text-align:center;"> 0.006 </td>
</tr>
<tr>
<td style="text-align:center;"> 1.0 </td>
<td style="text-align:center;"> 0.000 </td>
</tr>
</tbody>
</table>
很顯然,如果 $\pi=0.4$ 時,我們觀察到的事件發生的概率要比 $\pi$ 取其它值時更大。於是小總結一下目前爲止的步驟如下:
- 觀察到實驗數據(10人中4個患病) ;
- 假定這數據服從二項分佈的概率模型,計算不同($\pi$ 的取值不同的) 情況下,該事件按照假定模型發生的概率;
- 通過比較,我們選擇了能夠讓觀察事件發生概率最高的參數取值 ($\pi=0.4$)。
至此,我們可以知道,似然方程,是一個關於未知參數 $\pi$ 的函數,我們目前位置做的就是找到這個函數的最大值 (maximised),和使之成爲最大值時的 $\pi$ :
$$L(\pi|X=4)=\binom{10}{4}\pi^4(1-\pi)^{10-4}$$
我們可以畫出這個似然方程的形狀, $\pi\in[0,1]$
```{r binomial-likelihood, fig.asp=.7, fig.width=6, fig.cap='Binomial Likelihood', fig.align='center', out.width='90%', cache=TRUE}
x <- seq(0,1,by=0.001)
y <- (factorial(10)/(factorial(4)*(factorial(6))))*(x^4)*((1-x)^6)
plot(x, y, type = "l", ylim = c(0,0.3), ylab = "L(\U03C0)", xlab = "\U03C0")
#title("Figure 1. Binomial Likelihood")
abline(h=0.251, lty=2)
abline(v=0.4, lty=2)
```
從圖形上我們也能確認,$\pi=0.4$ 時能夠讓這個似然方程取得最大值。
## 似然方程的一般化定義
對於一個概率模型,如果其參數爲 $\theta$,那麼在給定觀察數據 $\underline{x}$ 時,該參數的似然方程被定義爲:
$L(\theta|\underline{x})=P(\underline{x}|\theta)$
注意:
1. $P(\underline{x}|\theta)$ 可以是概率(離散分佈) 方程,也可以是概率密度(連續型變量) 方程。對於此方程,$\theta$ 是給定的,然後再計算某些事件發生的概率。
2. $L(\theta|\underline{x})$ 是一個關於參數 $\theta$ 的方程,此時,$\underline{x}$ 是固定不變的(觀察值) 。我們希望通過這個方程求出能夠使觀察到的事件發生概率最大的參數值。
3. 似然方程**不是**一個概率密度方程。
另一個例子:
有一組觀察數據是離散型隨機變量 $X$,它符合概率方程 $f(x|\theta)$。下表羅列了當 $\theta$ 分別取值 $1,2,3$ 時的概率方程的值,試求每個觀察值 $X = 0,1,2,3,4$ 的最大似然參數估計:
```{r inference02, cache=TRUE, echo=FALSE, eval=FALSE}
dt <- read.csv("/home/ccwang/Documents/full-website-content/static/files/likelihoodtable1.csv", header = T)
#names(dt) <- c(" ", "0.05", "0.1", "0.2", "0.5")
#dt[,1] <- c(" ", 0.05, 0.01)
kable(dt, "html",align = "c",caption = "Exercise 1") %>%
# column_spec(1:5, bold = T, border_right = T) %>%
kable_styling(bootstrap_options = c("striped", "bordered"))# %>%
# add_header_above(c("\u03b1" = 1, "\u03b2" = 4))
```
<table class="table table-striped table-bordered" style="margin-left: auto; margin-right: auto;">
<caption>Exercise 12.3</caption>
<thead><tr>
<th style="text-align:center;"> $x$ </th>
<th style="text-align:center;"> $f(x|1)$ </th>
<th style="text-align:center;"> $f(x|2)$ </th>
<th style="text-align:center;"> $f(x|3)$ </th>
</tr></thead>
<tbody>
<tr>
<td style="text-align:center;"> 0 </td>
<td style="text-align:center;"> 1/3 </td>
<td style="text-align:center;"> 1/4 </td>
<td style="text-align:center;"> 0 </td>
</tr>
<tr>
<td style="text-align:center;"> 1 </td>
<td style="text-align:center;"> 1/3 </td>
<td style="text-align:center;"> 1/4 </td>
<td style="text-align:center;"> 0 </td>
</tr>
<tr>
<td style="text-align:center;"> 2 </td>
<td style="text-align:center;"> 0 </td>
<td style="text-align:center;"> 1/4 </td>
<td style="text-align:center;"> 1/6 </td>
</tr>
<tr>
<td style="text-align:center;"> 3 </td>
<td style="text-align:center;"> 1/6 </td>
<td style="text-align:center;"> 1/4 </td>
<td style="text-align:center;"> 1/2 </td>
</tr>
<tr>
<td style="text-align:center;"> 4 </td>
<td style="text-align:center;"> 1/6 </td>
<td style="text-align:center;"> 0 </td>
<td style="text-align:center;"> 1/3 </td>
</tr>
</tbody>
</table>
```{r inference03, cache=TRUE, echo=FALSE, eval=FALSE}
library(knitr)
library(kableExtra)
dt <- read.csv("/home/ccwang/Documents/full-website-content/static/files/likelihoodtable2.csv", header = T)
#names(dt) <- c(" ", "0.05", "0.1", "0.2", "0.5")
#dt[,1] <- c(" ", 0.05, 0.01)
kable(dt, "html",align = "c",caption = "Exercise 1") %>%
# column_spec(1:5, bold = T, border_right = T) %>%
kable_styling(bootstrap_options = c("striped", "bordered"))# %>%
# add_header_above(c("\u03b1" = 1, "\u03b2" = 4))
```
<table class="table table-striped table-bordered" style="margin-left: auto; margin-right: auto;">
<caption>Exercise 12.3 answer</caption>
<thead><tr>
<th style="text-align:center;"> $x$ </th>
<th style="text-align:center;"> $f(x|1)$ </th>
<th style="text-align:center;"> $f(x|2)$ </th>
<th style="text-align:center;"> $f(x|3)$ </th>
<th style="text-align:center;"> $\theta$ </th>
</tr></thead>
<tbody>
<tr>
<td style="text-align:center;"> 0 </td>
<td style="text-align:center;"> 1/3 </td>
<td style="text-align:center;"> 1/4 </td>
<td style="text-align:center;"> 0 </td>
<td style="text-align:center;"> **1** </td>
</tr>
<tr>
<td style="text-align:center;"> 1 </td>
<td style="text-align:center;"> 1/3 </td>
<td style="text-align:center;"> 1/4 </td>
<td style="text-align:center;"> 0 </td>
<td style="text-align:center;"> **1** </td>
</tr>
<tr>
<td style="text-align:center;"> 2 </td>
<td style="text-align:center;"> 0 </td>
<td style="text-align:center;"> 1/4 </td>
<td style="text-align:center;"> 1/6 </td>
<td style="text-align:center;"> **2** </td>
</tr>
<tr>
<td style="text-align:center;"> 3 </td>
<td style="text-align:center;"> 1/6 </td>
<td style="text-align:center;"> 1/4 </td>
<td style="text-align:center;"> 1/2 </td>
<td style="text-align:center;"> **3** </td>
</tr>
<tr>
<td style="text-align:center;"> 4 </td>
<td style="text-align:center;"> 1/6 </td>
<td style="text-align:center;"> 0 </td>
<td style="text-align:center;"> 1/3 </td>
<td style="text-align:center;"> **3** </td>
</tr>
</tbody>
</table>
## 對數似然方程 log-likelihood
似然方程的最大值,可通過求 $L(\theta|data)$ 的最大值獲得,也可以通過求該方程的對數方程 $\ell(\theta|data)$ 的最大值獲得。傳統上,我們估計最大方程的最大值的時候,會給參數戴一頂“帽子”(因爲這是觀察獲得的數據告訴我們的參數) : $\hat{\theta}$。並且我們發現對數似然方程比一般的似然方程更加容易微分,因此求似然方程的最大值就變成了求對數似然方程的最大值:
$$\frac{d\ell}{d\theta}=\ell^\prime(\theta)=0\\
AND\\
\frac{d^2\ell}{d\theta^2}<0$$
要注意的是,微分不一定總是能幫助我們求得似然方程的最大值。如果說參數本身的定義域是有界限的話,微分就行不通了:
```{r likelihood-limited, fig.asp=.7, fig.width=6, fig.cap='Likelihood function with a limited domain', fig.align='center', out.width='90%', cache=TRUE}
x <- seq(0,3,by=0.001)
y <- (x-1)^2-5
plot(x, y, type = "l", ylim = c(-5,0-1), ylab = "L(\U03B8)", xlab = "\U03B8")
#title("Figure 2. Likelihood function with \n a limited domain")
abline(v=3, lty=2)
```
**證明:當 $L(\theta|data)$ 取最大值時,該方程的對數方程 $\ell(\theta|data)$ 也是最大值:**
如果似然方程是連續可導,只有一個最大值,且可以二次求導,假設 $\hat{\theta}$ 使該方程取最大值,那麼:
$$\frac{dL}{d\theta}=0, \frac{d^2L}{d\theta^2}<0 \Rightarrow \theta=\hat{\theta}$$
令 $\ell=\text{log}L$ 那麼 $\frac{d\ell}{dL}=\ell^\prime=\frac{1}{L}$:
$$\frac{d\ell}{d\theta}=\frac{d\ell}{dL}\cdot\frac{dL}{d\theta}=\frac{1}{L}\cdot\frac{dL}{d\theta}$$
當 $\ell(\theta|data)$ 取最大值時:
$$\frac{d\ell}{d\theta}=0\Leftrightarrow\frac{1}{L}\cdot\frac{dL}{d\theta}=0\\
\because \frac{1}{L}\neq0 \\
\therefore \frac{dL}{d\theta}=0\\
\Leftrightarrow \theta=\hat{\theta}$$
$$
\begin{aligned}
\frac{d^2\ell}{d\theta^2} &= \frac{d}{d\theta}(\frac{d\ell}{dL}\cdot\frac{dL}{d\theta})\\
&= \frac{d\ell}{dL}\cdot\frac{d^2L}{d\theta^2} + \frac{dL}{d\theta}\cdot\frac{d}{d\theta}(\frac{d\ell}{dL})
\end{aligned}
$$
當 $\theta=\hat{\theta}$ 時,$\frac{dL}{d\theta}=0$ 且 $\frac{d^2L}{d\theta^2}<0 \Rightarrow \frac{d^2\ell}{d\theta^2}<0$
所以,求獲得 $\ell(\theta|data)$ 最大值的 $\theta$ 即可令 $L(\theta|data)$ 獲得最大值。
## 極大似然估計 (maximum likelihood estimator, MLE) 的性質:
1. 漸進無偏 Asymptotically unbiased: <br> $n\rightarrow \infty \Rightarrow E(\hat{\Theta}) \rightarrow \theta$
2. 漸進最高效能 Asymptotically efficient: <br> $n\rightarrow \infty \Rightarrow Var(\hat{\Theta})$ 是所有參數中方差最小的估計
3. 漸進正態分佈 Asymptotically normal: <br> $n\rightarrow \infty \Rightarrow \hat{\Theta} \sim N(\theta, Var(\hat{\Theta}))$
4. 變形後依然保持不變 Transformation invariant: <br> $\hat{\Theta}$ 是 $\theta$ 的MLE時 $\Rightarrow g(\hat{\Theta})$ 是 $g(\theta)$ 的 MLE
5. 信息足夠充分 Sufficient:<br> $\hat{\Theta}$ 包含了觀察數據中所有的能夠用於估計參數的信息
6. 始終不變 consistent: <br> $n\rightarrow\infty\Rightarrow\hat{\Theta}\rightarrow\theta$ 或者可以寫成:$\varepsilon>0, lim_{n\rightarrow\infty}P(|\hat{\Theta}-\theta|>\varepsilon)=0$
## 率的似然估計 Likelihood for a rate {#likelihood-poi}
如果在一項研究中,參與者有各自不同的追蹤隨訪時間(長度) ,那麼我們應該把事件(疾病) 的發病率用率的形式(多少事件每單位人年, e.g. per person year of observation) 。如果這個發病率的參數用 $\lambda$ 來表示,所有參與對象的隨訪時間之和爲 $p$ 人年。那麼這段時間內的期望事件(疾病發病) 次數爲:$\mu=\lambda p$。假設事件(疾病發病) 發生是相互獨立的,可以使用泊松分佈來模擬期望事件(疾病發病) 次數 $D$:
$$D\sim Poi(\mu)$$
假設我們觀察到了 $D=d$ 個事件,我們獲得這個觀察值的概率應該用以下的模型:
$$Prob(D=d)=e^{-\mu}\frac{\mu^d}{d!}=e^{-\lambda p}\frac{\lambda^dp^d}{d!}$$
因此,$\lambda$ 的似然方程是:
$$L(\lambda|observed \;data)=e^{-\lambda p}\frac{\lambda^dp^d}{d!}$$
所以,$\lambda$ 的對數似然方程是:
$$
\begin{aligned}
\ell(\lambda|observed\;data) &= \text{log}(e^{-\lambda p}\frac{\lambda^dp^d}{d!}) \\
&= -\lambda p+d\:\text{log}(\lambda)+d\:\text{log}(p)-\text{log}(d!) \\
\end{aligned}
$$
解 $\ell^\prime(\lambda|data)=0$:
$$
\begin{aligned}
\ell^\prime(\lambda|data) &= -p+\frac{d}{\lambda}=0\\
\Rightarrow \hat{\lambda} &= \frac{d}{p} \\
\end{aligned}
$$
**注意:**
在對數似然方程中,不包含參數的部分,對與似然方程的形狀不產生任何影響,我們在微分對數似然方程的時候,這部分也都自動消失。所以不包含參數的部分,與我們如何獲得極大似然估計是無關的。因此,我們常常在寫對數似然方程的時候就把其中沒有參數的部分直接忽略了。例如上面泊松分佈的似然方程中,$d\:\text{log}(p)-\text{log}(d!)$ 不包含參數 $\lambda$ 可以直接不寫出來。
## 有 $n$ 個獨立觀察時的似然方程和對數似然方程
當有多個獨立觀察時,總體的似然方程等於各個觀察值的似然方程之**乘積**。如果 $X_1,\dots,X_n\stackrel{i.i.d}{\sim}f(\cdot|\theta)$
$$L(\theta|x_1,\cdots,x_n)=f(x_1,\cdots,x_n|\theta)=\prod_{i=1}^nf(x_i|\theta)\\
\Rightarrow \ell(\theta|x_1,\cdots,x_n)=\sum_{i=1}^n\text{log}(f(x_i|\theta))$$
# 對數似然比 Log-likelihood ratio {#llr}
對數似然比的想法來自於將對數似然方程圖形的 $y$ 軸重新調節 (rescale) 使之最大值爲零。這可以通過計算該分佈方程的**對數似然比 (log-likelihood ratio)** 來獲得:
$$llr(\theta)=\ell(\theta|data)-\ell(\hat{\theta}|data)$$
由於 $\ell(\theta)$ 的最大值在 $\hat{\theta}$ 時, 所以,$llr(\theta)$ 就是個當 $\theta=\hat{\theta}$ 時取最大值,且最大值爲零的方程。很容易理解我們叫這個方程爲對數似然比,因爲這個方程就是將似然比 $LR(\theta)=\frac{L(\theta)}{L(\hat{\theta})}$ 取對數而已。
[之前](https://winterwang.github.io/post/likelihood/)我們也確證了,不包含我們感興趣的參數的方程部分可以忽略掉。還是用上一節 10人中4人患病的例子:
$$L(\pi|X=4)=\binom{10}{4}\pi^4(1-\pi)^{10-4}\\
\Rightarrow \ell(\pi)=\text{log}[\pi^4(1-\pi)^{10-4}]\\
\Rightarrow llr(\pi)=\ell(\pi)-\ell(\hat{\pi})=\text{log}\frac{\pi^4(1-\pi)^{10-4}}{0.4^4(1-0.4)^{10-4}}$$
其實由上也可以看出 $llr(\theta)$ 只是將對應的似然方程的 $y$ 軸重新調節了一下而已。形狀是沒有改變的:
```{r binomial-logornot, fig.asp=.7, fig.width=6,fig.cap='Binomial likelihood ratio and log-likelihood ratio', fig.align='center', out.width='90%', cache=TRUE}
par(mfrow=c(1,2))
x <- seq(0,1,by=0.001)
y <- (x^4)*((1-x)^6)/(0.4^4*0.6^6)
z <- log((x^4)*((1-x)^6))-log(0.4^4*0.6^6)
plot(x, y, type = "l", ylim = c(0,1.1),yaxt="n",
frame.plot = FALSE, ylab = "LR(\U03C0)", xlab = "\U03C0")
axis(2, at=seq(0,1, 0.2), las=2)
title(main = "Binomial likelihood ratio")
abline(h=1.0, lty=2)
segments(x0=0.4, y0=0, x1=0.4, y1=1, lty = 2)
plot(x, z, type = "l", ylim = c(-10, 1), yaxt="n", frame.plot = FALSE,
ylab = "llr(\U03C0)", xlab = "\U03C0" )
axis(2, at=seq(-10, 0, 2), las=2)
title(main = "Binomial log-likelihood ratio")
abline(h=0, lty=2)
segments(x0=0.4, y0=-10, x1=0.4, y1=0, lty = 2)
```
## 正態分佈數據的極大似然和對數似然比
假設單個樣本 $y$ 是來自一組服從正態分佈數據的觀察值:$Y\sim N(\mu, \tau^2)$
那麼有:
$$
\begin{aligned}
f(y|\mu) &= \frac{1}{\sqrt{2\pi\tau^2}}e^{(-\frac{1}{2}(\frac{y-\mu}{\tau})^2)} \\
\Rightarrow L(\mu|y) &=\frac{1}{\sqrt{2\pi\tau^2}}e^{(-\frac{1}{2}(\frac{y-\mu}{\tau})^2)} \\
\Rightarrow \ell(\mu)&=\text{log}(\frac{1}{\sqrt{2\pi\tau^2}})-\frac{1}{2}(\frac{y-\mu}{\tau})^2\\
omitting&\;terms\;not\;in\;\mu \\
&= -\frac{1}{2}(\frac{y-\mu}{\tau})^2 \\
\Rightarrow \ell^\prime(\mu) &= 2\cdot[-\frac{1}{2}(\frac{y-\mu}{\tau})\cdot\frac{-1}{\tau}] \\
&=\frac{y-\mu}{\tau^2} \\
let \; \ell^\prime(\mu) &= 0 \\
\Rightarrow \frac{y-\mu}{\tau^2} &= 0 \Rightarrow \hat{\mu} = y\\
\because \ell^{\prime\prime}(\mu) &= \frac{-1}{\tau^2} < 0 \\
\therefore \hat{\mu} &= y \Rightarrow \ell(\hat{\mu}=y)_{max}=0 \\
llr(\mu)&=\ell(\mu)-\ell(\hat{\mu})=\ell(\mu)\\
&=-\frac{1}{2}(\frac{y-\mu}{\tau})^2
\end{aligned}
$$
## $n$ 個獨立正態分佈樣本的對數似然比 {#llr-chi1}
假設一組觀察值來自正態分佈 $X_1,\cdots,X_n\stackrel{i.i.d}{\sim}N(\mu,\sigma^2)$,先假設 $\sigma^2$ 已知。將觀察數據 $x_1,\cdots, x_n$ 標記爲 $\underline{x}$。 那麼:
$$
\begin{aligned}
L(\mu|\underline{x}) &=\prod_{i=1}^nf(x_i|\mu)\\
\Rightarrow \ell(\mu|\underline{x}) &=\sum_{i=1}^n\text{log}f(x_i|\mu)\\
&=\sum_{i=1}^n[-\frac{1}{2}(\frac{x_i-\mu}{\sigma})^2]\\
&=-\frac{1}{2\sigma^2}\sum_{i=1}^n(x_i-\mu)^2\\
&=-\frac{1}{2\sigma^2}[\sum_{i=1}^n(x_i-\bar{x})^2+\sum_{i=1}^n(\bar{x}-\mu)^2]\\
omitting&\;terms\;not\;in\;\mu \\
&=-\frac{1}{2\sigma^2}\sum_{i=1}^n(\bar{x}-\mu)^2\\
&=-\frac{n}{2\sigma^2}(\bar{x}-\mu)^2 \\
&=-\frac{1}{2}(\frac{\bar{x}-\mu}{\sigma/\sqrt{n}})^2\\
\because \ell(\hat{\mu}) &= 0 \\
\therefore llr(\mu) &= \ell(\mu)-\ell(\hat{\mu}) = \ell(\mu)
\end{aligned}
$$
## $n$ 個獨立正態分佈樣本的對數似然比的分佈 {#llr-chi}
假設我們用 $\mu_0$ 表示總體均數這一參數的值。要注意的是,每當樣本被重新取樣,似然,對數似然方程,對數似然比都隨着觀察值而變 (即有自己的分佈)。
考慮一個服從正態分佈的單樣本 $Y$: $Y\sim N(\mu_0,\tau^2)$。那麼它的對數似然比:
$$llr(\mu_0|Y)=\ell(\mu_0)-\ell(\hat{\mu})=-\frac{1}{2}(\frac{Y-\mu_0}{\tau})^2$$
根據**卡方分佈** (Section \@ref(chi-square-distribution)) 的定義:
$$\because \frac{Y-\mu_0}{\tau}\sim N(0,1)\\
\Rightarrow (\frac{Y-\mu_0}{\tau})^2 \sim \mathcal{X}_1^2\\
\therefore -2llr(\mu_0|Y) \sim \mathcal{X}_1^2$$
所以,如果有一組服從正態分佈的觀察值:$X_1,\cdots,X_n\stackrel{i.i.d}{\sim}N(\mu_0,\sigma^2)$,且 $\sigma^2$ 已知的話:
$$-2llr(\mu_0|\bar{X})\sim \mathcal{X}_1^2$$
根據**中心極限定理** (Section \@ref(CLT)),可以將上面的結論一般化:
```{theorem inference04, cache=TRUE}
如果 $X_1,\cdots,X_n\stackrel{i.i.d}{\sim}f(x|\theta)$。 那麼當重複多次從參數爲 $\theta_0$ 的總體中取樣時,那麼統計量 $-2llr(\theta_0)$ 會漸進於自由度爲 $1$ 的卡方分佈: $$-2llr(\theta_0)=-2\{\ell(\theta_0)-\ell(\hat{\theta})\}\xrightarrow[n\rightarrow\infty]{}\;\sim \mathcal{X}_1^2$$
```
## 似然比信賴區間
如果樣本量 $n$ 足夠大 (通常應該大於 $30$),根據上面的定理:
$$-2llr(\theta_0)=-2\{\ell(\theta_0)-\ell(\hat{\theta})\}\sim \mathcal{X}_1^2$$
所以:
$$Prob(-2llr(\theta_0)\leqslant \mathcal{X}_{1,0.95}^2=3.84) = 0.95\\
\Rightarrow Prob(llr(\theta_0)\geqslant-3.84/2=-1.92) = 0.95$$
故似然比的 $95\%$ 信賴區間就是能夠滿足 $llr(\theta)=-1.92$ 的兩個 $\theta$ 值。
### 以二項分佈數據爲例 {#binomial-ex}
繼續用本文開頭的例子:
$$llr(\pi)=\ell(\pi)-\ell(\hat{\pi})=\text{log}\frac{\pi^4(1-\pi)^{10-4}}{0.4^4(1-0.4)^{10-4}}$$
如果令 $llr(\pi)=-1.92$ 在代數上可能較難獲得答案。然而從圖形上,如果我們在 $y=-1.92$ 畫一條橫線,和該似然比方程曲線相交的兩個點就是我們想要求的信賴區間的上限和下限:
```{r bin-llr-95,fig.height=5, fig.width=8, warning=FALSE, message=FALSE, fig.cap='Log-likelihood ratio for binomial example, with 95% confidence intervals shown', fig.align='center', out.width='90%', cache=TRUE}
x <- seq(0,1,by=0.001)
z <- log((x^4)*((1-x)^6))-log(0.4^4*0.6^6)
plot(x, z, type = "l", ylim = c(-10, 1), yaxt="n", frame.plot = FALSE,
ylab = "llr(\U03C0)", xlab = "\U03C0" )
axis(2, at=seq(-10, 0, 2), las=2)
abline(h=0, lty=2)
abline(h=-1.92, lty=2)
segments(x0=0.15, y0=-12, x1=0.15, y1=-1.92, lty = 2)
segments(x0=0.7, y0=-12, x1=0.7, y1=-1.92, lty = 2)
axis(1, at=c(0.15,0.7))
text(0.9, -1, "-1.92")
arrows(0.8, -1.92, 0.8, 0, lty = 1, length = 0.08)
arrows( 0.8, 0, 0.8, -1.92, lty = 1, length = 0.08)
```
從上圖中可以讀出,$95\%$ 對數似然比信賴區間就是 $(0.15, 0.7)$
### 以正態分佈數據爲例 {#normal-ex}
本文前半部分證明過,
$X_1,\cdots,X_n\stackrel{i.i.d}{\sim}N(\mu,\sigma^2)$,先假設 $\sigma^2$ 已知。將觀察數據 $x_1,\cdots, x_n$ 標記爲 $\underline{x}$。 那麼:
$$llr(\mu|\underline{x}) = \ell(\mu|\underline{x})-\ell(\hat{\mu}) = \ell(\mu|\underline{x}) \\
=-\frac{1}{2}(\frac{\bar{x}-\mu}{\sigma/\sqrt{n}})^2$$
很顯然,這是一個關於 $\mu$ 的二次方程,且最大值在 MLE $\hat{\mu}=\bar{x}$ 時取值 $0$。所以可以通過對數似然比法求出均值的 $95\%$ 信賴區間公式:
$$-2\times[-\frac{1}{2}(\frac{\bar{x}-\mu}{\sigma/\sqrt{n}})^2]=3.84\\
\Rightarrow L=\bar{x}-\sqrt{3.84}\frac{\sigma}{\sqrt{n}} \\
U=\bar{x}+\sqrt{3.84}\frac{\sigma}{\sqrt{n}} \\
note: \;\sqrt{3.84}=1.96$$
注意到這和我們之前求的正態分佈均值的信賴區間公式 (Section \@ref(CI-for-sample-mean)) 完全一致。
## 練習題
### Q1
a) 假設十個對象中有三人死亡,用二項分佈模型來模擬這個例子,求這個例子中參數 $\pi$ 的似然方程和圖形 (likelihood) ?
**解**
$$\begin{aligned}
L(\pi|3) &= \binom{10}{3}\pi^3(1-\pi)^{10-3} \\
omitting\;&terms\;not\;in\;\pi \\
\Rightarrow \ell(\pi|3) &= \text{log}[\pi^3(1-\pi)^7] \\
&= 3\text{log}\pi+7\text{log}(1-\pi)\\
\Rightarrow \ell^\prime(\pi|3)&= \frac{3}{\pi}-\frac{7}{1-\pi} \\
let \; \ell^\prime& =0\\
&\frac{3}{\pi}-\frac{7}{1-\pi} = 0 \\
&\frac{3-10\pi}{\pi(1-\pi)} = 0 \\
\Rightarrow MLE &= \hat\pi = 0.3
\end{aligned}$$
```{r bin3-10,fig.width=6, echo=FALSE, message=FALSE, fig.cap='Binomial likelihood function 3 out of 10 subjects', fig.align='center', out.width='90%', cache=TRUE}
pi <- seq(0, 1, by=0.01)
L <- (pi^3)*((1-pi)^7)
plot(pi, L, type = "l", ylim = c(0, 0.0025),yaxt="n", col="darkblue",
frame.plot = FALSE, ylab = "", xlab = "\U03C0")
grid(NA, 5, lwd = 1)
abline(v=0.3, lty=2)
axis(1, at=0.3, las=0)
axis(2, at=seq(0,0.0025,0.0005), las=2)
#title(main = "Binomial likelihood function\n 3 out of 10 subjects")
```
b) 計算似然比,並作圖,注意方程圖形未變,$y$ 軸的變化;取對數似然比,並作圖
```{r bin3-10-ratio,fig.width=6, message=FALSE, warning=FALSE, message=FALSE, fig.cap='Binomial likelihood ratio function 3 out of 10 subjects', fig.align='center', out.width='90%', cache=TRUE}
LR <- L/max(L) ; head(LR)
plot(pi, LR, type = "l", ylim = c(0, 1),yaxt="n", col="darkblue",
frame.plot = FALSE, ylab = "", xlab = "\U03C0")
grid(NA, 5, lwd = 1)
axis(2, at=seq(0,1,0.2), las=2)
title(main = "Binomial likelihood ratio function\n 3 out of 10 subjects")
```
```{r bin3-10-logratio,fig.width=6, message=FALSE, warning=FALSE, message=FALSE, fig.cap='Binomial log-likelihood ratio function 3 out of 10 subjects', fig.align='center', out.width='90%', cache=TRUE}
logLR <- log(L/max(L))
plot(pi, logLR, type = "l", ylim = c(-4, 0),yaxt="n", col="darkblue",
frame.plot = FALSE, ylab = "", xlab = "\U03C0")
grid(NA, 5, lwd = 1)