# # M A Tanner, Tools for Statistical Inference: Methods for Exploration # of Posterior Distributions and Likelihood Functions (2nd edn), # Berlin: Springer-Verlag 1993, Section 6.5.2. # N <- 1000 # number of chains n <- 100 # length of chain Theta <- rep(0,N) loglik <- function(theta){ 125*log(2+theta) + 38*log(1-theta) + 34*log(theta) } alpha <- function(theta,phi) min(exp(loglik(phi)-loglik(theta)),1) for (j in 1:N){ theta <- runif(1) k1 <- 0 for (i in 2:n){ phi <- runif(1) k <- rbinom(1,1,alpha(theta,phi)) k1 <- k1 + k theta <- theta + k*(phi-theta) } Theta[j] <- theta } plot(density(Theta))