How to Use Hierarchical Forecasting with Nixtla

Jul 20, 2024 | Data Science

Hierarchical forecasting is a critical tool in various sectors, allowing organizations to make coherent predictions across different levels. In this blog post, we’ll guide you through the concepts, features, installation, and usage of the HierarchicalForecast library by Nixtla, a powerful tool for probabilistic hierarchical forecasting utilizing statistical and econometric methods.

Understanding Hierarchical Forecasting

Imagine a multi-layered cake where each layer is a different level of data aggregation—like geographical regions, product categories, or even sales figures by store. Hierarchical forecasting is all about ensuring that the cake, when sliced at different layers, still offers a consistent flavor, meaning coherent predictions. Nixtla’s HierarchicalForecast library helps achieve this by providing various reconciliation methods to maintain flavor consistency across the cake layers.

Features of HierarchicalForecast

  • Classic reconciliation methods:
    • BottomUp: Adds predictions from lower levels to upper levels.
    • TopDown: Distributes forecasts from the top levels through the hierarchy.
  • Alternative reconciliation methods:
    • MiddleOut: Anchors base predictions at a middle level, applying top-down and bottom-up methods selectively.
    • MinTrace: Minimizes forecast variance of coherent forecasts.
    • ERM: Optimizes a reconciliation matrix with an L1 regularized objective.
  • Probabilistic coherent methods:
    • Normality: Utilizes variance-covariance under a normality assumption.
    • Bootstrap: Generates distributions of reconciled predictions using a bootstrap approach.
    • PERMBU: Reconciles predictions by incorporating rank permutation copulas.

Installation

To get started, you can install the HierarchicalForecast library using either of the package managers:

  • Using pip:
    pip install hierarchicalforecast
  • Using conda:
    conda install -c conda-forge hierarchicalforecast

How to Use HierarchicalForecast

To provide a practical understanding, let’s walk through a minimal example:

# Minimal Example
import numpy as np
import pandas as pd
from datasetsforecast.hierarchical import HierarchicalData
from statsforecast.core import StatsForecast
from statsforecast.models import AutoARIMA, Naive
from hierarchicalforecast.core import HierarchicalReconciliation
from hierarchicalforecast.evaluation import HierarchicalEvaluation
from hierarchicalforecast.methods import BottomUp, TopDown, MiddleOut

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

# Split the train-test sets
Y_test_df = Y_df.groupby('unique_id').tail(4)
Y_train_df = Y_df.drop(Y_test_df.index)

# Compute base auto-ARIMA predictions
fcst = StatsForecast(df=Y_train_df,
                     models=[AutoARIMA(season_length=4), Naive()],
                     freq='Q', n_jobs=-1)
Y_hat_df = fcst.forecast(h=4)

# Reconcile the base predictions
reconcilers = [
    BottomUp(),
    TopDown(method='forecast_proportions'),
    MiddleOut(middle_level='CountryPurposeState', top_down_method='forecast_proportions')]
hrec = HierarchicalReconciliation(reconcilers=reconcilers)
Y_rec_df = hrec.reconcile(Y_hat_df=Y_hat_df, Y_df=Y_train_df, S=S, tags=tags)

# Evaluation
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_df=Y_test_df.set_index('unique_id'),
                   tags=tags, benchmark='Naive')

In this example, we start by importing the necessary libraries, then load a hierarchical dataset. We proceed to split the dataset into training and test sets, compute predictions using AutoARIMA and Naive models, and reconcile these predictions with various methods. Finally, we evaluate the results to see how closely they match the actual values.

Troubleshooting

If you encounter issues during installation or execution:

  • Check that your Python version is compatible with the library requirements.
  • Ensure all dependencies (like statsforecast and datasetsforecast) are correctly installed. You can install them with:
    pip install statsforecast datasetsforecast
  • If you have specific errors, searching those in forums or raising issues in the GitHub repository can also be helpful.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

Hierarchical forecasting plays an integral role in making sound predictions across various levels of data. By using the HierarchicalForecast library, you can leverage sophisticated reconciliation methods to ensure your forecasts are both coherent and reliable.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox