Skip to content

fix: support prediction intervals with sparse horizons - #858

Open
Yanze (David) Wu (David-Wu1119) wants to merge 1 commit into
Nixtla:mainfrom
David-Wu1119:fix/sparse-horizons-prediction-intervals
Open

Yanze (David) Wu (David-Wu1119) wants to merge 1 commit into
Nixtla:mainfrom
David-Wu1119:fix/sparse-horizons-prediction-intervals

Conversation

@David-Wu1119

Copy link
Copy Markdown
Contributor

Description

With sparse horizons, predict, cross_validation and the frozen-model backtests of transfer only produce the trained steps, e.g. two rows per series for horizons=[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 to h, so every interval path fails:

from sklearn.linear_model import LinearRegression
from mlforecast import MLForecast
from mlforecast.utils import PredictionIntervals, generate_daily_series

df = generate_daily_series(3, min_length=100, max_length=120)
fcst = MLForecast(models=[LinearRegression()], freq="D", lags=[1, 7])
fcst.fit(df, horizons=[7, 14], prediction_intervals=PredictionIntervals(h=14))
fcst.predict(h=14, level=[90])
# ValueError: cannot reshape array of size 12 into shape (2,3,14)
  • predict(level=...) and cross_validation(..., level=...): cannot reshape array of size 12 into shape (2,3,14).
  • transfer(df, ScaleAlignment()) then predict(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 check h * n_series.

The fix adds MLForecast._n_steps(h), the number of steps per series up to h (the trained ones for sparse horizons, h otherwise), and passes it to the interval helpers and to the backtest row check. Recursive models and dense horizons keep h steps, 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] and max_horizon=14, and so are the intervals at those steps, for intervals from fit and after transfer with Recalibration or ScaleAlignment. ErrorScaling pools 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 on main with the errors above and pass with this change.
  • Local suite (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:

  • This PR title follows the Conventional Commits format (e.g. feat: ..., fix: ...) and has a clear description.
  • The tests pass (local scope above; CI matrix not run).
  • All linting tasks pass.
  • The notebooks are clean (no notebooks changed).

Found while auditing mlforecast's prediction intervals with Claude Code, which also wrote the fix and the tests.

🤖 Generated with Claude Code

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]>
Copilot AI balanced review requested due to automatic review settings November 30, 2026 16:28

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@github-actions github-actions Bot added the fix label Nov 30, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-30T16:30:55.498720Z ef0c5bb PR opened
ℹ️ About Codex in GitHub

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants