Run a fully customizable NIMBLE MCMC configuration in parallel (baseline / onlySlice / overrides)
Source:R/diagnostics.R
run_custom_mcmc_parallel.RdGeneral-purpose parallel backend to run multiple independent NIMBLE MCMC chains while
programmatically controlling the sampler configuration:
a global "base policy" (e.g. baseline samplers or onlySlice=TRUE) plus optional
targeted overrides on specific nodes or node families (e.g. force NUTS on beta,
set RW_block on logq[1:5], etc.). This is designed for robust, model-agnostic
experimentation in diagnostic-driven workflows.
Usage
run_custom_mcmc_parallel(
build_fn,
niter,
nburnin = floor(0.25 * niter),
thin = 1L,
monitors = NULL,
nchains = 1L,
seed = 1L,
n_cores = nchains,
extra_export = character(0),
parallel_backend = c("PSOCK", "FORK", "AUTO"),
worker_log = TRUE,
base_policy = c("baseline", "onlySlice", "none"),
useConjugacy = FALSE,
override_nodes = NULL,
override_families = NULL,
family_roots = NULL,
ensure_unsampled = TRUE,
allow_hmc = TRUE,
mcmc_project = c("cmodel", "model"),
buildDerivs = NULL
)Arguments
- build_fn
Function that builds one chain. Preferably supports
build_fn(chain_id = 1L, export_global = FALSE)and returns at leastmodel, and optionallycmodelandconf.- niter
Integer; total number of MCMC iterations.
- nburnin
Integer; burn-in iterations to discard.
- thin
Integer; thinning interval (keep 1 draw every
thiniterations).- monitors
Character vector of monitor roots; passed to configuration (or added when possible).
- nchains
Integer; number of independent chains.
- seed
Integer; base RNG seed; chain
iusesseed + i - 1.- n_cores
Integer; number of workers (default =
nchains).- extra_export
Character vector of additional global object names to export to PSOCK workers.
- parallel_backend
One of
"PSOCK","FORK", or"AUTO".- worker_log
Logical; if TRUE (PSOCK), writes worker output to a temporary log and appends tail on errors.
- base_policy
One of
"baseline","onlySlice","none".- useConjugacy
Logical; forwarded to
nimble::configureMCMCwhen building a fresh config.- override_nodes
Optional list of override rules for explicit targets. Each element is a list with fields:
target(character vector of node names),type(sampler type), optionalcontrol(list).- override_families
Optional list of override rules for families. Each element is a list with fields:
family(root name),type, optionalcontrol.- family_roots
Optional named list mapping family root -> explicit node vector, used to override default family expansion.
- ensure_unsampled
Logical; if TRUE, add
sliceto any remaining unsampled nodes after applying overrides.- allow_hmc
Logical; if TRUE, enables nimbleHMC integration when overrides request
NUTS/HMC.- mcmc_project
One of
"cmodel"or"model"; controls theproject=argument used when compiling the MCMC. Use"model"to match some legacy patterns (e.g. GEREM historical scripts).- buildDerivs
Optional (reserved for future compatibility); currently ignored because the model is built in
build_fn.
Details
Key features:
Base policy:
"baseline"Use
confreturned bybuild_fn(e.g. default RW/dirichlet/posterior_predictive)."onlySlice"Rebuild MCMC configuration with
nimble::configureMCMC(..., onlySlice=TRUE)."none"Start from a configuration with all samplers removed, then add only what is requested via overrides (advanced).
Targeted overrides:
override_nodesList of rules applied to explicit node targets.
override_familiesList of rules applied to all stochastic nodes sharing the same root (family).
Rules are lists with at least
typeandtarget(for nodes) orfamily(for families), and optionalcontrol. Supported samplertypevalues include"slice","RW","RW_block","AF_slice","NUTS","NUTS_block"(the latter require nimbleHMC).Unsampled-node safety: if
ensure_unsampled=TRUE, any remaining unsampled stochastic nodes receive aslicesampler to avoid invalid configurations.Parallel backends:
"PSOCK"(portable) or"FORK"(Unix);"AUTO"selects"FORK"on Unix and"PSOCK"otherwise.GEREM-style stability option:
mcmc_projectcontrols whether the compiled MCMC is built against the compiled model ("cmodel") or the uncompilednimbleModel("model"). Some legacy scripts and certain models may be more stable withmcmc_project="model".
Requirements / contract:
build_fnmust return a list containing at least$model(animbleModelobject).For
base_policy="baseline",build_fnmust also provide$conf(aMCMCconf).If
$cmodelis not returned, it is compiled internally (per chain).For true parallel execution,
build_fnshould acceptchain_id(and optionallyexport_global) so each worker can build an independent chain with distinct initial values.
Returned object:
samples: anmcmc.listwhen possible (otherwise a list of per-chain samples)runtime_s: the maximum wall time across chains for therunMCMCphaseconf:NULLin parallel mode (unambiguous); a singleconfobject in sequential modeconf_by_chain: list of per-chainMCMCconfobjects (also stored as an attribute)