- Importance Sampling
- Metropolis Hastings Algorithm
- Gibbs Sampler
Then if \(Y_1, Y_2, \dots Y_n\) form an identically \(Q\)-distributed sequence obeying the SLLN \[ \frac{1}{n} \sum_{i=1}^n g(Y_i) w^*(Y_i) = \frac{\frac{1}{n} \sum_{i=1}^n g(Y_i) w(Y_i)}{\frac{1}{n} \sum_{i=1}^n w(Y_i)} \to \frac{c_Y}{c_X} E_Q \left\{ g(Y) w(Y) \right\} = E_P \{ g(X) \} \] with probability 1 as \(n \to \infty\).
Note that \(w^*\) is a function having the properties of a probability distribution on the sample \(Y_1, \dots ,Y_n\) \[ w^*(Y_i) \ge 0, \; i = 1, \dots, n \text{ and } \sum_{i=1}^n w^*(Y_i) = 1 . \]
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)
| Count | Parameter | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|---|
| Captured | \(c_i\) | 30 | 22 | 29 | 26 | 31 | 32 | 35 |
| Newly Caught | \(m_i\) | 30 | 8 | 17 | 7 | 9 | 8 | 5 |
Let \(N\) be the population size, \(I\) be the number of census attempts where \(c_i\) were captured (\(I=7\) in our case), and \(r\) be the total number captured (\(r = \sum_{i=1}^I m_i = 84\))
Then \[ L(N,\alpha | c,r) \propto \frac{N!}{(N-r)!} \prod_{i=1}^{I} \alpha_i ^{c_i} (1-\alpha_i) ^ {N-c_i} \]
Then we can consider the chain \[ \left(N , \alpha\right) \rightarrow \left(N' , \alpha\right) \rightarrow \left(N' , \alpha'\right) \] or \[ \left(N , \alpha\right) \rightarrow \left(N , \alpha'\right) \rightarrow \left(N' , \alpha'\right) , \] where both involve a ``block'' update of \(\alpha\)
First, we can write the data into R
captured <- c(30, 22, 29, 26, 31, 32, 35) new.captures <- c(30, 8, 17, 7, 9, 8, 5) total.r <- sum(new.captures)
The following R code implements the Gibbs sampler
gibbs.chain <- function(n, N.start = 94, alpha.start = rep(.5,7)) {
output <- matrix(0, nrow=n, ncol=8)
for(i in 1:n){
neg.binom.prob <- 1 - prod(1-alpha.start)
N.new <- rnbinom(1, 85, neg.binom.prob) + total.r
beta1 <- captured + .5
beta2 <- N.new - captured + .5
alpha.new <- rbeta(7, beta1, beta2)
output[i,] <- c(N.new, alpha.new)
N.start <- N.new
alpha.start <- alpha.new
}
return(output)
}
How can we tell if the chain is mixing well?
Then we consider some preliminary simulations to ensure the chain is mixing well
trial <- gibbs.chain(1000)
plot.ts(trial[,1], main = "Trace Plot for N")
for(i in 1:7){
plot.ts(trial[,(i+1)], main = paste("Alpha", i))
}
acf(trial[,1], main = "Lag Plot for N")
for(i in 1:7){
acf(trial[,(i+1)], main = paste("Lag Alpha", i))
}
Now for a more complete simulation to estimate posterior means and a 90% Bayesian credible region
sim <- gibbs.chain(10000) N <- sim[,1] alpha1 <- sim[,2]
par(mfrow=c(1,2)) hist(N, freq=F, main="Estimated Marginal Posterior for N") hist(alpha1, freq=F, main ="Estimating Marginal Posterior for Alpha 1")
par(mfrow=c(1,1))
library(mcmcse)
## mcmcse: Monte Carlo Standard Errors for MCMC
## Version 1.3-2 created on 2017-07-03.
## copyright (c) 2012, James M. Flegal, University of California, Riverside
## John Hughes, University of Colorado, Denver
## Dootika Vats, University of Warwick
## Ning Dai, University of Minnesota
## For citation information, type citation("mcmcse").
## Type help("mcmcse-package") to get started.
ess(N)
## [1] 8736.499
ess(alpha1)
## [1] 8702.875
par(mfrow=c(1,2)) estvssamp(N) estvssamp(alpha1)
par(mfrow=c(1,1))
mcse(N)
## $est ## [1] 89.4245 ## ## $se ## [1] 0.02909663
mcse.q(N, .05)
## $est ## [1] 85 ## ## $se ## [1] 0.04338107
mcse.q(N, .95)
## $est ## [1] 94 ## ## $se ## [1] 0.06135594
mcse(alpha1)
## $est ## [1] 0.3376739 ## ## $se ## [1] 0.0005387394
mcse.q(alpha1, .05)
## $est ## [1] 0.2564481 ## ## $se ## [1] 0.0009462308
mcse.q(alpha1, .95)
## $est ## [1] 0.4224168 ## ## $se ## [1] 0.001145234