Skip to content

1237: Speed up score() for quantile forecasts - #1256

Draft
nikosbosse wants to merge 4 commits into
mainfrom
issue-1237-faster-quantile-scoring
Draft

nikosbosse wants to merge 4 commits into
mainfrom
issue-1237-faster-quantile-scoring

Conversation

@nikosbosse

@nikosbosse nikosbosse commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

CLAUDE:

Description

This PR closes #1237.

It speeds up score() for quantile-based forecasts with three internal changes. No function signature, argument or return value changes, and the contract between score() and the metrics (observed, predicted matrix, quantile_level) stays as it is.

On the example from the issue (120,000 forecasts, 23 quantile levels), score() with the default metrics goes from 12.3s to 2.2s on my machine, with identical scores.

Change Where Before After
Build observed and predicted with one sort if all forecasts have the same quantile levels; otherwise use the existing path score.forecast_quantile() 6.7s (incl. validation) 1.8s (incl. validation)
Build the interval table from matrix columns instead of dcast() quantile_to_interval_numeric() 0.72s per call, six calls with the default metrics 0.04s per call
Vectorise over forecasts; forecasts with NA are still handled one by one bias_quantile() 1.42s 0.03s

Design notes:

Visible differences, all for input that score() rejects or never produces, i.e. only when calling the metrics directly:

  • bias_quantile() with an NA in observed still errors, but the message now reads "Contains missing values" instead of "May not be NA".
  • bias_quantile() with infinite predictions used to fail with "missing value where TRUE/FALSE needed" whenever two neighbouring predictions were both Inf. It now returns a bias, or NA if the median cannot be interpolated because it lies between a finite and an infinite prediction.
  • Quantile levels that differ only after the 10th decimal (e.g. 0.3 and 0.1 * 3) used to fail in wis() with an unrelated error about upper and lower. They now fail with "Quantile levels must be unique after rounding to 10 digits".
  • wis() with an NA quantile level and no level on one side of the median now reports "No valid forecast intervals found" rather than the message about symmetric intervals.

How I checked that scores are unchanged, comparing this branch against main:

  • identical score() output on the issue's example;
  • identical score() output, including row order, on example_quantile, a shuffled copy, a copy with mixed quantile levels (fallback path) and one with a missing row;
  • wis(), bias_quantile() and interval_coverage() on 14 edge cases across all argument combinations (no median, incomplete intervals, unsorted levels, levels 0 and 1, median only, asymmetric levels; each with and without NA).

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. Full test suite: 0 failures, 1073 passes.
  • I have added or updated unit tests where necessary. This is a performance refactor, so a test that fails before and passes after is N/A: scores are unchanged by design. The new tests cover the new code paths (vectorised bias against the one-by-one result including ties and interpolated medians, the interval table, the fast and fallback input paths, the error paths).
  • I have updated the documentation if required. N/A: no user-facing documentation changes.
  • I have built the package locally and run rebuilt docs using roxygen2. N/A: no roxygen changes.
  • My code follows the established coding standards and I have run lintr::lint_package() to check for style issues introduced by my changes. Linted the six changed files, no lints.
  • 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.

Adversarial review

An independent agent reviewed the PR and compared the branch with main on 325 targeted and 1,600 random cases; it found no difference in scores for input that score() accepts. Addressed:

  • score() failed if a forecast-unit column was called scoringutils_id; the temporary column name is now made unique (with a test).
  • An NA quantile level gave a cryptic error in the interval helper; fixed (with tests).
  • Near-duplicate quantile levels were silently accepted by the new helper; they now raise an error (with a test).
  • The bias tests did not catch a missing "observation on the median" rule or a wrong interpolation weight; added ties, an asymmetric set of levels without a median and explicit expected values.
  • Three existing error/return branches were no longer reached by any test; added tests that reach them.
  • The NA-in-observed message depended on the row; observed is now checked once up front.
  • The changed behaviour for infinite predictions was undisclosed; now listed above.

Not applied:

  • Replacing the fallback path with the new construction applied per group of quantile levels. Keeping the old path verbatim means forecasts with differing quantile levels are scored by exactly the code that scored them before.
  • Sharing the duplicated "No valid forecast intervals found" message between the two helpers, and a test asserting that the fast path is taken: both small, left out to keep the diff focused.

🤖 Generated with Claude Code

nikosbosse and others added 3 commits November 10, 2026 17:54
Build the inputs to the metrics with one sort when all forecasts share the
same quantile levels, build the interval table from matrix columns instead of
dcast(), and vectorise bias_quantile(). No function signature changes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1256      +/-   ##
==========================================
+ Coverage   99.18%   99.20%   +0.02%     
==========================================
  Files          41       41              
  Lines        2331     2403      +72     
==========================================
+ Hits         2312     2384      +72     
  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.

- use a temporary id column name that cannot clash with existing columns
- handle NA and near-duplicate quantile levels in quantile_to_interval()
- check observed for missing values once in bias_quantile()
- strengthen tests for ties, interpolated medians and error paths

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.

Speed up score() for quantile forecasts: reshape once, and compute WIS components and coverage from the quantile matrix

1 participant