Skip to content

BUG: Predict only the dependent variable in constrained RecursiveLS - #11147

Merged
bashtage merged 9 commits into
statsmodels:mainfrom
bashtage:wip-recursivels-constrained-prediction
Nov 24, 2026
Merged

bashtage merged 9 commits into
statsmodels:mainfrom
bashtage:wip-recursivels-constrained-prediction

Conversation

@bashtage

Copy link
Copy Markdown
Member

Stacked on #11104 (itself stacked on #10940), merge those first (their commits are the first ones here; this PR adds two commits, the fix and a DOC commit): #11104 builds the get_prediction docstring that both commits edit. Other open PRs append tests at the end of the same test file: keep both appended blocks.

BUG: Accept dynamic date keys that cover several dates

A dynamic date key that matches several dates of a DatetimeIndex, such
as "2001-04" for daily data, gives a slice of locations, which raised
"TypeError: unsupported operand type(s) for -=: 'slice' and 'int'" in
AutoReg.predict (and so ARDL.predict), MLEResults.get_prediction
(SARIMAX, ARIMA, UnobservedComponents, ...), STLForecastResults,
ETSResults and RecursiveLSResults.get_prediction. Dynamic prediction
now begins at the first date of the key, as MLEResults.simulate already
does for a multi-date anchor.

TST: Check a single-date dynamic key of RecursiveLS

Covers the branch of the new slice check where a date string matches a
single date, so that its location is used unchanged.

TST: Pass param_index in the new ETS tests

Since #10975 merged, ETSModel.fit, fit_constrained and smooth warn when
param_index is not given (the default changes from "integer" to "names"
after statsmodels 0.17). The tests this PR adds called them without it,
so they leaked that FutureWarning. Pass param_index="integer", as the
existing tests do; results are unchanged.

BUG: Begin RecursiveLS dynamic prediction at a dynamic date

RecursiveLSResults.get_prediction (and so predict) used the location of a
dynamic date string as an offset relative to start. With start=10,
dynamic="2001-04-01" began dynamic prediction on 2001-04-11, silently. The
shared docstring says that a date is not interpreted as an offset, and
MLEResults.get_prediction converts it to an offset by subtracting start. A
dynamic Timestamp or datetime raised "TypeError: Addition/subtraction of
integers and integer-arrays with Timestamp is no longer supported".

Changes

  • A dynamic date string, Timestamp or datetime is converted to its
    location and then to an offset relative to start, as in
    MLEResults.get_prediction, so dynamic prediction begins at that date.
    Results change when start is not the first observation.
  • The docstring notes the change in a versionchanged directive.

Not changed

  • An integer or bool dynamic, which is an offset relative to start.
  • A dynamic date when start is the first observation (the default).
  • The check that raises NotImplementedError for dynamic prediction in a
    model with constraints. As for an integer offset of 0, a dynamic date
    equal to start is not caught by it.

Tests

  • With start=10 or start="2000-01-11", a dynamic date string, month string,
    Timestamp or datetime gives the predictions from the coefficients
    estimated through the previous period up to the date, and from those
    estimated through the period before the date after it, and the same
    predictions and variances as the integer offset. All eight cases fail on
    main.

BUG: Predict only the dependent variable in constrained RecursiveLS

RecursiveLS imposes linear constraints through extra endog variables, one
per constraint, whose design rows are the constraint matrix. The results
remove them from the forecasts and the forecast errors, but
RecursiveLSResults.get_prediction (and so predict) used the prediction of
the state space model with all of them: with ndarray data, predicted_mean
held the predictions of the constraint variables as a second column (and
var_pred_mean the full covariance matrices), and with pandas data
get_prediction and predict raised "TypeError: Index(...) must be called
with a collection of some kind".

The prediction results are restricted to the first endog variable before
the PredictionResults are built: the arrays they cache (endog, design,
obs_intercept, obs_cov, forecasts, forecasts_error and
forecasts_error_cov) keep only its rows, so the filtered and smoothed
forecasts and the signals, computed from these, are those of the
dependent variable too.

Stacked on the PR that begins dynamic prediction at a dynamic date
(wip-recursivels-dynamic-date): it builds the get_prediction docstring
that the versionchanged note goes into.

Changes

  • With constraints, get_prediction and predict return the predictions of
    the dependent variable only, as a 1-d array or a Series with the index of
    the data, for every information_set, also with signal_only.
  • A versionchanged note under Returns in the get_prediction docstring.

Not changed

  • Models without constraints.
  • Out-of-sample and dynamic prediction with constraints, which still raise
    NotImplementedError.

Tests

  • With the constraint x1 = x2, the model is the regression on const and
    x1 + x2: for predicted, filtered and smoothed information sets, with and
    without signal_only, the predicted mean, its variance and the confidence
    intervals equal those of that regression after the diffuse periods, and
    so do the arrays of the underlying prediction results; the design is the
    regressors. The one-step predictions equal z_t' b, with b from OLS of y
    on z = (1, x1 + x2) over the observations before t.
  • With pandas data, the predictions equal those of the ndarray data and
    have the index of the data, also in summary_frame and predict.
  • The new tests fail on the base, and every changed line is guarded (11
    mutants, all killed). They pass with pandas 3.0.5, 2.3.3 and 1.5.3.

DOC: Remove extend_model and extend_kwargs from RecursiveLS get_prediction

RecursiveLSResults.get_prediction takes its docstring from
MLEResults.get_prediction, which documents extend_model and
extend_kwargs. RecursiveLSResults.get_prediction does not take them:
out-of-sample predictions use the design matrix built from exog (or a
design keyword), not an extended model. Given by keyword, they land in
**kwargs and have no effect.

