-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeeting2b.Rmd
169 lines (140 loc) · 3.58 KB
/
meeting2b.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
---
title: "3 September 2022 jointprob meeting"
output: html_notebook
editor_options:
markdown:
wrap: 72
---
# Grid Approximations
A. Five-step protocol in one dimension.
1. Define a grid
```{r}
p_grid <-seq(from=0,to=1,length.out=100)
```
2. Define the prior
```{r}
prior <-rep(1,100)/sum(rep(1,100))
sum(prior)
```
3. Compute the likelihood at each value in grid
```{r}
likelihood <- dbinom(6,size=9,prob=p_grid)
sum(likelihood)
```
4. Compute product of likelihood and prior
```{r}
unstd.posterior <-likelihood*prior
```
5. Standardize the posterior, so it sums to 1.
```{r}
posterior <-unstd.posterior/sum(unstd.posterior)
sum(posterior)
```
```{r}
plot( p_grid, posterior,
type="b",
xlab="probability ofwater",
ylab="posteriorprobability")
mtext( "100 points")
```
```{r}
p_grid <-seq(from=0,to=1,length.out=100)
prior <-rep(1,100)/sum(rep(1,100))
sum(prior)
likelihood <-dbinom(6,size=9,prob=p_grid)
sum(likelihood)
unstd.posterior <-likelihood*prior
posterior <-unstd.posterior/sum(unstd.posterior)
sum(posterior)
plot( p_grid, posterior,
type="b",
xlab="probability ofwater",
ylab="posteriorprobability")
mtext( "100 points")
```
## Repeat with Step prior
```{r}
sprior <- ifelse(p_grid < 0.5, 0, 1)/sum(ifelse(p_grid < 0.5, 0, 1))
sum(sprior)
slikelihood <-dbinom(6,size=9,prob=p_grid)
unstd.sposterior <-slikelihood*sprior
sposterior <-unstd.sposterior/sum(unstd.sposterior)
sum(sposterior)
plot( p_grid, sposterior,
type="b",
xlab="probability of water",
ylab="posterior probability")
points(p_grid, sprior, col='magenta')
points(p_grid, posterior, col='cyan')
mtext( "Step prior, 100 grid points")
legend(x = "topright",
c("sposterior", "sprior", "posterior"),
cex=.8,
col=c("black","magenta","cyan"),
lwd = 2,
bty = "n"
)
```
## Repeat with Peaked prior
```{r}
pkprior <- exp( -5*abs(p_grid -0.5))/sum(exp( -5*abs(p_grid -0.5)))
sum(pkprior)
pklikelihood <-dbinom(6,size=9,prob=p_grid)
unstd.pkposterior <-pklikelihood*pkprior
pkposterior <-unstd.pkposterior/sum(unstd.pkposterior)
sum(pkposterior)
plot( p_grid, pkposterior,
type="b",
xlab="Probability of water",
ylab="Posterior probability")
points(p_grid, pkprior, col='magenta')
points(p_grid, posterior, col='cyan')
mtext( "Peaked prior, 100 grid points")
legend(x = "topright",
c("pkposterior", "pkprior", "posterior"),
cex=.8,
col=c("black","magenta","cyan"),
lwd = 2,
bty = "n"
)
```
## Triangular distriution
```{r}
#install.packages(extraDistr)
library(extraDistr)
??extraDistr
x <- rtriang(1e5, 0, 1, 0.1)
hist(x, 100, freq = FALSE)
#curve(dtriang(x, 5, 7, 6), 3, 10, n = 500, col = "red", add = TRUE)
#hist(ptriang(x, 5, 7, 6))
#plot(ecdf(x))
#curve(ptriang(x, 5, 7, 6), 3, 10, n = 500, col = "red", lwd = 2, add = TRUE)
```
## Repeat with triangle prior
```{r}
tprior <- dtriang(p_grid, 0, 1, 0.1)/sum(dtriang(p_grid, 0, 1, 0.1))
sum(tprior)
plot( p_grid, tprior,
type="b",
xlab="Probability of water",
ylab="Prior probability")
```
```{r}
tlikelihood <-dbinom(6,size=9,prob=p_grid)
unstd.tposterior <-tlikelihood*tprior
tposterior <-unstd.tposterior/sum(unstd.tposterior)
tposterior
plot( p_grid, tposterior,
type="b",
xlab="Probability of water",
ylab="Prior and posterior probabilities")
points(p_grid, tprior, col='magenta')
points(p_grid, posterior, col='cyan')
legend(x = "topright",
c("tposterior", "tprior", "posterior"),
cex=.8,
col=c("black","magenta","cyan"),
lwd = 2,
bty = "n"
)
```