Nixtla / hierarchicalforecast

Probabilistic Hierarchical forecasting šŸ‘‘ with statistical and econometric methods.
https://nixtlaverse.nixtla.io/hierarchicalforecast
Apache License 2.0
590 stars 76 forks source link

ENH: Decrease wall time of ``_get_PW_matrices`` for ``BottomUp`` and ``BottomUpSparse`` #276

Closed christophertitchen closed 4 months ago

christophertitchen commented 4 months ago

This PR significantly reduces the wall time and memory utilisation of bottom-up reconciliation for both dense and sparse "summing" matrices, $S$. The functionality provided by this package is helpful, but I found the reconcilers impractical to use for the large hierarchical and grouped structures that I encounter in my job, so my plan is to create a few PRs to improve the performance and functionality where possible for this use case of mine in particular.

Note: $P$ refers to the matrix that maps the base forecasts to the bottom level, such that $\tilde{y}_{h} = SP\hat{y}_{h}$. I was originally a bit confused by the notation in this package because as these are linear methods, $P$ would usually represent the projection matrix which contains the leverages and map the base forecasts onto the coherent subspace, which in this case is actually $SP$. I usually see this matrix referred to as $G$ in the literature, but I know Hyndman is doing some work on reconciliation notation so perhaps this is something to be standardised in the future. šŸ˜Š

This improvement is achieved through the following modifications to the _get_PW_matrices method for BottomUp and BottomUpSparse:

These changes effectively deprecates the idx_bottom parameter for BottomUp and BottomUpSparse, which although used in the following situations, does not change the output assuming $S$ is standard:

The benchmarks for _get_PW_matrices across a few popular hierarchical datasets of different sizes are below, measured on a machine with 6/12 x 2.6 GHz (dynamic frequency scaling to 4.5 GHz) cores and 16 GB RAM.

Reconciler Dataset $\textup{dim} \left ( P \right )$ Current PR Wall Time Reduction
BottomUp Labour $\left( 32 \times 57 \right)$ $10.7\, \mu s$ $2.18\, \mu s$ $79.6 \%$
BottomUp Wiki2 $\left( 150 \times 199 \right)$ $47.3\, \mu s$ $6.57\, \mu s$ $86.1 \%$
BottomUp TourismLarge $\left( 304 \times 555 \right)$ $171\, \mu s$ $19.6\, \mu s$ $88.5 \%$
BottomUp M5 $\left( 30\, 490 \times 42\, 840 \right)$ $12.1\, s$ $66\, ms$ $99.5 \%$
BottomUpSparse Labour $\left( 32 \times 57 \right)$ $601\, \mu s$ $189\, \mu s$ $68.6 \%$
BottomUpSparse Wiki2 $\left( 150 \times 199 \right)$ $1.68\, ms$ $192\, \mu s$ $88.6 \%$
BottomUpSparse TourismLarge $\left( 304 \times 555 \right)$ $4.83\, ms$ $198\, \mu s$ $95.9 \%$
BottomUpSparse M5 $\left( 30\, 490 \times 42\, 840 \right)$ $41\, s$ $578\, \mu s$ $100 \%$
review-notebook-app[bot] commented 4 months ago

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

CLAassistant commented 4 months ago

CLA assistant check
All committers have signed the CLA.

elephaint commented 4 months ago

@jmoralez Happy to review but I suggest to first merge #264?