9E1. Which of the following is a requirement of the simple Metropolis algorithm?
#3
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 because distribution of proposed parameter values is adjusted to currect parameter values. Gibbs sampler uses pairs of priors and likelihoods that have anlytic solutions for the posterior of an individual parameter.
9E3. Which sort of parameters can Hamiltonian Monte Carlo not handle? Can you explain why?
# HMC cannot handle discrete parameters because there is no slope. We can only use continuous 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 is the number of independent samples
# Samples here means the number of iterations of the Markov chains, not data points. Markov chains are typically autocorrelated, making sequential samples not independent.
9E5. Which value should Rhat approach, when a chain is sampling the posterior distribution correctly?
# Rhat should approach 1 from the top.
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?
set.seed(41)
y<- rnorm (1000,mean=0, sd=1)
plot(y=y,x=1:1000, type="l")
9E7. Repeat the problem above, but now for a trace rank plot.
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.3 <- 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.3 , depth=2)
## mean sd 5.5% 94.5%
## a[1] 0.8865641 0.015675064 0.86151228 0.91161584
## a[2] 1.0505700 0.009936200 1.03469005 1.06644998
## b[1] 0.1325054 0.074201565 0.01391697 0.25109384
## b[2] -0.1425765 0.054747215 -0.23007312 -0.05507987
## sigma 0.1094896 0.005934686 0.10000482 0.11897436
pairs(m8.3)
m8.3_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.3_unif , depth=2)
## mean sd 5.5% 94.5%
## a[1] 0.8865660 0.015679985 0.86150638 0.9116257
## a[2] 1.0505643 0.009939372 1.03467922 1.0664493
## b[1] 0.1325012 0.074223992 0.01387695 0.2511255
## b[2] -0.1425734 0.054764265 -0.23009725 -0.0550495
## sigma 0.1095249 0.005939468 0.10003244 0.1190173
pairs(m8.3_unif)
# Does not have detectible influence on the posterior distribution of sigma
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?
m8.3_exp <- 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(0.3)
) ,
data=dd)
precis(m8.3_exp , depth=2)
## mean sd 5.5% 94.5%
## a[1] 0.8865625 0.015678147 0.86150576 0.91161918
## a[2] 1.0505766 0.009938184 1.03469348 1.06645976
## b[1] 0.1326112 0.074215548 0.01400042 0.25122198
## b[2] -0.1425751 0.054757917 -0.23008887 -0.05506142
## sigma 0.1095117 0.005937683 0.10002216 0.11900129
pairs(m8.3_exp)
# There is not differences in the posterior distribution.
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?
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 )
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?