Repository navigation
1237: Speed up score() for quantile forecasts - #1256
Draft
nikosbosse wants to merge 4 commits into
Draft
nikosbosse wants to merge 4 commits into
nikosbosse wants to merge 4 commits into
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
- 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
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 #1237.
It speeds up
score()for quantile-based forecasts with three internal changes. No function signature, argument or return value changes, and the contract betweenscore()and the metrics (observed,predictedmatrix,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.observedandpredictedwith one sort if all forecasts have the same quantile levels; otherwise use the existing pathscore.forecast_quantile()dcast()quantile_to_interval_numeric()NAare still handled one by onebias_quantile()Design notes:
wis(), the three component metrics and the two coverages still each build the interval table. At 0.04s per call that is cheap enough that nothing needs to be shared between metrics.wis()proposed in the issue is not included. Its quantile-score identity only holds forweigh = TRUEand it would add a second code path; replacing the shared reshape helper gets nearly all of the gain.transform_forecasts()validates the forecast six times, via[.forecaston its internal:=updates #1236 / 1236: Validate forecasts only twice in transform_forecasts() #1238).Visible differences, all for input that
score()rejects or never produces, i.e. only when calling the metrics directly:bias_quantile()with anNAinobservedstill 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 bothInf. It now returns a bias, orNAif the median cannot be interpolated because it lies between a finite and an infinite prediction.0.3and0.1 * 3) used to fail inwis()with an unrelated error aboutupperandlower. They now fail with "Quantile levels must be unique after rounding to 10 digits".wis()with anNAquantile 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:score()output on the issue's example;score()output, including row order, onexample_quantile, a shuffled copy, a copy with mixed quantile levels (fallback path) and one with a missing row;wis(),bias_quantile()andinterval_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 withoutNA).Checklist
lintr::lint_package()to check for style issues introduced by my changes. Linted the six changed files, no lints.Adversarial review
An independent agent reviewed the PR and compared the branch with
mainon 325 targeted and 1,600 random cases; it found no difference in scores for input thatscore()accepts. Addressed:score()failed if a forecast-unit column was calledscoringutils_id; the temporary column name is now made unique (with a test).NAquantile level gave a cryptic error in the interval helper; fixed (with tests).NA-in-observedmessage depended on the row;observedis now checked once up front.Not applied:
🤖 Generated with Claude Code