Skip to content

412: Warn about quantile crossing and add get_crossing_quantiles() - #1254

Draft
nikosbosse wants to merge 7 commits into
mainfrom
issue-412-non-monotonic-quantiles
Draft

nikosbosse wants to merge 7 commits into
mainfrom
issue-412-non-monotonic-quantiles

Conversation

@nikosbosse

@nikosbosse nikosbosse commented Nov 8, 2026 •

Copy link
Copy Markdown
Collaborator

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 in run_safely(), so users got one warning per failed metric, and the affected columns were silently dropped for all forecasts:

  • bias fails with "Predictions must not be decreasing with increasing quantile level".
  • wis, overprediction, underprediction and dispersion fail with "All values in upper need 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:

  • New exported helper get_crossing_quantiles(data, forecast_unit = NULL) (keyword diagnose-inputs), modelled on get_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 in predicted/quantile_level are ignored within each forecast (as bias_quantile(na.rm = TRUE) does). predicted and quantile_level must be numeric.
  • Validation warning (Add check to validate_forecast() that predictions must not decrease with quantile #412): assert_forecast.forecast_quantile() now issues a cli_warn when quantiles cross, if verbose = TRUE. This covers as_forecast_quantile() and assert_forecast(). The warning gives the number of affected forecasts, names wis() and bias_quantile() as metrics that may fail, and points to get_crossing_quantiles().
  • Error hints on the score() path: the bias_quantile() error and the upper < lower error in the internal assert_input_interval() (used by wis() and its components) both get an i bullet pointing to get_crossing_quantiles(). run_safely() includes the full message, so score()'s per-metric warnings now carry the hint too.
  • Docs: as_forecast_quantile() and get_crossing_quantiles() explain which metrics fail and that score() drops their columns.

Notes for the maintainer:

  • New export, please sign off on the name. get_crossing_quantiles() uses the standard term "quantile crossing". Possible alternatives are get_non_monotonic_quantiles() or get_decreasing_quantiles().
  • score() does not issue an extra crossing warning. clean_forecast() validates with verbose = FALSE, so score() does not repeat the validation warning. Instead, the per-metric failure warnings that score() already emits now point to get_crossing_quantiles(). This covers objects built under suppressWarnings() or loaded from disk, without adding a second warning.
  • No metric behaviour changes. bias_quantile() and wis() still error on crossing quantiles, as agreed in the Add check to validate_forecast() that predictions must not decrease with quantile #412 thread: add a helper, keep enforcing in the metrics, warn at validation. example_quantile does 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:

  • Impact understated (major). Docs, NEWS, the warning text and this description said that only bias_quantile() is affected. They now also name wis() and its components, and they say that score() warns and drops the columns rather than erroring. The upper < lower error used by wis() now points to get_crossing_quantiles(). A new test runs score() on a crossing forecast and checks that both the wis and bias failure 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 stays verbose-only, so score() does not warn twice.
  • Unexplained as.data.table() call and missing type checks (minor). I added a comment explaining that the call strips the forecast class so that [.forecast does not re-validate. I added assert_numeric() checks for predicted and quantile_level. A character predicted column previously compared strings lexically; this is now tested.
  • NA semantics (minor). The docs now say that NA handling matches bias_quantile(na.rm = TRUE).
  • Brittle warning test (nit). The test now uses an explicit three-forecast data.frame instead of example_quantile row positions. The no-warning cases (ties and example_quantile) are in their own test_that() block.
  • Duplicate expect_error() (nit). The two calls are folded into one regex covering both the message and the hint.
  • Counting (nit). The count now uses 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 on main.

  • 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 and example_quantile not warning, for the bias_quantile() hint, and for the hint in score()'s wis/bias failure warnings. I verified that they fail before the change and pass after it. With R/ and NAMESPACE reverted to origin/main and the helper file removed, the targeted test files gave 7 failures/errors. Against the pre-review PR head, the two tests added after review (the score() hint and the predicted type check) failed. With the final code all pass.

  • I have updated the documentation if required. This covers the new man/get_crossing_quantiles.Rd and the updated man/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 NAMESPACE export 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 with lintr::lint(). The only hits are "no visible binding" warnings, which come from the package not being installed. The object_usage_linter false positive on n_crossing, which is used only inside the cli glue string, is silenced with a #nolint block, as in run_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. Merged main to resolve the NEWS conflict.

🤖 Generated with Claude Code

nikosbosse and others added 5 commits November 8, 2026 13:15
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

codecov Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.19%. Comparing base (779d7d6) to head (9814b97).

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

nikosbosse and others added 2 commits November 9, 2026 23:28
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

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.

Check quantile prediction order helper function Add check to validate_forecast() that predictions must not decrease with quantile

1 participant