Skip to content

BUG: Pass extend_kwargs on in VARMAXResults.get_prediction - #11167

Merged
bashtage merged 2 commits into
statsmodels:mainfrom
bashtage:wip-varmax-prediction-extend-kwargs
Nov 24, 2026
Merged

bashtage merged 2 commits into
statsmodels:mainfrom
bashtage:wip-varmax-prediction-extend-kwargs

Conversation

@bashtage

Copy link
Copy Markdown
Member

No other open PR changes VARMAXResults.get_prediction. A key given in extend_kwargs wins over the default trend_offset, as in the shared code (_get_extension_time_varying_matrices uses setdefault) and in VARMAXResults.extend. That shared code writes the default trend_offset into the caller's dict (on main also for SARIMAX and the other models; a separate fix). varmax.py merges cleanly with every open PR that changes it (#11083, #11085-#11089, #11103, #11121, #11133, #11157, #11162); test_varmax.py conflicts with #11083, #11085-#11089, #11103, #11121, #11133 and #11157 only where both append tests at the end: keep both blocks.

VARMAXResults.get_prediction built its own extend_kwargs (the trend
offset of the model of the forecast periods) and passed it to
MLEResults.get_prediction together with **kwargs, so any call with
extend_kwargs, documented in the shared docstring, raised "TypeError:
MLEResults.get_prediction() got multiple values for keyword argument
'extend_kwargs'", also extend_kwargs={}. get_forecast, forecast and
predict call get_prediction and raised as well.

Changes

  • get_prediction takes extend_kwargs and passes it on.
  • The trend offset that VARMAX set is dropped: the shared code that builds
    the extended model (_get_extension_time_varying_matrices) sets the same
    default, trend_offset + nobs, with setdefault. A key given in
    extend_kwargs therefore wins over the default, as for every other state
    space model and as in VARMAXResults.extend.

Not changed

  • Results without extend_kwargs: the predictions, their variances, the
    dynamic predictions and the forecasts of 60 VARMAX specifications (each
    trend with 0, 1 and 2 exog, two orders, two trend offsets) are identical
    to main.
  • simulate (of the results and of the model) passed extend_kwargs on
    already; with zero shocks it equals the forecast for both an empty
    extend_kwargs and a trend_offset.

Tests

  • VARMAX(1, 0) with a linear trend, without and with exog, at fixed
    parameters: get_forecast, forecast, predict and get_prediction with an
    empty extend_kwargs and with a trend_offset equal the forecasts computed
    by hand from the VAR recursion (the trend values after the first forecast
    come from the trend_offset). Every case fails on main.

  • tests added / passed.

  • code/documentation is well formatted.

  • properly formatted commit message. See
    NumPy's guide.

AI Disclosure

Contributions must comply with the statsmodels AI Policy.

Tick exactly one of the following. If AI tools were used, replace the <...>
placeholders in the second option with the tool name(s) and how the tool's output was
used.

  • No AI tools were used to develop this pull request.
  • AI tools were used.
    Tool(s): Claude Code (Claude Opus 5.5).
    Used for: finding the bug in a coverage audit of the time series code, writing the fix and its tests, and checking them (tests failing on the base, mutation check).
    I have personally read, understood, and can explain every line of this diff, and
    have verified the statistical/numerical correctness of the change.

🤖 Generated with Claude Code

@bashtage

Copy link
Copy Markdown
Member Author

Merge order. These open PRs build on this one (their branches contain its commits), so please merge this PR first:

Once this PR is merged, their diffs shrink to their own changes. Each of them also says "Stacked on #..." at the top of its description.

Merge-order note generated with Claude Code.

VARMAXResults.get_prediction built its own extend_kwargs (the trend
offset of the model of the forecast periods) and passed it to
MLEResults.get_prediction together with **kwargs, so any call with
extend_kwargs, documented in the shared docstring, raised "TypeError:
MLEResults.get_prediction() got multiple values for keyword argument
'extend_kwargs'", also extend_kwargs={}. get_forecast, forecast and
predict call get_prediction and raised as well.

Changes
- get_prediction takes extend_kwargs and passes it on.
- The trend offset that VARMAX set is dropped: the shared code that builds
  the extended model (_get_extension_time_varying_matrices) sets the same
  default, trend_offset + nobs, with setdefault. A key given in
  extend_kwargs therefore wins over the default, as for every other state
  space model and as in VARMAXResults.extend.

Not changed
- Results without extend_kwargs: the predictions, their variances, the
  dynamic predictions and the forecasts of 60 VARMAX specifications (each
  trend with 0, 1 and 2 exog, two orders, two trend offsets) are identical
  to main.
- simulate (of the results and of the model) passed extend_kwargs on
  already; with zero shocks it equals the forecast for both an empty
  extend_kwargs and a trend_offset.

Tests
- VARMAX(1, 0) with a linear trend, without and with exog, at fixed
  parameters: get_forecast, forecast, predict and get_prediction with an
  empty extend_kwargs and with a trend_offset equal the forecasts computed
  by hand from the VAR recursion (the trend values after the first forecast
  come from the trend_offset). Every case fails on main.
@bashtage
bashtage force-pushed the wip-varmax-prediction-extend-kwargs branch from 350f5df to d76f506 Compare November 24, 2026 16:17
bashtage added a commit to bashtage/statsmodels that referenced this pull request Nov 24, 2026
The out-of-sample extension of state space models set its defaults with
setdefault on the extend_kwargs dict that the caller passed: the trend
offset of the extended model (MLEModel, used by VARMAX since statsmodels#11167, and
SARIMAX) and validate_specification=False (SARIMAX). Reusing that dict
for another model, or for the same model after append/extend, silently
used the first model's trend offset. For example, with ek = {},
SARIMAX(y, exog=x[:40], order=(1, 0, 0), trend="t")
.smooth([.1, .5, .3, 1]).get_forecast(3, exog=x[40:], extend_kwargs=ek)
and then the 30-observation model's forecast(3, exog=x[30:33],
extend_kwargs=ek) gave [3.42, 5.46, 6.24] instead of [3.42, 4.46, 4.94].

Stacked on statsmodels#11167, which makes VARMAX pass extend_kwargs on to the
shared MLEModel code (before, VARMAX raised for any extend_kwargs).

Changes
- MLEModel._get_extension_time_varying_matrices and
  SARIMAX._get_extension_time_varying_matrices copy extend_kwargs before
  setting their defaults.
- A versionchanged note under extend_kwargs in MLEResults.get_prediction
  (shared by the SARIMAX and VARMAX docstrings).

Tests (test_forecasting.py)
- SARIMAX with a time trend and exog, and VARMAX with "ct" and exog: the
  dict passed to get_forecast of a 40-observation model stays empty, and
  the forecasts of a 30-observation model with that dict equal its
  forecasts without extend_kwargs. The SARIMAX case fails with the old
  sarimax.py, the VARMAX case with the old mlemodel.py.
@bashtage
bashtage marked this pull request as ready for review November 24, 2026 18:46
@bashtage
bashtage merged commit 299f72b into statsmodels:main Nov 24, 2026
10 of 11 checks passed
@bashtage
bashtage deleted the wip-varmax-prediction-extend-kwargs branch November 24, 2026 23:20
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.

1 participant