Repository navigation
fix: support prediction intervals with sparse horizons - #858
Open
Yanze (David) Wu (David-Wu1119) wants to merge 1 commit into
Open
Yanze (David) Wu (David-Wu1119) wants to merge 1 commit into
Yanze (David) Wu (David-Wu1119) wants to merge 1 commit into
Conversation
With horizons=[7, 14], predict, cross_validation and the transfer backtests only produce the trained steps, two rows per series. The conformity scores computed at fit have the same layout, but the interval helpers reshaped scores and forecasts assuming one row per step up to h, so predict(level=...) raised "cannot reshape array" and the Recalibration and ErrorScaling backtests raised that the predictions did not align with the validation windows. Pass the number of steps each series actually has to the interval helpers and to the backtest row check. Dense horizons and recursive models keep h steps, so their intervals don't change. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Atishay Kasliwal (atishay-kasliwal)
left a comment
There was a problem hiding this comment.
With horizons=[7, 14] and PredictionIntervals(h=14), predict(h=14, level=[80, 95]) now works, and the intervals for steps 7 and 14 are identical to a max_horizon=14 fit. Master raised ValueError: cannot reshape array of size 12 into shape (2,3,14). Recursive forecasts with intervals are unchanged.
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.
Description
With sparse
horizons,predict,cross_validationand the frozen-model backtests oftransferonly produce the trained steps, e.g. two rows per series forhorizons=[7, 14]. The conformity scores computed at fit have the same layout. The interval helpers, however, reshape scores and forecasts assuming one row per step up toh, so every interval path fails:predict(level=...)andcross_validation(..., level=...):cannot reshape array of size 12 into shape (2,3,14).transfer(df, ScaleAlignment())thenpredict(level=...):cannot reshape array of size 12 into shape (14).transfer(df, Recalibration(...))/ErrorScaling():Frozen-model backtest predictions did not align with the validation windows, from the backtest row checkh * n_series.The fix adds
MLForecast._n_steps(h), the number of steps per series up toh(the trained ones for sparse horizons,hotherwise), and passes it to the interval helpers and to the backtest row check. Recursive models and dense horizons keephsteps, so their intervals don't change.With the change, the models and conformity scores for steps 7 and 14 are the same for
horizons=[7, 14]andmax_horizon=14, and so are the intervals at those steps, for intervals from fit and aftertransferwithRecalibrationorScaleAlignment.ErrorScalingpools all steps into one ratio, so its sparse and dense intervals legitimately differ.Tests:
test_horizons_prediction_intervals_match_max_horizon(fit intervals,Recalibration(h=14),ScaleAlignment()): all three cases fail onmainwith the errors above and pass with this change.pytest tests --ignore=tests/distributed_ray --ignore=tests/distributed_spark --ignore=tests/test_m4.py --ignore=tests/test_distributed_forecast.py -n 4, Python 3.11, macOS): 2300 passed, 14 skipped.pre-commit run --all-files(ruff check, ruff format, mypy) passes.Not run: the distributed tests (dask, spark, ray extras not installed),
tests/test_m4.py, and the docs notebooks.Checklist:
feat: ...,fix: ...) and has a clear description.Found while auditing mlforecast's prediction intervals with Claude Code, which also wrote the fix and the tests.
🤖 Generated with Claude Code