# 訊息

## English

X:(蛀) 蛀牙與否 (Cavity)
Y:(痛) 牙痛與否 (Toothache)
Z:(找) 是否找到痛的牙 (Catch)

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)
\begin{align} P(找到|蛀) = P(蛀|找到) \frac{P(找到)}{P(蛀)} \end{align}

# 解答

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.

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

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
, , 沒找

沒痛    痛

, , 找

沒痛    痛



> p[,"沒痛",]
沒找    找

> sum(p[,"沒痛",])
[1] 0.8


> p[,,"找"]
沒痛    痛

> 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

> 0<=p
, , 沒找

沒痛   痛

, , 找

沒痛   痛

> p<=1
, , 沒找

沒痛   痛

, , 找

沒痛   痛

> 0<=p & p <=1
, , 沒找

沒痛   痛

, , 找

沒痛   痛



>  sum(p[2,,2])/sum(p[2,,])
[1] 0.9


> p[,2,2]
沒蛀    蛀
0.016 0.108
> sum(p[,2,2])
[1] 0.124


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
, , 沒找

沒痛    痛

, , 找

沒痛    痛

> p[,"沒痛",]
沒找    找

> sum(p[,"沒痛",])
[1] 0.8
> p[,,"找"]
沒痛    痛

> 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

> 0<=p
, , 沒找

沒痛   痛

, , 找

沒痛   痛

> p<=1
, , 沒找

沒痛   痛

, , 找

沒痛   痛

> 0<=p & p <=1
, , 沒找

沒痛   痛

, , 找

沒痛   痛

> 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
>