• Stars
    star
    568
  • Rank 78,502 (Top 2 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created over 2 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Probabilistic Hierarchical forecasting πŸ‘‘ with statistical and econometric methods.

Nixtla Β Slack

Hierarchical Forecast πŸ‘‘

Probabilistic hierarchical forecasting with statistical and econometric methods

CI Python PyPi conda-nixtla License

HierarchicalForecast offers a collection of reconciliation methods, including BottomUp, TopDown, MiddleOut, MinTrace and ERM. And Probabilistic coherent predictions including Normality, Bootstrap, and PERMBU.

πŸ“š Intro

A vast amount of time series datasets are organized into structures with different levels or hierarchies of aggregation. Examples include categories, brands, or geographical groupings. Coherent forecasts across levels are necessary for consistent decision-making and planning. Hierachical Forecast offers differnt reconciliation methods that render coherent forecasts across hierachies. Until recent, this methods were mainly avaiable in the R ecosystem. This Python-based framework aims to bridge the gap between statistical modeling and Machine Learning in the time series field.

🎊 Features

  • Classic reconciliation methods:
    • BottomUp: Simple addition to the upper levels.
    • TopDown: Distributes the top levels forecasts trough the hierarchies.
  • Alternative reconciliation methods:
    • MiddleOut: It anchors the base predictions in a middle level. The levels above the base predictions use the bottom-up approach, while the levels below use a top-down.
    • MinTrace: Minimizes the total forecast variance of the space of coherent forecasts, with the Minimum Trace reconciliation.
    • ERM: Optimizes the reconciliation matrix minimizing an L1 regularized objective.
  • Probabilistic coherent methods:
    • Normality: Uses MinTrace variance-covariance closed form matrix under a normality assumption.
    • Bootstrap: Generates distribution of hierarchically reconciled predictions using Gamakumara's bootstrap approach.
    • PERMBU: Reconciles independent sample predictions by reinjecting multivariate dependence with estimated rank permutation copulas, and performing a Bottom-Up aggregation.

Missing something? Please open an issue here or write us in Slack

πŸ“– Why?

Short: We want to contribute to the ML field by providing reliable baselines and benchmarks for hierarchical forecasting task in industry and academia. Here's the complete paper.

Verbose: HierarchicalForecast integrates publicly available processed datasets, evaluation metrics, and a curated set of standard statistical baselines. In this library we provide usage examples and references to extensive experiments where we showcase the baseline's use and evaluate the accuracy of their predictions. With this work, we hope to contribute to Machine Learning forecasting by bridging the gap to statistical and econometric modeling, as well as providing tools for the development of novel hierarchical forecasting algorithms rooted in a thorough comparison of these well-established models. We intend to continue maintaining and increasing the repository, promoting collaboration across the forecasting community.

πŸ’» Installation

You can install HierarchicalForecast's the Python package index pip with:

pip install hierarchicalforecast

You can also can install HierarchicalForecast's from conda with:

conda install -c conda-forge hierarchicalforecast

🧬 How to use

The following example needs statsforecast and datasetsforecast as additional packages. If not installed, install it via your preferred method, e.g. pip install statsforecast datasetsforecast. The datasetsforecast library allows us to download hierarhical datasets and we will use statsforecast to compute the base forecasts to be reconciled.

You can open a complete example in Colab Open In Colab

Minimal Example:

# !pip install -U numba statsforecast datasetsforecast
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 BottomUp, TopDown, 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'])


# Compute base level predictions 
sf = StatsForecast(df=Y_df, 
                   models=[AutoARIMA(season_length=12), Naive()], 
                   freq='M', n_jobs=-1)

forecasts_df = sf.forecast(h=12)

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

hrec = HierarchicalReconciliation(reconcilers=reconcilers)

reconciled_forecasts = hrec.reconcile(Y_hat_df=forecasts_df, S=S, tags=tags)

Evaluation

Assumes you have a test dataframe.

from hierarchicalforecast.core import HierarchicalEvaluation

def mse(y, y_hat):
    return np.mean((y-y_hat)**2)

evaluator = HierarchicalEvaluation(evaluators=[mse])
evaluator.evaluate(Y_hat_df=Y_rec_df, Y_test=Y_test_df, 
                   tags=tags, benchmark='Naive')

πŸ“– Documentation (WIP)

Here is a link to the documentation.

πŸ“ƒ License

This project is licensed under the MIT License - see the LICENSE file for details.

🏟 HTS projects

In the R ecosystem, we recommend checking out fable, and the now-retired hts. In Python we want to acknowledge the following libraries hiere2e, sktime, darts, pyhts, scikit-hts.

πŸ“š References and Acknowledgements

This work is highly influenced by the fantastic work of previous contributors and other scholars who previously proposed the reconciliation methods presented here. We want to highlight the work of Rob Hyndman, George Athanasopoulos, Shanika L. Wickramasuriya, Souhaib Ben Taieb, and Bonsoo Koo. For a full reference link, please visit the Reference section of this paper. We encourage users to explore this literature review.

πŸ™ How to cite

If you enjoy or benefit from using these Python implementations, a citation to this hierarchical forecasting reference paper will be greatly appreciated.

@article{olivares2022hierarchicalforecast,
    author    = {Kin G. Olivares and
                 Federico Garza and 
                 David Luo and 
                 Cristian ChallΓΊ and
                 Max Mergenthaler and
                 Souhaib Ben Taieb and
                 Shanika L. Wickramasuriya and
                 Artur Dubrawski},
    title     = {{HierarchicalForecast}: A Reference Framework for Hierarchical Forecasting in Python},
    journal   = {Work in progress paper, submitted to Journal of Machine Learning Research.},
    volume    = {abs/2207.03517},
    year      = {2022},
    url       = {https://arxiv.org/abs/2207.03517},
    archivePrefix = {arXiv}
}

More Repositories

1

statsforecast

Lightning ⚑️ fast forecasting with statistical and econometric models.
Python
3,846
star
2

neuralforecast

Scalable and user friendly neural 🧠 forecasting algorithms.
Python
3,001
star
3

nixtla

TimeGPT-1: production ready pre-trained Time Series Foundation Model for forecasting and anomaly detection. Generative pretrained transformer for time series trained on over 100B data points. It's capable of accurately predicting various domains such as retail, electricity, finance, and IoT with just a few lines of code πŸš€.
Jupyter Notebook
2,208
star
4

mlforecast

Scalable machine πŸ€– learning for time series forecasting.
Python
501
star
5

tsfeatures

Calculates various features from time series data. Python implementation of the R package tsfeatures.
Python
362
star
6

Nixtla

Automated time series processing and forecasting.
Python
253
star
7

transfer-learning-time-series

Transfer πŸ€— Learning for Time Series Forecasting
Jupyter Notebook
123
star
8

datasetsforecast

Datasets for time series forecasting
Jupyter Notebook
38
star
9

fpp3-python

Forecasting: principles and practice in python
Jupyter Notebook
13
star
10

timegpt-forecaster-streamlit

TimeGPT forecaster example using streamlit
Python
12
star
11

vantage

Use TimeGPT to predict cloud costs and detect anomalies.
Python
11
star
12

public-slides

Nixtla Public Slides
Python
6
star
13

nixtlats

6
star
14

popol-vuh

Popol Vuh: Nixtla's operating system
Python
6
star
15

utilsforecast

Python
4
star
16

nixtlar

R SDK for TimeGPT
R
3
star
17

m4-forecasts

ZIP version of M4 forecasts uploaded to https://github.com/Mcompetitions/M4-methods/tree/master/Point%20Forecasts.
2
star
18

m5-forecasts

ZIP version of dataset and forecasts uploaded to https://drive.google.com/drive/folders/1D6EWdVSaOtrP1LEFh1REjI3vej6iUS_4.
2
star
19

nixtla-commons

Nixtla shared assets
CSS
2
star
20

blog

Jupyter Notebook
1
star
21

docs

MDX
1
star
22

how-to-contribute-nixtlaverse

Instruction to contribute to the Nixtla libraries
1
star