Metropolis-Hastings algorithm
- Dr. Jarad Niemi
STAT 544 - Iowa State University
April 2, 2019
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 1 / 32
Metropolis-Hastings algorithm Dr. Jarad Niemi STAT 544 - Iowa State - - PowerPoint PPT Presentation
Metropolis-Hastings algorithm Dr. Jarad Niemi STAT 544 - Iowa State University April 2, 2019 Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 1 / 32 Outline Metropolis-Hastings algorithm Independence proposal Random-walk
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 1 / 32
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 2 / 32
Metropolis-Hastings algorithm
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 3 / 32
Metropolis-Hastings algorithm
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 4 / 32
Metropolis-Hastings algorithm
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 5 / 32
Independence Metropolis-Hastings
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 6 / 32
Independence Metropolis-Hastings
proposed= −1 proposed= 0 proposed= 1 current= −1 current= 0 current= 1 −2 −1 1 2 3 −2 −1 1 2 3 −2 −1 1 2 3 0.0 0.1 0.2 0.3 0.4 0.0 0.1 0.2 0.3 0.4 0.0 0.1 0.2 0.3 0.4
theta y distribution
proposal target
accept
FALSE TRUE
value
current proposed Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 7 / 32
Independence Metropolis-Hastings
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 8 / 32
Independence Metropolis-Hastings
0.0 0.2 0.4 −2 2
theta density distribution
proposal target Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 9 / 32
Independence Metropolis-Hastings
−1 1 25 50 75 100
Iteration (t) θ
0.0 2.5 5.0 7.5 10.0 25 50 75 100
Iteration (t) θ
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 10 / 32
Independence Metropolis-Hastings
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 11 / 32
Independence Metropolis-Hastings
0.0 0.1 0.2 0.3 0.4 −4 −2 2 4
x value density
target proposal 1 10 100 −4 −2 2 4
theta target / proposal
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 12 / 32
Independence Metropolis-Hastings
−2 2 250 500 750 1000
Iteration (t) θ
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 13 / 32
Random-walk Metropolis
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 14 / 32
Random-walk Metropolis
−3 −2 −1 1 2 3 0.0 0.1 0.2 0.3 0.4 x dnorm(x) Target Proposal
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 15 / 32
Random-walk Metropolis
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 16 / 32
Random-walk Metropolis
−2 −1 1 2 25 50 75 100
t θ
0.0 2.5 5.0 7.5 10.0 25 50 75 100
t θ
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 17 / 32
Random-walk Metropolis Optimal tuning parameter
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 18 / 32
Random-walk Metropolis Optimal tuning parameter
−0.4 0.0 0.4 0.8 25 50 75 100
iteration theta as.factor(v)
0.1 10 Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 19 / 32
Random-walk Metropolis Binomial model
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 20 / 32
Random-walk Metropolis Binomial model
n = 10000 log_q = function(theta, y=3, n=10) { if (theta<0 | theta>1) return(-Inf) (y-0.5)*log(theta)+(n-y-0.5)*log(1-theta) } current = 0.5 # Initial value samps = rep(NA,n) for (i in 1:n) { proposed = rnorm(1, current, 0.4) # tuning parameter is 0.4 logr = log_q(proposed)-log_q(current) if (log(runif(1)) < logr) current = proposed samps[i] = current } length(unique(samps))/n # acceptance rate [1] 0.3746 Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 21 / 32
Random-walk Metropolis Binomial model
2000 4000 6000 8000 10000 0.0 0.2 0.4 0.6 0.8 Index samps
Histogram of samps
samps Density 0.0 0.2 0.4 0.6 0.8 0.0 0.5 1.0 1.5 2.0 2.5 3.0
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 22 / 32
Random-walk Metropolis Normal model
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 23 / 32
Random-walk Metropolis Normal model
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 24 / 32
Random-walk Metropolis Normal model
n = 20 y = rnorm(n) sum_y2 = sum(y^2) nybar = mean(y) log_q = function(x) { if (x[2]<0) return(-Inf)
} B = 10 M = 100 samps = matrix(NA, nrow=B*M, ncol=2) a_rate = rep(NA, B) # Initialize S = diag(2) # S_0 current = c(0,1) Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 25 / 32
Random-walk Metropolis Normal model
# Adapt for (b in 1:B) { for (m in 1:M) { i = (b-1)*M+m proposed = mvrnorm(1, current, 2.4^2*S/2) logr = log_q(proposed) - log_q(current) if (log(runif(1)) < logr) current = proposed samps[i,] = current } a_rate[b] = length(unique(samps[1:i,1]))/length(samps[1:i,1]) S = var(samps[1:i,]) } a_rate [1] 0.0300000 0.2700000 0.3566667 0.4000000 0.4240000 0.4333333 0.4200000 0.4175000 0.4166667 0.4270000 var(samps) # S_B [,1] [,2] [1,] 0.04898222 0.00255292 [2,] 0.00255292 0.02365873 Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 26 / 32
Random-walk Metropolis Normal model
samps = as.data.frame(samps); names(samps) = c("mu","sigma"); samps$iteration = 1:nrow(samps) ggplot(melt(samps, id.var='iteration', variable.name='parameter'), aes(x=iteration, y=value)) + geom_line() + facet_wrap(~parameter, scales='free')+ theme_bw()
mu sigma 250 500 750 1000 250 500 750 1000 0.75 1.00 1.25 1.50 −0.5 0.0 0.5
iteration value
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 27 / 32
Random-walk Metropolis Normal model
# Final run K = 10000 samps = matrix(NA, nrow=K, ncol=2) for (k in 1:K) { proposed = mvrnorm(1, current, 2.4^2*S/2) logr = log_q(proposed) - log_q(current) if (log(runif(1)) < logr) current = proposed samps[k,] = current } length(unique(na.omit(samps[,1])))/length(na.omit(samps[,1])) # acceptance rate [1] 0.3947 Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 28 / 32
Random-walk Metropolis Normal model
mu sigma 2500 5000 7500 10000 2500 5000 7500 10000 1.0 1.5 2.0 −1.0 −0.5 0.0 0.5 1.0
iteration value
mu sigma −1.0 −0.5 0.0 0.5 1.0 1.0 1.5 2.0 0.0 0.5 1.0 1.5 2.0 0.0 0.5 1.0 1.5
value density
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 29 / 32
Random-walk Metropolis Hierarchical binomial model
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 30 / 32
Random-walk Metropolis Hierarchical binomial model
Corr: 0.984
alpha beta alpha beta 1000 2000 3000 4000 5000 1000 2000 3000 4000 2000 4000 6000 1000 2000 3000 4000
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 31 / 32
Summary
Jarad Niemi (STAT544@ISU) Metropolis-Hastings April 2, 2019 32 / 32