scheidan / BarkerMCMC.jl

gradient based MCMC sampler

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BarkerMCMC

Dev Build Status Coverage

A Monte Carlo Markov Chain sampler that makes use of gradient information. Proposed by Livingstone et al. (2021)

The adaptative preconditioning is based on Andrieu and Thoms (2008), Algorithm 4 in Section 5. For details see Algorithm 7.2 of the supporting information of Livingstone et al. (2021).

Installation

] add BarkerMCMC

Usage

See the documentation for all arguments.

using BarkerMCMC

# --- Target distribution
function log_p_rosebruck_2d(x; k=1/200)
    -k*(100*(x[2] - x[1]^2)^2 + (1 - x[1])^2)
end

function ∇log_p_rosebruck_2d(x; k=1/200)
    [-2*k*(200*x[1]^3 - 200*x[1]*x[2] + x[1] -1),   # d/dx[1]
     -200*k*(x[2] - x[1]^2)]                        # d/dx[2]
end

# --- Sampling
# see `?barker_mcmc` for all options
res = barker_mcmc(log_p_rosebruck_2d,
                  ∇log_p_rosebruck_2d,
                  [5.0, 5.0];
                  n_iter = 1_000,
                  target_acceptance_rate=0.4)

res.samples
res.log_p

# --- Result

# acceptance rate
length(unique(res.samples[:,1])) / size(res.samples, 1)

# You may want to use `MCMCChains.jl` for plots and diagonstics
# (must be installed separately)

using MCMCChains
using StatsPlots

chain = Chains(res.samples, [:x1, :x2])
chains[200:10:end]                 # remove burn-in and thinning

plot(chains)
meanplot(chain)
histogram(chain)
autocorplot(chain)
corner(chain)

Related Julia Packages

Hamiltonian Monte Carlo (gradient based)

Adaptive MCMC (without gradient)

References

Andrieu, C., Thoms, J., 2008. A tutorial on adaptive MCMC. Statistics and computing 18, 343–373.

Livingstone, S., Zanella, G., 2021. The Barker proposal: Combining robustness and efficiency in gradient-based MCMC. Journal of the Royal Statistical Society: Series B (Statistical Methodology). https://doi.org/10.1111/rssb.12482 (see https://github.com/gzanella/barker for the R code used)

About

gradient based MCMC sampler

License:GNU General Public License v3.0


Languages

Language:Julia 100.0%