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

MiddleOut with bootstrap fails. #168

Open
melopeo opened this issue Jan 23, 2023 · 2 comments
Open

MiddleOut with bootstrap fails. #168

melopeo opened this issue Jan 23, 2023 · 2 comments
Labels

Comments

@melopeo
Copy link
Contributor

melopeo commented Jan 23, 2023

It seems that the combination of reconciler MiddleOut and intervals_method="bootstrap" fails.

import pandas as pd

# compute base forecast no coherent
from statsforecast.core import StatsForecast
from statsforecast.models import AutoARIMA, Naive

# obtain hierarchical reconciliation methods and evaluation
from hierarchicalforecast.core import HierarchicalReconciliation
from hierarchicalforecast.methods import MiddleOut

# obtain hierarchical datasets
from datasetsforecast.hierarchical import HierarchicalData

# Load TourismSmall dataset
Y_df, S, tags = HierarchicalData.load("./data", "TourismSmall")
Y_df["ds"] = pd.to_datetime(Y_df["ds"])

level = [10, 20, 30, 40, 50, 60, 70, 80, 90]

# Compute base level predictions
sf = StatsForecast(df=Y_df, models=[AutoARIMA(season_length=4)], freq="Q", n_jobs=1)

forecasts_df = sf.forecast(h=8, fitted=True, level=level)

# Reconcile the base predictions
reconcilers = [MiddleOut(top_down_method='average_proportions', middle_level='Country/Purpose/State')]

hrec = HierarchicalReconciliation(reconcilers=reconcilers)

reconciled_forecasts = hrec.reconcile(
    Y_hat_df=forecasts_df,
    Y_df=sf.forecast_fitted_values(),
    S=S,
    tags=tags,
    level=level,
    intervals_method="bootstrap",
)

This is the error message

Traceback (most recent call last):
  File "/Users/XXX/workplace/YYY/learning/middleout_example.py", line 30, in <module>
    reconciled_forecasts = hrec.reconcile(
  File "/Users/XXX/anaconda3/envs/YYY_py38/lib/python3.8/site-packages/hierarchicalforecast/core.py", line 207, in reconcile
    fcsts[f'{model_name}/{reconcile_fn_name}-lo-{lv}'] = fcsts_model[f'lo-{lv}'].flatten()
KeyError: 'lo-10'
@kdgutier
Copy link
Collaborator

Hi @melopeo,

Thanks for reporting the issue.
Currently we do not compute an explicit projection matrix P for the MiddleOut method.
To have a reconciliation of the type: y_tilde = S P y_hat_simulated on which the bootstrap method relied.

I think one should be able to compute a corresponding projection matrix P by multiplying the implicit BottomUp and TopDown matrices.

We will take a look.

@elephaint
Copy link
Contributor

Note: check the solution to this for all interval methods

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants