Type: Package
Title: Coherent Forecast Combination for Linearly Constrained Multiple Time Series
Version: 0.1.2
Description: Methods and tools designed to improve the forecast accuracy for a linearly constrained multiple time series, while fulfilling the linear/aggregation relationships linking the components (Girolimetto and Di Fonzo, 2024 <doi:10.48550/arXiv.2412.03429>). 'FoCo2' offers multi-task forecast combination and reconciliation approaches leveraging input from multiple forecasting models or experts and ensuring that the resulting forecasts satisfy specified linear constraints. In addition, linear inequality constraints (e.g., non-negativity of the forecasts) can be imposed, if needed.
License: GPL (≥ 3)
Encoding: UTF-8
Depends: R (≥ 3.4), Matrix, FoReco
Imports: stats, cli, methods, quadprog, osqp
RoxygenNote: 7.3.2
URL: https://github.com/danigiro/FoCo2/, https://danigiro.github.io/FoCo2/
BugReports: https://github.com/danigiro/FoCo2/issues/
Suggests: testthat (≥ 3.0.0)
Config/testthat/edition: 3
NeedsCompilation: no
Packaged: 2025-06-12 14:50:41 UTC; daniele
Author: Daniele Girolimetto ORCID iD [aut, cre, fnd, cph], Tommaso Di Fonzo ORCID iD [aut, fnd]
Maintainer: Daniele Girolimetto <daniele.girolimetto@unipd.it>
Repository: CRAN
Date/Publication: 2025-06-14 08:50:02 UTC

FoCo2: Coherent Forecast Combination for Linearly Constrained Multiple Time Series

Description

Methods and tools designed to improve the forecast accuracy for a linearly constrained multiple time series, while fulfilling the linear/aggregation relationships linking the components (Girolimetto and Di Fonzo, 2024 doi:10.48550/arXiv.2412.03429). FoCo2 offers multi-task forecast combination and reconciliation approaches leveraging input from multiple forecasting models or experts and ensuring that the resulting forecasts satisfy specified linear constraints. In addition, linear inequality constraints (e.g., non-negativity of the forecasts) can be imposed, if needed.

Author(s)

Maintainer: Daniele Girolimetto daniele.girolimetto@unipd.it (ORCID) [funder, copyright holder]

Authors:

See Also

Useful links:


Cross-sectional covariance matrix approximation

Description

Extended version of the FoReco::cscov function, introducing two new approximations for the covariance matrix (both shrunk and sample versions). Specifically, shrbe/sambe assume no correlation between experts, while shrbv/sambv assume no correlation between variables.

Usage

## S3 method for class 'shrbe'
cscov(comb = "shrbe", ..., n = NULL, p = NULL, matNA = NULL,
      res = NULL, mse = TRUE, shrink_fun = NULL)

## S3 method for class 'sambe'
cscov(comb = "sambe", ..., n = NULL, p = NULL, matNA = NULL,
      res = NULL, mse = TRUE)

## S3 method for class 'shrbv'
cscov(comb = "shrbv", ..., n = NULL, p = NULL, matNA = NULL,
      res = NULL, mse = TRUE, shrink_fun = NULL)

## S3 method for class 'sambv'
cscov(comb = "sambv", ..., n = NULL, p = NULL, matNA = NULL,
      res = NULL, mse = TRUE)

Arguments

comb

A string specifying the reconciliation method.

  • FoReco approaches: "ols", "wls", "shr", "sam".

  • "shrbe"/"sambe" - shrunk/sample block-diagonal covariance by experts.

  • "shrbv"/"sambv" - shrunk/sample block-diagonal covariance by variables.

...

Arguments passed on to FoReco::cscov.

n

Total number of variables, n.

p

Total number of experts, p.

matNA

A (n \times p) matrix consisting of 0s and 1s, where each element indicates whether expert j (column) has provided a forecast for variable i (row). If expert j has provided a forecast for variable i, the corresponding element (i,j) is 1; otherwise, it is 0.

