# Question 3a log.dens <- function(x1,x2,nu) { # log of the joint density of x1,x2 a <- (x1+x2)/(nu+2) log(nu) + log(nu+1) - (nu+2) * log(exp(a)+exp(a-x1)+exp(a-x2)) } nu <- 1 # set parameter nu #################################################### t <- seq(-5, 5, length.out=200) # define one side of the grid mat <- exp(outer(t,t,"log.dens",nu=nu)) # evaluate the density over a grid defined by t filled.contour(t,t,mat) # create the plot title("Density for nu=1") # give it a title #################################################### n <- 1e4 # set the sample size x <- matrix(nrow=n,ncol=2) # create matrix to hold sample cur.x <- c(0,0) # set the initial value cur.logf <- log.dens(cur.x[1],cur.x[2],nu=nu)# evaluate density at cur.x n.accepted <- 0 # counter of the accepted values for (i in 1:n) { # repeat n times ... new.x <- cur.x + sqrt(9) * rnorm(2) # create proposed value new.logf <- log.dens(new.x[1],new.x[2],nu=nu) # evaluate density at proposed value prob.acceptance <- exp(new.logf-cur.logf) # probability of acceptance if (runif(1)