Repository navigation
412: Warn about quantile crossing and add get_crossing_quantiles() - #1254
Draft
nikosbosse wants to merge 7 commits into
Draft
nikosbosse wants to merge 7 commits into
nikosbosse wants to merge 7 commits into
Conversation
Add get_crossing_quantiles(), which returns all rows of quantile forecasts whose predictions decrease with increasing quantile level. assert_forecast() for quantile forecasts (and hence as_forecast_quantile()) now warns when such forecasts are present, pointing users to the helper. bias_quantile() keeps erroring on them. Closes #412, closes #895. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add a hint to the bias_quantile() error for crossing quantiles, test that ties and example_quantile do not warn, use a standalone roxygen example and place the NEWS bullet under a New features heading. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Docs, NEWS and the validation warning now say that wis() and its components, not only bias_quantile(), fail for crossing quantiles and that score() drops those columns. - The upper < lower error in assert_input_interval() points to get_crossing_quantiles(), so score()'s per-metric warnings carry the hint for wis too. - get_crossing_quantiles() asserts numeric predicted/quantile_level, explains the class strip and documents its NA handling. - Use uniqueN() to count crossing forecasts; tests use explicit data. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1254 +/- ##
==========================================
+ Coverage 99.18% 99.19% +0.01%
==========================================
Files 41 42 +1
Lines 2331 2371 +40
==========================================
+ Hits 2312 2352 +40
Misses 19 19 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…ic-quantiles # Conflicts: # NEWS.md
Sort the complete rows once and compare neighbouring rows instead of sorting within every forecast. On 2.65M rows (115k forecasts) the check takes about 0.4s instead of 1.2s, which matters because it runs during forecast validation. Results are unchanged; adds tests for data without forecast unit columns and for rows with missing values. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This branch has not been deployed
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.
CLAUDE:
Description
This PR closes #412 and closes #895.
Quantile forecasts whose predictions decrease with increasing quantile level (quantile crossing) used to pass validation. The problem only showed up inside
score(). There,score()wraps each metric inrun_safely(), so users got one warning per failed metric, and the affected columns were silently dropped for all forecasts:biasfails with "Predictions must not be decreasing with increasing quantile level".wis,overprediction,underpredictionanddispersionfail with "All values inupperneed to be greater than or equal to ...lower" whenever the crossing inverts a central prediction interval (e.g. the 0.05 quantile above the 0.95 quantile).Nothing told users which forecasts were affected (#895).
Changes:
get_crossing_quantiles(data, forecast_unit = NULL)(keyworddiagnose-inputs), modelled onget_duplicate_forecasts(). It returns every row of each forecast whose predictions, sorted by quantile level, are not non-decreasing. Ties are allowed, row order does not matter, and NAs inpredicted/quantile_levelare ignored within each forecast (asbias_quantile(na.rm = TRUE)does).predictedandquantile_levelmust be numeric.validate_forecast()that predictions must not decrease with quantile #412):assert_forecast.forecast_quantile()now issues acli_warnwhen quantiles cross, ifverbose = TRUE. This coversas_forecast_quantile()andassert_forecast(). The warning gives the number of affected forecasts, nameswis()andbias_quantile()as metrics that may fail, and points toget_crossing_quantiles().score()path: thebias_quantile()error and theupper < lowererror in the internalassert_input_interval()(used bywis()and its components) both get anibullet pointing toget_crossing_quantiles().run_safely()includes the full message, soscore()'s per-metric warnings now carry the hint too.as_forecast_quantile()andget_crossing_quantiles()explain which metrics fail and thatscore()drops their columns.Notes for the maintainer:
get_crossing_quantiles()uses the standard term "quantile crossing". Possible alternatives areget_non_monotonic_quantiles()orget_decreasing_quantiles().score()does not issue an extra crossing warning.clean_forecast()validates withverbose = FALSE, soscore()does not repeat the validation warning. Instead, the per-metric failure warnings thatscore()already emits now point toget_crossing_quantiles(). This covers objects built undersuppressWarnings()or loaded from disk, without adding a second warning.bias_quantile()andwis()still error on crossing quantiles, as agreed in the Add check tovalidate_forecast()that predictions must not decrease with quantile #412 thread: add a helper, keep enforcing in the metrics, warn at validation.example_quantiledoes not trigger the warning.Implementation: two independent drafts were compared by a judge agent; draft B was chosen as base. A few parts of draft A were added on top: extra negative tests, the roxygen example, and the NEWS heading.
Review
An independent adversarial agent reviewed the PR. Changes made in response:
bias_quantile()is affected. They now also namewis()and its components, and they say thatscore()warns and drops the columns rather than erroring. Theupper < lowererror used bywis()now points toget_crossing_quantiles(). A new test runsscore()on a crossing forecast and checks that both thewisandbiasfailure warnings carry the hint and that both columns are dropped.score()path never mentions crossing (minor). This is addressed by the hint in the per-metric failure messages, which is the reviewer's second option. The validation warning staysverbose-only, soscore()does not warn twice.as.data.table()call and missing type checks (minor). I added a comment explaining that the call strips the forecast class so that[.forecastdoes not re-validate. I addedassert_numeric()checks forpredictedandquantile_level. A characterpredictedcolumn previously compared strings lexically; this is now tested.bias_quantile(na.rm = TRUE).example_quantilerow positions. The no-warning cases (ties andexample_quantile) are in their owntest_that()block.expect_error()(nit). The two calls are folded into one regex covering both the message and the hint.uniqueN(crossing, by = ...).Rebutted: nothing. Every finding was applied.
Checklist
My PR is based on a package issue and I have explicitly linked it.
I have included the target issue or issues in the PR title as follows: issue-number: PR title
I have tested my changes locally. The full
devtools::test()run gives FAIL 0, PASS 1060. Its 4 warnings are unrelated and also occur onmain.I have added or updated unit tests where necessary. There are tests for
get_crossing_quantiles()(including type checks), for the validation warning, for ties andexample_quantilenot warning, for thebias_quantile()hint, and for the hint inscore()'swis/biasfailure warnings. I verified that they fail before the change and pass after it. WithR/andNAMESPACEreverted toorigin/mainand the helper file removed, the targeted test files gave 7 failures/errors. Against the pre-review PR head, the two tests added after review (thescore()hint and thepredictedtype check) failed. With the final code all pass.I have updated the documentation if required. This covers the new
man/get_crossing_quantiles.Rdand the updatedman/as_forecast_quantile.Rd.I have built the package locally and run rebuilt docs using roxygen2. Note that the local roxygen2 is 8.0.0 and the repo uses 7.3.3, so I reverted the unrelated churn. I added the
NAMESPACEexport by hand, as one line.My code follows the established coding standards and I have run
lintr::lint_package()to check for style issues introduced by my changes. I linted the changed files withlintr::lint(). The only hits are "no visible binding" warnings, which come from the package not being installed. Theobject_usage_linterfalse positive onn_crossing, which is used only inside the cli glue string, is silenced with a#nolintblock, as inrun_safely().I have added a news item linked to this PR.
I have reviewed CI checks for this PR and addressed them as far as I am able. CI pending.
Follow-up:
get_crossing_quantiles()now sorts the data once and compares neighbouring rows instead of sorting within each forecast. On 2.65M rows (115k forecasts) the check takes about 0.4s instead of 1.2s, which reduces what it adds to forecast validation. Results are unchanged. Added tests for data without forecast unit columns and for rows with missing values. Mergedmainto resolve the NEWS conflict.🤖 Generated with Claude Code