1. Understand the User's Goal: The user wants a "hierarchical forecasting stack." This means they want a comprehensive plan for building a system that generates forecasts for time series data that can be aggregated (e.g., Product -> Category -> Region -> Global) and then reconciled so the sums match up at different levels. They specifically ask for three components:
* Aggregation Levels (The structure).
* Reconciliation Algorithms (The math/methods).
* Metrics (How to evaluate performance).
-
Deconstruct the Problem:
- Hierarchies: What defines the structure? It's usually temporal (daily -> monthly) and spatial/categorical (bottom-level -> top-level).
- Base Forecasting: Before reconciliation, you need individual forecasts. I need to briefly mention this as a prerequisite, even though the user focused on the reconciliation stack.
- Reconciliation: This is the core. How do we make sure A+B=C?
- Evaluation: How do we know it's good? We need metrics for accuracy and for coherence (though reconciliation theoretically fixes coherence, we need to check residual incoherence or performance gains).
-
Drafting the "Stack" Architecture (Top-Down Approach):
-
Layer 1: Data & Hierarchical Structure (The Foundation)
- Need to define the hierarchy tree.
- Concept of "Bottom-up" vs. "Top-down" vs "Middle-out" (conceptually, though modern methods usually optimize globally).
- Key terminology: Aggregation matrix (S), Summing matrix.
-
Layer 2: Base Forecasting Generation (The Input)
- Even though the prompt asks for the reconciliation stack, I can't reconcile nothing. I need to specify what generates the base forecasts.
- Strategy: Different models for different levels (e.g., simple Exponential Smoothing for top, complex ML for bottom) vs. one global model.
-
Layer 3: Reconciliation Strategies (The Core Algorithm)
- This is the mathematical engine.
- Level 0: Naive: Bottom-up (simplest).
- Level 1: OLS (Ordinary Least Squares): MinT (Minimum Trace) - classic approach.
- Level 2: Weighted/Regularized: MinT with covariance estimation (shrinkage).
- Level 3: Modern/ML: Deep Learning approaches (e.g., soft constraints in loss functions).
-
Layer 4: Evaluation & Metrics (The QC)
- Need standard accuracy metrics (RMSE, MAE, MAPE).
- Need hierarchical specific metrics? Actually, since reconciliation forces coherence, the metric is usually "Does the reconciled forecast beat the base forecast?" (Accuracy gains) or "How stable is the reconciliation?".
- Metrics should be evaluated at all levels.
-
Refining the Content (Adding detail and structure):
- Aggregation: Let's use a retail example (Store -> Region -> Country).
- Algorithms: I must mention
MinT (Minimum Trace). It's the gold standard. I should also mention BottomUp, TopDown, and OLS/GLS.
- Metrics: CRPS (Continuous Ranked Probability Score) is good for probabilistic. For point forecasts, scaled errors (MASE, RMSSE) are better because they allow comparison across different scales (units vs dollars, or bottom level vs top level).
-
Structuring the Output: I will organize this as a "Blueprint" or "Architecture" with distinct layers.
- Introduction: Define the problem (coherence vs. accuracy).
- Layer 1: Structural Definition (The Graph).
- Layer 2: The Base Learners (The Heterogeneous Engine).
- Layer 3: Reconciliation Layer (The Optimization).
- Layer 4: Evaluation Framework (The Multi-Scale Scoreboard).
- Implementation Stack (Tools): Python libraries (HierarchicalForecast, Prophet, Nixtla).
-
Self-Correction/Review during drafting:
- Critique: Is "Aggregation Levels" a component of the stack or just the data structure?
- Correction: It's the data structure, but the management of it is part of the stack (e.g., how to handle granularities). I'll frame it as "Hierarchy Definition & Management."
- Critique: Should I suggest specific models like ARIMA?
- Correction: Better to suggest families of models. Deep learning is popular now, but statistical methods are robust baselines.
- Math Check: Make sure to mention the Summing Matrix S. That's crucial for the reconciliation step.
-
Final Polish (Formatting): Use bolding, lists, and code-like blocks for the matrix math to make it readable. Ensure the distinction between Temporal Hierarchies (time) and Grouped Hierarchies (categories) is clear.
-
Generating the Response: (Proceeding to generate output based on these structured thoughts.)
Designing a robust hierarchical forecasting stack requires a careful interplay between data structure, statistical independence, and optimization constraints. The goal is to ensure that the sum of lower-level forecasts (bottom-up) equals the higher-level forecasts (top-down) while minimizing error across the entire structure.
Here is a comprehensive plan for a Hierarchical Forecasting Stack, organized into four logical layers: Structure, Generation, Reconciliation, and Evaluation.
Layer 1: Structural Definition (The Graph)
Before modeling, you must define the hierarchy. This is not just a list of columns; it is a tree definition that dictates how aggregation occurs.
1.1. Hierarchy Types
- Strict Hierarchies: Tree-like structures where a child has only one parent (e.g., Product → Category → Department).
- Grouped Hierarchies: Children can belong to multiple parents (e.g., A product belongs to a "Category" hierarchy and a "Geography" hierarchy simultaneously).
- Temporal Hierarchies: Aggregation across time (e.g., Daily → Weekly → Monthly).
1.2. The Aggregation Matrix (S)
Construct the "Summing Matrix" (S).
- Bottom Level: The most granular time series (e.g., SKU-Location).
- Aggregate Levels: All higher levels derived from the bottom level.
- Mathematical representation: If yt is the vector of all series at time t, and bt is the vector of bottom-level series, then .
Layer 2: Base Forecast Generation (The Engines)
This layer generates "base forecasts"—independent predictions for every node in the hierarchy without regard for consistency.
2.1. Model Selection Strategy
Do not use "one model to rule them all." Use a Global Pooling Strategy or Model Selection per Node.
- High-Level Nodes (Sparse/Stable): Use classical statistical models (ARIMA, ETS) or simple ensembles.
- Low-Level Nodes (Noisy/Intermittent): Use models capable of handling zero-inflation (Croston, SBA) or gradient boosting (LightGBM/XGBoost) with lag features.
- External Regressors: If hierarchy coherence is sensitive to holidays or promotions, ensure models (like Prophet or ML models) ingest these features.
2.2. Probabilistic vs. Point Forecasts
- Generate Prediction Intervals (uncertainty) alongside point estimates. This is crucial for the reconciliation step, as uncertainty is often higher at lower levels.
Layer 3: Reconciliation Algorithms (The Glue)
This is the core of the stack. Base forecasts are usually incoherent (the sum of parts does not equal the whole). Reconciliation projects these forecasts onto the space of coherent forecasts.
3.1. The General Framework
We seek a revised forecast y~ such that y~=SGy, where are the base forecasts and is the "reconciliation matrix."
3.2. Algorithm Options (Ranked by Sophistication)
| Algorithm | Complexity | Description | Best Use Case |
|---|
| Bottom-Up (BU) | Low | Aggregates bottom-level forecasts up. Ignores signal from higher levels. | When bottom-level data is high quality and less noisy. |
| Top-Down (TD) | Low | Forecasts top level, then distributes proportions downwards. | When bottom-level data is noisy or intermittent. |
| MinT (Minimum Trace) | High (Recommended) | Finds the linear combination that minimizes the total variance of the reconciled errors. Requires estimating the covariance matrix of residuals. | The Gold Standard. Best for general-purpose accuracy. |
| ERM (Empirical Risk Minimization) | Medium | Minimizes the error on a validation set directly. | When you have ample historical data to train the reconciliation layer. |
3.3. Handling Covariance (For MinT)
To use MinT, you must estimate the covariance matrix (W) of the base forecast errors.
- Simple Scaling: Assume covariance is diagonal (independence) but scale by forecast variance.
- Shrinkage Estimators: Use shrinkage methods (like Ledoit-Wolf) to stabilize the covariance matrix estimation when the hierarchy is wide (many series).
Layer 4: Evaluation Metrics (The Scoreboard)
Standard metrics like MAE are insufficient because they ignore the scale differences between the top level (millions) and bottom level (units).
4.1. Scale-Independent Accuracy
- RMSSE (Root Mean Squared Scaled Error): The standard metric for hierarchical competitions (like M5).
- Why: It scales the error by the error of a naive "random walk" forecast, allowing you to compare performance across SKU level vs. Total level.
- MASE (Mean Absolute Scaled Error): The robust absolute version of RMSSE.
4.2. Coherence Metrics
Even after reconciliation, check for numerical drift.
- Incoherence Residual: ∣∣y~parent−∑. Ideally, this is 0.
4.3. Skill vs. Naive
- Calculate Relative RMSSE: Compare your model's RMSSE against a simple Naive forecast (last period's value).
- Weighted Summary: When reporting a single number for the whole hierarchy, use a weighted average of metrics (e.g., weighted by revenue/volume) so that top-level errors aren't drowned out by thousands of tiny low-level errors.
Proposed Stack Architecture (Python Implementation)
If you are building this today, the recommended technology stack is:
- Data Manipulation:
Pandas / Polars
- Hierarchy Processing:
hierarchicalforecast (Nixtla library) - Industry standard.
- Base Models:
StatsForecast (for ARIMA/ETS speed) or MLForecast (for LightGBM/XGBoost).
- Reconciliation:
hierarchicalforecast methods (MinT, BottomUp).
- Visualization:
Plotly (for interactive sunburst charts to visualize aggregation).
Example Workflow Logic:
- Ingest Data: Load (Date, SKU, Region, Sales).
- Define Hierarchy:
[["Region"], ["SKU"]].
- Aggregate: Generate the full
df containing levels Region, SKU, and Region/SKU.
- Model: Fit ETS to all series independently (parallelized).
- Get Residuals: Calculate the covariance matrix (W) from the training residuals.
- Reconcile: Apply
MinT using the covariance matrix.
- Score: Calculate RMSSE on the test set.