Changes

  • The two parameters are removed from the docstring.

Tests

  • The parameters in the docstring are those of the signature, in order
    (fails before this commit).

  • 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 statsmodels.tsa, 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

A `dynamic` date key that matches several dates of a DatetimeIndex, such
as "2001-04" for daily data, gives a slice of locations, which raised
"TypeError: unsupported operand type(s) for -=: 'slice' and 'int'" in
`AutoReg.predict` (and so `ARDL.predict`), `MLEResults.get_prediction`
(SARIMAX, ARIMA, UnobservedComponents, ...), `STLForecastResults`,
`ETSResults` and `RecursiveLSResults.get_prediction`. Dynamic prediction
now begins at the first date of the key, as `MLEResults.simulate` already
does for a multi-date `anchor`.
Covers the branch of the new slice check where a date string matches a
single date, so that its location is used unchanged.
Since statsmodels#10975 merged, ETSModel.fit, fit_constrained and smooth warn when
param_index is not given (the default changes from "integer" to "names"
after statsmodels 0.17). The tests this PR adds called them without it,
so they leaked that FutureWarning. Pass param_index="integer", as the
existing tests do; results are unchanged.
RecursiveLSResults.get_prediction (and so predict) used the location of a
dynamic date string as an offset relative to start. With start=10,
dynamic="2001-04-01" began dynamic prediction on 2001-04-11, silently. The
shared docstring says that a date is not interpreted as an offset, and
MLEResults.get_prediction converts it to an offset by subtracting start. A
dynamic Timestamp or datetime raised "TypeError: Addition/subtraction of
integers and integer-arrays with Timestamp is no longer supported".

Changes
- A dynamic date string, Timestamp or datetime is converted to its
  location and then to an offset relative to start, as in
  MLEResults.get_prediction, so dynamic prediction begins at that date.
  Results change when start is not the first observation.
- The docstring notes the change in a versionchanged directive.

Not changed
- An integer or bool dynamic, which is an offset relative to start.
- A dynamic date when start is the first observation (the default).
- The check that raises NotImplementedError for dynamic prediction in a
  model with constraints. As for an integer offset of 0, a dynamic date
  equal to start is not caught by it.

Tests
- With start=10 or start="2000-01-11", a dynamic date string, month string,
  Timestamp or datetime gives the predictions from the coefficients
  estimated through the previous period up to the date, and from those
  estimated through the period before the date after it, and the same
  predictions and variances as the integer offset. All eight cases fail on
  main.
RecursiveLS imposes linear constraints through extra endog variables, one
per constraint, whose design rows are the constraint matrix. The results
remove them from the forecasts and the forecast errors, but
RecursiveLSResults.get_prediction (and so predict) used the prediction of
the state space model with all of them: with ndarray data, predicted_mean
held the predictions of the constraint variables as a second column (and
var_pred_mean the full covariance matrices), and with pandas data
get_prediction and predict raised "TypeError: Index(...) must be called
with a collection of some kind".

The prediction results are restricted to the first endog variable before
the PredictionResults are built: the arrays they cache (endog, design,
obs_intercept, obs_cov, forecasts, forecasts_error and
forecasts_error_cov) keep only its rows, so the filtered and smoothed
forecasts and the signals, computed from these, are those of the
dependent variable too.

Stacked on the PR that begins dynamic prediction at a dynamic date
(wip-recursivels-dynamic-date): it builds the get_prediction docstring
that the versionchanged note goes into.

Changes
- With constraints, get_prediction and predict return the predictions of
  the dependent variable only, as a 1-d array or a Series with the index of
  the data, for every information_set, also with signal_only.
- A versionchanged note under Returns in the get_prediction docstring.

Not changed
- Models without constraints.
- Out-of-sample and dynamic prediction with constraints, which still raise
  NotImplementedError.

Tests
- With the constraint x1 = x2, the model is the regression on const and
  x1 + x2: for predicted, filtered and smoothed information sets, with and
  without signal_only, the predicted mean, its variance and the confidence
  intervals equal those of that regression after the diffuse periods, and
  so do the arrays of the underlying prediction results; the design is the
  regressors. The one-step predictions equal z_t' b, with b from OLS of y
  on z = (1, x1 + x2) over the observations before t.
- With pandas data, the predictions equal those of the ndarray data and
  have the index of the data, also in summary_frame and predict.
- The new tests fail on the base, and every changed line is guarded (11
  mutants, all killed). They pass with pandas 3.0.5, 2.3.3 and 1.5.3.
…ction

RecursiveLSResults.get_prediction takes its docstring from
MLEResults.get_prediction, which documents extend_model and
extend_kwargs. RecursiveLSResults.get_prediction does not take them:
out-of-sample predictions use the design matrix built from exog (or a
design keyword), not an extended model. Given by keyword, they land in
**kwargs and have no effect.

Changes
- The two parameters are removed from the docstring.

Tests
- The parameters in the docstring are those of the signature, in order
  (fails before this commit).
@bashtage
bashtage force-pushed the wip-recursivels-constrained-prediction branch from 5a40099 to 6e09248 Compare November 24, 2026 16:17
@bashtage
bashtage marked this pull request as ready for review November 24, 2026 18:31
@bashtage
bashtage merged commit 9c947a6 into statsmodels:main Nov 24, 2026
10 of 11 checks passed
@bashtage
bashtage deleted the wip-recursivels-constrained-prediction branch November 24, 2026 18:32
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