- Simulations
- Monte Carlo
- Markov Chain Monte Carlo
Given \(U \sim \text{Uniform}(0,1)\) and CDF \(F\) from a continuous distribution. Then \(X = F^{-1}(U)\) is a random variable with CDF \(F\).
Proof: \[ P(X\le a) = P (F^{-1}(U) \le a) = P ( U \le F(a)) = F(a) \]
Suppose \(X \sim \text{Exp}(\beta)\). Then we have density \[ f(x) = \beta^{-1} e^{-x/\beta} I(0<x<\infty) \] and CDF \[ F(x) = 1 - e^{-x/\beta} \] Also \[ y = 1 - e^{-x/\beta} \text{ iff } -x/\beta = \log (1-y) \text{ iff } x = -\beta \log (1-y). \] Thus, \(F^{-1} (y) = -\beta \log(1-y)\).
So if \(U \sim \text{Uniform}(0,1)\), then \(F^{-1} (u) = -\beta \log(1-u) \sim \text{Exp}(\beta)\).
x <- runif(10000) y <- - 3 * log(1-x) hist(y)
mean(y)
## [1] 3.017044
true.x <- seq(0,30, .5) true.y <- dexp(true.x, 1/3) hist(y, freq=F, breaks=30) points(true.x, true.y, type="l", col="red", lw=2)
Theorem: Let \(X \sim f_x\) and \(Y \sim f_y\) where the two densities have common support. Define \[ M = \sup_{x} \frac{f_x(x)}{f_y(x)}. \] If \(M< \infty\) then we can generate \(X \sim f_x\) as follows,
set.seed(1) mdat <- 30 # data sample size nsim <- 1e4 # Monte Carlo sample size theta.hat <- double(nsim) for (i in 1:nsim) { x <- rnorm(mdat) theta.hat[i] <- mean(x, trim = 0.25) }
mdat * mean(theta.hat^2)
## [1] 1.160488
mdat * var(theta.hat)
, is used because \(E(\theta) = 0\) by symmetry.R
gives 1.160488 for the Monte Carlo approximation to the relative efficiency.sqrt(var(mdat * theta.hat^2) / nsim)
## [1] 0.01648569
mdat
and nsim
appeared in our MCSE calculation.var(mdat * theta.hat^2)
that appeared in the MCSE is very different from the variance in mdat * var(theta.hat)
that might have been used as our Monte Carlo estimate.A Markov chain is a sequence \(X = \{ X_1, X_2, \dots \} \in \mathsf{X}\) of random elements having the property that the future depends on the past only through the present, that is, for any function \(g\) for which the expectations are defined \[ E \{ g(X_{n+1} , X_{n+2}, \dots) | X_n, X_{n-1}, \dots \} = E \{ g(X_{n+1} , X_{n+2}, \dots) | X_n \} . \]
Let \[ h(X_{n+1}) = E \{ g(X_{n+1} , X_{n+2}, \dots) | X_{n+1} \}. \] Then using the Markov property (above definition) and iterated expectations \[ \begin{aligned} E \{ g(X_{n+1} , X_{n+2}, \dots) | X_1, \dots, X_n \} & = E \{ E [ g(X_{n+1} , X_{n+2}, \dots) | X_1, \dots, X_{n+1} ] | X_1, \dots, X_n \} \\ & = E \{ E [ g(X_{n+1} , X_{n+2}, \dots) | X_{n+1} ] | X_1, \dots, X_n \} \\ & = E \{ h(X_{n+1}) | X_1, \dots, X_n \} \\ & = E \{ h(X_{n+1}) | X_n \} \; . \end{aligned} \]
Formally, \(\pi\) is an invariant distribution for a Markov kernel \(P\) if \(\pi P = \pi\).
Not all Markov chains have stationary distributions. But all of those of use in MCMC do since all Markov chains for MCMC are constructed to have a specified stationary distribution.
Theorem: Let \(X\) be a Harris ergodic Markov chain on \(\mathsf{X}\) with invariant distribution \(\pi\) and let \(g : \mathsf{X} \to \mathbb{R}\). Assume one of the following conditions:
Then for any initial distribution, as \(n \to \infty\) \[ \sqrt{n} (\bar{g}_n - \mu) \overset{D}{\rightarrow} N(0 , \sigma^2_g) . \]
ar1 <- function(m, rho, tau) { rho*m + rnorm(1, 0, tau) }
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) }
Let \(\rho=.95\) and \(\tau = 1\).
set.seed(20) tau <- 1 rho <- .95 out <- 10 # starting value n <- 10000 x <- ar1.gen(out, n, rho, tau)
plot(x[1:1000], xlab="Iteration", ylab="", pch=16,cex=0.4)
mu.hat <- mean(x) mu.hat
## [1] -0.1252549
b = 100 a = floor(n/b) y = sapply(1:a, function(k) return(mean(x[((k - 1) * b + 1):(k * b)]))) plot(y, xlab="Batch Number", ylab="", type="o") abline(h=mu.hat, lty=2)
var.hat = b * sum((y - mu.hat)^2)/(a - 1) se = sqrt(var.hat/n) c(var.hat, se)
## [1] 344.0124746 0.1854757
result <- c(round(mu.hat, 2), round(se, 2)) names(result) <- c("Estimate", "MCSE") result
## Estimate MCSE ## -0.13 0.19
ci <- mu.hat + c(-1,1) * 1.96 * se round(ci, 2)
## [1] -0.49 0.24
var.hat
## [1] 344.0125