############################################################################################# # # Posterior inference via # # 1) Sampling Importance Resampling (SIR) # 2) Metropolis-Hastings (MCMC) # # Source of the banana-shape joint posterior: # # Recursive Pathways to Marginal Likelihood Estimation with Prior-Sensitivity Analysis # By Ewan Cameron and Anthony Pettitt # Statistical Science # 2014, Vol. 29, No. 3, 397–419 # DOI: 10.1214/13-STS465 # Institute of Mathematical Statistics, 2014 # ############################################################################################# library(MASS) install.packages("shape") library(shape) ########################################## # Bivariate posterior # Prior of theta1 is U(-0.5,1) # Prior of theta2 is U(-0.5,1) # Prior independence of theta1 and theta2 ########################################## prior.likelihood = function(theta1,theta2){ prior=(1/1.5)^2 likelihood=exp(-(10*(0.45-theta1))^2/4-(20*(theta2/2-theta1^4))^2) return(prior*likelihood) } ######################## # SIR ######################## time.sir = system.time({ # Step 1: Sampling from proposal M = 100000 Mr = M/10 theta1.prior = runif(M,-0.5,1) theta2.prior = runif(M,-0.5,1) # Step 2: Computing resampling weights w = rep(0,M) for (i in 1:M) w[i] = prior.likelihood(theta1.prior[i],theta2.prior[i]) # Step 3: Resampling ind = sample(1:M,size=Mr,replace=TRUE,prob=w) theta1.post = theta1.prior[ind] theta2.post = theta2.prior[ind] thetas.sir = cbind(theta1.post,theta2.post) }) par(mfrow=c(2,2)) plot(theta1.prior,theta2.prior,xlab=expression(theta[1]),ylab=expression(theta[2]), main="Prior draws",pch=".") plot(theta1.post,theta2.post,xlim=range(theta1.prior),ylim=range(theta2.prior), xlab=expression(theta[1]),ylab=expression(theta[2]),pch=".", main="Posterior draws") hist(theta1.post,prob=TRUE,xlab=expression(theta[1]),main="",xlim=c(-0.5,1),col=3) title("Marginal posterior") abline(h=1/1.5,col=2,lwd=3) hist(theta2.post,prob=TRUE,xlab=expression(theta[2]),main="",xlim=c(-0.5,1),col=3) title("Marginal posterior") abline(h=1/1.5,col=2,lwd=3) # Posterior means E1.sir = mean(theta1.post) E2.sir = mean(theta2.post) ######################## # Metropolis-Hastings ######################## # Initial values theta1 = 0.25 theta2 = 0.25 M0 = 10000 M = 10000 L = 9 niter = M0+M*L thetas.mh = matrix(0,niter,2) time.mh = system.time({ for (i in 1:niter){ theta1d = runif(1,-0.5,1) theta2d = runif(1,-0.5,1) alpha = min(1,prior.likelihood(theta1d,theta2d)/prior.likelihood(theta1,theta2)) if (runif(1)