normal.mh=function(mu,sigma, sigmaq=c(sqrt(.6),sqrt(.4)), istart=100, istop = 2000) { # I wasn't sure I wanted as little flexibility in sigmaq as I show here. I considered using a default matrix and # computing the cholesky decomposition. I also thought about generate a bivariate normal using mvrnom in # MASS. Both choices seem to defeat the purpose of a simple choice for q, so I stuck with the choice you # see here. There's nothing magic about using .6 and .4 for variances; it's just the same values I had used in # class notes. isig=solve(sigma) iestop=istop+istart x <- matrix(0,2*iestop,nrow=2) x[,1]=mu for(i in 2:iestop) { y=c(rnorm(1,x[1,(i-1)],sigmaq[1]),rnorm(1,x[2,(i-1)],sigmaq[2])) sigy=t(y-mu)%*%isig%*%(y-mu) sigx=t(x[,(i-1)]-mu)%*%isig%*%(x[,(i-1)]-mu) alph=min(exp(-.5*(sigy-sigx)),1) if(runif(1)