Skip to content

BUG: Input checks for ETSModel and ETS get_prediction - #10937

Merged
bashtage merged 8 commits into
statsmodels:mainfrom
bashtage:wip-ets-input-checks
Nov 27, 2026
Merged

bashtage merged 8 commits into
statsmodels:mainfrom
bashtage:wip-ets-input-checks

Conversation

@bashtage

Copy link
Copy Markdown
Member

Five input checks of ETSModel and get_prediction, one commit each. The details are in each commit.

DEP: Deprecate unknown method values in ETS get_prediction

ETSResults.get_prediction(method=...) and PredictionResults document
method as "exact", "simulated" or None, but accepted any value and
treated everything other than "simulated" as "exact", silently:
method="foo", method=1 and also method="Simulated" (capital S)
returned the exact prediction intervals.

Changes:

  • A method other than "exact", "simulated" or None (checked with
    string_like, case-sensitively) issues a FutureWarning: 'method must
    be "exact", "simulated" or None, got 'foo'. Other values are treated as
    "exact". This is deprecated and will raise a ValueError after
    statsmodels 0.17 is released.' The value is still treated as "exact"
    and stored as given in method, so the results do not change. A
    ValueError right away would break code that ran, so this is a
    deprecation.
  • deprecated notes on method of get_prediction and
    PredictionResults.

Results do not change for any value of method.

Tests: test_get_prediction_unknown_method checks the warning (and that
it points at the caller) and that "foo", "Simulated", "EXACT" and 1 give
the exact intervals, and test_get_prediction_known_method_no_warning
that None, "exact" and "simulated" do not warn. The four unknown values
fail on main.

BUG: Accept a single column as endog in ETSModel

ETSModel documents endog as array_like, but an (nobs, 1) array or a
one-column DataFrame raised "ValueError: endog must be 1-dimensional".
Other statsmodels models and the data handling of the base model accept
a single column as the series (the model's data.endog and names are
already those of the series).

Changes:

  • ETSModel.prepare_data uses the column of a two-dimensional endog
    with one column. Input with more than one column, or a (1, nobs) row,
    raises the same ValueError as before.
  • The conversion of integer data to float uses that 1-D array. It used
    the original data, which would have been 2-D again.
  • A versionchanged note on endog.

Results of 1-D input do not change.

Tests: test_endog_single_column compares the start parameters, the
log-likelihood, the fitted values and the forecasts of a one-column
DataFrame, an (nobs, 1) float array and an (nobs, 1) integer array with
those of the 1-D data, and the forecast index and the prediction summary
frame for the DataFrame; test_endog_not_single_column checks that two
columns and a row still raise. The three single-column cases fail on
main.

BUG: Raise a ValueError when ETSModel cannot infer seasonal_periods

A seasonal ETSModel without seasonal_periods infers it from the
frequency of the date index of endog. Without such a frequency (numpy
data, a RangeIndex, or dates without a frequency) it raised a bare
AssertionError from freq_to_period(None):

ETSModel(y, seasonal="add")
# AssertionError

Changes:

  • This raises "ValueError: seasonal_periods must be given for a seasonal
    model if endog has no date index with a frequency."

Only input that raised is affected; models with seasonal_periods or
with a date frequency are unchanged.

Tests: test_seasonal_periods_required checks the error for numpy data,
a Series with a RangeIndex and a Series with irregular dates, for
additive and multiplicative seasonality. All six cases fail on main
(AssertionError).

DEP: Check the length of a known initial_seasonal in ETSModel

