1198: Fix bias_quantile() quantile level sorting and na.rm crash#1205
Open
nikosbosse wants to merge 1 commit into
Open
1198: Fix bias_quantile() quantile level sorting and na.rm crash#1205nikosbosse wants to merge 1 commit into
nikosbosse wants to merge 1 commit into
Conversation
bias_quantile_single_vector() sorted predictions by quantile level but left the quantile levels themselves unsorted, so predictions and levels became mispaired and unsorted input produced silently wrong bias values. Both vectors are now reordered together. Additionally, when na.rm = TRUE removed all quantile levels on one side of the median, interpolate_median() produced a length-zero median and the function crashed. bias_quantile() now returns NA_real_ in this case, consistent with na.rm = FALSE. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1205 +/- ##
=======================================
Coverage 98.15% 98.15%
=======================================
Files 41 41
Lines 2225 2228 +3
=======================================
+ Hits 2184 2187 +3
Misses 41 41 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Collaborator
Author
|
Automated review (Claude Fable, directed by @nikosbosse): Verdict: approve. Independently verified fail-before/pass-after and found no defects. Checks performed:
No must-fix findings. |
nikosbosse
marked this pull request as draft
July 18, 2026 13:53
nikosbosse
marked this pull request as ready for review
July 18, 2026 20:50
Collaborator
Author
|
lgtm |
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.
Description
This PR closes #1198.
bias_quantile()had two related bugs inbias_quantile_single_vector()(R/metrics-quantile.R):Wrong results with unsorted quantile levels: predictions were reordered by quantile level (
predicted <- predicted[order]) butquantile_levelitself was left unsorted, so predictions and levels became mispaired. The same forecast then produced different bias values depending on input order, e.g.bias_quantile(1.5, c(1, 2, 3), c(0.25, 0.5, 0.75))returned0.5while the identical forecastbias_quantile(1.5, c(3, 1, 2), c(0.75, 0.25, 0.5))returned-0.5. Fixed by sortingquantile_leveltogether withpredicted.Crash with
na.rm = TRUE: when NA removal stripped all quantile levels on one side of the median,interpolate_median()produced a length-zero median (viamax(numeric(0))returning-Infwith a warning) and the function crashed withargument is of length zero. Fixed by returningNA_real_when the median is no longer interpolable after NA removal, consistent withna.rm = FALSE, which also returnsNA_real_silently.New regression tests were written first and confirmed to fail on
main:After the fix, the full test suite passes (0 failed, 911 passed). Note one intentional behaviour change beyond bugfixing correctness: internally consistent forecasts passed with unsorted quantile levels, which previously either returned wrong values or errored with "Predictions must not be decreasing", now score correctly; genuinely inconsistent input still errors as before (existing tests unchanged).
Dev note: bug identified by an LLM audit (#1189); fix and tests implemented with LLM support, directed by @nikosbosse.
Checklist
lintron the changed files to check for style issues introduced by my changes.