Repository navigation
feat(core): accept X_df and futr_exog_list in detect_anomalies - #82
Open
sharmis-tp wants to merge 1 commit into
Open
sharmis-tp wants to merge 1 commit into
sharmis-tp wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #75
detect_anomalies()scores residuals from in-sample cross-validation, but unlikeforecast()andcross_validation()it had no way to pass known-future covariates. This addsX_dfandfutr_exog_listtodetect_anomalies()onForecasterandFoundationForecast, using the same contract andexog_strategyas forecast and CV.What changed
Forecaster.detect_anomalies(df, h, freq, n_windows, level, X_df=None, *, futr_exog_list=None). Covariates reach the existing CV split loop, which already builds a per-foldX_dffrom the holdout rows. There are two ways to pass them:df, as forcross_validation()(this already worked implicitly, and is now documented), orX_dfwith a row for every(unique_id, ds)indf. Every window is scored in-sample, so the covariates must cover the whole history. A newmerge_insample_exoghelper incore/exog/covariates.pymergesX_dfintodfbefore the windows are built and ignores any extra rows. It raises a clearValueErrorwhenX_dfis missing rows, has duplicate keys, lacksunique_id/ds, or repeats a covariate that is already a column ofdf.exog_strategy=False,X_dfandfutr_exog_listare ignored and scoring is univariate, as in forecast and CV.FoundationForecast.detect_anomaliespassesX_dfandfutr_exog_listto every member. As suggested in the thread, members without native exog setexog_strategy=False, so a mixed ensemble works._validate_models_horizon_exognow coversdetect_anomaliestoo, so an"auto"member without native exog fails up front with the usual "does not support known-future exogenous variables" error, andfallback_modelis not used in its place. This matches forecast and CV. One behaviour change: before this PR, exog columns indfwith such a member raised inside the model, and a configuredfallback_modelthen ran instead.Tests
tests/core/test_forecaster.py: a native-exog spy model (ExogSpyModelintests/helpers.py) checks thatdetect_anomaliescalls the forecast path with each fold'sX_dfrows and that its forecasts use them. Also covered:X_dfand exog columns indfgive identical results (even withX_dfshuffled);futr_exog_listselects columns;exog_strategy=FalseignoresX_df; the invalid-X_dferrors; and a non-native model rejectingX_df.tests/test_foundation_forecast.py: a mixed ensemble (native spy plus a model withexog_strategy=False) withX_df, and up-front validation before the fallback runs, both withX_dfand with exog columns indf.tests/models/test_models.py: a parametrizedtest_exog_detect_anomalies_with_X_dfthat skips models wheresupports_native_futr_exog()is false.DummyModelintests/helpers.pynow takesexog_strategy.Docs
docs/exogenous-variables.md:detect_anomaliesin the Data API andexog_strategytable, a new "Anomaly detection" section, and runnable examples (a mixedFoundationForecastwith exog indf, and Chronos-2 with a separateX_df).docs/forecasting-parameters.md: a note under "Anomaly detection defaults".README.md: the Highlights and support-matrix lines now coverdetect_anomaliesalongsideforecast/cross_validation.Checks run (Python 3.11,
uv sync --group dev)uv run pre-commit run --all-filesand--hook-stage manual(ruff, ruff-format, mypy, toml-sort): passuv run pytest tests/core tests/test_foundation_forecast.py tests/test_package_imports.py: 147 passed. The 12 new tests intests/core/test_forecaster.pyandtests/test_foundation_forecast.pyfail onmain.uv run pytest tests/models/test_models.py -k test_exog_detect_anomalies_with_X_df, run for the Chronos-2 (×2), Timestral-5M, TiRex-2 and T0 alpha/beta/gamma entries: 7 passed. TimesFM-3 and TimeH2O were not run locally, so CI covers them.uv run pytest -m docs -o addopts= -n 0 tests/docs/test_docs.py -k exogenous-variables.md: passuv run --group docs zensical build: no issues