
Calculate Prior and Posterior Quantiles with KL Divergence
Source:R/calc_model_posterior_quantiles.R
calc_model_posterior_quantiles.RdCalculates quantiles for both prior (all simulations) and posterior (weighted best subset) distributions, with the KL divergence of the posterior from the prior.
Usage
calc_model_posterior_quantiles(
results,
probs = c(0.025, 0.25, 0.5, 0.75, 0.975),
output_dir = "./results",
priors = NULL,
subset_col = "is_best_subset",
weight_col = "weight_best",
verbose = TRUE
)Arguments
- results
Data frame with calibration results containing parameter columns and index columns (is_finite, is_retained, is_best_subset)
- probs
Numeric vector of quantile probabilities (default: c(0.025, 0.25, 0.5, 0.75, 0.975))
- output_dir
Directory path to save results (default: "./results")
- priors
Optional priors list (as returned by
get_location_priors). When provided, prior distribution families are read from this object instead of the staticestimated_parameterslookup. This ensures posterior fitting uses the actual prior family, which is critical for staged estimation with country-specific priors. When NULL (default), falls back to the static lookup for backward compatibility.- subset_col
Character name of the boolean subset-membership column to use when computing posterior quantiles. Defaults to
"is_best_subset"(tier-selected subset). Pass"is_best_subset_opt"to drive the posterior from the optimizer-refined subset.- weight_col
Character name of the per-row weight column paired with
subset_col. Defaults to"weight_best".- verbose
Logical; print progress messages (default: TRUE)
Details
The function:
Calculates unweighted quantiles from ALL finite simulations (prior)
Calculates weighted quantiles from best subset (posterior)
Computes the information gain KL(posterior || prior) with the percentile-scale estimator of
calc_kl_divergence(since MOSAIC 1.4.0): posterior draws mapped to their prior percentiles, 20 equal bins, finite-ESS correction. It is invariant to the parameter's scale and at mostlog(20), about 3 nats; it is NA when the posterior's Kish ESS is below 20Returns both prior and posterior rows with a 'type' identifier
KL divergence appears only in posterior rows