訊息
|
本問題來自人工智慧的經典教科書 「Artificial Intelligence: A Modern Approach」第三版,475 頁。
問題描述:當病人來看牙醫時,該病人可能有蛀牙或沒蛀牙,也可能有牙痛或沒有牙痛,而牙醫可能會找到牙痛的原因或找不到。
因此有下列三個隨機變數
X:(蛀) 蛀牙與否 (Cavity)
Y:(痛) 牙痛與否 (Toothache)
Z:(找) 是否找到痛的牙 (Catch)
假如這個問題個統計機率都已經知道了,如下表所示。
|
牙痛 (Y=1) |
不牙痛 (Y=0) |
|
找到 (Z=1) |
找不到 (Z=0) |
找到 (Z=1) |
找不到 (Z=0) |
蛀牙(X=1) |
0.108 |
0.012 |
0.072 |
0.008 |
沒蛀牙 (X=0) |
0.016 |
0.064 |
0.144 |
0.576 |
如果將這個表格寫成一整排,那麼將會以下列機率表格顯示。
蛀 X |
痛 Y |
找 Z |
P(X,Y,Z) |
0 |
0 |
0 |
0.576 |
0 |
0 |
1 |
0.144 |
0 |
1 |
0 |
0.064 |
0 |
1 |
1 |
0.016 |
1 |
0 |
0 |
0.008 |
1 |
0 |
1 |
0.072 |
1 |
1 |
0 |
0.012 |
1 |
1 |
1 |
0.108 |
問題
習題1:請計算 P(沒痛) = P(Y=0) = ?
習題2:請計算 P(找到 | 牙痛) = ?
習題3:請問這是一個合理的機率分布嗎?
習題4:請計算 P(Z=1 | X=1) = ?
習題5:請計算 P(Z=1, Y=1) = ?
習題6:請計算 P(蛀 | 找到), P(蛀), P(找到), P(找到 | 蛀) ,然後驗證下列貝氏定理是否成立。
(1)
\begin{align} P(找到|蛀) = P(蛀|找到) \frac{P(找到)}{P(蛀)} \end{align}
解答
由於 R 的 陣列是用以行為主的順序 (Column Major Order),因此沒辦法直接與上列的表格對起來,請看下列檔案中的說明
http://cran.r-project.org/doc/manuals/R-lang.pdf
2.2.2 The dim attribute is used to implement arrays. The content of the array is stored in a
vector in column-major order and the dim attribute is a vector of integers specifying the
respective extents of the array. R ensures that the length of the vector is the product of
the lengths of the dimensions. The length of one or more dimensions may be zero.
所以我們必須先將真值表改為以行為主的方式,改寫後如下表所示。
蛀 X |
痛 Y |
找 Z |
P(X,Y,Z) |
0 |
0 |
0 |
0.576 |
1 |
0 |
0 |
0.008 |
0 |
1 |
0 |
0.064 |
1 |
1 |
0 |
0.012 |
0 |
0 |
1 |
0.144 |
1 |
0 |
1 |
0.072 |
0 |
1 |
1 |
0.016 |
1 |
1 |
1 |
0.108 |
而且 R 的陣列是從 1 開始算的,不像 C 語言是從 0 開始算的,因此還必須將上表修改如下:
蛀 X |
痛 Y |
找 Z |
P(X,Y,Z) |
1 |
1 |
1 |
0.576 |
2 |
1 |
1 |
0.008 |
1 |
2 |
1 |
0.064 |
2 |
2 |
1 |
0.012 |
1 |
1 |
2 |
0.144 |
2 |
1 |
2 |
0.072 |
1 |
2 |
2 |
0.016 |
2 |
2 |
2 |
0.108 |
> p <- array(c(0.576, 0.008, 0.064, 0.012, 0.144, 0.072, 0.016, 0.108),c(2,2,2))
> p
, , 1
[,1] [,2]
[1,] 0.576 0.064
[2,] 0.008 0.012
, , 2
[,1] [,2]
[1,] 0.144 0.016
[2,] 0.072 0.108
> p[1,1,1]
[1] 0.576
> p[2,1,1]
[1] 0.008
> p[1,2,1]
[1] 0.064
> p[2,2,1]
[1] 0.012
> p[1,1,2]
[1] 0.144
> p[2,1,2]
[1] 0.072
> p[1,2,2]
[1] 0.016
> p[2,2,2]
[1] 0.108
> dimnames(p)[[1]] = c("沒蛀", "蛀")
> dimnames(p)[[2]] = c("沒痛", "痛")
> dimnames(p)[[3]] = c("沒找", "找")
> p
, , 沒找
沒痛 痛
沒蛀 0.576 0.064
蛀 0.008 0.012
, , 找
沒痛 痛
沒蛀 0.144 0.016
蛀 0.072 0.108
解答1:P(沒痛) = 0.8
計算過程:
> p[,"沒痛",]
沒找 找
沒蛀 0.576 0.144
蛀 0.008 0.072
> sum(p[,"沒痛",])
[1] 0.8
解答2:P(找到 | 牙痛) = 0.62
> p[,,"找"]
沒痛 痛
沒蛀 0.144 0.016
蛀 0.072 0.108
> sum(p[,,"找"])
[1] 0.34
> sum(p[,"痛","找"])
[1] 0.124
> sum(p[,"痛","找"])/sum(sum(p[,"痛",]))
[1] 0.62
解答3:請問這是一個合理的機率分布嗎? (是的,因為總和為 1,而且每個機率直都介於 0 到1之間)
> sum(p)
[1] 1
> 0<=p<=1
錯誤: unexpected '<=' in "0<=p<="
> 0<=p
, , 沒找
沒痛 痛
沒蛀 TRUE TRUE
蛀 TRUE TRUE
, , 找
沒痛 痛
沒蛀 TRUE TRUE
蛀 TRUE TRUE
> p<=1
, , 沒找
沒痛 痛
沒蛀 TRUE TRUE
蛀 TRUE TRUE
, , 找
沒痛 痛
沒蛀 TRUE TRUE
蛀 TRUE TRUE
> 0<=p & p <=1
, , 沒找
沒痛 痛
沒蛀 TRUE TRUE
蛀 TRUE TRUE
, , 找
沒痛 痛
沒蛀 TRUE TRUE
蛀 TRUE TRUE
解答4:請計算 P(Z=1 | X=1) = 0.9
> sum(p[2,,2])/sum(p[2,,])
[1] 0.9
解答5:請計算 P(Z=1, Y=1) = 0.124
> p[,2,2]
沒蛀 蛀
0.016 0.108
> sum(p[,2,2])
[1] 0.124
解答6:請計算 P(蛀 | 找到), P(蛀), P(找到), P(找到 | 蛀) ,然後驗證下列貝氏定理是否成立。
P(蛀 | 找到) = p(找到|蛀) * p(蛀)/p(找到)
說明:
P(蛀 | 找到) = 0.5294118, P(蛀)=0.2, P(找到)=0.34, P(找到 | 蛀)=0.9
P(蛀 | 找到) = 0.5294118 = 0.9 * 0.2/0.34 = = p(找到|蛀) * p(蛀)/p(找到)
> pab = sum(p["蛀",,"找"])/sum(p[,,"找"]) ; pab = P(蛀 | 找到)
> pba = sum(p["蛀",,"找"])/sum(p["蛀",,]) ; pba = P(找到 | 蛀)
> pa = sum(p["蛀",,]) ; pa = P(蛀)
> pb = sum(p[,,"找"]) ; pb = P(找到)
> pab
[1] 0.5294118
> pba
[1] 0.9
> pa
[1] 0.2
> pb
[1] 0.34
> pba*pa/pb
[1] 0.5294118
> pab-pba*pa/pb
[1] 0
>
所以
p(蛀|找)
= sum(p["蛀",,"找"])/sum(p[,,"找"])
= pab
= pba * pa / pb
= p(找|蛀) * p(蛀)/p(找)
= sum(p["蛀",,"找"])/sum(p[,,"蛀"])* sum(p[,,"蛀"])/ sum(p["找",,])
完整的操作過程
> p <- array(c(0.576, 0.008, 0.064, 0.012, 0.144, 0.072, 0.016, 0.108),c(2,2,2))
> p
, , 1
[,1] [,2]
[1,] 0.576 0.064
[2,] 0.008 0.012
, , 2
[,1] [,2]
[1,] 0.144 0.016
[2,] 0.072 0.108
> p[1,1,1]
[1] 0.576
> p[2,1,1]
[1] 0.008
> p[1,2,1]
[1] 0.064
> p[2,2,1]
[1] 0.012
> p[1,1,2]
[1] 0.144
> p[2,1,2]
[1] 0.072
> p[1,2,2]
[1] 0.016
> p[2,2,2]
[1] 0.108
> dimnames(p)[[1]] = c("沒蛀", "蛀")
> dimnames(p)[[2]] = c("沒痛", "痛")
> dimnames(p)[[3]] = c("沒找", "找")
> p
, , 沒找
沒痛 痛
沒蛀 0.576 0.064
蛀 0.008 0.012
, , 找
沒痛 痛
沒蛀 0.144 0.016
蛀 0.072 0.108
> p[,"沒痛",]
沒找 找
沒蛀 0.576 0.144
蛀 0.008 0.072
> sum(p[,"沒痛",])
[1] 0.8
> p[,,"找"]
沒痛 痛
沒蛀 0.144 0.016
蛀 0.072 0.108
> sum(p[,,"找"])
[1] 0.34
> sum(p[,"痛","找"])
[1] 0.124
> sum(p[,"痛","找"])/sum(sum(p[,"痛",]))
[1] 0.62
> sum(p)
[1] 1
> 0<=p<=1
錯誤: unexpected '<=' in "0<=p<="
> 0<=p
, , 沒找
沒痛 痛
沒蛀 TRUE TRUE
蛀 TRUE TRUE
, , 找
沒痛 痛
沒蛀 TRUE TRUE
蛀 TRUE TRUE
> p<=1
, , 沒找
沒痛 痛
沒蛀 TRUE TRUE
蛀 TRUE TRUE
, , 找
沒痛 痛
沒蛀 TRUE TRUE
蛀 TRUE TRUE
> 0<=p & p <=1
, , 沒找
沒痛 痛
沒蛀 TRUE TRUE
蛀 TRUE TRUE
, , 找
沒痛 痛
沒蛀 TRUE TRUE
蛀 TRUE TRUE
> sum(p[2,,2])/sum(p[2,,])
[1] 0.9
> p[,2,2]
沒蛀 蛀
0.016 0.108
> sum(p[,2,2])
[1] 0.124
> pab = sum(p["蛀",,"找"])/sum(p[,,"找"])
> pba = sum(p["蛀",,"找"])/sum(p["蛀",,])
> pa = sum(p["蛀",,])
> pb = sum(p[,,"找"])
> pab
[1] 0.5294118
> pba
[1] 0.9
> pa
[1] 0.2
> pb
[1] 0.34
> pba*pa/pb
[1] 0.5294118
> pab-pba*pa/pb
[1] 0
>
Facebook
|
Post preview:
Close preview