{'fraction_non_alphanumeric': 0.035462508011108736, 'fraction_numerical': 0.008502456740012819, 'mean_word_length': 4.3328776486671225, 'pattern_counts': {'":': 0, '<': 1, '<?xml version=': 0, '>': 4, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Reconciling Hierarchical Forecasts via Bayes' Rule

We present a novel approach for reconciling hierarchical forecasts, based on Bayes rule. We define a prior distribution for the bottom time series of the hierarchy, based on the bottom base forecasts. Then we update their distribution via Bayes rule, based on the base forecasts for the upper time series. Under the Gaussian assumption, we derive the updating in closed-form. We derive two algorithms, which differ as for the assumed independencies. We discuss their relation with the MinT reconciliation algorithm and with the Kalman filter, and we compare them experimentally.


Introduction
Often time series are organized into a hierarchy. For example, the total visitors of a country can be divided into regions and the visitors of each region can be further divided into sub-regions. The most disaggregated time series of the hierarchy are referred to as bottom time series, while the remaining time series are referred to as upper time series.
Forecasts of hierarchical time series should be coherent ; for instance, the sum of the forecasts of the different regions should equal the forecast for the total. The forecasts are incoherent if they do not satisfy such constraints. A simple way for generating coherent forecasts is bottom-up: one takes the forecasts for the bottom time series and sums them up according to the summing constraints in order to produce forecasts for the entire hierarchy. Yet this approach does not consider the forecasts produced for the upper time series, which contain useful information. For instance, upper time series are smoother and allow to better estimate of the seasonal patterns and the effect of the covariates.
Thus, modern reconciliation methods [9,18] proceed in two steps. First, base forecasts are computed by fitting an independent model to each time series. Then, the base forecasts are adjusted to become coherent; this step is called reconciliation. The forecasts for the entire hierarchy are then obtained by summing up the reconciled bottom time series. Reconciled forecasts are generally more accurate than the base forecasts, as they benefit from information coming from multiple time series. The state-of-the art reconciliation algorithm is MinT [18], which minimizes the mean squared error of the reconciled forecasts by solving a generalized least squares problem; its point forecast, besides being coherent, are generally more accurate than the base forecast.
Hierarchical probabilistic forecasting is however still an open area of research. The algorithm by [17] constructs a coherent forecast in a bottom-up fashion, modelling via copulas the joint distribution of the bottom time series, while [13] proposes a top-down approach, where the top time series is forecasted and then disaggregated. Both algorithms are based on numerical procedures which have no closed-form solution; hence they are not easily interpretable. In [6], a geometric interpretation of the reconciliation process is provided. It is moreover shown that the log score is not proper with respect to incoherent probabilistic forecasts. As an alternative, the energy score can be used for comparing reconciled to unreconciled probabilistic hierarchical forecasts. In [1] multivariate Gaussian predictive densities and bootstrap densities are experimentally compared for hierarchical probabilistic forecasting.
We address probabilistic reconciliation using Bayes' rule. We define the prior beliefs about the bottom time series, based on the base forecasts for the bottom time series. We then update them incorporating the information contained in the forecasts for the upper time series. Under the Gaussian assumption, we compute the update in closed form, obtaining the posterior distribution about the bottom time series and then about the entire hierarchy. Our reconciled forecasts minimize the mean squared error; indeed, we prove that they match the point predictions of MinT, whose optimality has been proven in a frequentist way. Our algorithm provides the joint predictive distribution for the hierarchy; thus we call it pMinT, which stands for probabilistic MinT. We also provide a variant of pMinT, obtained by making an additional independence assumption; we call it LG, as it is related to the linear-Gaussian model [3,Chap.8.1.4].
We show a link between the reconciliation problem and the Kalman filter, opening the possibility of borrowing from the literature of the Kalman filter for future research. We then compare the algorithms on synthetic and real data sets, eventually concluding that pMinT yields more accurate probabilistic forecasts than both bottom-up and LG.
The paper is organized as follows: we introduce the reconciliation problem in Section 2, we discuss the algorithms in Sec. 3, we discuss the reconciliation of a simple hierarchy in Section 4 and we present the experiments in Section 5. Fig. 1 shows a hierarchy. We could interpret it as the visitors of a country, which are disaggregated first by region (R 1 , R 2 ) and then by sub-regions (R 11 , R 12 , R 21 , R 22 ). The most disaggregated time series (bottom time series) are shaded. The hierarchy contains m time series, of which n are bottom time series. We denote by uppercase letters the random variables and by lowercase letters their observations. The vector of observations available at time t for the entire hierarchy is y t ∈ R m ; they are observations from the set of random variables Y t . Vector y t can be broken down in two parts, namely

Time series reconciliation
The structure of the hierarchy is represented by the summing matrix S ∈ R m×n such that: The S matrix of hierarchy in Fig.1 is: where the sub-matrix A ∈ R (m−n)×n encodes which bottom time series should be summed up in order to obtain each upper time series. We denote by y t+h ∈ R m the base forecasts issued at time t about of y and referring to h steps ahead. We separate base forecasts for bottom time series ( b t+h ∈ R n ) and upper time series ( u t+h ∈ R m−n ), namely y t+h = [ u T t+h , b T t+h ] T . The variances of the error of the base forecasts will be used later. If forecasts for different time horizons are needed (e.g., h=1,2,3,..), the reconciliation is performed independently for each h. In the following we generically assume to reconcile the forecasts for h steps ahead.
The MinT reconciliation Most reconciliation algorithms [9], including MinT [18], assume the reconciled bottom forecasts ( b t+h ) to be a linear combination of the base forecasts ( y t+h ) available for the whole hierarchy, i.e. their objective is to find a matrix P h ∈ R n×m such that: Let us denote by E t+h = Y t+h − y t+h ∈ R m the vector of the errors of the base forecast h-steps ahead and by W h = E[ E t+h E T t+h | I t ] their covariance matrix, where I t denotes all the information available up to time t. In [18] it is proven that the reconciliation matrix given by: is optimal, in the sense that it minimizes the trace of the reconciliation errors' covariance matrix. The reconciled forecasts for the whole hierarchy are obtained by summing the reconciled bottom forecasts, and they are proven to minimize the mean squared error over the entire hierarchy.
Estimation of W h Estimating W h differently for each h is an open problem. For the case h=1 the estimation is simpler. The variance of the forecasts equals the variance of the residuals (i.e., the errors on 1-step predictions made on the training data) and cross-covariances are estimated as the covariance of the residuals. The best estimates are obtained [18] by shrinking the full covariance matrix towards a diagonal matrix, using the method of [15]. The case h >1 is instead problematic. The variance of the forecasts are obtained by increasing the 1-step variance through analytical formulas, which differ from the variance of the h-steps ahead residuals. Morevoer, the covariances in W h have to be numerically estimated by looking at the h-steps residuals. However, the number of h-steps residuals decreases with h, making the estimate more noisy.
As a workaround, [18] assumes W h = k h W 1 , where k h > 0 is an unknown constant which depends on h while W 1 is the covariance matrix of the onestep ahead errors. The underlying assumption is thus that all terms within the variance/covariance matrix of the errors grow in the same way with h. The advantage of this approach is that k h cancels out when computing the reconciled forecasts, as it can be seen by setting W h = k h W 1 in Eq.(4). Yet, k h appears in the expression of the variance of the reconciled forecasts. In the following we refer to the assumption W h = k h W 1 as "the k h assumption".

Probabilistic Reconciliation
We address the reconciliation problem by merging the probabilistic information contained in the base forecasts for the bottom and the upper time series. We perform the fusion using Bayes' rule.
We first define the prior about the bottom time series. We have observed the time series up to time t and we are interested in the reconciled forecasts for time t + h. We denote by B t+h the vector of the bottom time series at time t + h; this is thus a vector of random variables and B i t+h represents its ith element. We moreover denote by b t+h the vector of base forecasts for the bottom time series for time t + h, and by b t+h the actual observation of the bottom random variables at time t + h. Finally, I t,b is the information available up to time t regarding the bottom time series, i.e. the past values of the bottom time series: In the following we adopt the k h assumption for all the covariance matrices assuming moreover that, for a given h, the value of k h is shared among all the involved covariance matrices. As we will show later, this is equivalent to the k h assumption made by MinT. Let us hence denote the covariance matrix of the forecast h-steps ahead by Σ B,h = k h Σ B,1 . Assuming the bottom time series to be jointly Gaussian we have: Probabilistic bottom-up If we have no information about the upper time series, we can build a joint predictive distribution for the entire hierarchy by summing the bottom forecast via matrix S: which is a probabilistic bottom-up reconciliation. Note that, in this case, k h appears only in the expression of the variance.
Updating If the forecasts U t+h about the upper time series are available, then we can use them in order to update our prior. We assume: where Σ U,h = k h Σ U,1 is the covariance of the noise. We thus treat U t+h as a set of different sums of the future values of the bottom time series, corrupted by noise. Hence: The posterior distribution of the bottom time series is given by Bayes' rule:

Computing Bayes' rule
The posterior of Eq. (9) can be computed in closed form by assuming the vector (B t+h , U t+h ) to be jointly Gaussian distributed. The linear-Gaussian (LG) model [3,Chap.8.1.4]. computes analytically the updating by further assuming ε u t+h to be independent from B t+h . Yet this independence might not always hold in our case. Consider for instance a special event driving upwards most time series. As a result we would observe both high values of B t+h and negative values of ε u t+h , due to the underestimation of the upper time series. This would result in a correlation between B t+h and ε u t+h . We thus generalize the LG model by accounting for such correlation. We will later compare experimentally the results obtained adopting the LG model and its generalized version. We denote Cov Our first step for computing Bayes' rule is to express the joint distribution p(B t+h , U t+h | I t,b ). Since U t+h = AB t+h + ε u t+h , the expected values are: We now derive the different blocks of the covariance matrix. The crosscovariance between B t+h and U t+h is: where k h > 0 is the multiplicative constant (Sec.2) that yields the variance of the forecasts h-steps ahead, given the covariances of the forecasts 1-step ahead.
The covariance of the upper forecasts is: Hence the joint prior (i.e., before observing U t+h ) is: . Now we receive the forecast u t+h for the upper time series (recall that U t+h denotes the random variables while u t+h denotes observations). We obtain the posterior distribution for the bottom time series P (B t+h | u t+h ) by applying the standard formulas for the conditional distribution of a MVN distribution [12,Sec.4.3.1]. To have a shorter notation, let us define: where k h disappears from the expression of G. The reconciled bottom time series have then the following mean and variance: Thus the adjustment applied to the base forecasts is proportional to ( u t+h − A b t+h ), i.e. the difference between the prior mean and the uncertain observation (i.e., the forecasts) of the upper time series. The term ( u t+h − A b t+h ) is called the incoherence of the base forecasts in [18]. The mean of the reconciled bottom time series does not depend on k h , while the variance does.
The reconciled point forecast and the covariance for the entire hierarchy are:

Related works and optimality of the reconciliation
Bayes' rule is a well-known tool for information fusion [5, Sec. 2], and we apply it for the first time for forecast reconciliation. We will later prove that the posterior mean of our approach yields the same point predictions of MinT. Yet, our algorithm additionally provides the predictive distribution for the entire hierarchy; we thus call it pMinT, where p stands for probabilistic. We also contribute a novel reconciliation approach based on the linear-Gaussian (LG) model, which is obtained by setting M = 0 in the definition of G. Both pMinT and LG are thus probabilistic reconciliation algorithms.
We point out for the first time a link between the reconciliation problem and the Kalman filter, whose state-update equation can be derived from the linear-Gaussian model [14]. In particular, Equation (11)  The optimality of our approach can be informally proven by considering that it yields the posterior mean of the reconciled forecasts, which is the minimizer of the quadratic loss under the Gaussian assumption [12,Chap. 5.7]. Moreover, it has the same equation of the state-update step of the Kalman filter, which provably minimizes the mean squared error of the estimates without any distributional assumption [16,Chap.5]. In Sec. (4.1) we will moreover show that our point predictions correspond to those of MinT, which have been proven to be the minimizer of the mean-squared error.

The covariance matrices
where B 1:t = b 1:t denotes a realization b 1:t of B 1:t . Yet we only have one observation of B i t+1 , B j t+1 conditional on I t,b , which prevents estimating the covariance. We can overcome the problem with the following result, which shows that we can approximate Σ B,1 by computing the covariance of the residuals.
Let us consider the vectors of bottom time series B 1 , . . . , B t and the con- Note that B t+1 is a random vector as we have not yet observed B i , i = 1, . . . , t. In this section we show where E i t+1 := B i t+1 − B i t+1 , i = 1, . . . , n denotes the residual of the model fitted on the i-th time series, for the forecast horizon t + 1. If we observe I t,b , then we can approximate Cov , the covariance of the residuals of the models fitted on the bottom time series.
Consider now the conditional covariance on the left side of eq. (15), we have By taking the expectation on both sides we obtain We thus estimate the covariance matrix Σ B,1 using the covariance of the residuals of the models fitted on the bottom time series.

Reconciliation of a simple hierarchy
We now illustrate how the base forecasts interact during the reconciliation of a simple hierarchy. We consider a hierarchy constituted by two bottom time series (B 1 and B 2 ) and an upper time series U .
where for simplicity we remove the forecast horizon (t + h) from the notation. The summing matrix is: We start considering the simpler case of reconciliation via the LG algorithm. The matrix G is: since A = [1 1], Σ U,1 = σ 2 u and moreover: Note that G does not depend on h, as also shown in Eq. (10). The reconciled bottom forecasts are: where u is the base forecast for U .
The reconciled bottom forecast can be written as: Eq. (17) shows that the adjustment applied to the base forecasts depends on σ 2 u . If σ 2 u is large the adjustment is small, since the upper forecast is not informative. If on the contrary σ 2 u = 0, the sum of the reconciled bottom forecasts is forced to matchû, i.e.,b 1 +b 2 =û (this can be shown by re-working Eq. (17)).
We now show that the reconciled bottom forecast are a linear combination of the base forecasts. Let us define: After some algebra we obtain: Thus b 1 is a weighted average of two estimates: b 1 and ( u − b 2 ); the weight of b 1 decreases with σ 2 1 and increases with (σ 2 2 + σ 2 u ). The reconciliation carried out by pMinT is similar to what already discussed, once we adopt g * 1 and g * 2 in place of g 1 and g 2 : Thus pMinT accounts also for the cross-covariances σ u,1 , σ u,2 between the bottom time series and of noise affecting the forecasts for the upper time series.

Relationship with MinT
Our reconciled bottom time series can be written as: The matrix P h of pMinT is thus: Proposition 1. The matrices P h of MinT and pMinT are equivalent. The proof is given in the supplementary material.

Experiments
In this paper we take a probabilistic point of view; we thus assess the reconciled predictive distributions rather than the point forecasts. Our metric is the energy score (ES), a scoring rule for multivariate distributions [7]. The ES is the multivariate generalization of the continuous ranked probability score (CRPS), which is obtained by integrating the Brier score over the predictive distribution of the forecast [7]. Let y be the actual multivariate observation, and let us assume that we have k samples x 1 , x 2 , . . . , x k , from the multivariate predictive distribution F . The energy score is: The energy score is a loss function: the lower, the better. We consider three methods for probabilistic reconciliation: probabilistic bottom-up (BU), LG and pMinT. We did not find any package implementing the algorithms of [17,13]; thus we did not include them in our comparison. We estimate all the covariance matrices via the shrinkage estimator [15].
Base forecasts We consider two time series models to compute the base forecasts. The first is ets, which fits different exponential smoothing variants and eventually performs model selection via AICc. The second method is auto.arima. It first decides how to differentiate the time series to make it stationary; then, it looks for the best arma model which fits the stationary time series, performing model selection via AICc. Both approaches performed well in forecasting competitions; they are available from the forecast package [10] for R. In all simulations, we compute forecasts up to h=4. As no particular pattern exists in the relative performance of the methods as h varies, we present the results averaged over h=1,2,3,4.
Setting k h We are unaware of previous studies on how to set k h . In this paper we compare two heuristics, acknowledging that this remains an open problem. The two options are k h =h and k h =1. The choice k h =h is based on the following approximation. The variance of y t+1 around y t+1 is σ 2 ; assuming the independence of the errors, the variance of y t+1 around y t+h is hσ 2 . The approximation lies in the fact that we are modeling the variance of y t+1 (not y t+h ) around y t+h . Instead, the option k h = 1 keeps the variance fixed with h. This represents the short-term behavior of models which contain only seasonal terms and no autoregressive terms. For instance, when dealing with a monthly time series, the variance of such models is constant up to h=12.
Code The code of our experiments is available at: https://github.com/iamthejao/BayesianReconciliat

Synthetic data
We generate synthetic data sets using the hierarchy above, previously considered in the experiments of [18]. We simulate the four bottom time series as AR (1)  Since the time series are stationary, they basically fluctuate around their mean. In this case the magnitude of the incoherence is generally limited, allowing also the bottom-up reconciliation to be competitive. The ES of both pMinT and LG is on average 1.5% smaller than that of BU. We also note an advantage of LG over pMinT for small T , and instead the reverse for large T ; this might be the effect of the additional covariances estimated by pMinT (see σ u,1 and σ u,2 in Sec.4).

Experiments with real data sets
We consider two hierarchical time series: infantgts and tourism. Both are grouped time series, which is a generalization of hierarchical time series. In particular the time series of a given level are always sums of some bottom time series, but they are not necessarily sums of time series of the adjacent lower level. We call setup the combination of a data set and a forecasting method, such as <infangts,arima >. The pMinT algorithm yields the lowest energy score in most setups; in the next section we check whether the differences between methods are significant.
Statistical analysis For each setup we perform a significance tests for each pair of algorithms (pMinT vs BU, LG vs BU and pMinT vs LG), using the Bayesian signed-rank test [2], which returns the posterior probability of a method having lower median energy score than another (Tab. 3). Such posterior probabilities are numerically equivalent to (1p-value), where p-value is the p-value of the one-sided frequentist signed-rank test.
In most setups (Tab. 3) high posterior probabilities (implying low p-values) support the hypothesis of the pMinT having lower energy score than both BU and LG; moreover they also support the hypothesis of LG having lower energy score than BU. Meta-analysis We now perform a meta-analysis for for each pair of algorithms across the different setups, adopting the Poisson-binomial approach [11,4]. Consider for instance pMinT vs BU. We model each setup as a Bernoulli trial, whose possible outcomes are the victory of pMinT or BU. The probability of pMinT winning is taken for each setup from Tab. 3 (the probability of BU winning is just its complement to 1). We then repeat 10,000 simulations, in which we draw the outcome of each setup according to the probabilities of Tab. 3. We now report the probability of each method outperforming another method in more than half the setups, based on out of 10,000 simulations. Both pMinT and LG wins in more than half the setup with probability 1 against BU. Moreover, there is 0.85 probability of pMinT winning in more than half of the setups against LG. We thus recommend pMinT as a general default method for probabilistic reconciliation.

Conclusions
We have derived two algorithms (pMinT and LG) based on Bayes' rule for probabilistic reconciliation. We have also shown a didactic example which clarifies how base forecast and their variances interact during the reconciliation, In general pMinT yields better predictive distributions and thus we recommend it as a default. The LG method can be anyway an interesting alternative when dealing with small sample sizes. Future research could borrow ideas from the extensive literature of the Kalman filter, based on the link we pointed out between reconciliation and Kalman filter