res

A list of p numeric (N \times n) matrix containing the in-sample residuals. This input is used to compute some covariance matrices.

mse

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

shrink_fun

Shrinkage function of the covariance matrix, FoReco::shrink_estim (default).

Value

A (m \times m) symmetric positive (semi-)definite matrix, with m = \sum_{j = 1}^p n_j, n_j \leq n.

See Also

Other Optimal combination: csmtc(), csocc(), occmat()


Cross-sectional optimal multi-task forecast combination

Description

This function computes the optimal multi-task linear forecast combination, as described in Girolimetto and Di Fonzo (2024)

Usage

csmtc(base, comb = "ols", res = NULL, approach = "proj",
      nn = NULL, settings = NULL, bounds = NULL, agg_mat = NULL, ...)

Arguments

base

A list of p numeric (h \times n) matrix or multivariate time series (mts class) containing the base forecasts to be reconciled; h is the forecast horizon, n is the total number of time series (n = n_u + n_b) and p is the total number of experts.

comb

A string specifying the reconciliation method. For details, see cscov.

res

A list of p numeric (N \times n) matrix containing the in-sample residuals. This input is used to compute some covariance matrices.

approach

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

  • "proj" (default): zero-constrained projection approach.

  • "osqp": OSQP solver (Stellato et al., 2020).

nn

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

  • "osqp": OSQP solver (Stellato et al., 2020).

  • "sntz": heuristic "set-negative-to-zero".

settings

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

bounds

A matrix (see set_bounds in FoReco) with 3 columns (i,lower,upper), such that

  • Column 1 represents the cross-sectional series (i = 1, \dots, n).

  • Column 2 indicates the lower bound.

  • Column 3 indicates the upper bound.

agg_mat

A (n_u \times n_b) numeric matrix representing the cross-sectional aggregation matrix, mapping the n_b bottom-level (free) variables into the n_u upper (constrained) variables.

...

Arguments passed on to cscov.

Value

A (h \times n) numeric matrix of cross-sectional multi-task combined forecasts.

References

Girolimetto, D. and Di Fonzo, T. (2024), Coherent forecast combination for linearly constrained multiple time series, doi:10.48550/arXiv.2412.03429.

See Also

Other Optimal combination: cscov(), csocc(), occmat()

Examples

set.seed(123)
# (2 x 3) base forecasts matrix (simulated), expert 1
base1 <- matrix(rnorm(6, mean = c(20, 10, 10)), 2, byrow = TRUE)
# (10 x 3) in-sample residuals matrix (simulated), expert 1
res1 <- t(matrix(rnorm(n = 30), nrow = 3))

# (2 x 3) base forecasts matrix (simulated), expert 2
base2 <- matrix(rnorm(6, mean = c(20, 10, 10)), 2, byrow = TRUE)
# (10 x 3) in-sample residuals matrix (simulated), expert 2
res2 <- t(matrix(rnorm(n = 30), nrow = 3))

## BALANCED PANEL OF FORECASTS
# Base forecasts' and residuals' lists
brc <- list(base1, base2)
erc <- list(res1, res2)

# Aggregation matrix for Z = X + Y
A <- t(c(1,1))
rrc <- csocc(base = brc, agg_mat = A, comb = "shr", res = erc)

yc <- csmtc(base = brc, agg_mat = A, comb = "shr", res = erc)
M <- occmat(base = brc, agg_mat = A, comb = "shr", p = 2, res = erc)$M
M%*%t(yc)-t(rrc)

## UNBALANCED PANEL OF FORECASTS
base2[, 2] <- res2[, 2] <-  NA

# Base forecasts' and residuals' lists
bgc <- list(base1, base2)
egc <- list(res1, res2)
matNA <- matrix(1, 3, 2)
matNA[2,2] <- 0

# Aggregation matrix for Z = X + Y
A <- t(c(1,1))
rgc <- csocc(base = bgc, agg_mat = A, comb = "shr", res = egc)

