This chapter has been an informal introduction to Markov chain Monte Carlo (MCMC) estimation. The goal has been to introduce the purpose and approach MCMC algorithms. The major algorithms introduced were the Metropolis, Gibbs sampling, and Hamiltonian Monte Carlo algorithms. Each has its advantages and disadvantages. The ulam function in the rethinking package was introduced. It uses the Stan (mc-stan.org) Hamiltonian Monte Carlo engine to fit models as they are defined in this book. General advice about diagnosing poor MCMC fits was introduced by the use of a couple of pathological examples.
Place each answer inside the code chunk (grey box). The code chunks should contain a text response or a code that completes/answers the question or activity requested. Problems are labeled Easy (E), Medium (M), and Hard(H).
Finally, upon completion, name your final output .html file as: YourName_ANLY505-Year-Semester.html and publish the assignment to your R Pubs account and submit the link to Canvas. Each question is worth 5 points.
9E1. Which of the following is a requirement of the simple Metropolis algorithm?
#3. The proposal distribution must be symmetric.
9E2. Gibbs sampling is more efficient than the Metropolis algorithm. How does it achieve this extra efficiency? Are there any limitations to the Gibbs sampling strategy?
#gibbs sampling is more efficient since it use conjugate priors and there are fewer rejections than Metropolis algorithm. The limitation occurs when there are complex models of hundreds and more parameters
9E3. Which sort of parameters can Hamiltonian Monte Carlo not handle? Can you explain why?
#Monte Carlo cannot handle discrete parameters.
9E4. Explain the difference between the effective number of samples, n_eff as calculated by Stan, and the actual number of samples.
# n_eff estimate the number of independent/uncorrelated samples. The actual number of samples is the number of all samples in the distribution
9E5. Which value should Rhat approach, when a chain is sampling the posterior distribution correctly?
# Rhat will approach to 1
9E6. Sketch a good trace plot for a Markov chain, one that is effectively sampling from the posterior distribution. What is good about its shape? Then sketch a trace plot for a malfunctioning Markov chain. What about its shape indicates malfunction?
library(rethinking)
data(rugged)
d <- rugged
d$log_gdp <- log(d$rgdppc_2000)
dd <- d[ complete.cases(d$rgdppc_2000) , ]
dd$log_gdp_std <- dd$log_gdp / mean(dd$log_gdp)
dd$rugged_std <- dd$rugged / max(dd$rugged)
dd$cid <- ifelse( dd$cont_africa==1 , 1 , 2 )
m8.5 <- quap(
alist(
log_gdp_std ~ dnorm( mu , sigma ) ,
mu <- a[cid] + b[cid]*( rugged_std - 0.215 ) ,
a[cid] ~ dnorm( 1 , 0.1 ) ,
b[cid] ~ dnorm( 0 , 0.3 ) ,
sigma ~ dexp( 1 )
) ,
data=dd )
precis( m8.5 , depth=2 )
## mean sd 5.5% 94.5%
## a[1] 0.8865669 0.015674555 0.86151591 0.91161784
## a[2] 1.0505774 0.009935863 1.03469792 1.06645678
## b[1] 0.1324152 0.074199298 0.01383038 0.25099999
## b[2] -0.1424671 0.054745545 -0.22996104 -0.05497314
## sigma 0.1094859 0.005934190 0.10000192 0.11896988
dat_slim <- list(
log_gdp_std = dd$log_gdp_std,
rugged_std = dd$rugged_std,
cid = as.integer( dd$cid )
)
m9.1 <- ulam(
alist(
log_gdp_std ~ dnorm( mu , sigma ) ,
mu <- a[cid] + b[cid]*( rugged_std - 0.215 ) ,
a[cid] ~ dnorm( 1 , 0.1 ) ,
b[cid] ~ dnorm( 0 , 0.3 ) ,
sigma ~ dexp( 1 )
) , data=dat_slim , chains=4 , cores=4 , iter=1000)
show(m9.1)
## Hamiltonian Monte Carlo approximation
## 2000 samples from 4 chains
##
## Sampling durations (seconds):
## warmup sample total
## chain:1 0.07 0.05 0.12
## chain:2 0.04 0.04 0.08
## chain:3 0.06 0.05 0.11
## chain:4 0.07 0.04 0.11
##
## Formula:
## log_gdp_std ~ dnorm(mu, sigma)
## mu <- a[cid] + b[cid] * (rugged_std - 0.215)
## a[cid] ~ dnorm(1, 0.1)
## b[cid] ~ dnorm(0, 0.3)
## sigma ~ dexp(1)
precis(m9.1, 2)
## mean sd 5.5% 94.5% n_eff Rhat4
## a[1] 0.8867217 0.016338863 0.85957563 0.91173657 2645.244 0.9990264
## a[2] 1.0504420 0.009899126 1.03487430 1.06658873 2786.581 0.9991639
## b[1] 0.1329518 0.077675003 0.00891268 0.26055939 2894.593 0.9989814
## b[2] -0.1445457 0.054386124 -0.23068667 -0.05756065 2468.251 0.9985194
## sigma 0.1115960 0.005793520 0.10275828 0.12128524 1981.100 1.0006205
m9.2<-ulam(
alist(
y ~ dnorm(mu,sigma),
mu<-alpha,
alpha ~ dnorm(0,1000),
sigma~dexp(0.0001)
),data=list(y=c(-1,1)),chains=4,cores=4
)
## Warning: There were 57 divergent transitions after warmup. See
## http://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
## to find out why this is a problem and how to eliminate them.
## Warning: There were 3 transitions after warmup that exceeded the maximum treedepth. Increase max_treedepth above 10. See
## http://mc-stan.org/misc/warnings.html#maximum-treedepth-exceeded
## Warning: Examine the pairs() plot to diagnose sampling problems
## Warning: The largest R-hat is 1.13, indicating chains have not mixed.
## Running the chains for more iterations may help. See
## http://mc-stan.org/misc/warnings.html#r-hat
## Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
## Running the chains for more iterations may help. See
## http://mc-stan.org/misc/warnings.html#bulk-ess
## Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
## Running the chains for more iterations may help. See
## http://mc-stan.org/misc/warnings.html#tail-ess
traceplot(m9.2)
## [1] 1000
## [1] 1
## [1] 1000
9E7. Repeat the problem above, but now for a trace rank plot.
trankplot(m9.1)
trankplot(m9.2)
9M1. Re-estimate the terrain ruggedness model from the chapter, but now using a uniform prior for the standard deviation, sigma. The uniform prior should be dunif(0,1). Use ulam to estimate the posterior. Does the different prior have any detectible influence on the posterior distribution of sigma? Why or why not?
data(rugged)
d <- rugged
d$log_gdp <- log(d$rgdppc_2000)
dd <- d[ complete.cases(d$rgdppc_2000) , ]
dd$log_gdp_std <- dd$log_gdp/ mean(dd$log_gdp)
dd$rugged_std<- dd$rugged/max(dd$rugged)
dd$cid<-ifelse(dd$cont_africa==1,1,2)
m8.5 <- quap(
alist(
log_gdp_std ~ dnorm( mu , sigma ) ,
mu <- a[cid] + b[cid]* (rugged_std-0.215) ,
a[cid] ~ dnorm(1,0.1),
b[cid] ~ dnorm(0,0.3),
sigma ~ dexp(1)
) ,
data=dd)
precis(m8.5 , depth=2)
## mean sd 5.5% 94.5%
## a[1] 0.8865417 0.015674761 0.86149043 0.91159302
## a[2] 1.0505593 0.009936038 1.03467962 1.06643903
## b[1] 0.1325464 0.074200330 0.01395991 0.25113282
## b[2] -0.1426812 0.054746197 -0.23017619 -0.05518619
## sigma 0.1094877 0.005934424 0.10000335 0.11897206
m8.5_unif <- quap(
alist(
log_gdp_std ~ dnorm( mu , sigma ) ,
mu <- a[cid] + b[cid]* (rugged_std-0.215) ,
a[cid] ~ dnorm(1,0.1),
b[cid] ~ dnorm(0,0.3),
sigma ~ dunif(0,1)
) ,
data=dd)
precis(m8.5_unif , depth=2)
## mean sd 5.5% 94.5%
## a[1] 0.8865670 0.015680504 0.86150651 0.91162746
## a[2] 1.0505777 0.009939697 1.03469217 1.06646328
## b[1] 0.1326126 0.074226256 0.01398473 0.25124052
## b[2] -0.1425548 0.054766080 -0.23008153 -0.05502798
## sigma 0.1095286 0.005939969 0.10003536 0.11902179
pairs(m8.5_unif)
9M2. Modify the terrain ruggedness model again. This time, change the prior for b[cid] to dexp(0.3). What does this do to the posterior distribution? Can you explain it?
data(rugged)
d <- rugged
d$log_gdp <- log(d$rgdppc_2000)
dd <- d[ complete.cases(d$rgdppc_2000) , ]
dd$log_gdp_std <- dd$log_gdp / mean(dd$log_gdp)
dd$rugged_std <- dd$rugged / max(dd$rugged)
dd$cid <- ifelse( dd$cont_africa==1 , 1 , 2 )
m9.1 <- ulam(
alist(
log_gdp_std ~ dnorm( mu , sigma ) ,
mu <- a[cid] + b[cid]*( rugged_std - 0.215 ) ,
a[cid] ~ dnorm( 1 , 0.1 ) ,
b[cid] ~ dexp(0.3) ,
sigma ~ dexp( 1 )
) , data=dat_slim , chains=4 , cores=4 )
show( m9.1 )
## Hamiltonian Monte Carlo approximation
## 2000 samples from 4 chains
##
## Sampling durations (seconds):
## warmup sample total
## chain:1 0.40 0.23 0.63
## chain:2 0.36 0.23 0.58
## chain:3 0.47 0.17 0.64
## chain:4 0.42 0.13 0.55
##
## Formula:
## log_gdp_std ~ dnorm(mu, sigma)
## mu <- a[cid] + b[cid] * (rugged_std - 0.215)
## a[cid] ~ dnorm(1, 0.1)
## b[cid] ~ dexp(0.3)
## sigma ~ dexp(1)
precis(m9.1, 2)
## mean sd 5.5% 94.5% n_eff Rhat4
## a[1] 0.88769142 0.016096756 0.861018092 0.91402859 1623.4755 1.0008459
## a[2] 1.04805441 0.010145572 1.031565845 1.06399498 1758.1827 0.9993031
## b[1] 0.14420350 0.074703995 0.029129295 0.26810729 965.2774 1.0037840
## b[2] 0.01798377 0.017681205 0.001057972 0.05397641 1886.6241 0.9984038
## sigma 0.11400570 0.006096506 0.104747403 0.12375238 1399.5379 0.9999419
9M3. Re-estimate one of the Stan models from the chapter, but at different numbers of warmup iterations. Be sure to use the same number of sampling iterations in each case. Compare the n_eff values. How much warmup is enough?
m9.1 <- ulam(
alist(
log_gdp_std ~ dnorm( mu , sigma ) ,
mu <- a[cid] + b[cid]*( rugged_std - 0.215 ) ,
a[cid] ~ dnorm( 1 , 0.1 ) ,
b[cid] ~ dnorm( 0 , 0.3 ) ,
sigma ~ dexp( 1 )
) , data=dat_slim , chains=4 , cores=4,iter=1000 )
precis( m9.1 , 2 )
## mean sd 5.5% 94.5% n_eff Rhat4
## a[1] 0.8867241 0.015609865 0.862394985 0.91062438 2721.598 0.9984524
## a[2] 1.0503786 0.010600678 1.033776642 1.06730740 4080.025 0.9992420
## b[1] 0.1324841 0.077079797 0.009557509 0.25822546 1838.689 1.0004896
## b[2] -0.1417984 0.055667178 -0.232323218 -0.05060567 2481.425 0.9993072
## sigma 0.1115912 0.005896351 0.102662135 0.12122701 2038.821 1.0013389
9H1. Run the model below and then inspect the posterior distribution and explain what it is accomplishing.
mp <- ulam(
alist(
a ~ dnorm(0,1),
b ~ dcauchy(0,1)
), data=list(y=1) , chains=1 )
##
## SAMPLING FOR MODEL 'bcf56ee89f6cf2a4224a4139ff01c7d4' NOW (CHAIN 1).
## Chain 1:
## Chain 1: Gradient evaluation took 0 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
## Chain 1: Adjust your expectations accordingly!
## Chain 1:
## Chain 1:
## Chain 1: Iteration: 1 / 1000 [ 0%] (Warmup)
## Chain 1: Iteration: 100 / 1000 [ 10%] (Warmup)
## Chain 1: Iteration: 200 / 1000 [ 20%] (Warmup)
## Chain 1: Iteration: 300 / 1000 [ 30%] (Warmup)
## Chain 1: Iteration: 400 / 1000 [ 40%] (Warmup)
## Chain 1: Iteration: 500 / 1000 [ 50%] (Warmup)
## Chain 1: Iteration: 501 / 1000 [ 50%] (Sampling)
## Chain 1: Iteration: 600 / 1000 [ 60%] (Sampling)
## Chain 1: Iteration: 700 / 1000 [ 70%] (Sampling)
## Chain 1: Iteration: 800 / 1000 [ 80%] (Sampling)
## Chain 1: Iteration: 900 / 1000 [ 90%] (Sampling)
## Chain 1: Iteration: 1000 / 1000 [100%] (Sampling)
## Chain 1:
## Chain 1: Elapsed Time: 0.017 seconds (Warm-up)
## Chain 1: 0.024 seconds (Sampling)
## Chain 1: 0.041 seconds (Total)
## Chain 1:
## Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
## Running the chains for more iterations may help. See
## http://mc-stan.org/misc/warnings.html#bulk-ess
## Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
## Running the chains for more iterations may help. See
## http://mc-stan.org/misc/warnings.html#tail-ess
traceplot(mp)
## [1] 1000
## [1] 1
## [1] 1000
Compare the samples for the parameters a and b. Can you explain the different trace plots? If you are unfamiliar with the Cauchy distribution, you should look it up. The key feature to attend to is that it has no expected value. Can you connect this fact to the trace plot?