1 Motivation

2 The continuous uniform distribution as prior

\[ f(p)= \begin{cases} \frac{1}{b-a},\; \text{if}\; a \leq p \leq b \\\ 0,\text{elsewhere} \end{cases} \]

\[ f(p)= \begin{cases} 1, \; \text{if}\; 0 \leq p \leq 1 \\ 0,\text{elsewhere} \end{cases} \]

\[ f(y|p)=\binom{n}{y}p^y(1-p)^{n-y} \]

\[ f(p|y)\approx p^y(1-p)^{n-y} \]

3 The Beta distribution as prior

\[ f(p)= \begin{cases} \frac{\Gamma(a+b)}{\Gamma(a)\Gamma(b)}p^{a-1}(1-p)^{b-1}, \; \text{if}\; 0 \leq p \leq 1 \\ 0, \text{elsewhere} \end{cases} \]

\[\begin{align} E(p) &= \frac{a}{a+b} \notag \\ Mode(p) &= \frac{a-1}{a+b-2}, \text{if}\; a,b > 1 \notag \\ Var(p) &= \frac{ab}{(a+b)^2(a+b+1)} \notag \end{align}\]

4 The Beta distribution as prior

\[ f(s|p)=\binom{n}{s}p^s(1-p)^f \]

with the Beta prior

\[ f(p)=\frac{\Gamma(a+b)}{\Gamma(a)\Gamma(b)}p^{a-1}(1-p)^{b-1} \]

will give us the posterior density

\[ f(p|s) \propto f(p) \times f(s|p)=p^{a+s-1}(1-p)^{b+f-1} \]

\[\frac{\Gamma(n+a+b)}{\Gamma(a+s)\Gamma(b+f)} \] then the resulting pdf is that of a beta distribution with \(a' = a + s\) and \(b' = b + f\)

5 From Beta prior to Beta posterior

6 How to determine the values of a and b of the Beta prior?

\[ P(Y\leq y_q)=q \]

7 Revisiting the sleep patterns of graduating college students

library(ProbBayes)
beta.select(list(x=0.3,p=0.50),
            list(x=0.5,p=0.90))
## [1] 3.26 7.19
p <- seq(0, 1, length = 1000)
a <- 3.26
b <- 7.19
s <- 11
f <- 16
prior <- dbeta(p, a, b)
like <- dbeta(p, s+1, f+1)
post <- dbeta(p, a+s, b+f)
plot(p, post, type = "l", ylab = "Density", lty = 2, lwd = 3)
lines(p, like, lty = 1, lwd = 3)
lines(p, prior, lty = 3, lwd = 3)
legend(.7, 4, c("Prior", "Likelihood", "Posterior"),
       lty=c(3, 1, 2), lwd = c( 3, 3, 3))

\[\hat{p}=\frac{a{'}}{a{'}+b{'}} = \frac{14.26}{14.26 + 23.19} \approx 0.38\]

round(mean(rbeta(1000,14.26, 23.19)),2)
## [1] 0.38
ProbBayes::beta_interval(0.9,c(14.26, 23.19))

ProbBayes::beta_area(lo = 0.75, hi = 1.0,
          shape_par = c(14.26, 23.19))

1- pbeta(0.75,14.26, 23.19)
## [1] 9.529512e-07
#simulates n=1000 observations
rval <- rbeta(1000, 14.26, 23.19)
prop <- sum(rval >= 0.75) / 1000
print(prop)
## [1] 0

8 Bayesian Prediction

9 Bayesian Prediction: an example

pred.prob <- pbetap(c(14.26, 23.19),20,12)
print(pred.prob)
## [1] 0.03976734
pred.prob.dist <- pbetap(c(14.26, 23.19),20,0:20)
discint(cbind(0:20,pred.prob.dist), 0.90)
## $prob
## [1] 0.9088212
## 
## $set
## [1]  4  5  6  7  8  9 10 11 12

10 Bayesian Prediction

pred_p_sim <- rbeta(1000, 14.26, 23.19)
pred_y_sim <- rbinom(1000, 20, pred_p_sim)
print(mean(pred_y_sim))
## [1] 7.676

11 Predictive Checking: Comparing Bayesian Models

beta.select(list(x=0.20,p=0.5),
            list(x=0.40,p=0.90))
## [1] 2.07 7.32
pred_p_sim1 <- rbeta(1000, 14.07,15.32)
pred_y_sim1 <- rbinom(1000, 20, pred_p_sim1)
mean(pred_y_sim1)
## [1] 9.513
probs <- c(0.5, 0.5) 
# assumes that the 2 priors are equally plausible
beta_prior1 <- c(3.26, 7.19)
beta_prior2 <- c(2.07, 7.32)
beta_par <- rbind(beta_prior1, beta_prior2)
output <- binomial.beta.mix(probs, beta_par, c(12, 8))
posterior_odds <- output$probs[1] / output$probs[2]
print(posterior_odds)
## beta_prior1 
##    2.536484

12 Posterior predictive checking

sim.p <- rbeta(1000, 14.26, 23.19)
sim.y <- rbinom(1000, 20, sim.p)
hist(sim.y,xlab="Simulated Y", main = " ")
abline(v = 7.668, col = "red", lwd = 3, lty = 2)