yc <- csmtc(base = bgc, agg_mat = A, comb = "shr", res = egc)
M <- occmat(base = bgc, agg_mat = A, comb = "shr", p = 2, res = egc, matNA = matNA)$M
M%*%t(yc)-t(rgc)


Cross-sectional optimal coherent forecast combination

Description

This function implements the optimal linear coherent forecast combination for a linearly constrained (e.g., hierarchical/grouped) multiple time series combining forecasts from multiple sources (experts). The method minimizes forecast errors while ensuring that the resulting forecasts are coherent, meaning they satisfy the constraints across the time series. Using a linear constrained optimization approach, csocc combines forecasts from multiple experts, accounting for the relations between variables and constraints. In addition, linear inequality constraints (e.g. non-negative forecasts) can be imposed, if needed.

Usage

csocc(base, agg_mat, cons_mat, comb = "ols", res = NULL,
      approach = "proj", nn = NULL, settings = NULL, bounds = NULL, ...)

Arguments

base

A list of p numeric (h \times n) matrix or multivariate time series (mts class) containing the base forecasts to be reconciled; h is the forecast horizon, n is the total number of time series (n = n_u + n_b) and p is the total number of experts.

agg_mat

A (n_u \times n_b) numeric matrix representing the cross-sectional aggregation matrix, mapping the n_b bottom-level (free) variables into the n_u upper (constrained) variables.

cons_mat

A (n_u \times n) numeric matrix representing the cross-sectional zero constraints: each row represents a constraint equation, and each column represents a variable. The matrix can be of full rank, meaning the rows are linearly independent, but this is not a strict requirement, as the function allows for redundancy in the constraints.

comb

A string specifying the reconciliation method. For details, see cscov.

res

A list of p numeric (N \times n) matrix containing the in-sample residuals. This input is used to compute some covariance matrices.

approach

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

  • "proj" (default): zero-constrained projection approach.

  • "strc": structural approach.

nn

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

  • "osqp": OSQP solver (Stellato et al., 2020).

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

settings

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

bounds

A matrix (see set_bounds in FoReco) with 3 columns (i,lower,upper), such that

  • Column 1 represents the cross-sectional series (i = 1, \dots, n).

  • Column 2 indicates the lower bound.

  • Column 3 indicates the upper bound.

...

Arguments passed on to cscov.

Details

If an expert does not provide a forecast for a given variable, the missing value can be represented as NA. For instance, consider a case where the number of experts is p = 4, with n = 3 variables and h = 2. If the second and third experts do not provide forecasts for, respectively, the second and first variable, the corresponding matrix in the base list will be as follows:

\texttt{base[[1]]} = \begin{bmatrix} \hat{y}_{1,1}^{1} & \hat{y}_{2,1}^{1} & \hat{y}_{3,1}^{1} \\ \hat{y}_{1,2}^{1} & \hat{y}_{2,2}^{1} & \hat{y}_{3,2}^{1} \end{bmatrix}, \quad \texttt{base[[2]]} = \begin{bmatrix} \hat{y}_{1,1}^{2} & \texttt{NA} & \hat{y}_{3,1}^{2} \\ \hat{y}_{1,2}^{2} & \texttt{NA} & \hat{y}_{3,2}^{2} \end{bmatrix},

\texttt{base[[3]]} = \begin{bmatrix} \texttt{NA} & \hat{y}_{2,1}^{3} & \hat{y}_{3,1}^{3} \\ \texttt{NA} & \hat{y}_{2,2}^{3} & \hat{y}_{3,2}^{3} \end{bmatrix}, \quad \texttt{base[[4]]} = \begin{bmatrix} \hat{y}_{1,1}^{4} & \hat{y}_{2,1}^{4} & \hat{y}_{3,1}^{4} \\ \hat{y}_{1,2}^{4} & \hat{y}_{2,2}^{4} & \hat{y}_{3,2}^{4} \end{bmatrix}.

Value

