镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

fix(ts): prevent overlapping AR and MA lags in SARIMAX configurations - #1661

Open
Laasya priya Bhadri (laasyapriya2007) wants to merge 2 commits into
microsoft:mainfrom
laasyapriya2007:fix-sarimax-overlapping-lags
Open

Laasya priya Bhadri (laasyapriya2007) wants to merge 2 commits into
microsoft:mainfrom
laasyapriya2007:fix-sarimax-overlapping-lags

Conversation

@laasyapriya2007

Copy link
Copy Markdown

Fixes #845

Problem

AutoML's sarimax learner occasionally suggested configurations where a lag appears in both the seasonal and non-seasonal autoregressive or moving-average components. For example, the config from the issue ({"p": 4, "d": 0, "q": 5, "P": 1, "D": 3, "Q": 2, "s": 4}, e.g. via user-supplied starting_points) makes statsmodels raise:

ValueError: Invalid model: autoregressive lag(s) {4} are in both the seasonal
and non-seasonal autoregressive components.

Non-seasonal AR lags are {1..p} and seasonal AR lags are {s, 2s, ...}, so whenever p >= s (with P > 0) lag s is duplicated. The same overlap exists on the MA side when q >= s with Q > 0. The overlap can be injected by user-supplied starting_points or user-provided custom search spaces that bypass the built-in search-space bounds.

Solution

1. Config clamping in SARIMAX.config2params (flaml/automl/time_series/ts_model.py): the override calls the parent implementation, then, when s > 0, clamps p to max(0, s - 1) if P > 0 and p >= s, and clamps q to max(0, s - 1) if Q > 0 and q >= s. This keeps every lag strictly below the first seasonal lag so no lag can be shared between the seasonal and non-seasonal polynomials, while leaving non-overlapping configs untouched.

2. Defensive handling in SARIMAX.fit(): model construction and fitting are wrapped in a targeted try/except ValueError. Only the statsmodels overlapping-lag error (message containing "are in both the seasonal and non-seasonal") triggers the fallback; any other ValueError is unrelated and propagates unchanged (nothing is silently hidden). On the fallback path a meaningful warning is logged describing the original order, the seasonal order and the error, self.params is updated to the actually-fitted (clamped) order so reported params are not misleading, and the model is refit once with the safe order.

Tests

Added test_sarimax_overlapping_lags_validation() in test/automl/test_forecast.py:

  • the exact overlapping config from the issue (p=4, q=5, P=1, Q=2, s=4 — both AR and MA overlap) is clamped to p=3, q=3 and fits without the overlapping-lag ValueError, producing a usable 6-step forecast;
  • AR-only overlap (p>=s, P>0, Q=0) clamps p only; MA-only overlap (q>=s, Q>0, P=0) clamps q only — both fit successfully;
  • a non-overlapping config passes through unchanged.

Actual results with python -m pytest test/automl/test_forecast.py -k "sarimax" -q:

1 passed, 35 deselected, 3 warnings in 18.83s

(the warnings are pre-existing statsmodels ValueWarning/ConvergenceWarning notices on synthetic data)

Additionally verified manually:

  • the AutoML scenario from the issue (estimator_list=["sarimax"] with the invalid starting_points) now completes: fit succeeded, best_estimator = sarimax;
  • the fit() fallback path handles invalid params injected after init and an unrelated ValueError still propagates;
  • the exog (regressors) code path fits and predicts correctly after the refactor.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The fallback reduces both AR and MA orders when only one seasonal component overlaps, incorrectly discarding valid terms from the non-overlapping component.

1 open issue
What changed in this PR

This PR addresses #845 by preventing overlapping seasonal and non-seasonal lags from interrupting FLAML’s SARIMAX forecasting.

Changes:

  • Clamp overlapping AR and MA orders during configuration conversion.
  • Add a targeted retry for overlapping-lag errors, with warning logging and parameter updates.
  • Add regression tests for combined overlap, single-component overlap, and unchanged valid configurations.
File Description
test/​automl/​test_forecast.py Tests lag clamping and successful fitting and forecasting.
flaml/​automl/​time_series/​ts_model.py Adds configuration clamping and defensive fit recovery.

Review effort: Balanced

Comment thread flaml/automl/time_series/ts_model.py Resolved

@thinkall Li Jiang (thinkall) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Changes requested at 6d43bf0a128bbf6f755d3b538014832ce23f6c24: two independently reproduced P2 correctness issues.

  1. Preserve valid sparse lag specifications. SARIMAX(task="ts_forecast", p=[1,3], d=0, q=0, P=1, D=0, Q=0, s=12) now raises TypeError: '>=' not supported between instances of 'list' and 'int' in config2params, although lags 1 and 3 do not overlap seasonal lag 12. The corresponding MA configuration (p=0, q=[1,3], P=0, Q=1, s=12) fails likewise. Both configurations actually fit and produce six finite predictions on exact main 46bce0ce989aae0271e68e5daac5b8a9b0522b87 in both native Windows Python 3.10 and 3.13 stacks. The new head fails during construction, before fitting. Handle scalar order counts and iterable lag specifications separately and add non-overlapping sparse-order regression coverage.

  2. Preserve the other valid order in the fallback. I independently confirmed Copilot's existing finding, without duplicating or resolving that thread. With post-init injected (p,d,q)=(4,0,6) and seasonal (1,0,0,4), only AR overlaps, but the actual fitted backend order becomes (3,0,3) instead of retaining valid MA order 6. The MA-only analogue (6,0,4) / seasonal (0,0,1,4) also fits (3,0,3) instead of (6,0,3). These are actual statsmodels construction/retry/fit results on both stacks, not simulated backend failures. The injection explicitly exercises the advertised defensive path; this is not a claim that normal built-in search necessarily reaches that path.

