| Title: | Simple Metropolis-Hastings MCMC Algorithm |
|---|---|
| Description: | A very bare-bones interface to use the Metropolis-Hastings Monte Carlo Markov Chain algorithm. It is suitable for teaching and testing purposes. |
| Authors: | Hugo Gruson [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-4094-1476>) |
| Maintainer: | Hugo Gruson <[email protected]> |
| License: | GPL-3 |
| Version: | 0.1.1.9000 |
| Built: | 2026-05-25 08:29:23 UTC |
| Source: | https://codeberg.org/Bisaloo/simpleMH |
Simple Metropolis-Hastings MCMC
simpleMH(f, inits, theta.cov, max.iter, coda = FALSE, ...)simpleMH(f, inits, theta.cov, max.iter, coda = FALSE, ...)
f |
function that returns a single scalar value proportional to the log probability density to sample from. |
inits |
numeric vector with the initial values for the parameters to estimate |
theta.cov |
covariance matrix of the parameters to estimate. |
max.iter |
maximum number of function evaluations |
coda |
logical. Should the samples be returned as coda::mcmc
object? (defaults to |
... |
further arguments passed to |
if coda = FALSE a list with:
samples: A two dimensional array of samples with dimensions
generation x parameter
log.p: A numeric vector with the log density evaluate at each generation.
if coda = TRUE a list with:
samples: A object of class coda::mcmc containing all samples.
log.p: A numeric vector with the log density evaluate at each generation.
p.log <- function(x) { B <- 0.03 return(-x[1]^2/200 - 1/2*(x[2]+B*x[1]^2-100*B)^2) } simpleMH(p.log, inits=c(0, 0), theta.cov = diag(2), max.iter=3000)p.log <- function(x) { B <- 0.03 return(-x[1]^2/200 - 1/2*(x[2]+B*x[1]^2-100*B)^2) } simpleMH(p.log, inits=c(0, 0), theta.cov = diag(2), max.iter=3000)