A (h \times n) numeric matrix of cross-sectional combined and reconciled forecasts.

References

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

Girolimetto, D. and Di Fonzo, T. (2024), Coherent forecast combination for linearly constrained multiple time series, doi:10.48550/arXiv.2412.03429.

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. doi:10.1007/s12532-020-00179-2

See Also

Other Optimal combination: cscov(), csmtc(), occmat()

Examples

set.seed(123)
# (2 x 3) base forecasts matrix (simulated), expert 1
base1 <- matrix(rnorm(6, mean = c(20, 10, 10)), 2, byrow = TRUE)
# (10 x 3) in-sample residuals matrix (simulated), expert 1
res1 <- t(matrix(rnorm(n = 30), nrow = 3))

# (2 x 3) base forecasts matrix (simulated), expert 2
base2 <- matrix(rnorm(6, mean = c(20, 10, 10)), 2, byrow = TRUE)
# (10 x 3) in-sample residuals matrix (simulated), expert 2
res2 <- t(matrix(rnorm(n = 30), nrow = 3))

## BALANCED PANEL OF FORECASTS
# Base forecasts' and residuals' lists
brc <- list(base1, base2)
erc <- list(res1, res2)

# Aggregation matrix for Z = X + Y
A <- t(c(1,1))
rrc <- csocc(base = brc, agg_mat = A, comb = "wls", res = erc)

# Zero constraints matrix for Z - X - Y = 0
C <- t(c(1, -1, -1))
rrc <- csocc(base = brc, cons_mat = C, comb = "wls", res = erc) # same results

## UNBALANCED PANEL OF FORECASTS
base2[, 2] <- res2[, 2] <-  NA

# Base forecasts' and residuals' lists
bgc <- list(base1, base2)
egc <- list(res1, res2)

# Aggregation matrix for Z = X + Y
A <- t(c(1,1))
rgc <- csocc(base = bgc, agg_mat = A, comb = "shrbe", res = egc)


Cross-sectional sequential combination-reconciliation

Description

This function performs a two-step process designed to first combine forecasts from multiple models or experts and then apply reconciliation techniques to ensure coherence.

Usage

csscr(base, fc = "sa", comb = "ols", res = NULL, mse = TRUE, shrink = TRUE,
      nnw = FALSE, factorized = FALSE, ...)

Arguments

base

A list of p numeric (h \times n) matrix or multivariate time series (mts class) containing the base forecasts to be reconciled; h is the forecast horizon, n is the total number of time series (n = n_u + n_b) and p is the total number of experts.

fc

A string specifying the combination method:

  • "sa" - (default) simple average (equal weights).

  • "var" - (uses res) weights derived from the inverse of forecasts variances/MSE as proposed by Bates and Granger (1969).

  • "cov" - (uses res) weights derived using the whole forecast error covariance matrix, as proposed by Newbold and Granger (1974).

comb

A string specifying the reconciliation method: "ols", "wls", "shr", "sam" (see FoReco). If comb = "none", no reconciliation is performed and the combined forecasts are directly returned.

res

A list of p numeric (N \times n) matrix containing the in-sample residuals. This input is used to compute some covariance matrices.

mse

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

shrink

If TRUE (default), the covariance matrix for fc = "cov" is shrunk.

nnw

If TRUE for fc = "cov", the weights are constrained to be non-negative (Conflitti et al., 2015). The default is FALSE.

factorized

Value to be passed to the quadprog::solve.QP, only when nnw = TRUE.

...

Arguments passed on to FoReco::csrec (e.g., agg_mat or cons_mat).

Value

A (h \times n) numeric matrix of cross-sectional combined and reconciled forecasts.

References

Bates, J. and Granger, C. W. (1969), The combination of forecasts, Operations Research Quarterly, 20, 451–468. doi:10.1057/jors.1969.103.

Conflitti, C., De Mol, C., and Giannone, D. (2015), Optimal combination of survey forecasts. International Journal of Forecasting, 31(4), 1096–1103. doi:10.1016/j.ijforecast.2015.03.009.

