Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] TBATS can not produce a forecast with intervals when FH > History Period #4491

Closed
ngupta23 opened this issue Apr 17, 2023 · 2 comments · Fixed by #4492
Closed

[BUG] TBATS can not produce a forecast with intervals when FH > History Period #4491

ngupta23 opened this issue Apr 17, 2023 · 2 comments · Fixed by #4492
Labels
bug Something isn't working module:forecasting forecasting module: forecasting, incl probabilistic and hierarchical forecasting
Projects

Comments

@ngupta23
Copy link
Contributor

Describe the bug

To Reproduce

import numpy as np
import pandas as pd
import logging
from sktime.forecasting.tbats import TBATS


# generate train set values
np.random.seed(42)
LEN_HISTORY = 50
train = pd.Series(data=np.random.randint(1, 100, LEN_HISTORY))

# train model
estimator = TBATS(
    use_box_cox=False,
    use_trend=True,
    use_damped_trend=False,
    sp=10,
    use_arma_errors=False,
)
estimator.fit(train)

# FH = Historical length works!
try:
    fcst1 = estimator.predict_interval(
        coverage=0.8, fh=np.array(range(1, LEN_HISTORY + 1))
    )
except Exception as e:
    print("FH <= History")
    logging.exception(e)

# FH > Historical length DOES NOT work
try:
    fcst2 = estimator.predict_interval(
        coverage=0.8, fh=np.array(range(1, LEN_HISTORY + 2))
    )
except Exception as e:
    print("FH > History")
    logging.exception(e)
FH > History
ERROR:root:Length mismatch: Expected axis has 1 elements, new values have 51 elements
Traceback (most recent call last):
  File "analysis/tbats/TBATS_prediction_interval_issue.py", line 34, in <module>
    coverage=0.8, fh=np.array(range(1, LEN_HISTORY + 2))
  File "/home/<hidden>/anaconda3/envs/<hidden>/lib/<hidden>/site-packages/sktime/forecasting/base/_base.py", line 636, in predict_interval
    pred_int = self._predict_interval(fh=fh, X=X_inner, coverage=coverage)
  File "/home/<hidden>/anaconda3/envs/<hidden>/lib/<hidden>/site-packages/sktime/forecasting/base/adapters/_tbats.py", line 293, in _predict_interval
    _, tbats_pred_int = self._tbats_forecast_with_interval(fh, c)
  File "/home/<hidden>/anaconda3/envs/<hidden>/lib/<hidden>/site-packages/sktime/forecasting/base/adapters/_tbats.py", line 217, in _tbats_forecast_with_interval
    pred_int = self._get_pred_int(lower=lower, upper=upper)
  File "/home/<hidden>/anaconda3/envs/<hidden>/lib/<hidden>/site-packages/sktime/forecasting/base/adapters/_tbats.py", line 345, in _get_pred_int
    pred_int.index = fh_out.to_absolute(self.cutoff)
  File "/home/<hidden>/anaconda3/envs/<hidden>/lib/<hidden>/site-packages/pandas/core/generic.py", line 5154, in __setattr__
    return object.__setattr__(self, name, value)
  File "pandas/_libs/properties.pyx", line 66, in pandas._libs.properties.AxisProperty.__set__
  File "/home/<hidden>/anaconda3/envs/<hidden>/lib/<hidden>/site-packages/pandas/core/generic.py", line 564, in _set_axis
    self._mgr.set_axis(axis, labels)
  File "/home/<hidden>/anaconda3/envs/<hidden>/lib/<hidden>/site-packages/pandas/core/internals/managers.py", line 227, in set_axis
    f"Length mismatch: Expected axis has {old_len} elements, new "
ValueError: Length mismatch: Expected axis has 1 elements, new values have 51 elements

Expected behavior
Requesting Prediction Intervals should work when FH > len(history) since it works when just requesting predictions without intervals (so it there should not be a historical data insufficiency issue).

Versions

sktime 0.16.0 and sktime 0.17.0
@ngupta23 ngupta23 added the bug Something isn't working label Apr 17, 2023
@fkiraly
Copy link
Collaborator

fkiraly commented Apr 17, 2023

FYI @yarnabrina,this looks similar to code you are currently refactoring for statsmodels.
This seems to come from fh, have you encountered this before?

@fkiraly fkiraly added the module:forecasting forecasting module: forecasting, incl probabilistic and hierarchical forecasting label Apr 17, 2023
@fkiraly fkiraly added this to Needs triage & validation in Bugfixing via automation Apr 18, 2023
@fkiraly
Copy link
Collaborator

fkiraly commented Apr 18, 2023

update: bug confirmed, python 3.10, current main

This is also not coming from fh, that is fine.
It is coming from preceding code, which produces pred_int of wrong length, as soon as the prediction index exceeds length of the previous series.

@fkiraly fkiraly moved this from Needs triage & validation to Investigating in Bugfixing Apr 18, 2023
@fkiraly fkiraly moved this from Investigating to Under review in Bugfixing Apr 18, 2023
fkiraly added a commit that referenced this issue Apr 22, 2023
Fixes #4491

The `_predict_interval` interface in `_TbatsAdapter` was a bit of a
mess.

There was also really strange code that triggered specifically whenever
the `fh` was longer than the training data, I was unable to understand
why that condition should be the same as "contains in-sample prediction
intervals".

This is the code:
```python
        # If pred_int contains in-sample prediction intervals
        if len(pred_int) > len(self._y):
            len_out = len(pred_int) - len(self._y)
            # Workaround for slicing with negative index
            pred_int["idx"] = [x for x in range(-len(self._y), len_out)]
```

It explains @ngupta23's issues in #4491, but I don't get why this was
there in the first place.

Either way, I just completely rewrote the interface to prediction
intervals from scratch.
The logic was simple, it is producing `nan` for insample predictions.

Contains the failure case from
#4491 as a test.
Bugfixing automation moved this from Under review to Fixed/resolved Apr 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working module:forecasting forecasting module: forecasting, incl probabilistic and hierarchical forecasting
Projects
Bugfixing
Fixed/resolved
Development

Successfully merging a pull request may close this issue.

2 participants