Stat365 Homework 4
Stat365 Homework 4
For the whole homework use random seed 36501, when you generate
independent samples from the approximated posterior using grid
approximation, and when you use MCMC to derive the posterior.
1. 6.5 b
2. Suppose your prior is what described in section 5.1 f ( π ) =e−e π , πϵ [0 ,1],
and conditional on π ,Y ∨π Bin(n , π ). Suppose that in n = 10
independent trials you observed Y = 2 success (same as in 6.5). Use
grid approximation (same grid as 6.5) to get the posterior distribution
of π and plot it. Also, get the overlay plot of the posterior you got from
6.5b and the posterior you got here, with different colors for each
posterior. Compare the two posteriors and describe the difference
between them. The overlay plot should look like this
To get the posterior from a prior with PDF function f ( pi ) ,you need to
first define a function in R for it using
f <- function(pi) {
exp(1) – exp(pi)
}
Then your prior will be defined as prior <- f(pi_grid)
To get an overlay plot, you need to
For each method, create a type variable for the prior type, for
example it can take value “Beta(2,2)” for your simulated results
when prior is given as Beta(2,2). It can take value “e - e^pi” for
your simulated results when prior is given as f ( π ) =e−e π
Use rbind function to concatenate the two simulated data together
by rows
Use ggplot(data = , aes( x = pi_grid, y = posterior,
color = type)) + geom_* to get the overlay plot
3. 6.6 b, 6.7 b
Please note that your likelihood for Poisson or normal will be the
product of the likelihood for each observed value. There is setup for
Grid approximation for Gamma-Poisson model in section 6.1
4. 6.13, 6.15, 6.17. Please note that
There is setup for MCMC for Gamma-Poisson model in section 6.2
Wen you setup MCMC for Normal – Normal model, since the
parameter and data are both unbounded, you can simply use
parameters {real mu} to denote that the parameter mu can be
any real number.
Produce the trace plot and density plot for each chain individually
for 6.13, produce the trace plot and density plot for all chains
combined for 6.15. and produce the tract plot and density plot for
the individual chain overlaying for 6.17
For all 6.13, 6.15 and 6.17, in addition to the questions a - e asked
in the textbook, please also do the following f – h
f. Get the effective sample size ratio, is it reasonable?
g. Output the autocorrelation plot for each of the chain (in one plot)
and describe whether the autocorrelation drops quickly to 0 by 5.
h. Calculate ^R , is it satisfactory?