| Title: | Langevin Diffusion Samplers with a C++ Backend |
|---|---|
| Description: | Provides lightweight, dependency-minimal implementations of Langevin diffusion based Markov chain Monte Carlo samplers, including the Unadjusted Langevin Algorithm (ULA) and the Metropolis-Adjusted Langevin Algorithm (MALA). The core sampling loops are written in C++ via 'Rcpp' and 'RcppArmadillo' for performance, while exposing a simple R-level interface where the user supplies the gradient of the negative log-density (and, for MALA, the negative log-density itself). Intended as a building block for Bayesian inference and stochastic optimization rather than a full probabilistic programming framework. Methods follow Roberts and Tweedie (1996) <doi:10.2307/3318418> and Roberts and Rosenthal (1998) <doi:10.1111/1467-9868.00123>. |
| Authors: | Behrooz Moosavi [aut, cre] (ORCID: <https://orcid.org/0000-0003-4378-3167>) |
| Maintainer: | Behrooz Moosavi <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-05-30 07:21:34 UTC |
| Source: | https://github.com/behroozmoosavi/langevinflow |
Asymptotically exact sampler for .
At each step a Langevin proposal is generated as in ula
and then accepted or rejected by the Metropolis-Hastings rule so that
is exactly invariant. Optimal acceptance rates are typically
near 0.574 in high dimension (Roberts & Rosenthal, 1998).
mala(init_x, U, grad_u, step_size, n_iter, beta = 1, burn_in = 0L)mala(init_x, U, grad_u, step_size, n_iter, beta = 1, burn_in = 0L)
init_x |
Numeric vector. Starting state of the chain. |
U |
Function. Takes a numeric vector of length
|
grad_u |
Function. Returns the gradient of |
step_size |
Positive numeric scalar. Discretization step
|
n_iter |
Positive integer. Number of iterations. |
beta |
Positive numeric scalar. Inverse temperature; defaults to 1. |
burn_in |
Non-negative integer, strictly less than |
An object of class "langevin_chain" (see ula
for structure). The acceptance_rate component is populated and
accepted is a logical vector indicating per-iteration outcomes
(post-burn-in).
Roberts, G. O., & Rosenthal, J. S. (1998). Optimal scaling of discrete approximations to Langevin diffusions. Journal of the Royal Statistical Society, Series B, 60(1), 255-268.
# Standard 2D Gaussian: U(x) = 0.5 * ||x||^2. set.seed(1) U <- function(x) 0.5 * sum(x^2) grad_u <- function(x) x fit <- mala(init_x = c(3, -3), U = U, grad_u = grad_u, step_size = 0.3, n_iter = 2000, burn_in = 500) summary(fit)# Standard 2D Gaussian: U(x) = 0.5 * ||x||^2. set.seed(1) U <- function(x) 0.5 * sum(x^2) grad_u <- function(x) x fit <- mala(init_x = c(3, -3), U = U, grad_u = grad_u, step_size = 0.3, n_iter = 2000, burn_in = 500) summary(fit)
Trace plot for each dimension, plus a marginal histogram. For
high-dimensional chains, only the first max_dim coordinates are
shown.
## S3 method for class 'langevin_chain' plot(x, max_dim = 4L, ...)## S3 method for class 'langevin_chain' plot(x, max_dim = 4L, ...)
x |
An object of class |
max_dim |
Maximum number of coordinates to display. Defaults to 4. |
... |
Passed to |
Called for side effect. Returns x invisibly.
Computes per-coordinate posterior summaries (mean, standard deviation, and selected quantiles) from a fitted Langevin chain.
## S3 method for class 'langevin_chain' summary(object, probs = c(0.025, 0.5, 0.975), ...)## S3 method for class 'langevin_chain' summary(object, probs = c(0.025, 0.5, 0.975), ...)
object |
An object of class |
probs |
Numeric vector of probabilities for quantile computation. |
... |
Currently unused. |
Invisibly, a data frame of summaries. Printed by default.
Draws a Markov chain whose stationary distribution approximates
using the Euler-Maruyama
discretization of the overdamped Langevin diffusion. The discretization
introduces a bias that vanishes as step_size tends to zero; for
an asymptotically exact sampler, use mala.
ula(init_x, grad_u, step_size, n_iter, beta = 1, burn_in = 0L)ula(init_x, grad_u, step_size, n_iter, beta = 1, burn_in = 0L)
init_x |
Numeric vector. Starting state of the chain. Its length defines the dimension. |
grad_u |
Function. Takes a numeric vector of length
|
step_size |
Positive numeric scalar. Discretization step
|
n_iter |
Positive integer. Number of iterations to run. |
beta |
Positive numeric scalar. Inverse temperature; defaults to 1. |
burn_in |
Non-negative integer. Number of initial samples to
discard. Must be strictly less than |
An object of class "langevin_chain", which is a list with
components:
Numeric matrix of post-burn-in samples; rows index iterations and columns index dimensions.
Character string "ULA".
Echoed inputs.
NA for ULA (no accept/reject step).
Wall-clock runtime of the sampler in seconds.
Roberts, G. O., & Tweedie, R. L. (1996). Exponential convergence of Langevin distributions and their discrete approximations. Bernoulli, 2(4), 341-363.
# Standard 2D Gaussian target: U(x) = 0.5 * ||x||^2, grad_U(x) = x. set.seed(1) grad_u <- function(x) x fit <- ula(init_x = c(3, -3), grad_u = grad_u, step_size = 0.05, n_iter = 2000, burn_in = 500) summary(fit)# Standard 2D Gaussian target: U(x) = 0.5 * ||x||^2, grad_U(x) = x. set.seed(1) grad_u <- function(x) x fit <- ula(init_x = c(3, -3), grad_u = grad_u, step_size = 0.05, n_iter = 2000, burn_in = 500) summary(fit)