With initialization_method="known", initial_seasonal is documented as
"an array of length seasonal_periods", but its length was not checked:

  • a scalar or a single value ([5.0], a (1, 1) array) was broadcast
    silently to all seasonal states;
  • other lengths, an (m, 1) column and a 0-d array were accepted by the
    constructor and failed later, when the model was smoothed or fit, with
    a NumPy error ("could not broadcast input array from shape (2,) into
    shape (4,)", or an IndexError for a 0-d array);
  • a (1, m) row was used with its seasonal states in reverse order,
    silently, as the reversal of the internal parameter vector only
    reversed its first axis.

Changes:

  • A single value issues a FutureWarning: "initial_seasonal has a single
    value, which is used for all 4 initial seasonal states. This is
    deprecated and will raise a ValueError after statsmodels 0.17 is
    released. Pass an array of length seasonal_periods instead." The value
    is still broadcast, so the results do not change. It ran, so this is a
    deprecation rather than an error.
  • Any other shape than (seasonal_periods,) raises "ValueError:
    initial_seasonal must have length seasonal_periods (4), got an array
    of shape (2,)." in the constructor (and in
    set_initialization_method). This includes the (1, m) row, which gave
    a silently wrong model.
  • deprecated notes on initial_seasonal of ETSModel and
    set_initialization_method.

Results of an initial_seasonal of length seasonal_periods (a list, a
tuple, an ndarray or a Series) and of a single value do not change.

Tests: test_initial_seasonal_single_value checks the warning and
compares a list, a float, a np.float64 and a (1, 1) array with the
explicitly repeated value; test_initial_seasonal_wrong_length checks
the error for lengths 2 and 5, a column, a row and a 0-d array; and
test_initial_seasonal_forms that the valid forms do not warn and give
the documented order (the first prediction uses the first value). The
nine warning and error cases fail on main.

BUG: Use a single row or column as the known initial_seasonal of ETSModel

A known initial_seasonal given as a (1, seasonal_periods) row ran, but its seasonal states were used in reverse order, and an (seasonal_periods, 1) column raised a numpy error when the model was used. The previous commit made both raise a ValueError. A single row or column is now used as the 1-d array, as a single column endog is, so the row gives the results of the same values as a list: they change from the reversed order. Other shapes still raise.

