Rebecca C. Steorts
June 11, 2016
Recall the Beta-Bernoulli model: \[ X\mid \theta \sim \text{Bernoulli}(\theta) \] \[ \theta \sim \text{Beta}(a,b) \] where \( a,b \) are fixed parameters.
Goals:
Let's review the tasks and break them down into the most important information in each task.
We now provide solutions to the five tasks.
The data can be simulated as follows:
set.seed(123)
obs.data <- rbinom(n = 100, size = 1, prob = 0.01)
### Bernoulli LH Function ###
# Input: the observed data, theta grid #
# Output: Produces likelihood values #
myBernLH <- function(obs.data, theta){
N <- length(obs.data)
x <- sum(obs.data)
LH <- ((theta)^x)*((1 - theta)^(N-x))
return(LH)
}
We now plot the likelihood as a function of \( \theta. \)
### Plot LH for a grid of theta values ###
# Create the grid #
theta.sim <- seq(from = 0, to = 1, length.out = 1000)
# Store the LH Values #
sim.LH <- myBernLH(obs.data = obs.data, theta = theta.sim)
# Create the Plot #
### Function to determine posterior parameters based on ###
### observed data and prior assumptions ###
# Inputs - Prior Paramters, observed data #
myPosteriorParam <- function(pri.a, pri.b, obs.data){
N <- length(obs.data)
x <- sum(obs.data)
post.a <- pri.a + x
post.b <- pri.b + N - x
post.param <- list('post.a' = post.a,
'post.b' = post.b)
return(post.param)
}
# Find posterior parameters for two different priors #
# a = 1, b = 1 #
non.inform <- myPosteriorParam(pri.a = 1, pri.b = 1, obs.data = obs.data)
inform <- myPosteriorParam(pri.a = 3, pri.b = 1, obs.data = obs.data)
# a = 3, b = 1 #
# print the output values
print(c(non.inform,inform))
$post.a
[1] 2
$post.b
[1] 100
$post.a
[1] 4
$post.b
[1] 100
We first create non-informative and informative priors and posteriors
### Create a plot of LH, Pri, Posterior using the simulated seq ###
non.inform.den <- dbeta(x = theta.sim, shape1 = non.inform$post.a,
shape2 = non.inform$post.b)
inform.den <- dbeta(x = theta.sim, shape1 = inform$post.a,
shape2 = inform$post.b)
pri.inform <- dbeta(x = theta.sim, shape1 = 3,
shape2 = 1)
pri.non.inform <- dbeta(x = theta.sim, shape1 = 1,
shape2 = 1)
What do you observe?
require(xtable)
## Create confidence/ credible intervals - informative ##
# Credible Interval #
my.sim <- rbeta(n = 1000, shape1 = inform$post.a,
shape2 = inform$post.b)
my.credI <- quantile(my.sim, prob = c(0.025, 0.975))
# Confidence Interval #
p.hat <- sum(obs.data) / length(obs.data)
my.confI <- p.hat + qnorm(p = c(0.025, 0.975)) *
sqrt(p.hat*(1-p.hat)/length(obs.data))
# Store the results #
results <- rbind(my.credI, my.confI)
rownames(results) <- c('Credible Interval', 'Confidence Interval')
# present these results in a nice table #
print(xtable(results, digits = 4), comment = FALSE)
\begin{table}[ht] \centering \begin{tabular}{rrr} \hline & 2.5\% & 97.5\% \ \hline Credible Interval & 0.0107 & 0.0799 \ Confidence Interval & -0.0095 & 0.0295 \ \hline \end{tabular} \end{table}