Skip to content

Round the PSIS tail length down in n_pareto() - #437

Open
soujasK wants to merge 4 commits into
stan-dev:masterfrom
soujasK:fix/408-n-pareto-floor
Open

soujasK wants to merge 4 commits into
stan-dev:masterfrom
soujasK:fix/408-n-pareto-floor

Conversation

@soujasK

@soujasK soujasK commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Part of #408

As discussed in #408, n_pareto() now uses floor() for the PSIS tail length instead of ceiling(), same as the PSIS paper (Appendix H) and posterior::ps_tail_length().

Changes:

  • R/psis.R: ceiling() -> floor(). I also fixed the roxygen note, which said 3*sqrt(S)/r_eff while the code computes 3*sqrt(S/r_eff).
  • tests/testthat/_snaps: updated the snapshots that changed. The tail can be one draw shorter, so the values move a little (e.g. elpd_loo -83.58926 -> -83.58963 in deprecated_extractors). Every changed snapshot block has a real value change.
  • tests/testthat/test_loo_moment_matching.R: the pinned mcse_elpd_loo from 23005f4 goes from 0.1904162 to 0.1941738. I checked that the same test setup gives exactly 0.1904162 with ceiling() and 0.1941738 with floor(), so the change comes only from the tail length. @avehtari could you check this one is ok?
  • NEWS.md: add a bullet

Tests: devtools::test() passes locally (1366 passed, 0 failed, 0 skipped) with R 4.4.3. The serialized snapshots were written with R 4.4.3, so their header bytes differ from the old ones written with 4.5.0. expect_snapshot_value() compares the deserialized values, so this doesn't affect the tests.

@florence-bockting

AI disclosure: I used Claude to help prepare this change. I reviewed and understood all of it and can explain and defend it in review.

n_pareto() used ceiling() for the tail length, while the PSIS paper
(Appendix H) and posterior::ps_tail_length() use floor(). As discussed
in stan-dev#408 it only has to be an integer, so loo now uses floor() too for
consistency. The roxygen note also said 3*sqrt(S)/r_eff, which is fixed
to match the code (3*sqrt(S/r_eff)).

The tail can be one draw shorter, so results change slightly. The
affected snapshots are updated, and the pinned mcse_elpd_loo value in
the moment matching test goes from 0.1904162 to 0.1941738.

Part of stan-dev#408

Signed-off-by: Soujas K <kudchadkarsoujas@gmail.com>
@codecov-commenter

codecov-commenter commented Dec 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.81%. Comparing base (aca15cc) to head (7498582).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #437   +/-   ##
=======================================
  Coverage   92.81%   92.81%           
=======================================
  Files          31       31           
  Lines        3074     3074           
=======================================
  Hits         2853     2853           
  Misses        221      221           

☔ 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 Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

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

Status Name Current Incoming Interval
✔️ loo_matrix 1.18s 1.18s [-1.65%, +1.31%]
✔️ loo_function 1.19s 1.17s [-4.34%, +1.03%]

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

Comment thread NEWS.md Outdated
posterior >= 1.8.0 by @soujasK in #433
* Round the PSIS tail length down instead of up in the internal
`n_pareto()`, as in the PSIS paper and `posterior::ps_tail_length()`, by
@soujasK in #437

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably mention in the NEWS.md also the output change that this fix causes. Something like "Results of psis(), loo(), E_loo() and other functions that use psis() can change slightly."

Comment thread R/psis.R
Comment on lines 53 to 54
#' \item{`tail_len`}{
#' Vector of tail lengths used for fitting the generalized Pareto distribution.

@florence-bockting florence-bockting Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we should probably add the information about the output change.

#'   \item{`tail_len`}{
#'     Vector of tail lengths used for fitting the generalized Pareto  distribution. 
#'     The tail length is `floor(min(0.2 * S, 3 * sqrt(S / r_eff)))`. Earlier versions of loo
#'     rounded up, so results can differ slightly.
#'   }

@jgabry do you think that's enough here and in the NEWS.md or should we communicate this additionally somewhere else?

Comment thread R/psis.R

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, add a test that fails on the old code.

test_that("psis() rounds the tail length down", {
  psis_out <- psis(-example_loglik_matrix())
  expect_equal(unique(attr(psis_out, "tail_len")), 94)
})

@florence-bockting florence-bockting left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much, this looks already very good.
Just some minor comments.

@soujasK

soujasK commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Yeah @florence-bockting i would review the small changes and apply where applicable

Mention in NEWS.md and the psis() docs that results can change slightly,
and add a test that fails with the old ceiling rounding.

Signed-off-by: Soujas K <kudchadkarsoujas@gmail.com>
@soujasK

soujasK commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @florence-bockting!I added all three:the NEWS.md note about the output change,the formula in the tail_len docs (and regenerated man/psis.Rd),and your test,which fails with the old ceiling and passes with floor.

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