Learn R Programming

FoReco (version 1.2.0)

terec: Optimal combination temporal reconciliation

Description

This function performs forecast reconciliation for a single time series using temporal hierarchies (Athanasopoulos et al., 2017, Nystrup et al., 2020). The reconciled forecasts can be computed using either a projection approach (Byron, 1978, 1979) or the equivalent structural approach by Hyndman et al. (2011). Non-negative (Di Fonzo and Girolimetto, 2023) and immutable reconciled forecasts can be considered.

Usage

terec(base, agg_order, tew = "sum", comb = "ols", res = NULL,
      approach = "proj", nn = NULL, settings = NULL, bounds = NULL,
      immutable = NULL, ...)

Value

A (\(h(k^\ast+m) \times 1\)) numeric vector of temporal reconciled forecasts.

Arguments

base

A (\(h(k^\ast + m) \times 1\)) numeric vector containing the base forecasts to be reconciled, ordered from lowest to highest frequency; \(m\) is the maximum aggregation order, \(k^\ast\) is the sum of a chosen subset of the \(p - 1\) factors of \(m\) (excluding \(m\) itself) and \(h\) is the forecast horizon for the lowest frequency time series.

agg_order

Highest available sampling frequency per seasonal cycle (max. order of temporal aggregation, \(m\)), or a vector representing a subset of \(p\) factors of \(m\).

tew

A string specifying the type of temporal aggregation. Options include: "sum" (simple summation, default), "avg" (average), "first" (first value of the period), and "last" (last value of the period).

comb

A string specifying the reconciliation method. For a complete list, see tecov.

res

A (\(N(k^\ast+m) \times 1\)) optional numeric vector containing the in-sample residuals or validation errors ordered from the lowest frequency to the highest frequency. This vector is used to compute come covariance matrices.

approach

A string specifying the approach used to compute the reconciled forecasts. Options include:

  • "proj" (default): Projection approach according to Byron (1978, 1979).

  • "strc": Structural approach as proposed by Hyndman et al. (2011).

  • "proj_osqp": Numerical solution using osqp for projection approach.

  • "strc_osqp": Numerical solution using osqp for structural approach.

nn

A string specifying the algorithm to compute non-negative forecasts:

  • "osqp": quadratic programming optimization (osqp solver, Girolimetto 2025).

  • "bpv": block principal pivoting algorithm (Wickramasuriya et al., 2020).

  • "nfca": negative forecasts correction algorithm (Kourentzes and Athanasopoulos, 2021; Girolimetto 2025).

  • "nnic": iterative non-negative reconciliation with immutable constraints (Girolimetto 2025).

  • "sntz": heuristic "set-negative-to-zero" (Di Fonzo and Girolimetto, 2023; Girolimetto 2025).

settings

A list of control parameters.

  • nn = "osqp" An object of class osqpSettings specifying settings for the osqp solver. For details, refer to the osqp documentation (Stellato et al., 2020)

  • nn = "bpv"

    • ptype = "fixed": permutation method: "random" or "fixed"

    • par = 10: the number of full exchange rules that may be attempted

    • tol = sqrt(.Machine$double.eps): the tolerance criteria

    • gtol = sqrt(.Machine$double.eps): the gradient tolerance criteria

    • itmax = 100: the maximum number of algorithm iterations

  • nn = "nfca" and nn = "nnic"

    • tol = sqrt(.Machine$double.eps): the tolerance criteria

    • itmax = 100: the maximum number of algorithm iterations

  • nn = "sntz"

    • type = "bu": the type of set-negative-to-zero heuristic: "bu" for bottom-up, "tdp" for top-down proportional, "tdsp" for top-down square proportional, "tdvw" for top-down variance weighted (the res param is used). See Girolimetto (2025) for details.

    • tol = sqrt(.Machine$double.eps): the tolerance identification of negative values

bounds

A matrix (see set_bounds) with 4 columns (\(k,j,lower,upper\)), such that

  • Column 1 represents the temporal aggregation order (\(k = m,\dots,1\)).

  • Column 2 represents the temporal forecast horizon (\(j = 1,\dots,m/k\)).

  • Columns 3 and 4 indicates the lower and upper bounds, respectively.

immutable

