- Like Ordinary Monte Carlo (OMC), but better?
- SLLN and Markov chain CLT
- Variance estimation
- AR(1) example
- Metropolis-Hastings algorithm (with an exercise)
The following will provide an observation from the MC 1 step ahead
ar1 <- function(m, rho, tau) { rho*m + rnorm(1, 0, tau) }
Next, we add to this function so that we can give it a Markov chain and the result will be p observations from the Markov chain
ar1.gen <- function(mc, p, rho, tau, q=1) { loc <- length(mc) junk <- double(p) mc <- append(mc, junk) for(i in 1:p){ j <- i+loc-1 mc[(j+1)] <- ar1(mc[j], rho, tau) } return(mc) }
set.seed(20) library(mcmcse)
## mcmcse: Monte Carlo Standard Errors for MCMC ## Version 1.2-1 created on 2016-03-24. ## copyright (c) 2012, James M. Flegal, University of California,Riverside ## John Hughes, University of Minnesota ## Dootika Vats, University of Minnesota ## For citation information, type citation("mcmcse"). ## Type help("mcmcse-package") to get started.
tau <- 1 rho <- .95 out <- 0 eps <- 0.1 start <- 1000 r <- 1000
out <- ar1.gen(out, start, rho, tau) MCSE <- mcse(out)$se N <- length(out) t <- qt(.975, (floor(sqrt(N) - 1))) muhat <- mean(out) check <- MCSE * t while(eps < check) { out <- ar1.gen(out, r, rho, tau) MCSE <- append(MCSE, mcse(out)$se) N <- length(out) t <- qt(.975, (floor(sqrt(N) - 1))) muhat <- append(muhat, mean(out)) check <- MCSE[length(MCSE)] * t } N <- seq(start, length(out), r) t <- qt(.975, (floor(sqrt(N) - 1))) half <- MCSE * t sigmahat <- MCSE*sqrt(N) N <- seq(start, length(out), r) / 1000
plot(N, muhat, main="Estimates of the Mean", xlab="Iterations (in 1000's)") points(N, muhat, type="l", col="red") ; abline(h=0, lwd=3) legend("bottomright", legend=c("Observed", "Actual"), lty=c(1,1), col=c(2,1), lwd=c(1,3))
plot(N, sigmahat, main="Estimates of Sigma", xlab="Iterations (in 1000's)") points(N, sigmahat, type="l", col="red"); abline(h=20, lwd=3) legend("bottomright", legend=c("Observed", "Actual"), lty=c(1,1), col=c(2,1), lwd=c(1,3))
plot(N, 2*half, main="Calculated Interval Widths", xlab="Iterations (in 1000's)", ylab="Width", ylim=c(0, 1.8)) points(N, 2*half, type="l", col="red"); abline(h=0.2, lwd=3) legend("topright", legend=c("Observed", "Cut-off"), lty=c(1,1), col=c(2,1), lwd=c(1,3))
A key problem is we only get to observe \(t\) observations from \(\left\{ X_t \right\}\), which are serially dependent
Setting \(X_0 = x_0\) (somehow), the Metropolis-Hastings algorithm generates \(X_{t+1}\) given \(X_t = x_t\) as follows:
In this case, the MH ratio is
\[
R(x_t, X^*) = \frac{f(x^*) g (x_t)}{f(x_t) g (x^*)},
\] and the resulting Markov chain will be irreducible and aperiodic if \(g > 0\) where \(f>0\)
A good envelope function \(g\) should resemble \(f\), but should cover \(f\) in the tails
Common choices of \(h(\cdot)\) are symmetric zero mean random variables with a scale parameter, e.g. a Uniform(\(-a,a\)), Normal(\(0, \sigma^2\)), \(c*T_{\nu}, \dots\)
If the support of \(f\) is connected and \(h\) is positive in a neighborhood of 0, then the chain is irreducible and aperiodic.
Exercise: Suppose \(f \sim Exp(1)\)
Independence Metropolis sampler with Exp(\(\theta\)) proposal
ind.chain <- function(x, n, theta = 1) { ## if theta = 1, then this is an iid sampler m <- length(x) x <- append(x, double(n)) for(i in (m+1):length(x)){ x.prime <- rexp(1, rate=theta) u <- exp((x[(i-1)]-x.prime)*(1-theta)) if(runif(1) < u) x[i] <- x.prime else x[i] <- x[(i-1)] } return(x) }
Random Walk Metropolis sampler with N(\(0,\sigma\)) proposal
rw.chain <- function(x, n, sigma = 1) { m <- length(x) x <- append(x, double(n)) for(i in (m+1):length(x)){ x.prime <- x[(i-1)] + rnorm(1, sd = sigma) u <- exp((x[(i-1)]-x.prime)) u if(runif(1) < u && x.prime > 0) x[i] <- x.prime else x[i] <- x[(i-1)] } return(x) }
trial0 <- ind.chain(1, 500, 1) trial1 <- ind.chain(1, 500, 2) trial2 <- ind.chain(1, 500, 1/2) rw1 <- rw.chain(1, 500, .2) rw2 <- rw.chain(1, 500, 1) rw3 <- rw.chain(1, 500, 5)