Nixtla / statsforecast

Lightning ⚡️ fast forecasting with statistical and econometric models.
https://nixtlaverse.nixtla.io/statsforecast
Apache License 2.0
3.92k stars 276 forks source link

[statsforecast, MSTL]: Prediction Interval throw error #622

Closed Azlich closed 1 year ago

Azlich commented 1 year ago

What happened + What you expected to happen

  1. I follow MSTL tutorial from MultipleSeasonalities notebook (https://github.com/Nixtla/statsforecast/blob/main/nbs/docs/tutorials/MultipleSeasonalities.ipynb). I got Exception: You have to instantiate either the trend forecaster class or MSTL class with prediction_intervals to calculate them in prediction interval step

  2. I expected to run the code without error and get prediction interval

Versions / Dependencies

StatsForecast 1.6.0

Reproduction script

from statsforecast import StatsForecast
from statsforecast.models import MSTL, AutoARIMA
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/panambY/Hourly_Energy_Consumption/master/data/PJM_Load_hourly.csv')
df.columns = ['ds', 'y']
df.insert(0, 'unique_id', 'PJM_Load_hourly')

models = [MSTL(
    season_length=[24, 24 * 7],
    trend_forecaster=AutoARIMA(),
)]
sf = StatsForecast(
    models=models,
    freq='H',
)
sf = sf.fit(df=df)
forecasts = sf.predict(h=24, level=[90]) # throw error
forecasts.head()

Issue Severity

None

jmoralez commented 1 year ago

Hey @Azlich, thanks for using statsforecast.

As the error states you have two options:

Instantiate the trend forecaster with prediction intervals

from statsforecast.utils import ConformalIntervals

models = [MSTL(
    season_length=[24, 24 * 7],
    trend_forecaster=AutoARIMA(prediction_intervals=ConformalIntervals(h=24)),  # define the intervals here
)]
sf = StatsForecast(
    models=models,
    freq='H',
)
sf = sf.fit(df=df)
sf.predict(h=24, level=[90])

Instantiate the MSTL class with prediction intervals

from statsforecast.utils import ConformalIntervals

models = [MSTL(
    season_length=[24, 24 * 7],
    trend_forecaster=AutoARIMA(),
    prediction_intervals=ConformalIntervals(h=24),  # define the intervals here
)]
sf = StatsForecast(
    models=models,
    freq='H',
)
sf = sf.fit(df=df)
sf.predict(h=24, level=[90])

Please let us know if you have further doubts.

Azlich commented 1 year ago

Thank you for your response.

Does this mean that MSTL doesn't support Probabilistic forecasting natively? So I need to use Conformal Prediction to make probabilistic forecasts for the MSTL model.

I saw the StatsForecast's Models page (https://nixtla.github.io/statsforecast/src/core/models_intro.html) states that MSTL supports Probabilistic Forecasting.

I'm new to this topic. But thanks a lot for your help.

jmoralez commented 1 year ago

You'd need the conformal intervals for the trend forecaster in case it doesn't support producing prediction intervals out of the box but in this case the ARIMA model does, so this indeed seems like a bug. We'll work on it and have a fix soon. Thanks for raising this!

sam-hatem commented 11 months ago

Issue not fixed