Description Usage Arguments Details Value Examples
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
.
1 | sampler_mcmc(pdf, start, iterations = 1024, sigma_prop = NULL)
|
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. |
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.
A list containing
the history of visited places (a n x d matrix, n = iterations; d = dimensions)
acceptance ratio - the proportions of proposals that were accepted (numeric)
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
)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.