Repository navigation
Sort only the tail in do_psis_i() - #413
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #413 +/- ##
==========================================
+ Coverage 92.48% 92.49% +0.01%
==========================================
Files 31 31
Lines 3139 3146 +7
==========================================
+ Hits 2903 2910 +7
Misses 236 236 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Contributor
|
This is how benchmark results would change (along with a 95% confidence interval in relative change) if 328af19 is merged into master:
|
jgabry
requested changes
Oct 21, 2026
jgabry
left a comment
Member
There was a problem hiding this comment.
Two tiny suggestions, otherwise this looks good.
Member
Author
|
Thanks, applied both suggestions. Results are still identical to master on the same test inputs |
jgabry
approved these changes
Oct 21, 2026
jgabry
left a comment
Member
There was a problem hiding this comment.
Looks good. Ok to merge when CI passes
VisruthSK
approved these changes
Oct 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Related to #260.
#290 will replace this code with
posterior::ps_tail(), and stan-dev/posterior#452 adds the same partial sort there. loo has to work with the posterior version on CRAN until a posterior release contains #452, so this PR makes the same change in loo's own code now. When #290 is merged, this code goes away.do_psis_i()sorted all S log ratios of each observation to find the tail oftail_lenvalues and the cutoff below it. It now finds the cutoff with a partial sort (sort.int(partial = S - tail_len)) and sorts only the values at or above the cutoff. The results are identical:order()andsort.int(index.return = TRUE)sort doubles stably, and the candidate set contains all values that tie with the cutoff, so the selected tail and its order do not change.Speed
Median time of
psis()on one core (Intel i5-1335U, R 4.6.1), log ratiosrnorm() + rexp(rate = 2),r_eff = 1:Tests
psis()returnsidentical()objects on master and with this PR for 8 inputs:example_loglik_matrix()andexample_loglik_array()(which have tied draws), normal and t(1) log ratios, rounded log ratios with many ties, a column with-Infvalues, S = 30, and a constant tail.New test in
tests/testthat/test_psis.R:do_psis_i()gives an identical result to the previous full-sort version for two vectors with ties; in the first one, 9 tail values equal the cutoff.devtools::test()withNOT_CRAN=truepasses all 1267 tests and gives no warnings (R 4.6.1, testthat 3.3.2, Linux x86_64).