Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/mcmc/execution/chain_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@
#include "mcmc/samplers/metropolis_sampler.h"


namespace {

// Store this sample's NUTS per-iteration diagnostics, if the chain is collecting
// them and the step produced a NUTSDiagnostics object. Keeps the dynamic_cast
// and the wide store_nuts_diagnostics call out of the main sampling loop.
void store_nuts_diagnostics_if_present(ChainResult& chain_result, int sample_index,
const SamplerBase& sampler, const StepResult& result) {
if (!chain_result.has_nuts_diagnostics || !sampler.has_nuts_diagnostics()) return;
auto* diag = dynamic_cast<NUTSDiagnostics*>(result.diagnostics.get());
if (diag) {
chain_result.store_nuts_diagnostics(sample_index, diag->tree_depth, diag->divergent,
diag->non_reversible, diag->energy, diag->accept_prob);
}
}

} // namespace


SamplerSpec resolve_sampler_spec(const std::string& sampler_type) {
if (sampler_type == "nuts") {
return SamplerSpec{SamplerKind::NUTS, /*learn_sd=*/true, /*nuts_diag=*/true, /*am_diag=*/false};
Expand Down Expand Up @@ -88,12 +106,7 @@ void run_mcmc_chain(
if (schedule.sampling(iter)) {
int sample_index = iter - config.no_warmup;

if (chain_result.has_nuts_diagnostics && sampler->has_nuts_diagnostics()) {
auto* diag = dynamic_cast<NUTSDiagnostics*>(result.diagnostics.get());
if (diag) {
chain_result.store_nuts_diagnostics(sample_index, diag->tree_depth, diag->divergent, diag->non_reversible, diag->energy, diag->accept_prob);
}
}
store_nuts_diagnostics_if_present(chain_result, sample_index, *sampler, result);

if (chain_result.has_am_diagnostics) {
chain_result.store_am_diagnostics(sample_index, result.accept_prob);
Expand Down
Loading