-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter3.r
More file actions
220 lines (163 loc) · 5.48 KB
/
chapter3.r
File metadata and controls
220 lines (163 loc) · 5.48 KB
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
#3.2
p_grid <- seq(from=0, to=1, length.out=1000)
prior <- rep(1, 1000)
likelihood <- dbinom(6, size=9, prob=p_grid)
posterior <- likelihood * prior
posterior <- posterior / sum(posterior)
#3.3
samples <- sample(p_grid, prob=posterior, size=1e4, replace=TRUE)
#3.4
plot(samples)
#3.5
library(rethinking)
Options(mc.cores = parallel::detectCores())
dens(samples)
#3.6
# add posterior probs where p < 0.5
sum(posterior[p_grid <0.5])
#3.7
sum(samples < 0.5) / 1e4
#3.8
sum(samples> 0.5 & samples < 0.75) / 1e4
#3.9
quantile(samples, 0.8)
#3.10
quantile(samples, c(.1, .9))
#3.11
p_grid <- seq(from=0, to=1, length.out=1000)
prior <- rep(1, 1000)
likelihood <- dbinom(3, size=3, prob=p_grid)
posterior <- likelihood * prior
posterior <- posterior / sum(posterior)
samples <- sample(p_grid, size=1e4, replace=TRUE, prob=posterior)
#3.12
PI(samples, prob=0.5)
#3.13
HPDI(samples, prob=0.5)
#3.14
p_grid[which.max(posterior)]
#3.15
chainmode(samples, adj=0.01)
#3.16
mean(samples)
median(samples)
#3.17
# weighted average loss
sum(posterior*abs(0.5-p_grid))
#3.18
loss <- sapply(p_grid, function(d) sum(posterior*abs(d - p_grid)))
#3.19
p_grid[which.min(loss)]
#3.20
dbinom(0:2, size=2, prob=0.7)
#3.22
# sample from binomial
rbinom(10, size=2, prob=0.7)
#3.23
dummy_w <- rbinom(1e5, size=2, prob=0.7)
table(dummy_w)/1e5
#3.24
dummy_w <- rbinom(1e5, size=9, prob=0.7)
simplehist(dummy_w, xlab='dummy water count')
#3.25
w <- rbinom (1e4, size=9, prob=.6)
simplehist(w)
#3.26
w <- rbinom(1e4, size=9, prob=samples)
simplehist(w)
#
#Exercises
library(rethinking)
p_grid <- seq(from=0, to=1, length.out=1000)
prior <- rep(1, 1000)
likelihood <- dbinom(6, size=9, prob=p_grid)
posterior <- likelihood * prior
posterior <- posterior / sum(posterior)
set.seed(100)
samples <- sample(p_grid, prob=posterior, size=1e4, replace=TRUE)
#3E1
mean(samples < 0.2)
#3E2
mean(samples > 0.8)
#3E3
mean(samples > 0.2 & samples < 0.8)
#3E4
quantile(samples, 0.2)
#3E5
quantile(samples, 0.8)
#3E6
HPDI(samples, prob=.66)
# quarter of values representing water (p) provides 66% of probabilty mass
#3E7
PI(samples, prob=.66)
# Not too skewed, as HPDI and PI are almost equal
#3M1
p_grid <- seq(from=0, to=1, length.out=1000)
prior <- rep(1, 1000)
likelihood <- dbinom(8, size=15, prob=p_grid)
posterior <- likelihood * prior
posterior <- posterior / sum(posterior)
set.seed(100)
samples <- sample(p_grid, prob=posterior, size=1e4, replace=TRUE)
#3M2
HPDI(samples, prob=.9)
#3M3
#simulate posterior predictive check
# what is probabilty of observing 8 water in 15 tosses
w <- rbinom(1e4, size=15, prob=samples)
table(w)/1e4
#observating 8 water is .1473
#3M5
p_grid <- seq(from=0, to=1, length.out=1000)
prior <- ifelse(p_grid < 0.5, 0, 1)
likelihood <- dbinom(8, size=15, prob=p_grid)
posterior <- likelihood * prior
posterior <- posterior / sum(posterior)
set.seed(100)
samples <- sample(p_grid, prob=posterior, size=1e4, replace=TRUE)
HPDI(samples, prob=.9)
#the range is narrower
w <- rbinom(1e4, size=15, prob=samples)
table(w)/1e4
#probability is higher, but peak has shifted to higher values
# 70 percent of 15 is 10.5
#3H1
library(rethinking)
data(homeworkch3)
boys_born = sum(birth1) + sum(birth2)
p_grid <- seq(from=0, to=1, length.out=1000)
prior <- rep(1,200)
likelihood <- dbinom(boys_born, size=200, prob=p_grid)
posterior <- likelihood * prior
posterior <- posterior / sum(posterior)
plot(p_grid, posterior, type="b",
xlab="probability of boy", ylab="posterior probability")
p_grid[which.max(posterior)]
#3H2
set.seed(100)
samples <- sample(p_grid, prob=posterior, size=1e4, replace=TRUE)
#50, 89 and 97 highest probability intervals
HPDI(samples, prob=.5)
HPDI(samples, prob=.89)
HPDI(samples, prob=.97)
#3H3
simulation_boys <- rbinom(1e4, size = 200, prob=samples )
dens(simulation_boys)
abline(v=111)
#peak of simulation is a little bit higher than actual data, but generally the central peak is around 111.
#3H4
#3H3
#Calculate again? or using size 100 in simulation??
simulation_boys_first <- rbinom(1e4, size = 100, prob=samples )
dens(simulation_boys_first)
abline(v=sum(birth1))
#actually number of boys in firth birth is lower than in simulated distribution (which is based on 2 births)
#3H5
#Are they independent?
girl_first <- 100 - sum(birth1)
boy_after_girl <- birth2[birth1==0]
simulation3h5 <- rbinom(1e4, size=girl_first, prob=samples)
dens(simulation3h5)
abline(v=sum(boy_after_girl))
#male births that followed girls much higher than distribution male and female in first birth
# people selected who was born?