Validation: the intended scalar-overlap, targeted retry/logging, unrelated-error propagation, direct native fit/predict, repository regression and public AutoML control suite passes 16 cases per stack (32 total). The same test definitions on exact main give 7 expected feature-counterfactual failures and 9 passing controls per stack. The public AutoML starting-point case also passes on main, so it is a passing control, not proof that the invalid supplied trial was necessarily evaluated. The dedicated sparse-order probes add four current failures/four native main fit-predict controls; the fallback probes add four wrong-order reproductions/four prior unhandled-overlap controls. Original outputs and qualifications are retained; no contributor source was changed.

The planned Medium independent review-code execution remains blocked at its authenticated CLI prerequisite: no complementary reviewer, six-lens result or clean skill signoff is claimed. Driver-confirmed issues and current-head Balanced Copilot feedback are distinct evidence. No PR approval is submitted.

Posted by thinkall-agent-auto-reviewer

Comment thread flaml/automl/time_series/ts_model.py Outdated
# non-seasonal orders below the first seasonal lag to keep configs valid.
s = params.get("s", 0)
if s > 0:
if params.get("P", 0) > 0 and params.get("p", 0) >= s:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[P2] Keep valid sparse AR/MA lag lists working

These scalar comparisons reject existing valid statsmodels lag-list configs. For example, p=[1,3], q=0, P=1, D=0, Q=0, s=12, d=0 has no overlap, but construction now raises TypeError at p >= s; the MA analogue fails at q >= s. Both fit and produce six finite predictions on exact main in native Python 3.10/3.13 controls. Preserve iterable lag specifications rather than comparing them directly to the scalar seasonal period, with regression tests.

Posted by thinkall-agent-auto-reviewer

@laasyapriya2007

Copy link
Copy Markdown
Author

Thanks @thinkkll and Copilot for the reviews — both findings addressed in 3b13e2d.

1. Preserve the non-overlapping order in the fallback (both reviews) — agreed, this was a real flaw: the fallback clamped p and q unconditionally, so an AR-only overlap (e.g. injected (p, d, q)=(4, 0, 6), seasonal (1, 0, 0, 4)) degraded the valid MA order 6 to 3. The clamping conditions are now shared with config2params via SARIMAX._clamp_overlapping_orders(), which clamps only the component whose lags actually overlap (P > 0 and p >= s for AR, Q > 0 and q >= s for MA). Reproduced both reported cases: AR-only now fits (3, 0, 6) and the MA-only analogue (6, 0, 4)/(0, 0, 4, 4) fits (6, 0, 3).

2. Keep flexible (sparse) lag specifications valid — the scalar comparisons could raise TypeError for list/tuple specs (e.g. Q=(0, 12)). Orders are now type-checked (int/np.integer) and non-scalar specs pass through untouched, restoring main's init behavior. One honest caveat from my reproduction: on exact main, the reported sparse configs also fail at fit() — statsmodels raises its own ValueError: Invalid AR order. Appears to be a boolean list ... — so this PR keeps them constructible and passes them to statsmodels for validation rather than making them fit (I could not reproduce "six finite predictions on main" for p=(0, 1, 13)/Q=(0, 12); happy to take another look with an exact repro script if I'm missing a code path). (int, np.integer) is used deliberately so numpy-domain values from flaml.tune still clamp.

Tests (extended test_sarimax_overlapping_lags_validation): AR-only and MA-only fallback cases asserting the non-overlapping order is preserved and forecasts are produced; sparse-spec passthrough (with and without seasonal terms enabled) asserting no TypeError. python -m pytest test/automl/test_forecast.py -k "sarimax" -q → 1 passed, 35 deselected. Also re-verified locally: both-component overlap still clamps to (3, 0, 3)\), unrelated ValueErrors still propagate, exog path fits/predicts, and the issue #845 AutoML starting_points` scenario still succeeds.

Re: the earlier Build attempt failures (ubuntu 3.12/3.13) — the latest attempt of that run is green on all jobs; this push re-triggers CI.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the SARIMAX lag-overlap changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@thinkall Li Jiang (thinkall) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Verified the corrective delta from 6d43bf0a128bbf6f755d3b538014832ce23f6c24 to 3b13e2d493c6a24d34c206826b74a33cfd3f16cf. Both specifically reported P2 issues are corrected in the tested paths: valid sparse AR/MA lag lists/tuples pass through without scalar comparison errors, and the defensive AR-only/MA-only fallback preserves the other valid order (6) instead of clamping both components to 3.

On native Windows Python 3.10 and 3.13, 17 cases pass per stack (34 total). The same current test definitions against the previous head give 9 expected failures and 8 passing controls per stack. Coverage includes actual sparse-list statsmodels fit/six-step prediction, actual post-init defensive fallback fitted-order checks, NumPy-integer/scalar normalization, retained endog/exog data, retry logging and unrelated ValueError propagation. The fallback injection is explicitly a defensive-path test, not a claim that normal built-in search must reach it. Original failures and the earlier changes request remain preserved.

This clears only those two driver-confirmed findings, not whole-feature or independent review-code signoff. The planned Medium complementary six-lens review remains blocked at the authenticated CLI prerequisite; no reviewer model or clean skill result is claimed. Broader sampled-season HPO and optional/version coverage are not cleared by these tests. No earlier review was dismissed and no foreign thread was resolved. Final approval still requires all fresh same-head source, thread, Balanced Copilot and applicable contexts/actual workflows to succeed.

Posted by thinkall-agent-auto-reviewer

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sarimax in automl_time_series_forecast.ipynb sometimes suggest wrong config

3 participants