-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chapter2.R
163 lines (123 loc) · 4.33 KB
/
Chapter2.R
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
library(rethinking)
##### EASY
#2E1
# (2) and (4) correspond to the statement
#2E2
# Only (3) corresponds to the expression
#2E3
# (1) and (3) correspond to the expression
#2E4
# Probability is an idea created by humans to somehow quantify how likely it is for the tossed globe to show water.
# It is subjective, as it is based on the creator's believes.
##### MEDIUM
#2M1
# Define grid
p_grid <- seq(from = 0, to = 1, length.out = 20)
#Define prior
prior <- rep(1, 20)
#Compute likelihood at each value in grid
# (1) W, W, W
# p^3
likelihood <- dbinom(3, size = 3, p = p_grid)
# (2) W, W, W, L
# p^3 * (1-p)
likelihood <- dbinom(3, size = 4, p = p_grid)
# (3) L, W, W, L, W, W, W
# p^5(1-p)^2
likelihood <- dbinom(5, size = 7, p = p_grid)
#Compute product of likelihood and prior
unstd.posterior <- likelihood * prior
# standardize the posterior so it sums to 1
posterior <- unstd.posterior / sum(unstd.posterior)
#Display the posterior distribution
plot(p_grid, posterior, type = "b",
x_lab = "probability of water", ylab = "posterior probability")
mtext("20 points")
#2M2
#Define grid
p_grid <- seq(from = 0, to = 1, length.out = 20)
#Define prior
prior <- rep(0, 20)
prior[11:20] <- 1
#Now just execute lines 31-47
#2M3
# P(Earth) = P(Mars) = 0.5
# P(Land | Earth) = 0.3
# P(Land | Mars) = 1.0
# P(Earth | Land ) = P (Earth, Land) / P( Land)
# = P (Land | Earth) * P (Earth) / P(Land)
# = P (Land | Earth) * P (Earth) / (P(Land | Earth) * P(Earth) + P(Land | Mars) * P(Mars))
# = 0.3 * 0.5 / (0.3*0.5 + 1 * 0.5)
# \approx 0.23
#2M4
# Card 1: BB
# Card 2: BW
# Card 3: WW
# Way cards are in the bag: BB BW WW
# P(First side = black) = 2/3
# P(Second side = black) = 1/2
# P(Second side = black | First side = black) = 1/2 * 2/3 = 1/3
# Way cards are in the bag: BB WB WW
# P(First side = black) = 1/3
# P(Second side = black) = 1
# P(Second side = black | First side = black) = 1/3 * 1
# Need to add these up
# P(Second side = black | First side = Black) = 2/3
#2M5
# The same exercise as #2M4, we only need to consider instead:
# Way cards are in bag: BB BB BW WW
# Way cards are in bag: BB BB WB WW
# Hence, I will not work through this exercise in detail
#2M6
# 1 way to get the BB card
# 2 ways to get the BW card
# 3 ways to get the WW card
# 2 * 1 ways to get the BB card and black in first pull
# 1 * 2 ways to get the BW card and black in the first pull
# 0 * 3 ways to get the WW card and black in the first pull
# 4 ways to obtain a black side in the first pull
# For the second pull to also be black, we must've pulled the BB card
# BB card stands for 2 of these 4 ways
# I.e. p = 2/4 = 1/2.
#2M7
# BB WB WW
# We draw B W
# 2 ways , 1 way -> 2 ways
# BB WW WB
# We draw B W
# 2 ways , 2 ways -> 4 ways
# BW WW BB
# We draw B W
# 1 way, 2 ways -> 2 ways
# Togehter -> 6 ways possible, 8 ways altogether to produce sequence
# p = 6/8 = 0.75
##### HARD
#2H1
# P(Species A) = P(Species B) = 1/2
# P(twins | Species A) = 0.1
# P(twins | Species B) = 0.2
# We are looking for the following probability P(twins born, t = 2 | twins born, t = 1)
# = P(twins born, t = 2, twins born, t = 1) / P(twins born, t = 1)
# P(twins born, t = 1) = P(twins born | species A)*P(Species A) + P(twins born | species B)*P(Species B)
# = 0.1 * 1/2 + 0.2 * 1/2 = 0.15
# P(twins born, t = 2, twins born, t = 1) = P(twins born | A) * P(twins born | A) * P(A) + P(twins born | B) * P(twins born | B) * P(B)
# = 0.1*0.1*1/2 + 0.2*0.2*1/2 = 0.005 + 0.04 = 0.025
# P(twins born, t = 2 | twins born, t = 1) = 0.025 / 0.15
#2H2
# P(A | twins, t = 1) = P(A, twins) / P(twins, t = 1)
# = 0.1 * 1/2 / 0.15 = 1/3
#2H3
# P(A | single, t = 2 , twins, t = 1) = 0.045 / 0.125
# P(single, twins) = 1/2 * 0.1 * 0.9 + 1/2 * 0.2 * 0.8 = 0.125
# P(A, single, twins) = 1/2 0.1 * 0.9 = 0.045
#2H4
# P(Test spec A | A) = 0.8 = P(Test A, A) / Spec(A)
# P(Test spec B | B) = 0.65 = P(Test B, B) / Spec(B)
# P(Test spec A | B) = 0.35
#Ignoring birth data
# P(A | test A) = P(A, test A) / P(test A)
# = 0.8 * spec(A) / ((0.8) * spec(A) + 0.35*spec(A))
# = 0.8 * 0.5 / (0.8 * 0.5 + 0.35 * 0.5) (*)
#Taking birth data into account
# P(A) = 0.045 / 0.125
# Replace 0.5 in formula (*) by the result of line 161