Processing math: 100%
+ - 0:00:00
Notes for current slide
Notes for next slide

Bayesian calculus

Implementing MCMC

Marie-Pierre Etienne, Etienne Rivot

https://marieetienne.github.io/bayesian_and_ecology

M2 Mode - Data science

1 / 11

Monte Carlo Markov Chain algorithm (MCMC)

Metropolis Hastings algorithm

Key idea: building a reversible Markov chain with [θ|y] as stationnary distribution

2 / 11

Monte Carlo Markov Chain algorithm (MCMC)

Metropolis Hastings algorithm

Key idea: building a reversible Markov chain with [θ|y] as stationnary distribution

  1. Initialization θ(0) an admissible initial value

  2. For i in 1:nIter

    • Propose a new candidate value θ(i)c sampled from a proposal distribution g(.|θ(i1))
    • Compute Metropolis Hastings ratio ri=[y|θ(i)c][θ(i)c][y|θ(i1)][θ(i1)]g(θ(i1)|θ(i)c)g(θ(i)c|θ(i1))
    • Define θ(i)={θ(i)c with probability min(ri,1)θ(i1) with probability 1min(ri,1)`
2 / 11

Monte Carlo Markov Chain algorithm (MCMC)

Generic Implementation

# [Y|theta]
likelihood<- function(theta, y){
}
# [theta]
dprior <- function(theta){
}
# d(theta_c|theta_i-1)
dstep <- function(theta1, theta2){
}
# sample theta_prop from theta
rstep <- function(theta){
}
ratio_MH <- function(theta_prop, theta_c, ...){
ratio_post <- likelihood(theta_prop) * dprior(theta_prop) /
(likelihood(theta_c) * dprior(theta_c))
ratio_step <- dstep(theta_prop, theta_c) /
dstep(theta_c, theta_prop)
return(ratio_post * ratio_step)
}
3 / 11

Monte Carlo Markov Chain algorithm (MCMC)

Beta Binomial model

Y|Bin(N,p),p|Beta(1,1)

Data

N=10,y=3

Propose a MCMC algorithm

4 / 11

Monte Carlo Markov Chain algorithm (MCMC)

Implementation for a beta Binomial model

# [Y|theta]
likelihood<- function(theta, y, N){
if(theta$p < 0 | theta$p > 1) {
return(0)
} else {
return(dbinom(x = y, size = N, prob = theta$p))
}
}
# [theta]
dprior <- function(theta, a.prior = 1, b.prior =1){
dbeta(theta$p, shape1 = a.prior, shape2 = b.prior)
}
5 / 11

Monte Carlo Markov Chain algorithm (MCMC)

Implementation for a beta Binomial model

# d(theta_c|theta_i-1)
dstep <- function(theta1, theta2, sd.explore = 0.1){
dnorm(theta2$p, mean = theta1$p, sd = sd.explore)
}
# sample theta_prop from theta
rstep <- function(theta, sd.explore = 0.1){
theta_prop= list()
theta_prop$p <- rnorm(1, mean = theta$p, sd = sd.explore)
return(theta_prop)
}
6 / 11

Monte Carlo Markov Chain algorithm (MCMC)

Implementation for a beta Binomial model

# sample theta_prop from theta
ratio_MH <- function(theta_prop, theta_c,
a.prior = 1, b.prior = 1, sd.explore = 0.1,
y = y, N = N){
ratio_post <- likelihood(theta_prop, y, N) * dprior(theta = theta_prop, a.prior = a.prior, b.prior = b.prior) /
(likelihood(theta_c, y, N) * dprior(theta = theta_c, a.prior = a.prior, b.prior = b.prior))
ratio_step <- dstep(theta1 = theta_prop, theta2 = theta_c, sd.explore = sd.explore) /
dstep(theta1 = theta_c, theta2 = theta_prop, sd.explore = sd.explore)
return(ratio_post * ratio_step)
}
7 / 11

Monte Carlo Markov Chain algorithm (MCMC)

Implementation for a beta Binomial model

## data
y = 3; N = 10
## init
p_init = 0.5
## prior
a.prior = 1; b.prior = 1
## algo parameters
n_iter <- 10000; sd.explore = 0.2
##sampling
theta_sample <- tibble(p = rep(NA, n_iter))
## initialisation
theta_sample$p[1] <- p_init
## algo
for(i in 2:n_iter){
theta_prop <- rstep(theta = theta_sample[i-1,], sd.explore = sd.explore)
u <- ratio_MH(theta_prop = theta_prop,
theta_c = theta_sample[i-1,],
a.prior = a.prior , b.prior = b.prior, sd.explore = sd.explore, y =y, N =N)
if( runif(1) < u ){
theta_sample[i,] <- theta_prop
} else {
theta_sample[i,] <- theta_sample[i-1,]
}
}
8 / 11

Monte Carlo Markov Chain algorithm (MCMC)

Implementation for a beta Binomial model

theta_sample %>%
rowid_to_column(var = "iter") %>%
ggplot() + aes(x = iter, y =p) +
geom_line()

9 / 11

Monte Carlo Markov Chain algorithm (MCMC)

Implementation for a beta Binomial model

p1 <- theta_sample %>%
ggplot() + aes(x = p) +
geom_histogram(aes(y =after_stat(density)), alpha = 0.5) +
geom_density()
p1
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

10 / 11

Monte Carlo Markov Chain algorithm (MCMC)

Implementation for a beta Binomial model

posterior_exact <- tibble(p = seq(0,1, length.out = 10001)) %>%
mutate(posterior = dbeta(p, shape1 = a.prior + y, shape2 = b.prior + N - y))
p1 + geom_line(data = posterior_exact, aes(x=p, y =posterior), col = 'red')
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

11 / 11

Monte Carlo Markov Chain algorithm (MCMC)

Metropolis Hastings algorithm

Key idea: building a reversible Markov chain with [θ|y] as stationnary distribution

2 / 11
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Alt + fFit Slides to Screen
Esc Back to slideshow