The test compares a row and a column with the list of the same values.

  • 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 contract audit, writing the fix and its tests, and checking them (tests failing on main, 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
bashtage force-pushed the wip-ets-input-checks branch from ded07f4 to a0f1769 Compare November 21, 2026 21:40
@bashtage
bashtage force-pushed the wip-ets-input-checks branch 7 times, most recently from 18449e9 to b416e5f Compare November 25, 2026 18:38
`ETSResults.get_prediction(method=...)` and `PredictionResults` document
`method` as "exact", "simulated" or None, but accepted any value and
treated everything other than "simulated" as "exact", silently:
`method="foo"`, `method=1` and also `method="Simulated"` (capital S)
returned the exact prediction intervals.

Changes:

- A `method` other than "exact", "simulated" or None (checked with
  `string_like`, case-sensitively) issues a FutureWarning: 'method must
  be "exact", "simulated" or None, got 'foo'. Other values are treated as
  "exact". This is deprecated and will raise a ValueError after
  statsmodels 0.17 is released.' The value is still treated as "exact"
  and stored as given in `method`, so the results do not change. A
  ValueError right away would break code that ran, so this is a
  deprecation.
- `deprecated` notes on `method` of `get_prediction` and
  `PredictionResults`.

Results do not change for any value of `method`.

Tests: `test_get_prediction_unknown_method` checks the warning (and that
it points at the caller) and that "foo", "Simulated", "EXACT" and 1 give
the exact intervals, and `test_get_prediction_known_method_no_warning`
that None, "exact" and "simulated" do not warn. The four unknown values
fail on main.
`ETSModel` documents `endog` as array_like, but an (nobs, 1) array or a
one-column DataFrame raised "ValueError: endog must be 1-dimensional".
Other statsmodels models and the data handling of the base model accept
a single column as the series (the model's `data.endog` and names are
already those of the series).

Changes:

- `ETSModel.prepare_data` uses the column of a two-dimensional `endog`
  with one column. Input with more than one column, or a (1, nobs) row,
  raises the same ValueError as before.
- The conversion of integer data to float uses that 1-D array. It used
  the original data, which would have been 2-D again.
- A `versionchanged` note on `endog`.

Results of 1-D input do not change.

Tests: `test_endog_single_column` compares the start parameters, the
log-likelihood, the fitted values and the forecasts of a one-column
DataFrame, an (nobs, 1) float array and an (nobs, 1) integer array with
those of the 1-D data, and the forecast index and the prediction summary
frame for the DataFrame; `test_endog_not_single_column` checks that two
columns and a row still raise. The three single-column cases fail on
main.
A seasonal `ETSModel` without `seasonal_periods` infers it from the
frequency of the date index of `endog`. Without such a frequency (numpy
data, a RangeIndex, or dates without a frequency) it raised a bare
`AssertionError` from `freq_to_period(None)`:

    ETSModel(y, seasonal="add")
    # AssertionError

Changes:

- This raises "ValueError: seasonal_periods must be given for a seasonal
  model if endog has no date index with a frequency."

Only input that raised is affected; models with `seasonal_periods` or
with a date frequency are unchanged.

Tests: `test_seasonal_periods_required` checks the error for numpy data,
a Series with a RangeIndex and a Series with irregular dates, for
additive and multiplicative seasonality. All six cases fail on main
(AssertionError).
With `initialization_method="known"`, `initial_seasonal` is documented as
"an array of length seasonal_periods", but its length was not checked:

- a scalar or a single value (`[5.0]`, a (1, 1) array) was broadcast
  silently to all seasonal states;
- other lengths, an (m, 1) column and a 0-d array were accepted by the
  constructor and failed later, when the model was smoothed or fit, with
  a NumPy error ("could not broadcast input array from shape (2,) into
  shape (4,)", or an IndexError for a 0-d array);
- a (1, m) row was used with its seasonal states in reverse order,
  silently, as the reversal of the internal parameter vector only
  reversed its first axis.

Changes:

- A single value issues a FutureWarning: "initial_seasonal has a single
  value, which is used for all 4 initial seasonal states. This is
  deprecated and will raise a ValueError after statsmodels 0.17 is
  released. Pass an array of length seasonal_periods instead." The value
  is still broadcast, so the results do not change. It ran, so this is a
  deprecation rather than an error.
- Any other shape than (seasonal_periods,) raises "ValueError:
  initial_seasonal must have length seasonal_periods (4), got an array
  of shape (2,)." in the constructor (and in
  `set_initialization_method`). This includes the (1, m) row, which gave
  a silently wrong model.
- `deprecated` notes on `initial_seasonal` of `ETSModel` and
  `set_initialization_method`.

Results of an `initial_seasonal` of length seasonal_periods (a list, a
tuple, an ndarray or a Series) and of a single value do not change.

Tests: `test_initial_seasonal_single_value` checks the warning and
compares a list, a float, a np.float64 and a (1, 1) array with the
explicitly repeated value; `test_initial_seasonal_wrong_length` checks
the error for lengths 2 and 5, a column, a row and a 0-d array; and
`test_initial_seasonal_forms` that the valid forms do not warn and give
the documented order (the first prediction uses the first value). The
nine warning and error cases fail on main.
…odel

A known initial_seasonal given as a (1, seasonal_periods) row ran, but its seasonal states were used in reverse order, and an (seasonal_periods, 1) column raised a numpy error when the model was used. The previous commit made both raise a ValueError. A single row or column is now used as the 1-d array, as a single column endog is, so the row gives the results of the same values as a list: they change from the reversed order. Other shapes still raise.

The test compares a row and a column with the list of the same values.
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.
@bashtage
bashtage force-pushed the wip-ets-input-checks branch from b416e5f to 71f888f Compare November 27, 2026 00:30
@bashtage
bashtage marked this pull request as ready for review November 27, 2026 21:36
@bashtage
bashtage merged commit ecf35b5 into statsmodels:main Nov 27, 2026
10 checks passed
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