#Target distribution target = function(theta){ 0.2*dnorm(theta,2,1)+0.8*dnorm(theta,10,1) } # 1st Proposal distributon proposal1 = function(theta){ dnorm(theta,6,5) } # 2nd Proposal distributon proposal2 = function(theta,thetaold){ dnorm(theta,thetaold,2) } theta = seq(-9,21,length=1000) par(mfrow=c(1,1)) plot(theta,target(theta),xlab=expression(theta),ylab="Density",type="l",lwd=2) lines(theta,proposal1(theta),lwd=2,col=2) legend("topleft",legend=c("Target","Proposal"),col=1:2,lwd=2,lty=1) # Metropolis-Hastings algorithm (based on 1st proposal) th = -9 set.seed(233243) M0 = 5000 M = 5000 niter = M0+M thetas = rep(0,niter) for (i in 1:niter){ thstar = rnorm(1,6,5) num = target(thstar)/proposal1(thstar) den = target(th)/proposal1(th) accept = min(1,num/den) if (runif(1)