Configure and run HMC/NUTS safely (global / subset / auto)
Source:R/diagnostics.R
configure_hmc_safely_bis.RdMinimal, strict wrapper around NIMBLE + nimbleHMC that:
builds the model,
configures NUTS either globally (model signature), on a subset of nodes (conf + model + nodes signature), or in auto mode,
compiles the MCMC,
runs MCMC.
Usage
configure_hmc_safely_bis(
build_fn,
niter,
nburnin,
thin,
nchains,
monitors = NULL,
nuts_mode = c("all", "subset", "auto", "none"),
nuts_nodes = NULL,
enable_WAIC = FALSE,
buildDerivs = TRUE,
compiler_cores = max(1L, parallel::detectCores(TRUE)%/%2L),
show_compiler_output = TRUE,
project_name = NULL,
out_dir = NULL,
inits = NULL,
save_samples = TRUE
)Arguments
- build_fn
Function; model builder (e.g.
build_M) used to create the NIMBLE model before configuring HMC/NUTS. If.fresh_build()exists, it will be called as.fresh_build(build_fn, monitors, thin). Otherwise,build_fn()must return a list with at leastmodelandconf(NIMBLE objects).- niter
Integer. Total number of iterations.
- nburnin
Integer (>= 0). Number of burn-in iterations (0 allowed).
- thin
Integer (>= 1). Thinning interval.
- nchains
Integer (>= 1). Number of chains.
- monitors
Character vector or
NULL. Passed through to building and/or global HMC configuration.- nuts_mode
Character. One of
c("all","subset","auto","none"):- "all"
Global NUTS via
nimbleHMC::configureHMC(model, monitors = ..., enableWAIC = ..., buildDerivs = ...).- "subset"
NUTS only on
nuts_nodesvianimbleHMC::configureHMC(conf, model, nodes = ..., ...).- "auto"
Let
nimbleHMC::configureHMC(conf, model, ...)choose targets automatically.- "none"
Do not modify the existing configuration.
- nuts_nodes
Character vector of node names (can include indexed forms such as
"beta[]") whennuts_mode = "subset".- enable_WAIC
Logical. Only relevant for
"all"(model-signature path).- buildDerivs
Logical. Passed to
nimbleHMC::configureHMC().- compiler_cores
Integer. Passed to
nimbleOptions(numCompilerCores = ...).- show_compiler_output
Logical. Passed to
nimbleOptions(showCompilerOutput = ...).- project_name
Character or
NULL. If non-NULL, setsoptions(nimbleProjectName = ...)to sanitize the C++ cache.- out_dir
Character or
NULL. If non-NULL, saves asessionInfo()log and, optionally, samples as.rds.- inits
NULLor a list of per-chain initial values to pass tonimble::runMCMC()(length must matchnchainsif provided).- save_samples
Logical. If
TRUE(default) andout_diris non-NULL, savesamplesas.rds. IfFALSE, skip saving samples.
Value
A list with elements:
conf: final MCMC configuration (with HMC/NUTS if configured),cmcmc: compiled MCMC object,samples:coda::mcmc.listof posterior samples,runtime_s: wall time in seconds,build: the build object returned bybuild_fnor.fresh_build().