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

Introduction tutorial #102

Merged
merged 16 commits into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions hierarchicalforecast/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@
def _build_fn_name(fn) -> str:
fn_name = type(fn).__name__
func_params = fn.__dict__

# Take default parameter out of names
args_to_remove = ['insample']
if not func_params.get('nonnegative', False):
args_to_remove += ['nonnegative']

if fn_name == 'MinTrace' and \
func_params['method']=='mint_shrink':
if func_params['mint_shr_ridge'] == 2e-8:
args_to_remove += ['mint_shr_ridge']

func_params = [f'{name}-{value}' for name, value in func_params.items() if name not in args_to_remove]
if func_params:
fn_name += '_' + '_'.join(func_params)
Expand Down Expand Up @@ -58,12 +66,12 @@ def reconcile(self,

The `reconcile` method is analogous to SKLearn `fit` method, it applies different
reconciliation methods instantiated in the `reconcilers` list.

Most reconciliation methods can be described by the following convenient
linear algebra notation:

$$\\tilde{\mathbf{y}}_{[a,b],\\tau} = \mathbf{S}_{[a,b][b]} \mathbf{P}_{[b][a,b]} \hat{\mathbf{y}}_{[a,b],\\tau}$$

where $a, b$ represent the aggregate and bottom levels, $\mathbf{S}_{[a,b][b]}$ contains
the hierarchical aggregation constraints, and $\mathbf{P}_{[b][a,b]}$ varies across
reconciliation methods. The reconciled predictions are $\\tilde{\mathbf{y}}_{[a,b],\\tau}$, and the
Expand All @@ -77,7 +85,7 @@ def reconcile(self,
`tags`: Each key is a level and its value contains tags associated to that level.<br>
`level`: float list 0-100, confidence levels for prediction intervals.<br>
`intervals_method`: str, method used to calculate prediction intervals, one of `normality`, `bootstrap`, `permbu`.<br>

**Returns:**<br>
`y_tilde`: pd.DataFrame, with reconciled predictions.
"""
Expand Down
2 changes: 1 addition & 1 deletion hierarchicalforecast/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ class MinTrace:
**Parameters:**<br>
`method`: str, one of `ols`, `wls_struct`, `wls_var`, `mint_shrink`, `mint_cov`.<br>
`nonnegative`: bool, reconciled forecasts should be nonnegative?<br>
`mint_shr_ridge`: float, ridge numeric protection to MinTrace-shr covariance estimator.<br>
`mint_shr_ridge`: float=2e-8, ridge numeric protection to MinTrace-shr covariance estimator.<br>

**References:**<br>
- [Wickramasuriya, S. L., Athanasopoulos, G., & Hyndman, R. J. (2019). \"Optimal forecast reconciliation for
Expand Down
28 changes: 21 additions & 7 deletions nbs/core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,17 @@
"def _build_fn_name(fn) -> str:\n",
kdgutier marked this conversation as resolved.
Show resolved Hide resolved
kdgutier marked this conversation as resolved.
Show resolved Hide resolved
" fn_name = type(fn).__name__\n",
" func_params = fn.__dict__\n",
"\n",
" # Take default parameter out of names\n",
" args_to_remove = ['insample']\n",
" if not func_params.get('nonnegative', False):\n",
" args_to_remove += ['nonnegative']\n",
"\n",
" if fn_name == 'MinTrace' and \\\n",
" func_params['method']=='mint_shrink':\n",
" if func_params['mint_shr_ridge'] == 2e-8:\n",
" args_to_remove += ['mint_shr_ridge']\n",
"\n",
" func_params = [f'{name}-{value}' for name, value in func_params.items() if name not in args_to_remove]\n",
" if func_params:\n",
" fn_name += '_' + '_'.join(func_params)\n",
Expand Down Expand Up @@ -99,6 +107,10 @@
"test_eq(\n",
" _build_fn_name(MinTrace(method='ols', nonnegative=True)), \n",
" 'MinTrace_method-ols_nonnegative-True'\n",
")\n",
"test_eq(\n",
" _build_fn_name(MinTrace(method='mint_shr')), \n",
" 'MinTrace_method-mint_shr'\n",
")"
]
},
Expand Down Expand Up @@ -147,12 +159,12 @@
"\n",
" The `reconcile` method is analogous to SKLearn `fit` method, it applies different \n",
" reconciliation methods instantiated in the `reconcilers` list. \n",
" \n",
"\n",
" Most reconciliation methods can be described by the following convenient \n",
" linear algebra notation:\n",
"\n",
" $$\\\\tilde{\\mathbf{y}}_{[a,b],\\\\tau} = \\mathbf{S}_{[a,b][b]} \\mathbf{P}_{[b][a,b]} \\hat{\\mathbf{y}}_{[a,b],\\\\tau}$$\n",
" \n",
"\n",
" where $a, b$ represent the aggregate and bottom levels, $\\mathbf{S}_{[a,b][b]}$ contains\n",
" the hierarchical aggregation constraints, and $\\mathbf{P}_{[b][a,b]}$ varies across \n",
" reconciliation methods. The reconciled predictions are $\\\\tilde{\\mathbf{y}}_{[a,b],\\\\tau}$, and the \n",
Expand All @@ -166,7 +178,7 @@
" `tags`: Each key is a level and its value contains tags associated to that level.<br>\n",
" `level`: float list 0-100, confidence levels for prediction intervals.<br>\n",
" `intervals_method`: str, method used to calculate prediction intervals, one of `normality`, `bootstrap`, `permbu`.<br>\n",
" \n",
"\n",
" **Returns:**<br>\n",
" `y_tilde`: pd.DataFrame, with reconciled predictions. \n",
" \"\"\"\n",
Expand Down Expand Up @@ -758,16 +770,18 @@
"fcst = StatsForecast(df=Y_train_df,\n",
" #models=[ETS(season_length=12), Naive()],\n",
" models=[Naive()],\n",
" freq='Q', n_jobs=-1) \n",
"Y_hat_df = fcst.forecast(h=4)\n",
" freq='Q', n_jobs=-1)\n",
"Y_hat_df = fcst.forecast(h=4, fitted=True)\n",
"Y_fitted_df = fcst.forecast_fitted_values()\n",
"\n",
"# Reconcile the base predictions\n",
"Y_train_df = Y_train_df.reset_index().set_index('unique_id')\n",
"Y_hat_df = Y_hat_df.reset_index().set_index('unique_id')\n",
"reconcilers = [BottomUp(),\n",
" MinTrace(method='ols')]\n",
" MinTrace(method='mint_shrink')]\n",
"hrec = HierarchicalReconciliation(reconcilers=reconcilers)\n",
"Y_rec_df = hrec.reconcile(Y_hat_df=Y_hat_df, Y_df=Y_train_df,\n",
"Y_rec_df = hrec.reconcile(Y_hat_df=Y_hat_df, \n",
" Y_df=Y_fitted_df,\n",
" S=S_df, tags=tags)\n",
"Y_rec_df.groupby('unique_id').head(2)"
]
Expand Down
231 changes: 109 additions & 122 deletions nbs/examples/AustralianDomesticTourism-Bootstraped-Intervals.ipynb

Large diffs are not rendered by default.

342 changes: 165 additions & 177 deletions nbs/examples/AustralianDomesticTourism-Intervals.ipynb

Large diffs are not rendered by default.

327 changes: 157 additions & 170 deletions nbs/examples/AustralianDomesticTourism-Permbu-Intervals.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion nbs/examples/AustralianDomesticTourism.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@
"\n",
kdgutier marked this conversation as resolved.
Show resolved Hide resolved
"Observe that we can recover the results reported by the [Forecasting: Principles and Practice](https://otexts.com/fpp3/tourism.html). The original results were calculated using the R package [fable](https://github.com/tidyverts/fable).\n",
"\n",
"![image.png](./AustralianDomesticTourism-results-fable.png)"
"![Fable's reconciliation results](./imgs/AustralianDomesticTourism-results-fable.png)"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion nbs/examples/AustralianPrisonPopulation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@
"\n",
kdgutier marked this conversation as resolved.
Show resolved Hide resolved
"Observe that we can recover the results reported by the [Forecasting: Principles and Practice](https://otexts.com/fpp3/prison.html) book. The original results were calculated using the R package [fable](https://github.com/tidyverts/fable).\n",
"\n",
"![image](./AustralianPrisonPopulation-results-fable.png)"
"![Fable's reconciliation results](./imgs/AustralianPrisonPopulation-results-fable.png)"
]
},
{
Expand Down
Loading