In [13]:
options(repr.plot.width=5, repr.plot.height=4)
library(ggplot2)
library(bayesplot)
In [14]:
targetD <- function(x) {
    return(0.7 * dnorm(x, 0, 1) + 0.3 * dnorm(x, 2, .5))
    }
In [15]:
x <- seq(-5, 5, .01)
plot(x, targetD(x), type='l')

Metropolis-Hastings

In [16]:
stepsize <- 1
proposal <- function(x) {
    return(rnorm(1, x, stepsize))
}

initial <- 0
NSteps <- 10000
In [17]:
mcmc <- function(targetD, initial=0, proposalD=proposal, N=NSteps) {
    chain <- initial
    current <- initial
    for (i in 1:N) {
        new <- proposalD(current)
        if (runif(1) <= min(1, targetD(new)/targetD(current))) {
            current <- new
        }
        chain <- c(chain, current)
    }
    return(chain)
}
In [19]:
chain1 <- mcmc(targetD, initial=-10)
chain2 <- mcmc(targetD, initial=10)
In [31]:
chains <- array(dim = c(length(chain1), 2, 1), 
                dimnames=list(iterations=NULL, chains=c('chain1', 'chain2'), parameters='x'))
In [32]:
chains[,1,1] <- chain1
chains[,2,1] <- chain2
In [37]:
color_scheme_set("red")
In [39]:
mcmc_trace(chains, n_warmup = 1000)
In [45]:
mcmc_areas(chains[-(1:100),,], prob=0.95)
In [46]:
mcmc_acf(chains[-(1:100), , ])
In [48]:
library(coda)
Attaching package: ‘coda’

The following object is masked _by_ ‘.GlobalEnv’:

    mcmc

In [97]:
s1 <- coda::mcmc(data=chain1[1:5000])
s2 <- coda::mcmc(data=chain2[1:5000])
In [98]:
gelman.diag(mcmc.list(list(s1, s2)), autoburnin = T)
Potential scale reduction factors:

     Point est. Upper C.I.
[1,]          1          1