Normality
In many cases, only the time series at the lowest level of the
hierarchies (bottom time series) are available. HierarchicalForecast
has tools to create time series for all hierarchies and also allows you
to calculate prediction intervals for all hierarchies. In this notebook
we will see how to do it.
Aggregate bottom time series
In this example we will use the Tourism dataset from the Forecasting: Principles and Practice book. The dataset only contains the time series at the lowest level, so we need to create the time series for all hierarchies.
Country | Region | State | Purpose | ds | y | |
---|---|---|---|---|---|---|
0 | Australia | Adelaide | South Australia | Business | 1998-01-01 | 135.077690 |
1 | Australia | Adelaide | South Australia | Business | 1998-04-01 | 109.987316 |
2 | Australia | Adelaide | South Australia | Business | 1998-07-01 | 166.034687 |
3 | Australia | Adelaide | South Australia | Business | 1998-10-01 | 127.160464 |
4 | Australia | Adelaide | South Australia | Business | 1999-01-01 | 137.448533 |
The dataset can be grouped in the following non-strictly hierarchical structure.
Using the
aggregate
function from HierarchicalForecast
we can generate: 1. Y_df
: the
hierarchical structured series 2. S_df
: the
aggregation constraings dataframe with 3. tags
: a list
with the ‘unique_ids’ conforming each aggregation level.
unique_id | ds | y | |
---|---|---|---|
0 | Australia | 1998-01-01 | 23182.197269 |
1 | Australia | 1998-04-01 | 20323.380067 |
2 | Australia | 1998-07-01 | 19826.640511 |
3 | Australia | 1998-10-01 | 20830.129891 |
4 | Australia | 1999-01-01 | 22087.353380 |
Australia/ACT/Canberra/Business | Australia/ACT/Canberra/Holiday | Australia/ACT/Canberra/Other | Australia/ACT/Canberra/Visiting | Australia/New South Wales/Blue Mountains/Business | |
---|---|---|---|---|---|
Australia | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 |
Australia/ACT | 1.0 | 1.0 | 1.0 | 1.0 | 0.0 |
Australia/New South Wales | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 |
Australia/Northern Territory | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 |
Australia/Queensland | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 |
We can visualize the S
matrix and the data using the
HierarchicalPlot
class as follows.
Split Train/Test sets
We use the final two years (8 quarters) as test set.
Computing base forecasts
The following cell computes the base forecasts for each time series
in Y_df
using the AutoARIMA
and model. Observe that Y_hat_df
contains the forecasts but they are not coherent. To reconcile the
prediction intervals we need to calculate the uncoherent intervals using
the level
argument of StatsForecast
.
Reconcile forecasts
The following cell makes the previous forecasts coherent using the
HierarchicalReconciliation
class. Since the hierarchy structure is not strict, we can’t use methods
such as
TopDown
or
MiddleOut
.
In this example we use
BottomUp
and
MinTrace
.
If you want to calculate prediction intervals, you have to use the
level
argument as follows.
The dataframe Y_rec_df
contains the reconciled forecasts.
ds | AutoARIMA | AutoARIMA-lo-90 | AutoARIMA-lo-80 | AutoARIMA-hi-80 | AutoARIMA-hi-90 | AutoARIMA/BottomUp | AutoARIMA/BottomUp-lo-90 | AutoARIMA/BottomUp-lo-80 | AutoARIMA/BottomUp-hi-80 | … | AutoARIMA/MinTrace_method-mint_shrink | AutoARIMA/MinTrace_method-mint_shrink-lo-90 | AutoARIMA/MinTrace_method-mint_shrink-lo-80 | AutoARIMA/MinTrace_method-mint_shrink-hi-80 | AutoARIMA/MinTrace_method-mint_shrink-hi-90 | AutoARIMA/MinTrace_method-ols | AutoARIMA/MinTrace_method-ols-lo-90 | AutoARIMA/MinTrace_method-ols-lo-80 | AutoARIMA/MinTrace_method-ols-hi-80 | AutoARIMA/MinTrace_method-ols-hi-90 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
unique_id | |||||||||||||||||||||
Australia | 2016-01-01 | 26212.554688 | 24694.224609 | 25029.580078 | 27395.527344 | 27730.884766 | 24368.099609 | 23674.076441 | 23827.366706 | 24908.832513 | … | 25205.749397 | 24453.417115 | 24619.586229 | 25791.912565 | 25958.081679 | 26059.047512 | 24978.608364 | 25217.247087 | 26900.847937 | 27139.486661 |
Australia | 2016-04-01 | 25033.667969 | 23324.066406 | 23701.669922 | 26365.666016 | 26743.269531 | 22395.921875 | 21629.482078 | 21798.767146 | 22993.076604 | … | 23720.833190 | 22915.772233 | 23093.587632 | 24348.078748 | 24525.894148 | 24769.464257 | 23554.946551 | 23823.199470 | 25715.729045 | 25983.981963 |
Australia | 2016-07-01 | 24507.027344 | 22625.500000 | 23041.076172 | 25972.978516 | 26388.554688 | 22004.169922 | 21182.945074 | 21364.330624 | 22644.009219 | … | 23167.123691 | 22316.298074 | 22504.221604 | 23830.025777 | 24017.949308 | 24205.855344 | 22870.661086 | 23165.568073 | 25246.142616 | 25541.049603 |
Australia | 2016-10-01 | 25598.929688 | 23559.919922 | 24010.281250 | 27187.578125 | 27637.937500 | 22325.056641 | 21456.892977 | 21648.645996 | 23001.467285 | … | 23982.251913 | 23087.313715 | 23284.980478 | 24679.523348 | 24877.190111 | 25271.861336 | 23825.782311 | 24145.180634 | 26398.542038 | 26717.940362 |
Australia | 2017-01-01 | 26982.578125 | 24651.535156 | 25166.396484 | 28798.757812 | 29313.619141 | 23258.001953 | 22296.178714 | 22508.618508 | 24007.385398 | … | 25002.243615 | 24016.747195 | 24234.415731 | 25770.071498 | 25987.740034 | 26611.143736 | 24959.636647 | 25324.408272 | 27897.879201 | 28262.650825 |
Plot forecasts
Then we can plot the probabilistic forecasts using the following function.
Plot single time series
Plot hierarchichally linked time series
References
- Hyndman, R.J., & Athanasopoulos, G. (2021). “Forecasting: principles and practice, 3rd edition: Chapter 11: Forecasting hierarchical and grouped series.”. OTexts: Melbourne, Australia. OTexts.com/fpp3 Accessed on July 2022.
- Shanika L. Wickramasuriya, George Athanasopoulos, and Rob J. Hyndman. Optimal forecast reconciliation for hierarchical and grouped time series through trace minimization.Journal of the American Statistical Association, 114(526):804–819, 2019. doi: 10.1080/01621459.2018.1448825. URL https://robjhyndman.com/publications/mint/.