Girolimetto, D. and Di Fonzo, T. (2024), Coherent forecast combination for linearly constrained multiple time series. doi:10.48550/arXiv.2412.03429.

Newbold, P. and Granger, C. W. (1974), Experience with forecasting univariate time series and the combination of forecasts, Journal of the Royal Statistical Society, A, 137, 131–146. doi:10.2307/2344546

See Also

Sequential coherent combination: cssrc()

Examples

set.seed(123)
# (2 x 3) base forecasts matrix (simulated), expert 1
base1 <- matrix(rnorm(6, mean = c(20, 10, 10)), 2, byrow = TRUE)
# (10 x 3) in-sample residuals matrix (simulated), expert 1
res1 <- t(matrix(rnorm(n = 30), nrow = 3))

# (2 x 3) base forecasts matrix (simulated), expert 2
base2 <- matrix(rnorm(6, mean = c(20, 10, 10)), 2, byrow = TRUE)
# (10 x 3) in-sample residuals matrix (simulated), expert 2
res2 <- t(matrix(rnorm(n = 30), nrow = 3))

# Base forecasts' and residuals' lists
base <- list(base1, base2)
res <- list(res1, res2)

# Aggregation matrix for Z = X + Y
A <- t(c(1,1))
reco <- csscr(base = base, agg_mat = A, comb = "wls", res = res, fc = "sa")

# Zero constraints matrix for Z - X - Y = 0
C <- t(c(1, -1, -1))
reco <- csscr(base = base, cons_mat = C, comb = "wls", res = res, fc = "sa") # same results

# Incoherent combined forecasts
fc_comb <- csscr(base = base, comb = "none", fc = "sa")
round(C %*% t(fc_comb), 3) # Incoherent forecasts

Cross-sectional sequential reconciliation-combination

Description

This function applies a sequential method that first reconciles the base forecasts from each expert to satisfy the linear constraints, and then combines the reconciled forecasts obtained so far. cssrc may be applied only in 'balanced' cases (e.g., n_j = n \forall j, see Girolimetto and Di Fonzo, 2024)

Usage

cssrc(base, fc = "sa", comb = "ols", res = NULL, mse = TRUE, shrink = TRUE,
      nnw = FALSE, factorized = FALSE, ...)

Arguments

base

A list of p numeric (h \times n) matrix or multivariate time series (mts class) containing the base forecasts to be reconciled; h is the forecast horizon, n is the total number of time series (n = n_u + n_b) and p is the total number of experts.

fc

A string specifying the combination method:

  • "sa" - (default) simple average (equal weights).

  • "var" - (uses res) weights derived from the inverse of forecasts variances/MSE as proposed by Bates and Granger (1969).

  • "cov" - (uses res) weights derived using the whole forecast error covariance matrix, as proposed by Newbold and Granger (1974).

comb

A string specifying the reconciliation method: "ols", "wls", "shr", "sam" (see FoReco).

res

A list of p numeric (N \times n) matrix containing the in-sample residuals. This input is used to compute some covariance matrices.

mse

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

shrink

If TRUE (default), the covariance matrix for fc = "cov" is shrunk.

nnw

If TRUE for fc = "cov", the weights are constrained to be non-negative (Conflitti et al., 2015). The default is FALSE.

factorized

Value to be passed to the quadprog::solve.QP, only when nnw = TRUE.

...

Arguments passed on to FoReco::csrec (e.g., agg_mat or cons_mat).

Value

A (h \times n) numeric matrix of cross-sectional combined and reconciled forecasts.

References

Bates, J. and Granger, C. W. (1969), The combination of forecasts, Operations Research Quarterly, 20, 451–468. doi:10.1057/jors.1969.103.

Conflitti, C., De Mol, C., and Giannone, D. (2015), Optimal combination of survey forecasts. International Journal of Forecasting, 31(4), 1096–1103. doi:10.1016/j.ijforecast.2015.03.009.

