<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Adrien on Medium]]></title>
        <description><![CDATA[Stories by Adrien on Medium]]></description>
        <link>https://medium.com/@adeforceville_96412?source=rss-5d9aada08fda------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*qOp2us-3ccqXzaOw0lJDfA.jpeg</url>
            <title>Stories by Adrien on Medium</title>
            <link>https://medium.com/@adeforceville_96412?source=rss-5d9aada08fda------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 10:17:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@adeforceville_96412/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Hierarchical TimeSeries Reconciliation]]></title>
            <link>https://medium.com/@adeforceville_96412/hierarchical-timeseries-reconciliation-58addce2aeb7?source=rss-5d9aada08fda------2</link>
            <guid isPermaLink="false">https://medium.com/p/58addce2aeb7</guid>
            <category><![CDATA[hierarchical]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[reconciliation]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[time-series-forecasting]]></category>
            <dc:creator><![CDATA[Adrien]]></dc:creator>
            <pubDate>Wed, 01 Sep 2021 03:37:38 GMT</pubDate>
            <atom:updated>2021-09-10T10:14:15.331Z</atom:updated>
            <content:encoded><![CDATA[<p>This article offers an insight into <em>state-of-the-art</em> methods for reconciling, point-wise and probabilistic-wise, hierarchical time series (HTS). In addition, a python package for HTS reconciliation is introduced by illustrating the interest of reconciliation on the original dataset from 2020 M5 forecast competition that challenged top researchers worldwide to predict Walmart sales from their authentic data.</p><p>Table of contents</p><ol><li>What are hierarchical time series ?</li><li>What is reconciliation ?</li><li>Overview of state-of-the-art methods</li><li>Introduction to <strong><em>reconciliation_hts</em></strong></li><li>Complete example with M5 Walmart datatset</li></ol><h3>What are hierarchical time series ?</h3><p>Hierarchical time series(HTS) are a set of time series that are linked by a hierarchical structure. This means that we can represent this set of time series with a tree structure, where one node is a time series and whose leafs are time series themselves :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/238/1*r7tbnz6YJZpNaib3kM_CEQ.png" /><figcaption>Hierarchical Time Series with 7 time series</figcaption></figure><p>We generally assume that all the time series follow the same temporality.</p><p>HTS are found in all industries, and their analysis give <strong><em>valuable insight</em></strong> regarding business /operational matters.</p><ol><li><strong>Supply chain:</strong> Forecasting the demand across all hierarchical levels is paramount for optimal supply anticipation and effectively managing the shortage/overstock risk. Typical example : in retail, you have time series at various hierarchical levels ( Total sales, sales per state, sales per store, sales per category of product etc.).</li><li><strong>Yield management: </strong>Having an accurate insight into the upcoming demand in service or product helps for dynamic pricing purposes. Typical examples: a hotels group would have time series predicting the attendance at various levels ( worldwide, country-wide, region-wise, hotel-wise).</li><li><strong>Operational maters:</strong> Forecasting accurately the demand across all hierarchical levels is useful for anticipating needs such as workforce and equipment. Typical example : in the transportation industry, companies such as public transport operators within a city, have to adapt the frequency of bus/train/subway according to the demand throughout all hierarchical levels (city as a whole, district, line, station).</li></ol><p>This valuable insight can be provided with forecasting techniques, also know as time series analysis, a field that gathers <strong>statistical and machine learning models. </strong>You will <a href="https://otexts.com/fpp3/index.html">find here a remarkable overview of methods</a> for time series analysis and forecasting.</p><h3>What is reconciliation ?</h3><p>We are at this point : we have a set of time series linked by a hierarchical structure, and for each one of these time series we have computed a model for time series forecasting. The models do not have to be similar, each was just chosen according to the best results it yielded in forecasting the time series it is associated with. Hence, we gather forecasts that were generated independently, but we generally face the following issue : the forecasts are <em>incoherent</em><strong><em>. </em></strong>This means that the sum of the forecasts of the leaves of a given node is not equal to the forecast generated by the model associated to the node.</p><p>For example, if the forecast AA + forecast AB from the example tree structure above are not equal to the forecast A, then the forecasts generated by the models are <em>incoherent. </em>On the one hand, it highlights the bias of our models, on the other hand, from a business perspective, this can be embarrassing : how come you predict 5 shoes sale for store A and 4 shoes sale for store B, and tell me that you forecast 10 shoes sale in total ?</p><p>We can attempt to overcome this biais by exploiting the information contained in the tree structure, and here comes the field of <strong>reconciliation</strong>.</p><p><strong>HTS reconciliation is a field of study that aims at readjusting the forecasts yielded by independent models on a set of hierarchically-linked time series, in order to improve the forecasts and ensure that they are coherent and sum up correctly.</strong></p><p>Reconciliation is a tool that comes after the forecasts process, and slightly modifies the output of your statistical or machine learning models.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d1O784kU-udEQXCfHEFfLw.png" /><figcaption>Understanding when reconciliation intervenes. Forecast here is done for time t</figcaption></figure><h3>Mathematical framework</h3><p><em>Base forecasts </em><strong>Ỹ </strong><em>: </em>The vector of forecasts yielded by the statistical/machine learning models ( step 1 in image above).</p><p><em>Reconciled forecasts</em> <strong>Ŷ </strong>: The new vector of forecast after the reconciliation step.</p><p><em>Summing matrix </em><strong><em>S</em></strong> : The matrix that characterizes the hierarchical structure. Multiplying a vector of forecasts by <strong>S</strong> guarantees that the new vector of forecast is <em>coherent</em>.</p><p><em>Reconciliation matrix</em> <strong>G</strong> : The whole reconciliation process comes down to determining the right <strong>G</strong> matrix.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pIp7WnFw5iSzKW1yXFL2MA.png" /></figure><h3>Overview of methods (including state-of-the-art)</h3><ol><li><strong>Bottom-up</strong> : This reconciliation method only sums up the bottom forecasts ( AA/AB/BA/BB) altogether in order to compute the higher levels forecasts. You will find the complete mathematical details of the method <a href="https://otexts.com/fpp3/single-level.html#the-bottom-up-approach">right here</a>.</li><li><strong>Top-Down </strong>: This method uses only the time series at the top of the tree and aims at computing how to split recursively the forecast while you go downwards in the hierarchy. You will find the complete mathematical details of the method <a href="https://otexts.com/fpp3/single-level.html#top-down-approaches">right here</a>.</li><li><strong>Middle-out</strong> : This method combines both Bottom-up and Top-Down approaches in order. You will find the complete mathematical details of the method <a href="https://otexts.com/fpp3/single-level.html#middle-out-approach">right here</a>.</li><li><strong>MinT</strong> methods : These are a set of methods that were developed in in the years 2018–2020. Assuming that our base forecasts are unbiaised, this paper proved that in order to minimize the variance of the error of our reconciled forecasts (and hence recover better forecasts), the matrix G must be the following :</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/453/1*RYixhbPCnJ7MB8gLPv1GcQ.png" /></figure><p>Where W is the covariance matrix of the error of the bases forecasts. Having said that, estimating the matrix W is quite demanding. Hence, various approximations exist : <strong>OLS / Structural Scaling / Variance Scaling / MinTSample / MinTShrink</strong></p><p>You can find the mathematical details of these methods <a href="https://otexts.com/fpp3/reconciliation.html#the-mint-optimal-reconciliation-approach">right here</a>.</p><p>At that point, high level implementation of these methods can be found implemented in R, with the package <a href="https://github.com/tidyverts/fabletools"><em>fabletools</em></a> (part of CRAN). You can find the documentation <a href="https://cran.r-project.org/web/packages/fabletools/fabletools.pdf">right here</a>.</p><p>However, if you want to try reconciliation with Python, <a href="https://scikit-hts.readthedocs.io/en/latest/"><em>scikit-hts</em></a> is a package easy to handle with a scikit-learn familiar API. It allows you do to reconciliation of hierarchical time series. However, there are two major drawbacks with this package. First, it does not dissociate the prediction step from the reconciliation step. You have to choose a predictor from a short-list of predictors (auto ARIMA, SARIMAX, Facebook Prophet and Holt-Winter’s model). Hence, if you have a specific or complex dataset for which you want to develop your own predictors ( stacked predictors, neural networks, gradient boosting -based etc.), then you can not try reconciliation. Secondly, it only implements a limited number of reconciliation methods, excluding the MinTSample and MinTShrink that are the state-of-the-art methods today.</p><p>For this reason, a new Python package was developed in order to fill this gap : <strong>reconciliation-hts.</strong></p><h3>Introduction to reconciliation_hts</h3><p>reconciliation_hts is an open-source python package still being improved that enables the following :</p><ol><li><strong>Implement exhaustively all top reconciliation methods</strong> for hierarchical/grouped time series, whatever the prediction models developed are.</li><li><strong>Assess quickly if reconciliation improves your predictions</strong>, and identify which method is more suited</li><li><strong>Plot the newly reconciled forecasts</strong></li><li><strong>Compute the prediction intervals</strong> with a chosen level of confidence</li></ol><h3>Full example with M5 competition Walmart dataset</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*dCadRX7yjsZJkzTt" /></figure><p>We are going to demonstrate the assets of this package by using the original dataset of M5 forecasting competition based on Walmart historic of sales. Details of the competition and the dataset can be found <a href="https://mofc.unic.ac.cy/m5-competition/">right here</a>.</p><p>We will be using the <a href="https://www.kaggle.com/c/m5-forecasting-accuracy/data">sales_train_evaluation.csv dataset</a> in our example. We choose to keep all hierarchical levels but the last one, at the bottom. Indeed, this level is quite specific because is it the product-level with <strong>sparse time series. </strong>Sparse time series is a technical field in time series forecasting, and we would rather avoid this part in this article in order to focus on the reconciliation process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/950/1*2X8OArm-ZDUAdGT4l9Xqtw.png" /></figure><p>Eventually we gather :</p><ol><li>114 time series throughout our structure</li><li>70 bottom-series</li><li>A five-level hierarchy : Total sales, state sales, store sales, product category per store sales and product sub-category per store sales.</li></ol><p>Now let’s tackle the preprocessing step :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f594edb25ae4bf267008d157951bd2d6/href">https://medium.com/media/f594edb25ae4bf267008d157951bd2d6/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*isFpwcOr4x7WVO9NZytSBw.png" /></figure><p>We now have a dataset with the 70 bottom-level time series. We now want to compute the aggregated time series for the superior hierarchical levels :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1fcc7af08bc769c70378ecc19ecbd63e/href">https://medium.com/media/1fcc7af08bc769c70378ecc19ecbd63e/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KJAdaf2P_xvTQb75LmRj9Q.png" /></figure><p>Now, we have a dataset with all the 114 time series, and the we can identify which series each row corresponds to thanks to the value of the [‘state_id’ , ‘store_id’ , ‘cat_id’ , ‘dept_id’].</p><p>For the second row, ‘state_id’ = CA , ‘store_id’ = NaN , ‘cat_id’ = NaN and ‘dept_id’ = NaN. This means that the second row is the total sales in the state CA (California).</p><p>For the fith row, ‘state_id’ = CA , ‘store_id’ = CA_1, ‘cat_id’ = NaN and ‘dept_id’ = NaN. This means that the fith row is the total sales in the store CA_1 in the state CA(California).</p><p>Now, let’s understand how in theory we compute the error matrix needed for the top reconciliation methods :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nqasFfXt3w9grAdDrhGi7Q.png" /></figure><p>The key idea is that we want to to statistically estimate the residuals that our models yields, for all of the time series across the hierarchical structure. To be perfectly accurate, when you have a typical machine learning model, if you estimate the residuals on the train set, you underestimate them because of overfitting. Hence, you would need a <strong>calibration set</strong> in order to predict the outputs of your model on this set, and to have a genuine estimation of the residuals of your model on new data. However, when you use statistical models such as ARIMA or ETS, first you identify the best model (parameters p,q,d in ARIMA for instance), then you would fit the ARIMA model each time you make a prediction, so there is <strong>no need for a calibration set with statistical models.</strong></p><p>In our example, we choose to use the <a href="https://alkaline-ml.com/pmdarima/modules/generated/pmdarima.arima.auto_arima.html">auto_arima function</a> from pdmarima python package. Then, we fit the optimal parameters for our ARIMA model thanks to the auto_arima function on each of the 114 time-series, and we evaluate the residuals for each of these models. We choose to evaluate the residuals 100 times. We could have evaluate it on more data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*f2knbeiWgmQ6tGuH9aUG5Q.png" /></figure><p>Here is the code for generating the error matrix, the predictions and the real values. Let’s note that the choice for 100 estimation of the residuals and 100 vectors of predictions/reals values is a pure coïncidence and the choice is quite arbitrary (you want to estimate your matrix on as much data as you can, which we did not do here for computational reasons).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/42de1061fe1b0f889464c7d2ec8b984b/href">https://medium.com/media/42de1061fe1b0f889464c7d2ec8b984b/href</a></iframe><p>Now, we have our <em>base forecasts </em>ie. the forecasts yielded by our model. Now we are going to reconcile them and assess wether there is a reconciliation method that improves the performances of the forecasts.</p><p>In your terminal, run the following command :</p><pre>pip install hts_reconciliation</pre><p>Now, let’s reconcile !</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b94ad2b600a8f6d45fa93086857e6f04/href">https://medium.com/media/b94ad2b600a8f6d45fa93086857e6f04/href</a></iframe><p>We try reconciliation with one of the top state-of-the-art method : MinTSh. The method cross_val_score is meant to assess if reconciliation did improve our base forecasts.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*Nj-piJ5hxcWUc0KQXn0JYQ.png" /><figcaption>Output of the cross_val_score method</figcaption></figure><p>We can also choose to plot the real, predicted and reconciled values with the plot method, for the desired hierarchical level. Here we plot for the “total” level :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a29bb7d5ac1971122f45f63f1370dc0/href">https://medium.com/media/0a29bb7d5ac1971122f45f63f1370dc0/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MJsVf_qxnBDs_CB3q6zlRg.png" /></figure><p>Moreover, the packages has a method for probabilistic forecasting. This means that for all of the time series across the structure, you can compute the confidence intervals for a given level of confidence. The method is based on bootstrapping and Monte-Carlo simulations to have a sample of the forecasts distribution. Let’s assume that I want the confidence intervals for the first forecast on the test set (Day 1001) with a 95% level of confidence, the following line of code compute them :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6ca9267fbbb52e5acfe3eb5d99be5c62/href">https://medium.com/media/6ca9267fbbb52e5acfe3eb5d99be5c62/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/222/1*Dl28ri3oCEPwH1JbWUNAeA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=58addce2aeb7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>