Langevin diffusion Markov chain Monte Carlo samplers for R, with a C++
backend via Rcpp and RcppArmadillo.
Development version from GitHub:
# install.packages("devtools")
# devtools::install_github("BehroozMoosavi/LangevinFlow")
install.packages("LangevinFlow", repos = c('https://behroozmoosavi.r-universe.dev', 'https://cloud.r-project.org'))
The package exposes two samplers, both targeting $\pi(x) \propto \exp(-\beta U(x))$ where the user supplies the potential $U(x) = -\log \pi(x)$ (and, for MALA, its gradient).
library(LangevinFlow)
# Standard 2D Gaussian: U(x) = 0.5 * ||x||^2
grad_u <- function(x) x
set.seed(1)
fit <- ula(init_x = c(3, -3), grad_u = grad_u,
step_size = 0.05, n_iter = 5000, burn_in = 1000)
summary(fit)
plot(fit)
U <- function(x) 0.5 * sum(x^2)
grad_u <- function(x) x
set.seed(1)
fit <- mala(init_x = c(3, -3), U = U, grad_u = grad_u,
step_size = 0.4, n_iter = 5000, burn_in = 1000)
fit$acceptance_rate
| Sampler | Asymptotically exact? | Tuning sensitivity | Best for |
|---|---|---|---|
ula() |
No — biased by step_size
|
Less | Quick exploration; stochastic optimization |
mala() |
Yes | More (target ~0.574 acceptance) | Accurate inference |