Girolimetto, D. and Di Fonzo, T. (2024), Coherent forecast combination for linearly constrained multiple time series. doi:10.48550/arXiv.2412.03429.

Newbold, P. and Granger, C. W. (1974), Experience with forecasting univariate time series and the combination of forecasts, Journal of the Royal Statistical Society, A, 137, 131–146. doi:10.2307/2344546

See Also

Sequential coherent combination: csscr()

Examples

set.seed(123)
# (2 x 3) base forecasts matrix (simulated), expert 1
base1 <- matrix(rnorm(6, mean = c(20, 10, 10)), 2, byrow = TRUE)
# (10 x 3) in-sample residuals matrix (simulated), expert 1
res1 <- t(matrix(rnorm(n = 30), nrow = 3))

# (2 x 3) base forecasts matrix (simulated), expert 2
base2 <- matrix(rnorm(6, mean = c(20, 10, 10)), 2, byrow = TRUE)
# (10 x 3) in-sample residuals matrix (simulated), expert 2
res2 <- t(matrix(rnorm(n = 30), nrow = 3))

# Base forecasts' and residuals' lists
base <- list(base1, base2)
res <- list(res1, res2)

# Aggregation matrix for Z = X + Y
A <- t(c(1,1))
reco <- cssrc(base = base, agg_mat = A, comb = "wls", res = res, fc = "sa")

# Zero constraints matrix for Z - X - Y = 0
C <- t(c(1, -1, -1))
reco <- cssrc(base = base, cons_mat = C, comb = "wls", res = res, fc = "sa") # same results

# WARNING!
reco_v <- cssrc(base = base, agg_mat = A, comb = "wls", res = res, fc = "var")
round(C %*% t(reco_v), 3) # Incoherent forecasts


Matrices for the optimal coherent forecast combination

Description

This function computes the matrices required for the optimal coherent forecast combination csocc, as described in Girolimetto and Di Fonzo (2024). These matrices serve as the foundation for building forecasts that effectively combines the individual information from multiple experts while ensuring coherence across the variables.

Usage

occmat(agg_mat, cons_mat, p = NULL, matNA = NULL,
       comb = "ols", res = NULL, approach = "proj", ...)

Arguments

agg_mat

A (n_u \times n_b) numeric matrix representing the cross-sectional aggregation matrix, mapping the n_b bottom-level (free) variables into the n_u upper (constrained) variables.

cons_mat

A (n_u \times n) numeric matrix representing the cross-sectional zero constraints: each row represents a constraint equation, and each column represents a variable. The matrix can be of full rank, meaning the rows are linearly independent, but this is not a strict requirement, as the function allows for redundancy in the constraints.

p

Total number of experts, p.

matNA

A (n \times p) matrix consisting of 0s and 1s, where each element indicates whether expert j (column) has provided a forecast for variable i (row). If expert j has provided a forecast for variable i, the corresponding element (i,j) is 1; otherwise, it is 0.

comb

A string specifying the reconciliation method. For details, see cscov.

res

A list of p numeric (N \times n) matrix containing the in-sample residuals. This input is used to compute some covariance matrices.

approach

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

  • "proj" (default): zero-constrained projection approach.

  • "strc": structural approach.

...

Arguments passed on to cscov.

Value

A list of matrices:

M

Projection matrix.

Omega

Matrix of the combination weights of the optimal linear multi-task forecast combination.

W

Forecast error covariance matrix of the base forecasts.

Wc

Forecast error covariance matrix of the combined forecasts.

Wtilde

Forecast error covariance matrix of the reconciled combined forecasts.

K

Matrix that replicates a vector (see Girolimetto and Di Fonzo, 2024).

References

Girolimetto, D. and Di Fonzo, T. (2024), Coherent forecast combination for linearly constrained multiple time series, doi:10.48550/arXiv.2412.03429.

See Also

Other Optimal combination: cscov(), csmtc(), csocc()