The goal of this chapter is to introduce the purpose and approach MCMC algorithms.
The Metropolis Algorithm
is an example of MCMC, whose goal is to draw samples from unknown and complex target distributions.
The major algorithms introduced were the
1. Metropolis,
1. Gibbs sampling, and
1. Hamiltonian Monte Carlo algorithms.
A function in the rethinking package, map2stan, was introduced that uses the Stan Hamiltonian Monte Carlo engine to fit models as they are defined in this book.
__ I grasped very little of this chapter, but I want to peruse the remainder of the book before deciding where to dive deep, or should I review this chapter for its core concepts while the ignorance is still fresh?
I will give the book a full peruse
## R code 8.1 num_weeks <- 1e5 positions <- rep(0,num_weeks) current <- 10 for ( i in 1:num_weeks ) { # record current position positions[i] <- current # flip coin to generate proposal proposal <- current + sample( c(-1,1) , size=1 ) # now make sure he loops around the archipelago if ( proposal < 1 ) proposal <- 10 if ( proposal > 10 ) proposal <- 1 # move? prob_move <- proposal/current current <- ifelse( runif(1) < prob_move , proposal , current ) } ## R code 8.2 library(rethinking) data(rugged) d <- rugged d$log_gdp <- log(d$rgdppc_2000) dd <- d[ complete.cases(d$rgdppc_2000) , ] ## R code 8.3 m8.1 <- map( alist( log_gdp ~ dnorm( mu , sigma ) , mu <- a + bR*rugged + bA*cont_africa + bAR*rugged*cont_africa , a ~ dnorm(0,100), bR ~ dnorm(0,10), bA ~ dnorm(0,10), bAR ~ dnorm(0,10), sigma ~ dunif(0,10) ) , data=dd ) precis(m8.1) ## R code 8.4 dd.trim <- dd[ , c("log_gdp","rugged","cont_africa") ] str(dd.trim) ## R code 8.5 m8.1stan <- map2stan( alist( log_gdp ~ dnorm( mu , sigma ) , mu <- a + bR*rugged + bA*cont_africa + bAR*rugged*cont_africa , a ~ dnorm(0,100), bR ~ dnorm(0,10), bA ~ dnorm(0,10), bAR ~ dnorm(0,10), sigma ~ dcauchy(0,2) ) , data=dd.trim ) ## R code 8.6 precis(m8.1stan) ## R code 8.7 m8.1stan_4chains <- map2stan( m8.1stan , chains=4 , cores=4 ) precis(m8.1stan_4chains) ## R code 8.8 post <- extract.samples( m8.1stan ) str(post) ## R code 8.9 pairs(post) ## R code 8.10 pairs(m8.1stan) ## R code 8.11 show(m8.1stan) ## R code 8.12 plot(m8.1stan) ## R code 8.13 y <- c(-1,1) m8.2 <- map2stan( alist( y ~ dnorm( mu , sigma ) , mu <- alpha ) , data=list(y=y) , start=list(alpha=0,sigma=1) , chains=2 , iter=4000 , warmup=1000 ) ## R code 8.14 precis(m8.2) ## R code 8.15 m8.3 <- map2stan( alist( y ~ dnorm( mu , sigma ) , mu <- alpha , alpha ~ dnorm( 1 , 10 ) , sigma ~ dcauchy( 0 , 1 ) ) , data=list(y=y) , start=list(alpha=0,sigma=1) , chains=2 , iter=4000 , warmup=1000 ) precis(m8.3) ## R code 8.16 y <- rcauchy(1e4,0,5) mu <- sapply( 1:length(y) , function(i) sum(y[1:i])/i ) plot(mu,type="l") ## R code 8.17 y <- rnorm( 100 , mean=0 , sd=1 ) ## R code 8.18 m8.4 <- map2stan( alist( y ~ dnorm( mu , sigma ) , mu <- a1 + a2 , sigma ~ dcauchy( 0 , 1 ) ) , data=list(y=y) , start=list(a1=0,a2=0,sigma=1) , chains=2 , iter=4000 , warmup=1000 ) precis(m8.4) ## R code 8.19 m8.5 <- map2stan( alist( y ~ dnorm( mu , sigma ) , mu <- a1 + a2 , a1 ~ dnorm( 0 , 10 ) , a2 ~ dnorm( 0 , 10 ) , sigma ~ dcauchy( 0 , 1 ) ) , data=list(y=y) , start=list(a1=0,a2=0,sigma=1) , chains=2 , iter=4000 , warmup=1000 ) precis(m8.5) ## R code 8.20 mp <- map2stan( alist( a ~ dnorm(0,1), b ~ dcauchy(0,1) ), data=list(y=1), start=list(a=0,b=0), iter=1e4, warmup=100 , WAIC=FALSE ) ## R code 8.21 N <- 100 # number of individuals height <- rnorm(N,10,2) # sim total height of each leg_prop <- runif(N,0.4,0.5) # leg as proportion of height leg_left <- leg_prop*height + # sim left leg as proportion + error rnorm( N , 0 , 0.02 ) leg_right <- leg_prop*height + # sim right leg as proportion + error rnorm( N , 0 , 0.02 ) # combine into data frame d <- data.frame(height,leg_left,leg_right) ## R code 8.22 m5.8s <- map2stan( alist( height ~ dnorm( mu , sigma ) , mu <- a + bl*leg_left + br*leg_right , a ~ dnorm( 10 , 100 ) , bl ~ dnorm( 2 , 10 ) , br ~ dnorm( 2 , 10 ) , sigma ~ dcauchy( 0 , 1 ) ) , data=d, chains=4, start=list(a=10,bl=0,br=0,sigma=1) ) ## R code 8.23 m5.8s2 <- map2stan( alist( height ~ dnorm( mu , sigma ) , mu <- a + bl*leg_left + br*leg_right , a ~ dnorm( 10 , 100 ) , bl ~ dnorm( 2 , 10 ) , br ~ dnorm( 2 , 10 ) & T[0,] , sigma ~ dcauchy( 0 , 1 ) ) , data=d, chains=4, start=list(a=10,bl=0,br=0,sigma=1) )
What are Markoc chains
? What are samples from the chain
?
Half Cauchy? Cauchy distribution?
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.