sample_chain | R Documentation |
Sample a Markov chain using Metropolis-Hastings kernel with a user-specified target distribution and proposal (defaulting to Barker proposal), optionally adapting proposal parameters in a warm-up stage.
sample_chain(
target_distribution,
initial_state,
n_warm_up_iteration,
n_main_iteration,
proposal = barker_proposal(),
adapters = list(scale_adapter(), shape_adapter()),
show_progress_bar = TRUE,
trace_warm_up = FALSE
)
target_distribution |
Target stationary distribution for chain. One of:
|
initial_state |
Initial chain state. Either a vector specifying just
the position component of the chain state or a list output by |
n_warm_up_iteration |
Number of warm-up (adaptive) chain iterations to run. |
n_main_iteration |
Number of main (non-adaptive) chain iterations to run. |
proposal |
Proposal distribution object. Defaults to Barker proposal,
that is the output of |
adapters |
List of adapters to tune proposal parameters during warm-up.
Defaults to using list with instances of |
show_progress_bar |
Whether to show progress bars during sampling.
Requires |
trace_warm_up |
Whether to record chain traces and adaptation / transition statistics during (adaptive) warm-up iterations in addition to (non-adaptive) main chain iterations. |
A list with entries
final_state
: the final chain state,
traces
: a matrix with named columns contained traced variables for each
main chain iteration, with variables along columns and iterations along
rows.
statistics
: a matrix with named columns containing transition statistics
for each main chain iteration, with statistics along columns and iterations
along rows.
warm_up_traces
: a matrix with named columns contained traced variables
for each warm-up chain iteration, with variables along columns and
iterations along rows. Only present if trace_warm_up = TRUE
.
warm_up_statistics
: a matrix with named columns containing adaptation and
transition statistics for each warm-up chain iteration, with statistics
along columns and iterations along rows. Only present if
trace_warm_up = TRUE
.
target_distribution <- list(
log_density = function(x) -sum(x^2) / 2,
gradient_log_density = function(x) -x
)
withr::with_seed(876287L, {
results <- sample_chain(
target_distribution,
initial_state = stats::rnorm(2),
n_warm_up_iteration = 1000,
n_main_iteration = 1000
)
})
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.