-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProblem 10.1.3 Posterior Sampling.Rmd
53 lines (48 loc) · 1.14 KB
/
Problem 10.1.3 Posterior Sampling.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
---
title: "Problem 10From: Ben Lambert. “A Student’s Guide to Bayesian Statistics”."
output: html_notebook
---
```{r}
fPosteriorPredictive <- function(aNumSamples){
lX <- vector(length=aNumSamples)
for(i in 1:aNumSamples){
theta <- rgamma(1, 3+35, 0.5+5)
X <- rpois(1, theta)
lX[i] <- X
}
return(lX) }
```
```{r}
X <- fPosteriorPredictive(10000)
hist(X, breaks=seq(0, 100, 1), xlim = c(0, 20),
xlab="10,000 posterior predictive samples")
```
```{r}
mean(X<=3)
```
```{r}
mean(X>=11)
```
```{r}
mean(X>=20)
```
“Problem 10.1.7 By using your previously determined posterior as a prior, update your posterior to reflect the new datum. Graph the PDF for this new distribution.”
```{r}
curve(dgamma(x, 3+35+20, 0.5+5+1), 0, 20, xlab = "lambda", ylab = "pdf")
```
```{r}
fPosteriorPredictive <- function(aNumSamples){
lX <- vector(length=aNumSamples)
for(i in 1:aNumSamples){
theta <- rgamma(1, 3+35+20, 0.5+5+1)
X <- rpois(1, theta)
lX[i] <- X
}
return(lX) }
X <- fPosteriorPredictive(10000)
hist(X, breaks=seq(0, 100, 1), xlim = c(0, 20),
xlab="10,000 posterior predictive samples")
```
```{r}
mean(X>=20)
```