Skip to content

Add Monte Carlo diagnostics for the elpd of LOO-CV - #432

Draft
florence-bockting wants to merge 37 commits into
masterfrom
trust-diagnostics
Draft

florence-bockting wants to merge 37 commits into
masterfrom
trust-diagnostics

Conversation

@florence-bockting

@florence-bockting florence-bockting commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds Monte Carlo diagnostics for the elpd estimate of LOO-CV. Each of the four cv_ functions returns the estimate together with its MCSE, bias and predicted RMSE. Each function warns when the estimate fails its trust rule.

New exported functions

Function Method Trust rule
cv_psis(x, chain_id, psis_object) PSIS-LOO predicted RMSE < 1 nat, and no Pareto k above ps_khat_threshold()
cv_mixis(x, alpha, chain_id) mixture IS predicted RMSE < 1 nat
cv_brute_force(x, log_lik_full, chain_id) refits predicted RMSE < 1 nat
cv_bridge(x, log_lik_full, chain_id, chain_id_full) CV bridge every fold: finite elpd, MCSE < 0.3, gap <= 50 nat
  • mixis_alpha(): balanced mixture weights from a PSIS result.
  • mixis_n_eff(): the predicted ESS of the MixIS weights for a candidate alpha.

The cv_elpd class

  • estimates: total elpd, mcse, bias, rmse.
  • pointwise: elpd, mcse_elpd, bias_elpd per fold.
  • diagnostics: depends on the method (Pareto k, r_eff, gap, ...).
  • mc_error$interval: the 95% MC interval of the exact total elpd.
  • Methods: print(), print_dims(), dim(), as.data.frame().

Warnings

  • A failed trust rule: new class loo_warning_cv_elpd_untrusted.
  • A heavy tail of the MixIS common weights: loo_warning_pareto_k_high. This warning is not part of the trust rule.
  • The posterior ESS warnings of the per-fold calls are collected into one warning per class.

Agreement with the reference implementation

The values were cross-checked against loobench on four cases: base, heavy tail (k = 1.36), autocorrelated chains, and LOGO. All methods agree to >= 0.9997. There is one exception: the MixIS bias in the heavy-tail case.
In that case loobench calls ess_mean() on unscaled weights (max 7e-79). is_constant() then gives NA, and the ESS falls back to S. This PR rescales the weights first (ESS 4024), as the paper does.

Tests

New files: test_cv_psis.R, test_cv_mixis.R, test_cv_brute_force.R, test_cv_bridge.R, test_cv_elpd.R, plus a snapshot file and one case in test_warning_classes.R.

In this PR I used LLM assistance.

@codecov-commenter

codecov-commenter commented Nov 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.75926% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.30%. Comparing base (2d229e5) to head (5c04520).

Files with missing lines Patch % Lines
R/cv_elpd.R 94.65% 7 Missing ⚠️
R/cv_elpd-log_mean.R 94.79% 5 Missing ⚠️
R/cv_elpd-bridge.R 97.50% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #432      +/-   ##
==========================================
+ Coverage   92.82%   93.30%   +0.48%     
==========================================
  Files          31       37       +6     
  Lines        3078     3510     +432     
==========================================
+ Hits         2857     3275     +418     
- Misses        221      235      +14     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

This is how benchmark results would change (along with a 95% confidence interval in relative change) if 5c04520 is merged into master:

Status Name Current Incoming Interval
✔️ loo_matrix 1.49s 1.5s [-0.29%, +0.8%]
✔️ loo_function 1.33s 1.33s [-0.59%, +0.31%]

Further explanation regarding interpretation and methodology can be found in the documentation.

@avehtari

Copy link
Copy Markdown
Member

Looks good. In addition of going through the code myself, here are Claude assisted findings

  • NA when the likelihood rounds to 1. In .log_mean_diags(), exp(log_z - log_max) rounds all draws to one or two doubles when the log-lik values are above about -1e-16. Fewer than 5 exceedances are then positive, .fit_right_tail() returns NULL, and the MCSE and bias are NA. Example: Bernoulli, y = 1, eta ~ N(40, 1), S = 4000. cv_brute_force() gives mcse_elpd = NA, cv_psis() gives bias_elpd = NA, and the trust warning appears for a fold with elpd 0. The exceedances, the bound distances and the bulk deviations should be computed from the log values, as .lb_exceed_log() and .lb_bound_dist() in loobench do.

  • The log_lik_full bound can truncate the tail below an observed draw.** max_s exp(log_lik_full) is not a true bound. When it is between the GPD threshold and the largest refit draw, it truncates the tail below that draw. Example: fitted k = 0.62 and a bound 1 nat below the largest draw give MCSE 0.0288 instead of 0.0404. I changed the code in loobench https://github.com/avehtari/loobench/commit/641e49.

  • Check also the wording in doc "Its largest density per fold bounds the tail of the truncated-GPD MCSE." whether we could use some other word than bound. Maybe just "truncates the tail"?

  • cv_mixis(): var_v falls back to S when the ESS fails, but var_u and mcse use NA. A failed ESS of one v_i then gives a bias value, while the docs say NA.

  • cv_mixis(): max(abs(log_u)) > 1e10 sets the whole pointwise matrix to NA, including elpd. Maybe set only the MCSE and bias to NA, or explain 1e10.

  • cv_psis() warns about high Pareto k twice: from psis() (or throw_pareto_warnings()) and in the trust warning. Please give one warning.

  • mixis_n_eff() docs say "importance sampling from the posterior to the mixture". The draws come from the mixture, and the weights u = 1/m target the posterior. Please reverse the direction.

  • chain_id is the second argument of cv_psis() but the third of cv_mixis() and cv_brute_force(). Should the order be the same?

  • There is a minor change coming to CV bridge MCSE computation. For the tested cases less than 1% change, but improves consistency of the code and explanations. I'll implement that to loobench first.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants