FoReco
package for
cross-sectional, temporal and cross-temporal point forecast
reconciliationThe FoReco (Forecast Reconciliation) package is designed for point forecast reconciliation, a post-forecasting process aimed to improve the quality of the base forecasts for a system of linearly constrained (e.g. hierarchical/grouped) time series.
It offers classical (bottom-up and top-down), and modern (optimal and heuristic combination) forecast reconciliation procedures for cross-sectional, temporal, and cross-temporal linearly constrained time series.
The main functions are:
htsrec()
: cross-sectional (contemporaneous) forecast
reconciliation.thfrec()
: forecast reconciliation for a single time
series through temporal hierarchies.lccrec()
: level conditional forecast reconciliation for
genuine hierarchical/grouped time series.tdrec()
: top-down (cross-sectional, temporal,
cross-temporal) forecast reconciliation for genuine hierarchical/grouped
time series.ctbu()
: bottom-up cross-temporal forecast
reconciliation.tcsrec()
: heuristic first-temporal-then-cross-sectional
cross-temporal forecast reconciliation.cstrec()
: heuristic first-cross-sectional-then-temporal
cross-temporal forecast reconciliation.iterec()
: heuristic iterative cross-temporal forecast
reconciliation.octrec()
: optimal combination cross-temporal forecast
reconciliation.You can install the stable version on R CRAN.
install.packages('FoReco', dependencies = TRUE)
You can also install the development version from Github
# install.packages("devtools")
::install_github("daniGiro/FoReco") devtools
A two-level hierarchy with \(n = 8\) monthly time series. In the cross-sectional framework, at any time it is \(Tot = A + B + C\), \(A = AA + AB\) and \(B = BA + BB\) (\(nb = 5\) at the bottom level). For monthly data, the observations are aggregated to annual \((k = 12)\), semi-annual \((k = 6)\), four-monthly \((k = 4)\), quarterly \((k = 3)\), and bi-monthly \((k = 2)\) observations. The monthly bottom time series are simulated from five different SARIMA models. There are 180 monthly observations (15 years): the first 168 values (14 years) are used as training set, and the last 12 form the test set.
In the following script we simulate five independent monthly bottom time series, each of length 180 (15 complete years of monthly data).
library(FoReco)
library(forecast)
library(sarima)
<- NULL
values <- NULL
base <- NULL
residuals <- NULL
test
<- matrix(NA, nrow = 180, ncol = 5)
bottom # Model definition
<- list()
bts #ARIMA(1,0,0)(0,0,0)[12]
1]] <- list(ar=0.31,
bts[[nseasons=12)
#ARIMA(0,0,1)(0,0,0)[12]
2]] <- list(ma=0.61,
bts[[nseasons=12)
#ARIMA(0,1,1)(0,1,1)[12]
3]] <- list(ma=-0.1,
bts[[sma=-0.12,
iorder=1,
siorder=1,
nseasons=12)
#ARIMA(2,1,0)(0,0,0)[12]
4]] <- list(ar=c(0.38,0.25),
bts[[iorder=1,
nseasons=12)
#ARIMA(2,0,0)(0,1,1)[12]
5]] <- list(ar=c(0.30,0.12),
bts[[sma=0.23,
siorder=1,
nseasons=12)
<- c(58.85, 60.68, 59.26, 35.47, 58.61)
mm set.seed(525)
for(i in 1:5){
<- mm[i] + sim_sarima(n=180, model = bts[[i]],
bottom[,i] n.start = 200)
}colnames(bottom) <- c("AA", "AB", "BA", "BB", "C")
<- matrix(c(rep(1,5),
C rep(1,2), rep(0,3),
rep(0,2), rep(1,2), 0), byrow = TRUE, nrow = 3)
<- bottom%*%t(C)
upper colnames(upper) <- c("T", "A", "B")
$k1 <- ts(cbind(upper, bottom), frequency = 12)
valuescolnames(values$k1) <- c("T", "A", "B", "AA", "AB", "BA", "BB", "C")
More precisely, AA is simulated from an AR(1) process, AB from an MA(1), BA from an ARIMA(0,1,1)(0,1,1), BB from an ARIMA(2,1,0), and C from an ARIMA(2,0,0)(0,1,1). The higher levels series in the hierarchy (T,A,B) are obtained by simple summation of the five bottom time series.
Then we compute the temporally aggregated series at annual \((k = 12)\), semi-annual \((k = 6)\), four-monthly \((k = 4)\), quarterly \((k = 3)\), and bi-monthly \((k = 2)\) frequencies.
# BI-MONTHLY SERIES
$k2 <- ts(apply(values$k1, 2,
valuesfunction(x) colSums(matrix(x, nrow = 2))),
frequency = 6)
# QUARTERLY SERIES
$k3 <- ts(apply(values$k1, 2,
valuesfunction(x) colSums(matrix(x, nrow = 3))),
frequency = 4)
# FOUR-MONTHLY SERIES
$k4 <- ts(apply(values$k1, 2,
valuesfunction(x) colSums(matrix(x, nrow = 4))),
frequency = 3)
# SEMI-ANNUAL SERIES
$k6 <- ts(apply(values$k1, 2,
valuesfunction(x) colSums(matrix(x, nrow = 6))),
frequency = 2)
# ANNUAL SERIES
$k12 <- ts(apply(values$k1, 2,
valuesfunction(x) colSums(matrix(x, nrow = 12))),
frequency = 1)
The first 14 years of each simulated series are used as training set,
and the last year as test set. The forecasts are obtained using the
auto.arima
function of the forecast
package
(Hyndman et al., 2020).
# MONTHLY FORECASTS
$k1 <- matrix(NA, nrow = 12, ncol = ncol(values$k1))
base$k1 <- matrix(NA, nrow = 168, ncol = ncol(values$k1))
residualsfor (i in 1:ncol(values$k1)) {
<- values$k1[1:168, i]
train <- forecast(auto.arima(train), h = 12)
forecast_arima $k1[, i] <- forecast_arima$mean
base$k1[, i] <- forecast_arima$residuals
residuals
}$k1 <- ts(base$k1, frequency = 12, start = c(15, 1))
basecolnames(base$k1) <- c("T", "A", "B", "AA", "AB", "BA", "BB", "C")
$k1 <- ts(residuals$k1, frequency = 12)
residualscolnames(residuals$k1) <- c("T", "A", "B", "AA", "AB", "BA", "BB", "C")
$k1 <- values$k1[-c(1:168), ] test
The following plots show the actual values and the forecasts for the test year at any temporal aggregation level.
# BI-MONTHLY FORECASTS
$k2 <- matrix(NA, nrow = 6, ncol = ncol(values$k2))
base$k2 <- matrix(NA, nrow = 84, ncol = ncol(values$k2))
residualsfor (i in 1:ncol(values$k2)) {
<- values$k2[1:84, i]
train <- forecast(auto.arima(train), h = 6)
forecast_arima $k2[, i] <- forecast_arima$mean
base$k2[, i] <- forecast_arima$residuals
residuals
}$k2 <- ts(base$k2, frequency = 6, start = c(15, 1))
basecolnames(base$k2) <- c("T", "A", "B", "AA", "AB", "BA", "BB", "C")
$k2 <- ts(residuals$k2, frequency = 6)
residualscolnames(residuals$k2) <- c("T", "A", "B", "AA", "AB", "BA", "BB", "C")
$k2 <- values$k2[-c(1:84), ] test
# QUARTERLY FORECASTS
$k3 <- matrix(NA, nrow = 4, ncol = ncol(values$k3))
base$k3 <- matrix(NA, nrow = 56, ncol = ncol(values$k3))
residualsfor (i in 1:ncol(values$k3)) {
<- values$k3[1:56, i]
train <- forecast(auto.arima(train), h = 4)
forecast_arima $k3[, i] <- forecast_arima$mean
base$k3[, i] <- forecast_arima$residuals
residuals
}$k3 <- ts(base$k3, frequency = 4, start = c(15, 1))
basecolnames(base$k3) <- c("T", "A", "B", "AA", "AB", "BA", "BB", "C")
$k3 <- ts(residuals$k3, frequency = 4)
residualscolnames(residuals$k3) <- c("T", "A", "B", "AA", "AB", "BA", "BB", "C")
$k3 <- values$k3[-c(1:56), ] test
# FOUR-MONTHLY FORECASTS
$k4 <- matrix(NA, nrow = 3, ncol = ncol(values$k4))
base$k4 <- matrix(NA, nrow = 42, ncol = ncol(values$k4))
residualsfor (i in 1:ncol(values$k4)) {
<- values$k4[1:42, i]
train <- forecast(auto.arima(train), h = 3)
forecast_arima $k4[, i] <- forecast_arima$mean
base$k4[, i] <- forecast_arima$residuals
residuals
}$k4 <- ts(base$k4, frequency = 3, start = c(15, 1))
basecolnames(base$k4) <- c("T", "A", "B", "AA", "AB", "BA", "BB", "C")
$k4 <- ts(residuals$k4, frequency = 3)
residualscolnames(residuals$k4) <- c("T", "A", "B", "AA", "AB", "BA", "BB", "C")
$k4 <- values$k4[-c(1:42), ] test
# SEMI-ANNUAL FORECASTS
$k6 <- matrix(NA, nrow = 2, ncol = ncol(values$k6))
base$k6 <- matrix(NA, nrow = 28, ncol = ncol(values$k6))
residualsfor (i in 1:ncol(values$k6)) {
<- values$k6[1:28, i]
train <- forecast(auto.arima(train), h = 2)
forecast_arima $k6[, i] <- forecast_arima$mean
base$k6[, i] <- forecast_arima$residuals
residuals
}$k6 <- ts(base$k6, frequency = 2, start = c(15, 1))
basecolnames(base$k6) <- c("T", "A", "B", "AA", "AB", "BA", "BB", "C")
$k6 <- ts(residuals$k6, frequency = 2)
residualscolnames(residuals$k6) <- c("T", "A", "B", "AA", "AB", "BA", "BB", "C")
$k6 <- values$k6[-c(1:28), ] test
# ANNUAL FORECASTS
$k12 <- matrix(NA, nrow = 1, ncol = ncol(values$k12))
base$k12 <- matrix(NA, nrow = 14, ncol = ncol(values$k12))
residualsfor (i in 1:ncol(values$k12)) {
<- values$k12[1:14, i]
train <- forecast(auto.arima(train), h = 1)
forecast_arima $k12[, i] <- forecast_arima$mean
base$k12[, i] <- forecast_arima$residuals
residuals
}$k12 <- ts(base$k12, frequency = 1, start = c(15, 1))
basecolnames(base$k12) <- c("T", "A", "B", "AA", "AB", "BA", "BB", "C")
$k12 <- ts(residuals$k12, frequency = 1)
residualscolnames(residuals$k12) <- c("T", "A", "B", "AA", "AB", "BA", "BB", "C")
$k12 <- values$k12[-c(1:14), ] test
<- t(do.call(rbind, rev(base)))
base <- t(do.call(rbind, rev(residuals)))
res <- t(do.call(rbind, rev(test)))
test
<- c(12, 6, 4, 3, 2, 1)
kset <- 1
h colnames(base) <- paste("k", rep(kset, h * rev(kset)), "_h",
do.call("c", as.list(sapply(
rev(kset) * h,
function(x) seq(1:x)))),
sep = "")
colnames(test) <- paste("k", rep(kset, h * rev(kset)), "_h",
do.call("c", as.list(sapply(
rev(kset) * h,
function(x) seq(1:x)))),
sep = "")
<- 14
h colnames(res) <- paste("k", rep(kset, h * rev(kset)), "_h",
do.call("c", as.list(sapply(
rev(kset) * h,
function(x) seq(1:x)))),
sep = "")
colnames(C) <- c("AA", "AB", "BA", "BB", "C")
rownames(C) <- c("Tot", "A", "B")
<- values
obs <- list(base = base,
FoReco_data test = test,
res = res,
C = C,
obs = obs)
<- c(1,2,3,4,6,12)
K <- NULL
hts_recf_list for(i in 1:length(K)){
# base forecasts
<- which(simplify2array(strsplit(colnames(FoReco_data$base),
id split = "_"))[1, ] == paste("k", K[i], sep=""))
<- t(FoReco_data$base[, id])
mbase # residuals
<- which(simplify2array(strsplit(colnames(FoReco_data$res),
id split = "_"))[1, ] == "k1")
<- t(FoReco_data$res[, id])
mres <- htsrec(mbase, C = FoReco_data$C, comb = "shr",
hts_recf_list[[i]] res = mres, keep = "recf")
}names(hts_recf_list) <- paste("k", K, sep="")
<- t(do.call(rbind, hts_recf_list[rev(names(hts_recf_list))]))
hts_recf colnames(hts_recf) <- paste("k",
rep(K, sapply(hts_recf_list[rev(names(hts_recf_list))], NROW)),
colnames(hts_recf), sep="")
<- NROW(FoReco_data$base)
n <- matrix(NA, n, NCOL(FoReco_data$base))
thf_recf dimnames(thf_recf) <- dimnames(FoReco_data$base)
for(i in 1:n){
# ts base forecasts ([lowest_freq' ... highest_freq']')
<- FoReco_data$base[i, ]
tsbase # ts residuals ([lowest_freq' ... highest_freq']')
<- FoReco_data$res[i, ]
tsres <- thfrec(tsbase, m = 12, comb = "acov",
thf_recf[i,] res = tsres, keep = "recf")
}
<- tcsrec(FoReco_data$base, m = 12, C = FoReco_data$C,
tcs_recf thf_comb = "wlsv", hts_comb = "shr",
res = FoReco_data$res)$recf
<- cstrec(FoReco_data$base, m = 12, C = FoReco_data$C,
cst_recf thf_comb = "acov", hts_comb = "shr",
res = FoReco_data$res)$recf
<- iterec(FoReco_data$base,
ite_recf m = 12, C = FoReco_data$C,
thf_comb = "acov", hts_comb = "shr",
res = FoReco_data$res, start_rec = "thf")$recf
#> --------------------------------------------------------------------------------
#> Iter # | Cross-sec. incoherence | Temporal incoherence |
#> thf: 0 | 159.89 | 187.63 |
#> thf: 1 | 60.11 | 38.77 |
#> thf: 2 | 18.60 | 7.73 |
#> thf: 3 | 1.61 | 9.91e-01 |
#> thf: 4 | 4.16e-01 | 2.07e-01 |
#> thf: 5 | 3.03e-02 | 2.00e-02 |
#> thf: 6 | 9.18e-03 | 5.31e-03 |
#> thf: 7 | 5.18e-04 | 1.98e-04 |
#> thf: 8 | 1.30e-04 | 6.45e-05 |
#> thf: Convergence (starting from thf) achieved at iteration number 9!
#> thf: Temporal incoherence 4.94e-06 < 1e-05 tolerance
#> --------------------------------------------------------------------------------
The above plot shows the convergence path of both cross-sectional and temporal discrepancies.
<- octrec(FoReco_data$base, m = 12, C = FoReco_data$C,
oct_recf comb = "bdshr", res = FoReco_data$res, keep = "recf")
Monthly actual and forecasted values are shown in the following figure.
An evaluation of the quality of the reconciled forecasts can be
obtained by using appropriate accuracy indices (see
score_index()
).
Di Fonzo, T., Girolimetto, D. (2021), Cross-temporal forecast reconciliation: Optimal combination method and heuristic alternatives, International Journal of Forecasting, in press.
Hyndman R, Athanasopoulos G, Bergmeir C, Caceres G, Chhay L, O’Hara-Wild M, Petropoulos F, Razbash S, Wang E, Yasmeen F (2020). forecast: Forecasting functions for time series and linear models . R package version 8.13, https://pkg.robjhyndman.com/forecast/.
Kourentzes, N., Athanasopoulos, G. (2019), Cross-temporal coherent forecasts for Australian tourism, Annals of Tourism Research, 75, 393-409.
Nystrup, P., Lindström, E., Pinson, P., Madsen, H. (2020), Temporal hierarchies with autocorrelation for load forecasting, European Journal of Operational Research, 280, 3, 876–888.
Wickramasuriya, S.L., Athanasopoulos, G., Hyndman, R.J. (2019), Optimal forecast reconciliation for hierarchical and grouped time series through trace minimization, Journal of the American Statistical Association, 114, 526, 804-819.