post = function(theta){ exp(-0.5*(prod(theta)^2+sum(theta^2)-8*sum(theta))) } gradient = function(theta){ c(-theta[1]*theta[2]^2+theta[1]-4, -theta[2]*theta[1]^2+theta[2]-4) } leapfrog = function(theta,p,eps,M){ p = p + (eps/2)*gradient(theta) theta = theta + eps*iM%*%p p = p + (eps/2)*gradient(theta) return(list(theta=theta,p=p)) } N = 200 th1 = seq(-3,7,length=N) th2 = seq(-3,7,length=N) f = matrix(0,N,N) for (i in 1:N) for (j in 1:N) f[i,j] = post(c(th1[i],th2[j])) contour(th1,th2,f,drawlabels=FALSE) k = 2 # SIR M = 10000 thetas.sir = matrix(0,M,k) # RW-MH set.seed(32425) sd.th = 0.1 burnin = 1000 N = 20000 niter = burnin + N thetas.rwmh = matrix(0,niter,k) theta = c(0,0) for (iter in 1:niter){ theta.new = rnorm(2,theta,sd.th) accept = min(1,post(theta.new)/post(theta)) if (runif(1)