pmix = function(theta){ pi*dnorm(theta,mu[1],1)+(1-pi)*dnorm(theta,mu[2],1) } pdf(file="comparingMCMCschemes.pdf",width=15,height=10) for (dist in c(0,5,10,20)){ pi = 0.8 mu = c(0,dist) thetas = seq(-5,dist+5,length=1000) den = pmix(thetas) par(mfrow=c(1,1),c(1.02,0.82,0.82,0.42)) plot(thetas,den,type="l",xlab=expression(theta),ylab="Density") # MCMC chain M0 = 1000 M = 2000 niter = M0 + M # Random-walk Metropolis-Hastings set.seed(3141593) sds.rwmh = c(1,5,10,20) draws.rwmh = matrix(0,niter,4) par(mfrow=c(2,6),mai=c(0.4,0.4,0.8,0.2)) for (i in 1:4){ sd.rwmh = sds.rwmh[i] theta = 5 draw.rwmh = rep(0,niter) for (iter in 1:niter){ theta1 = rnorm(1,theta,sd.rwmh) accept = min(1,pmix(theta1)/pmix(theta)) u = runif(1) if (u