A matrix with 2 columns (\(k,j\)), such that

  • Column 1 represents the temporal aggregation order (\(k = m,\dots,1\)).

  • Column 2 represents the temporal forecast horizon (\(j = 1,\dots,m/k\)).

For example, when working with a quarterly time series:

  • t(c(4, 1)) - Fix the one step ahead annual forecast.

  • t(c(1, 2)) - Fix the two step ahead quarterly forecast.

...

Arguments passed on to tecov

mse

If TRUE (default) the errors used to compute the covariance matrix are not mean-corrected.

shrink_fun

Shrinkage function of the covariance matrix, shrink_estim (default)

References

Athanasopoulos, G., Hyndman, R.J., Kourentzes, N. and Petropoulos, F. (2017), Forecasting with Temporal Hierarchies, European Journal of Operational Research, 262, 1, 60-74. tools:::Rd_expr_doi("10.1016/j.ejor.2017.02.046")

Byron, R.P. (1978), The estimation of large social account matrices, Journal of the Royal Statistical Society, Series A, 141, 3, 359-367. tools:::Rd_expr_doi("10.2307/2344807")

Byron, R.P. (1979), Corrigenda: The estimation of large social account matrices, Journal of the Royal Statistical Society, Series A, 142(3), 405. tools:::Rd_expr_doi("10.2307/2982515")

Di Fonzo, T. and Girolimetto, D. (2023), Spatio-temporal reconciliation of solar forecasts, Solar Energy, 251, 13–29. tools:::Rd_expr_doi("10.1016/j.solener.2023.01.003")

Girolimetto, D. (2025), Non-negative forecast reconciliation: Optimal methods and operational solutions. Forecasting, 7(4), 64; tools:::Rd_expr_doi("10.3390/forecast7040064")

Hyndman, R.J., Ahmed, R.A., Athanasopoulos, G. and Shang, H.L. (2011), Optimal combination forecasts for hierarchical time series, Computational Statistics & Data Analysis, 55, 9, 2579-2589. tools:::Rd_expr_doi("10.1016/j.csda.2011.03.006")

Kourentzes, N. and Athanasopoulos, G. (2021) Elucidate structure in intermittent demand series. European Journal of Operational Research, 288, 141-152. tools:::Rd_expr_doi("10.1016/j.ejor.2020.05.046")

Nystrup, P., Lindström, E., Pinson, P. and Madsen, H. (2020), Temporal hierarchies with autocorrelation for load forecasting, European Journal of Operational Research, 280, 1, 876-888. tools:::Rd_expr_doi("10.1016/j.ejor.2019.07.061")

Stellato, B., Banjac, G., Goulart, P., Bemporad, A. and Boyd, S. (2020), OSQP: An Operator Splitting solver for Quadratic Programs, Mathematical Programming Computation, 12, 4, 637-672. tools:::Rd_expr_doi("10.1007/s12532-020-00179-2")

Wickramasuriya, S. L., Turlach, B. A., and Hyndman, R. J. (2020). Optimal non-negative forecast reconciliation. Statistics and Computing, 30(5), 1167–1182. tools:::Rd_expr_doi("10.1007/s11222-020-09930-0")

See Also

Regression-based reconciliation: csrec(), ctrec()

Temporal framework: teboot(), tebu(), tecov(), telcc(), temo(), temvn(), tesmp(), tetd(), tetools()

Examples

Run this code
set.seed(123)
# (7 x 1) base forecasts vector (simulated), m = 4
base <- rnorm(7, rep(c(20, 10, 5), c(1, 2, 4)))
# (70 x 1) in-sample residuals vector (simulated)
res <- rnorm(70)

m <- 4 # from quarterly to annual temporal aggregation
reco <- terec(base = base, agg_order = m, comb = "wlsv", res = res)

# Immutable reconciled forecast
# E.g. fix all the quarterly forecasts
imm_q <- expand.grid(k = 1, j = 1:4)
immreco <- terec(base = base, agg_order = m, comb = "wlsv",
                 res = res, immutable = imm_q)

# Non negative reconciliation
base[7] <- -base[7] # Making negative one of the quarterly base forecasts
nnreco <- terec(base = base, agg_order = m, comb = "wlsv",
                res = res, nn = "osqp")
recoinfo(nnreco, verbose = FALSE)$info

Run the code above in your browser using DataLab