Nixtla/hierarchicalforecast

MiddleOut with bootstrap fails.

melopeo opened this issue · 1 comments

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'

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.