MCMC

Dave White

2018-08-14

MCMC

What is MCMC

The biggest problem probabilistic in modeling is solving complex integrals

Intuition

Monte Carlo

Markov-Chains

Markov-Chain Monte-Carlo

Acceptance/Rejection algorithm

Metropolis-Hastings

Initialization

The Algorithm

Choosing a proposal Distributions

Basic Examples

Example Code: Metropolis - Normal Distribution, few samples

nSamp <- 100

#INITIALIZATION
x = rep(0,nSamp+1)
x[1] = 3

#ALGORITHM
for (i in 1:nSamp){

  #PROPOSAL BASED UPON PREVIOUS X
  P = rnorm(1,x[i],sd=1)

  #ACCEPTANCE RATIO
  A = ( dnorm(P,mean=0,sd=1))/( dnorm(x[i],mean=0,sd=1) )

  if  (runif(1) > A){            #GENERATE UNIFORM PROBABILITY
    x[i+1] = x[i]                #REJECT NEW, ACCEPT OLD
  } else{
    x[i+1] = P                   #REJECT OLD, ACCEPT NEW
  }
}

yl=c(-5,5)
xseq <- seq(yl[1],yl[2],.01)
dtrue <- dnorm(xseq,mean=0,sd=1)

t <- c(1:(nSamp+1))
d.y <- density(x)

lmat <- c(1,2,3,3,
          1,2,3,3,
          1,2,3,3)
lmat <- matrix(lmat,nrow=3,byrow=TRUE)
layout(lmat)

plot(dtrue, xseq, ylim=yl, xlim=rev(range(d.y$y)), type='l',ylab='',xlab='True')
plot(d.y$y, d.y$x, ylim=yl, xlim=rev(range(d.y$y)), type='l',yaxt='n',ylab='',xlab='Generated')
plot(t,x,type="p",ylim=yl,pch=20,yaxt='n',ylab='')
Example Output

Example: Metropolis - Normal Distribution, many samples

nSamp <- 100000

#INITIALIZATION
x = rep(0,nSamp+1)
x[1] = 3

#ALGORITHM
for (i in 1:nSamp){

  #PROPOSAL BASED UPON PREVIOUS X
  P = rnorm(1,x[i],sd=1)

  #ACCEPTANCE RATIO
  A = ( dnorm(P,mean=0,sd=1))/( dnorm(x[i],mean=0,sd=1) )

  if  (runif(1) > A){            #GENERATE UNIFORM PROBABILITY
    x[i+1] = x[i]                #REJECT NEW, ACCEPT OLD
  } else{
    x[i+1] = P                   #REJECT OLD, ACCEPT NEW
  }
}

yl=c(-5,5)
xseq <- seq(yl[1],yl[2],.01)
dtrue <- dnorm(xseq,mean=0,sd=1)

t <- c(1:(nSamp+1))
d.y <- density(x)

lmat <- c(1,2,3,3,
          1,2,3,3,
          1,2,3,3)
lmat <- matrix(lmat,nrow=3,byrow=TRUE)
layout(lmat)

plot(dtrue, xseq, ylim=yl, xlim=rev(range(d.y$y)), type='l',ylab='',xlab='True')
plot(d.y$y, d.y$x, ylim=yl, xlim=rev(range(d.y$y)), type='l',yaxt='n',ylab='',xlab='Generated')
plot(t,x,type="p",ylim=yl,pch=20,yaxt='n',ylab='')
Example Output

Example: Metropolis - Complex Bayes (2 logN observation, exponential prior)

nSamp <- 100000

#INITIALIZATION
x = rep(0,nSamp+1)
x[1] = 3

#ALGORITHM
for (i in 1:nSamp){

  #PROPOSAL BASED UPON PREVIOUS X
  P = rnorm(1,x[i],sd=1)

  #ACCEPTANCE RATIO
  A = ( dexp(P)^2 * dlnorm(P,meanlog=4,sdlog=2))/( dexp(x[i])^2 * dlnorm(x[i],meanlog=4,sdlog=2) )

  if  (runif(1) > A){            #GENERATE UNIFORM PROBABILITY
    x[i+1] = x[i]                #REJECT NEW, ACCEPT OLD
  } else{
    x[i+1] = P                   #REJECT OLD, ACCEPT NEW
  }
}

yl=c(-5,5)
#xseq <- seq(yl[1],yl[2],.01)
#dtrue <- dnorm(xseq,mean=0,sd=1)

t <- c(1:(nSamp+1))
d.y <- density(x)

lmat <- c(1,2,2,
          1,2,2,
          1,2,2)
lmat <- matrix(lmat,nrow=3,byrow=TRUE)
layout(lmat)

#plot(dtrue, xseq, ylim=yl, xlim=rev(range(d.y$y)), type='l',ylab='',xlab='True')
plot(d.y$y, d.y$x, ylim=yl, xlim=rev(range(d.y$y)), type='l',yaxt='n',ylab='',xlab='Generated')
plot(t,x,type="p",ylim=yl,pch=20,yaxt='n',ylab='')
Example Output

Issues

Starting in state of extremely low probability

Sparse distribution areas

Autocorrelation of samples from Markov Chain

Multivariate distributions

Curse of Dimensionality

Proposal algorithms

Metropolis

Gibbs sampler

When to use

Hamiltonian (variation of metropolis Hastings)

When to use

Benefits

Problems

Intuition

Use quantum particle physics as an analogy

1. Think of probability density as inverse energy well
2. Energy stats
Relation of Hamiltonian to a posterior density

Proposals

Solve for path of particle from U & K
Leapfrog integrator

Acceptance ratio

Stan

Summary

New

Importance samling
Tempered Gibbs sampling
Tempering
exploration vs exploitation
asymptotic variance

single and double flip update