Skip to content

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

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

florence-bockting wants to merge 46 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.

References

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.25551% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.25%. Comparing base (1d64b82) to head (7e4c14d).

Files with missing lines Patch % Lines
R/cv_elpd.R 94.92% 7 Missing ⚠️
R/cv_elpd-bridge.R 94.04% 5 Missing ⚠️
R/cv_elpd-log_mean.R 95.28% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #432      +/-   ##
==========================================
+ Coverage   92.81%   93.25%   +0.44%     
==========================================
  Files          31       37       +6     
  Lines        3075     3529     +454     
==========================================
+ Hits         2854     3291     +437     
- Misses        221      238      +17     

☔ 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 7e4c14d is merged into master:

Status Name Current Incoming Interval
✔️ loo_matrix 1.41s 1.41s [-0.31%, +1%]
✔️ loo_function 1.21s 1.2s [-1.59%, +0.87%]

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 avehtari/loobench@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.

@florence-bockting

florence-bockting commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor Author

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

Thank you for reviewing, I went through the points and summarise each below.
Each fix has a test that fails on the old code.

  • 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.

See 1c73f9e: .log_mean_diags() now computes the tail exceedances, the bulk deviations and the bound distance from the log values, as .lb_exceed_log() does.

  • 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 avehtari/loobench@641e49.

See 5a6e2b9: a bound below the largest draw (log gap above 1e-12 * max(1, |l_max|)) is dropped, and only the cap tau_M applies, as in loobench.

  • 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"?

See 5a6e2b9: now "Its largest density per fold truncates the GPD tail in the MCSE, unless it is below the largest refit draw."

  • 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.

See 117692e: I kept the code, because it matches mixis_jensen_bias() in loobench (v falls back to S, u gives NA). But I updated the docs that it is consistent with the code.

  • 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.

See 117692e: I kept NA for the whole pointwise matrix, as in .lb_mixis_terms() and the docs now explain the limit.

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

See d78993e: cv_psis() gives one warning. When the k part of the trust rule fails, the warning has both classes, loo_warning_cv_elpd_untrusted and loo_warning_pareto_k_high.

  • 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.

fixed in 2ef8d33

  • 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?

See ff10b2e: now cv_psis(x, psis_object = NULL, chain_id = NULL), so chain_id is the third argument in all cv_ functions.

  • 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.

See 7e4c14d: The fold MCSE of cv_bridge() is now the IJ of the fold influences. So the fold MCSE and the total MCSE use the same d_check. d_check <= 0 gives an NA MCSE. The trust rule now flags a fold with a non-finite MCSE.

@florence-bockting
florence-bockting marked this pull request as ready for review December 1, 2026 07:36
@florence-bockting
florence-bockting marked this pull request as draft December 1, 2026 07:54

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