# STATS 4CI3/6CI3 Winter 2019 # Some further examples of Markov Chain simulations # Consider sampling from the joint density # f(x,y) = 8xy 00) & (y<1), 8*x*y, 0) # The rgl package allows for nice 3D plots library(rgl) persp3d(f, xlim=c(0,1), ylim=c(0,1)) g <- function(x,y) (x>0)*(x<1)*(y>0)*(y<1) xy0 <- runif(2) x0 <- min(xy0) y0 <- max(xy0) # Since this is an indpendence MH algorithm we can generate # all the candidates at once x <- runif(N) y <- runif(N) u <- runif(N) for (i in 1:N) { x1 <- x[i] y1 <- y[i] p[i] <- f(x1,y1)/f(x0,y0)*g(x0,y0)/g(x1,y1) # Note that if x1>y1 then p[i]=0 so we will never # accept pairs that are outside the support. if (u[i]0 # For this we consider the joint distribution given by # x~unif(0,1), y|x~unif(x,1) # # This corresponds to the joint density # g(x,y) =1/(1-x) 00) & (x