sampler_mcmc: Markov Chain Monte Carlo Sampler

Description Usage Arguments Details Value Examples

View source: R/samplers.R

Description

This sampler navigates the proposal distribution following a random walk. At each step, it generates a new proposal from a proposal distribution (in this case a Gaussian centered at the current position) and chooses to accept it or reject it following the Metropolis-Hastings rule: it accepts it if the density of the posterior distribution at the proposed point is higher than at the current point. If the current position is denser, it still may accept the proposal with probability proposal_density / current_density.

Usage

1
sampler_mcmc(pdf, start, iterations = 1024, sigma_prop = NULL)

Arguments

pdf

Probability Density Function of the posterior distribution. Takes a vector as input

start

Vector. Starting point for the sampler

iterations

Numeric. Number of times the sampler runs

sigma_prop

Variance of the univariate proposal distribution. For multivariate proposals, covariance matrix of the proposal.

Details

As mentioned, the proposal distribution is a Normal distribution. Its mean is the current position, and its variance is equal to the sigma_prop parameter, which defaults to the identity matrix if not specified.

Value

A list containing

  1. the history of visited places (a n x d matrix, n = iterations; d = dimensions)

  2. acceptance ratio - the proportions of proposals that were accepted (numeric)

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# generate a density function that only takes a vector as input
pdf_func <- function(x){return(stats::dnorm(x, 0, 1))}
# Not giving a sigma_prop issues a warning, but the sampler runs anyway with a default value
chain <- sampler_mcmc(pdf_func, start = 0, iterations = 20)

# pdf functions can be easily created with the make_*_pdf helpers
density_function <- make_distr_pdf(distr::Beta(.5,.5))
chain <- sampler_mcmc(
 density_function,
 iterations = 20,
 start = 0,
 sigma_prop = .5
 )

lucas-castillo/SampleR documentation built on Jan. 1, 2021, 8:25 a.m.