From 7fe977ef1f3c202931dbe2f30d19609aec50d6ac Mon Sep 17 00:00:00 2001 From: Young Geun Kim Date: Wed, 5 Feb 2025 04:43:33 -0800 Subject: [PATCH] rearrange cpp source files based on headers --- R/RcppExports.R | 726 ++++++------- inst/include/bvhar/bayesfit | 10 - inst/include/bvhar/mniw | 10 + inst/include/bvhar/triangular | 8 + python/src/bvhar/_src/_bayes.cpp | 39 - .../bvhar/_src/{_forecast.cpp => _cta.cpp} | 70 +- python/src/bvhar/_src/_spillover.cpp | 33 - python/src/bvhar/model/_bayes.py | 12 +- src/RcppExports.cpp | 826 +++++++-------- src/estimate-bayes.cpp | 234 ---- src/estimate-ols.cpp | 155 --- src/expand.cpp | 555 ---------- src/forecast.cpp | 472 --------- src/mniw.cpp | 999 ++++++++++++++++++ src/ols.cpp | 630 +++++++++++ src/roll.cpp | 336 ------ src/spillover.cpp | 589 ----------- src/triangular.cpp | 709 +++++++++++++ 18 files changed, 3206 insertions(+), 3207 deletions(-) delete mode 100644 inst/include/bvhar/bayesfit create mode 100644 inst/include/bvhar/mniw create mode 100644 inst/include/bvhar/triangular delete mode 100644 python/src/bvhar/_src/_bayes.cpp rename python/src/bvhar/_src/{_forecast.cpp => _cta.cpp} (76%) delete mode 100644 python/src/bvhar/_src/_spillover.cpp delete mode 100644 src/estimate-bayes.cpp delete mode 100644 src/estimate-ols.cpp delete mode 100644 src/expand.cpp delete mode 100644 src/forecast.cpp create mode 100644 src/mniw.cpp create mode 100644 src/ols.cpp delete mode 100644 src/roll.cpp delete mode 100644 src/spillover.cpp create mode 100644 src/triangular.cpp diff --git a/R/RcppExports.R b/R/RcppExports.R index ea1ecfb9..0cf9797d 100644 --- a/R/RcppExports.R +++ b/R/RcppExports.R @@ -60,30 +60,207 @@ estimate_mniw <- function(num_chains, num_iter, num_burn, thin, mn_mean, mn_prec .Call(`_bvhar_estimate_mniw`, num_chains, num_iter, num_burn, thin, mn_mean, mn_prec, iw_scale, iw_shape, seed_chain, display_progress, nthreads) } -#' VAR with Shrinkage Priors +#' Forecasting BVAR(p) #' -#' This function generates parameters \eqn{\beta, a, \sigma_{h,i}^2, h_{0,i}} and log-volatilities \eqn{h_{i,1}, \ldots, h_{i, n}}. +#' @param object A `bvarmn` or `bvarflat` object +#' @param step Integer, Step to forecast +#' @param num_sim Integer, number to simulate parameters from posterior distribution +#' @details +#' n-step ahead forecasting using BVAR(p) recursively. #' -#' @param num_chain Number of MCMC chains -#' @param num_iter Number of iteration for MCMC -#' @param num_burn Number of burn-in (warm-up) for MCMC +#' For given number of simulation (`num_sim`), +#' +#' 1. Generate \eqn{(A^{(b)}, \Sigma_e^{(b)}) \sim MIW} (posterior) +#' 2. Recursively, \eqn{j = 1, \ldots, h} (`step`) +#' - Point forecast: Use \eqn{\hat{A}} +#' - Predictive distribution: Again generate \eqn{\tilde{Y}_{n + j}^{(b)} \sim A^{(b)}, \Sigma_e^{(b)} \sim MN} +#' - tilde notation indicates simulated ones +#' +#' @references +#' Lütkepohl, H. (2007). *New Introduction to Multiple Time Series Analysis*. Springer Publishing. [https://doi.org/10.1007/978-3-540-27752-1](https://doi.org/10.1007/978-3-540-27752-1) +#' +#' Litterman, R. B. (1986). *Forecasting with Bayesian Vector Autoregressions: Five Years of Experience*. Journal of Business & Economic Statistics, 4(1), 25. [https://doi:10.2307/1391384](https://doi:10.2307/1391384) +#' +#' Bańbura, M., Giannone, D., & Reichlin, L. (2010). *Large Bayesian vector auto regressions*. Journal of Applied Econometrics, 25(1). [https://doi:10.1002/jae.1137](https://doi:10.1002/jae.1137) +#' +#' Ghosh, S., Khare, K., & Michailidis, G. (2018). *High-Dimensional Posterior Consistency in Bayesian Vector Autoregressive Models*. Journal of the American Statistical Association, 114(526). [https://doi:10.1080/01621459.2018.1437043](https://doi:10.1080/01621459.2018.1437043) +#' +#' Karlsson, S. (2013). *Chapter 15 Forecasting with Bayesian Vector Autoregression*. Handbook of Economic Forecasting, 2, 791-897. doi:[10.1016/b978-0-444-62731-5.00015-4](https://doi.org/10.1016/B978-0-444-62731-5.00015-4) +#' +#' @noRd +forecast_bvar <- function(object, step, num_sim, seed) { + .Call(`_bvhar_forecast_bvar`, object, step, num_sim, seed) +} + +#' Forecasting Bayesian VHAR +#' +#' @param object A `bvharmn` object +#' @param step Integer, Step to forecast +#' @param num_sim Integer, number to simulate parameters from posterior distribution +#' @details +#' n-step ahead forecasting using VHAR recursively. +#' +#' For given number of simulation (`num_sim`), +#' +#' 1. Generate \eqn{(\Phi^{(b)}, \Sigma_e^{(b)}) \sim MIW} (posterior) +#' 2. Recursively, \eqn{j = 1, \ldots, h} (`step`) +#' - Point forecast: Use \eqn{\hat\Phi} +#' - Predictive distribution: Again generate \eqn{\tilde{Y}_{n + j}^{(b)} \sim \Phi^{(b)}, \Sigma_e^{(b)} \sim MN} +#' - tilde notation indicates simulated ones +#' +#' @references Kim, Y. G., and Baek, C. (2024). *Bayesian vector heterogeneous autoregressive modeling*. Journal of Statistical Computation and Simulation, 94(6), 1139-1157. +#' @noRd +forecast_bvharmn <- function(object, step, num_sim, seed) { + .Call(`_bvhar_forecast_bvharmn`, object, step, num_sim, seed) +} + +#' Out-of-Sample Forecasting of BVAR based on Rolling Window +#' +#' This function conducts an rolling window forecasting of BVAR with Minnesota prior. +#' +#' @param y Time series data of which columns indicate the variables +#' @param lag BVAR order +#' @param bayes_spec List, BVAR specification +#' @param include_mean Add constant term +#' @param step Integer, Step to forecast +#' @param y_test Evaluation time series data period after `y` +#' +#' @noRd +roll_bvar <- function(y, lag, bayes_spec, include_mean, step, y_test, seed_forecast, nthreads) { + .Call(`_bvhar_roll_bvar`, y, lag, bayes_spec, include_mean, step, y_test, seed_forecast, nthreads) +} + +#' Out-of-Sample Forecasting of BVAR based on Rolling Window +#' +#' This function conducts an rolling window forecasting of BVAR with Flat prior. +#' +#' @param y Time series data of which columns indicate the variables +#' @param lag BVAR order +#' @param bayes_spec List, BVAR specification +#' @param include_mean Add constant term +#' @param step Integer, Step to forecast +#' @param y_test Evaluation time series data period after `y` +#' +#' @noRd +roll_bvarflat <- function(y, lag, U, include_mean, step, y_test, seed_forecast, nthreads) { + .Call(`_bvhar_roll_bvarflat`, y, lag, U, include_mean, step, y_test, seed_forecast, nthreads) +} + +#' Out-of-Sample Forecasting of BVHAR based on Rolling Window +#' +#' This function conducts an rolling window forecasting of BVHAR with Minnesota prior. +#' +#' @param y Time series data of which columns indicate the variables +#' @param har `r lifecycle::badge("experimental")` Numeric vector for weekly and monthly order. +#' @param bayes_spec List, BVHAR specification +#' @param include_mean Add constant term +#' @param step Integer, Step to forecast +#' @param y_test Evaluation time series data period after `y` +#' +#' @noRd +roll_bvhar <- function(y, week, month, bayes_spec, include_mean, step, y_test, seed_forecast, nthreads) { + .Call(`_bvhar_roll_bvhar`, y, week, month, bayes_spec, include_mean, step, y_test, seed_forecast, nthreads) +} + +#' Out-of-Sample Forecasting of BVAR based on Expanding Window +#' +#' This function conducts an expanding window forecasting of BVAR with Minnesota prior. +#' +#' @param y Time series data of which columns indicate the variables +#' @param lag BVAR order +#' @param bayes_spec List, BVAR specification +#' @param include_mean Add constant term +#' @param step Integer, Step to forecast +#' @param y_test Evaluation time series data period after `y` +#' +#' @noRd +expand_bvar <- function(y, lag, bayes_spec, include_mean, step, y_test, seed_forecast, nthreads) { + .Call(`_bvhar_expand_bvar`, y, lag, bayes_spec, include_mean, step, y_test, seed_forecast, nthreads) +} + +#' Out-of-Sample Forecasting of BVAR based on Expanding Window +#' +#' This function conducts an expanding window forecasting of BVAR with Flat prior. +#' +#' @param y Time series data of which columns indicate the variables +#' @param lag BVAR order +#' @param bayes_spec List, BVAR specification +#' @param include_mean Add constant term +#' @param step Integer, Step to forecast +#' @param y_test Evaluation time series data period after `y` +#' +#' @noRd +expand_bvarflat <- function(y, lag, U, include_mean, step, y_test, seed_forecast, nthreads) { + .Call(`_bvhar_expand_bvarflat`, y, lag, U, include_mean, step, y_test, seed_forecast, nthreads) +} + +#' Out-of-Sample Forecasting of BVHAR based on Expanding Window +#' +#' This function conducts an expanding window forecasting of BVHAR with Minnesota prior. +#' +#' @param y Time series data of which columns indicate the variables +#' @param har `r lifecycle::badge("experimental")` Numeric vector for weekly and monthly order. +#' @param bayes_spec List, BVHAR specification +#' @param include_mean Add constant term +#' @param step Integer, Step to forecast +#' @param y_test Evaluation time series data period after `y` +#' +#' @noRd +expand_bvhar <- function(y, week, month, bayes_spec, include_mean, step, y_test, seed_forecast, nthreads) { + .Call(`_bvhar_expand_bvhar`, y, week, month, bayes_spec, include_mean, step, y_test, seed_forecast, nthreads) +} + +#' Generalized Spillover of Minnesota prior +#' +#' @param object varlse or vharlse object. +#' @param step Step to forecast. +#' @param num_iter Number to sample MNIW distribution +#' @param num_burn Number of burn-in #' @param thin Thinning -#' @param x Design matrix X0 -#' @param y Response matrix Y0 -#' @param param_reg Regression specification list -#' @param param_prior Prior specification list -#' @param param_intercept Intercept specification list -#' @param param_init Initialization specification list -#' @param grp_id Unique group id -#' @param grp_mat Group matrix -#' @param include_mean Constant term -#' @param seed_chain Seed for each chain -#' @param display_progress Progress bar +#' @param seed Random seed for boost library +#' +#' @noRd +compute_mn_spillover <- function(object, step, num_iter, num_burn, thin, seed) { + .Call(`_bvhar_compute_mn_spillover`, object, step, num_iter, num_burn, thin, seed) +} + +#' Rolling-sample Total Spillover Index of BVAR +#' +#' @param y Time series data of which columns indicate the variables +#' @param window Rolling window size +#' @param step forecast horizon for FEVD +#' @param num_iter Number to sample MNIW distribution +#' @param num_burn Number of burn-in +#' @param thin Thinning +#' @param lag BVAR order +#' @param bayes_spec BVAR specification +#' @param include_mean Add constant term +#' @param seed_chain Random seed for each window #' @param nthreads Number of threads for openmp #' #' @noRd -estimate_sur <- function(num_chains, num_iter, num_burn, thin, x, y, param_reg, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, seed_chain, display_progress, nthreads) { - .Call(`_bvhar_estimate_sur`, num_chains, num_iter, num_burn, thin, x, y, param_reg, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, seed_chain, display_progress, nthreads) +dynamic_bvar_spillover <- function(y, window, step, num_iter, num_burn, thin, lag, bayes_spec, include_mean, seed_chain, nthreads) { + .Call(`_bvhar_dynamic_bvar_spillover`, y, window, step, num_iter, num_burn, thin, lag, bayes_spec, include_mean, seed_chain, nthreads) +} + +#' Rolling-sample Total Spillover Index of BVHAR +#' +#' @param y Time series data of which columns indicate the variables +#' @param window Rolling window size +#' @param step forecast horizon for FEVD +#' @param num_iter Number to sample MNIW distribution +#' @param num_burn Number of burn-in +#' @param thin Thinning +#' @param week Week order +#' @param month Month order +#' @param bayes_spec BVHAR specification +#' @param include_mean Add constant term +#' @param seed_chain Random seed for each window +#' @param nthreads Number of threads for openmp +#' +#' @noRd +dynamic_bvhar_spillover <- function(y, window, step, num_iter, num_burn, thin, week, month, bayes_spec, include_mean, seed_chain, nthreads) { + .Call(`_bvhar_dynamic_bvhar_spillover`, y, window, step, num_iter, num_burn, thin, week, month, bayes_spec, include_mean, seed_chain, nthreads) } #' Compute VAR(p) Coefficient Matrices and Fitted Values @@ -181,290 +358,161 @@ infer_vhar <- function(object) { .Call(`_bvhar_infer_vhar`, object) } -#' Out-of-Sample Forecasting of VAR based on Expanding Window -#' -#' This function conducts an expanding window forecasting of VAR. +#' Forecasting Vector Autoregression #' -#' @param y Time series data of which columns indicate the variables -#' @param lag VAR order -#' @param include_mean Add constant term +#' @param object A `varlse` object #' @param step Integer, Step to forecast -#' @param y_test Evaluation time series data period after `y` -#' @param method Method to solve linear equation system. 1: normal equation, 2: cholesky, 3: HouseholderQR. -#' @param nthreads Number of threads for openmp +#' @details +#' n-step ahead forecasting using VAR(p) recursively, based on pp35 of Lütkepohl (2007). #' +#' @references Lütkepohl, H. (2007). *New Introduction to Multiple Time Series Analysis*. Springer Publishing. [https://doi.org/10.1007/978-3-540-27752-1](https://doi.org/10.1007/978-3-540-27752-1) #' @noRd -expand_var <- function(y, lag, include_mean, step, y_test, method, nthreads) { - .Call(`_bvhar_expand_var`, y, lag, include_mean, step, y_test, method, nthreads) +forecast_var <- function(object, step) { + .Call(`_bvhar_forecast_var`, object, step) } -#' Out-of-Sample Forecasting of VHAR based on Expanding Window -#' -#' This function conducts an expanding window forecasting of VHAR. +#' Forecasting Vector HAR #' -#' @param y Time series data of which columns indicate the variables -#' @param week Integer, order for weekly term -#' @param month Integer, order for monthly term -#' @param include_mean Add constant term +#' @param object A `vharlse` object #' @param step Integer, Step to forecast -#' @param y_test Evaluation time series data period after `y` -#' @param method Method to solve linear equation system. 1: normal equation, 2: cholesky, 3: HouseholderQR. -#' @param nthreads Number of threads for openmp +#' @details +#' n-step ahead forecasting using VHAR recursively. #' #' @noRd -expand_vhar <- function(y, week, month, include_mean, step, y_test, method, nthreads) { - .Call(`_bvhar_expand_vhar`, y, week, month, include_mean, step, y_test, method, nthreads) +forecast_vhar <- function(object, step) { + .Call(`_bvhar_forecast_vhar`, object, step) } -#' Out-of-Sample Forecasting of BVAR based on Expanding Window +#' Out-of-Sample Forecasting of VAR based on Rolling Window #' -#' This function conducts an expanding window forecasting of BVAR with Minnesota prior. +#' This function conducts an rolling window forecasting of VAR. #' #' @param y Time series data of which columns indicate the variables -#' @param lag BVAR order -#' @param bayes_spec List, BVAR specification +#' @param lag VAR order #' @param include_mean Add constant term #' @param step Integer, Step to forecast #' @param y_test Evaluation time series data period after `y` +#' @param method Method to solve linear equation system. 1: normal equation, 2: cholesky, 3: HouseholderQR. +#' @param nthreads Number of threads for openmp #' #' @noRd -expand_bvar <- function(y, lag, bayes_spec, include_mean, step, y_test, seed_forecast, nthreads) { - .Call(`_bvhar_expand_bvar`, y, lag, bayes_spec, include_mean, step, y_test, seed_forecast, nthreads) +roll_var <- function(y, lag, include_mean, step, y_test, method, nthreads) { + .Call(`_bvhar_roll_var`, y, lag, include_mean, step, y_test, method, nthreads) } -#' Out-of-Sample Forecasting of BVAR based on Expanding Window +#' Out-of-Sample Forecasting of VHAR based on Rolling Window #' -#' This function conducts an expanding window forecasting of BVAR with Flat prior. +#' This function conducts an rolling window forecasting of VHAR. #' #' @param y Time series data of which columns indicate the variables -#' @param lag BVAR order -#' @param bayes_spec List, BVAR specification +#' @param week Integer, order for weekly term +#' @param month Integer, order for monthly term #' @param include_mean Add constant term #' @param step Integer, Step to forecast #' @param y_test Evaluation time series data period after `y` +#' @param method Method to solve linear equation system. 1: normal equation, 2: cholesky, 3: HouseholderQR. +#' @param nthreads Number of threads for openmp #' #' @noRd -expand_bvarflat <- function(y, lag, U, include_mean, step, y_test, seed_forecast, nthreads) { - .Call(`_bvhar_expand_bvarflat`, y, lag, U, include_mean, step, y_test, seed_forecast, nthreads) +roll_vhar <- function(y, week, month, include_mean, step, y_test, method, nthreads) { + .Call(`_bvhar_roll_vhar`, y, week, month, include_mean, step, y_test, method, nthreads) } -#' Out-of-Sample Forecasting of BVHAR based on Expanding Window +#' Out-of-Sample Forecasting of VAR based on Expanding Window #' -#' This function conducts an expanding window forecasting of BVHAR with Minnesota prior. +#' This function conducts an expanding window forecasting of VAR. #' #' @param y Time series data of which columns indicate the variables -#' @param har `r lifecycle::badge("experimental")` Numeric vector for weekly and monthly order. -#' @param bayes_spec List, BVHAR specification +#' @param lag VAR order #' @param include_mean Add constant term #' @param step Integer, Step to forecast #' @param y_test Evaluation time series data period after `y` -#' -#' @noRd -expand_bvhar <- function(y, week, month, bayes_spec, include_mean, step, y_test, seed_forecast, nthreads) { - .Call(`_bvhar_expand_bvhar`, y, week, month, bayes_spec, include_mean, step, y_test, seed_forecast, nthreads) -} - -#' Out-of-Sample Forecasting of VAR-SV based on Rolling Window -#' -#' This function conducts an rolling window forecasting of BVAR-SV. -#' -#' @param y Time series data of which columns indicate the variables -#' @param lag VAR order -#' @param num_chains Number of MCMC chains -#' @param num_iter Number of iteration for MCMC -#' @param num_burn Number of burn-in (warm-up) for MCMC -#' @param thinning Thinning -#' @param param_sv SV specification list -#' @param param_prior Prior specification list -#' @param param_intercept Intercept specification list -#' @param param_init Initialization specification list -#' @param get_lpl Compute LPL -#' @param seed_chain Seed for each window and chain in the form of matrix -#' @param seed_forecast Seed for each window forecast +#' @param method Method to solve linear equation system. 1: normal equation, 2: cholesky, 3: HouseholderQR. #' @param nthreads Number of threads for openmp -#' @param grp_id Unique group id -#' @param grp_mat Group matrix -#' @param include_mean Constant term -#' @param stable Filter stable draws -#' @param step Integer, Step to forecast -#' @param y_test Evaluation time series data period after `y` -#' @param nthreads Number of threads #' #' @noRd -expand_bvarldlt <- function(y, lag, num_chains, num_iter, num_burn, thinning, sparse, level, fit_record, param_reg, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads) { - .Call(`_bvhar_expand_bvarldlt`, y, lag, num_chains, num_iter, num_burn, thinning, sparse, level, fit_record, param_reg, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads) +expand_var <- function(y, lag, include_mean, step, y_test, method, nthreads) { + .Call(`_bvhar_expand_var`, y, lag, include_mean, step, y_test, method, nthreads) } -#' Out-of-Sample Forecasting of VAR-SV based on Rolling Window +#' Out-of-Sample Forecasting of VHAR based on Expanding Window #' -#' This function conducts an rolling window forecasting of BVAR-SV. +#' This function conducts an expanding window forecasting of VHAR. #' #' @param y Time series data of which columns indicate the variables -#' @param lag VAR order -#' @param num_chains Number of MCMC chains -#' @param num_iter Number of iteration for MCMC -#' @param num_burn Number of burn-in (warm-up) for MCMC -#' @param thinning Thinning -#' @param param_sv SV specification list -#' @param param_prior Prior specification list -#' @param param_intercept Intercept specification list -#' @param param_init Initialization specification list -#' @param get_lpl Compute LPL -#' @param seed_chain Seed for each window and chain in the form of matrix -#' @param seed_forecast Seed for each window forecast -#' @param nthreads Number of threads for openmp -#' @param grp_id Unique group id -#' @param grp_mat Group matrix -#' @param include_mean Constant term -#' @param stable Filter stable draws +#' @param week Integer, order for weekly term +#' @param month Integer, order for monthly term +#' @param include_mean Add constant term #' @param step Integer, Step to forecast #' @param y_test Evaluation time series data period after `y` -#' @param nthreads Number of threads +#' @param method Method to solve linear equation system. 1: normal equation, 2: cholesky, 3: HouseholderQR. +#' @param nthreads Number of threads for openmp #' #' @noRd -expand_bvharldlt <- function(y, week, month, num_chains, num_iter, num_burn, thinning, sparse, level, fit_record, param_reg, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads) { - .Call(`_bvhar_expand_bvharldlt`, y, week, month, num_chains, num_iter, num_burn, thinning, sparse, level, fit_record, param_reg, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads) +expand_vhar <- function(y, week, month, include_mean, step, y_test, method, nthreads) { + .Call(`_bvhar_expand_vhar`, y, week, month, include_mean, step, y_test, method, nthreads) } -#' Out-of-Sample Forecasting of VAR-SV based on Rolling Window -#' -#' This function conducts an rolling window forecasting of BVAR-SV. +#' Generalized Spillover of VAR #' -#' @param y Time series data of which columns indicate the variables -#' @param lag VAR order -#' @param num_chains Number of MCMC chains -#' @param num_iter Number of iteration for MCMC -#' @param num_burn Number of burn-in (warm-up) for MCMC -#' @param thinning Thinning -#' @param param_sv SV specification list -#' @param param_prior Prior specification list -#' @param param_intercept Intercept specification list -#' @param param_init Initialization specification list -#' @param get_lpl Compute LPL -#' @param seed_chain Seed for each window and chain in the form of matrix -#' @param seed_forecast Seed for each window forecast -#' @param nthreads Number of threads for openmp -#' @param grp_id Unique group id -#' @param grp_mat Group matrix -#' @param include_mean Constant term -#' @param stable Filter stable draws -#' @param step Integer, Step to forecast -#' @param y_test Evaluation time series data period after `y` -#' @param nthreads Number of threads +#' @param object varlse or vharlse object. +#' @param step Step to forecast. #' #' @noRd -expand_bvarsv <- function(y, lag, num_chains, num_iter, num_burn, thinning, sv, sparse, level, fit_record, param_sv, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads) { - .Call(`_bvhar_expand_bvarsv`, y, lag, num_chains, num_iter, num_burn, thinning, sv, sparse, level, fit_record, param_sv, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads) +compute_ols_spillover <- function(object, step) { + .Call(`_bvhar_compute_ols_spillover`, object, step) } -#' Out-of-Sample Forecasting of VAR-SV based on Rolling Window -#' -#' This function conducts an rolling window forecasting of BVAR-SV. +#' Rolling-sample Total Spillover Index of VAR #' #' @param y Time series data of which columns indicate the variables +#' @param window Rolling window size +#' @param step forecast horizon for FEVD #' @param lag VAR order -#' @param num_chains Number of MCMC chains -#' @param num_iter Number of iteration for MCMC -#' @param num_burn Number of burn-in (warm-up) for MCMC -#' @param thinning Thinning -#' @param param_sv SV specification list -#' @param param_prior Prior specification list -#' @param param_intercept Intercept specification list -#' @param param_init Initialization specification list -#' @param get_lpl Compute LPL -#' @param seed_chain Seed for each window and chain in the form of matrix -#' @param seed_forecast Seed for each window forecast -#' @param nthreads Number of threads for openmp -#' @param grp_id Unique group id -#' @param grp_mat Group matrix -#' @param include_mean Constant term -#' @param stable Filter stable draws -#' @param step Integer, Step to forecast -#' @param y_test Evaluation time series data period after `y` -#' @param nthreads Number of threads -#' -#' @noRd -expand_bvharsv <- function(y, week, month, num_chains, num_iter, num_burn, thinning, sv, sparse, level, fit_record, param_sv, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads) { - .Call(`_bvhar_expand_bvharsv`, y, week, month, num_chains, num_iter, num_burn, thinning, sv, sparse, level, fit_record, param_sv, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads) -} - -#' Forecasting Vector Autoregression -#' -#' @param object A `varlse` object -#' @param step Integer, Step to forecast -#' @details -#' n-step ahead forecasting using VAR(p) recursively, based on pp35 of Lütkepohl (2007). -#' -#' @references Lütkepohl, H. (2007). *New Introduction to Multiple Time Series Analysis*. Springer Publishing. [https://doi.org/10.1007/978-3-540-27752-1](https://doi.org/10.1007/978-3-540-27752-1) -#' @noRd -forecast_var <- function(object, step) { - .Call(`_bvhar_forecast_var`, object, step) -} - -#' Forecasting Vector HAR -#' -#' @param object A `vharlse` object -#' @param step Integer, Step to forecast -#' @details -#' n-step ahead forecasting using VHAR recursively. +#' @param include_mean Add constant term #' #' @noRd -forecast_vhar <- function(object, step) { - .Call(`_bvhar_forecast_vhar`, object, step) +dynamic_var_spillover <- function(y, window, step, lag, include_mean, method, nthreads) { + .Call(`_bvhar_dynamic_var_spillover`, y, window, step, lag, include_mean, method, nthreads) } -#' Forecasting BVAR(p) -#' -#' @param object A `bvarmn` or `bvarflat` object -#' @param step Integer, Step to forecast -#' @param num_sim Integer, number to simulate parameters from posterior distribution -#' @details -#' n-step ahead forecasting using BVAR(p) recursively. -#' -#' For given number of simulation (`num_sim`), -#' -#' 1. Generate \eqn{(A^{(b)}, \Sigma_e^{(b)}) \sim MIW} (posterior) -#' 2. Recursively, \eqn{j = 1, \ldots, h} (`step`) -#' - Point forecast: Use \eqn{\hat{A}} -#' - Predictive distribution: Again generate \eqn{\tilde{Y}_{n + j}^{(b)} \sim A^{(b)}, \Sigma_e^{(b)} \sim MN} -#' - tilde notation indicates simulated ones -#' -#' @references -#' Lütkepohl, H. (2007). *New Introduction to Multiple Time Series Analysis*. Springer Publishing. [https://doi.org/10.1007/978-3-540-27752-1](https://doi.org/10.1007/978-3-540-27752-1) -#' -#' Litterman, R. B. (1986). *Forecasting with Bayesian Vector Autoregressions: Five Years of Experience*. Journal of Business & Economic Statistics, 4(1), 25. [https://doi:10.2307/1391384](https://doi:10.2307/1391384) -#' -#' Bańbura, M., Giannone, D., & Reichlin, L. (2010). *Large Bayesian vector auto regressions*. Journal of Applied Econometrics, 25(1). [https://doi:10.1002/jae.1137](https://doi:10.1002/jae.1137) -#' -#' Ghosh, S., Khare, K., & Michailidis, G. (2018). *High-Dimensional Posterior Consistency in Bayesian Vector Autoregressive Models*. Journal of the American Statistical Association, 114(526). [https://doi:10.1080/01621459.2018.1437043](https://doi:10.1080/01621459.2018.1437043) +#' Rolling-sample Total Spillover Index of VHAR #' -#' Karlsson, S. (2013). *Chapter 15 Forecasting with Bayesian Vector Autoregression*. Handbook of Economic Forecasting, 2, 791-897. doi:[10.1016/b978-0-444-62731-5.00015-4](https://doi.org/10.1016/B978-0-444-62731-5.00015-4) +#' @param y Time series data of which columns indicate the variables +#' @param window Rolling window size +#' @param step forecast horizon for FEVD +#' @param har VHAR order +#' @param include_mean Add constant term #' #' @noRd -forecast_bvar <- function(object, step, num_sim, seed) { - .Call(`_bvhar_forecast_bvar`, object, step, num_sim, seed) +dynamic_vhar_spillover <- function(y, window, step, week, month, include_mean, method, nthreads) { + .Call(`_bvhar_dynamic_vhar_spillover`, y, window, step, week, month, include_mean, method, nthreads) } -#' Forecasting Bayesian VHAR -#' -#' @param object A `bvharmn` object -#' @param step Integer, Step to forecast -#' @param num_sim Integer, number to simulate parameters from posterior distribution -#' @details -#' n-step ahead forecasting using VHAR recursively. +#' VAR with Shrinkage Priors #' -#' For given number of simulation (`num_sim`), +#' This function generates parameters \eqn{\beta, a, \sigma_{h,i}^2, h_{0,i}} and log-volatilities \eqn{h_{i,1}, \ldots, h_{i, n}}. #' -#' 1. Generate \eqn{(\Phi^{(b)}, \Sigma_e^{(b)}) \sim MIW} (posterior) -#' 2. Recursively, \eqn{j = 1, \ldots, h} (`step`) -#' - Point forecast: Use \eqn{\hat\Phi} -#' - Predictive distribution: Again generate \eqn{\tilde{Y}_{n + j}^{(b)} \sim \Phi^{(b)}, \Sigma_e^{(b)} \sim MN} -#' - tilde notation indicates simulated ones +#' @param num_chain Number of MCMC chains +#' @param num_iter Number of iteration for MCMC +#' @param num_burn Number of burn-in (warm-up) for MCMC +#' @param thin Thinning +#' @param x Design matrix X0 +#' @param y Response matrix Y0 +#' @param param_reg Regression specification list +#' @param param_prior Prior specification list +#' @param param_intercept Intercept specification list +#' @param param_init Initialization specification list +#' @param grp_id Unique group id +#' @param grp_mat Group matrix +#' @param include_mean Constant term +#' @param seed_chain Seed for each chain +#' @param display_progress Progress bar +#' @param nthreads Number of threads for openmp #' -#' @references Kim, Y. G., and Baek, C. (2024). *Bayesian vector heterogeneous autoregressive modeling*. Journal of Statistical Computation and Simulation, 94(6), 1139-1157. #' @noRd -forecast_bvharmn <- function(object, step, num_sim, seed) { - .Call(`_bvhar_forecast_bvharmn`, object, step, num_sim, seed) +estimate_sur <- function(num_chains, num_iter, num_burn, thin, x, y, param_reg, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, seed_chain, display_progress, nthreads) { + .Call(`_bvhar_estimate_sur`, num_chains, num_iter, num_burn, thin, x, y, param_reg, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, seed_chain, display_progress, nthreads) } #' Forecasting predictive density of BVAR @@ -677,176 +725,128 @@ roll_bvharsv <- function(y, week, month, num_chains, num_iter, num_burn, thinnin .Call(`_bvhar_roll_bvharsv`, y, week, month, num_chains, num_iter, num_burn, thinning, sv, sparse, level, fit_record, param_sv, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads) } -#' Out-of-Sample Forecasting of VAR based on Rolling Window +#' Out-of-Sample Forecasting of VAR-SV based on Rolling Window #' -#' This function conducts an rolling window forecasting of VAR. +#' This function conducts an rolling window forecasting of BVAR-SV. #' #' @param y Time series data of which columns indicate the variables #' @param lag VAR order -#' @param include_mean Add constant term -#' @param step Integer, Step to forecast -#' @param y_test Evaluation time series data period after `y` -#' @param method Method to solve linear equation system. 1: normal equation, 2: cholesky, 3: HouseholderQR. -#' @param nthreads Number of threads for openmp -#' -#' @noRd -roll_var <- function(y, lag, include_mean, step, y_test, method, nthreads) { - .Call(`_bvhar_roll_var`, y, lag, include_mean, step, y_test, method, nthreads) -} - -#' Out-of-Sample Forecasting of VHAR based on Rolling Window -#' -#' This function conducts an rolling window forecasting of VHAR. -#' -#' @param y Time series data of which columns indicate the variables -#' @param week Integer, order for weekly term -#' @param month Integer, order for monthly term -#' @param include_mean Add constant term -#' @param step Integer, Step to forecast -#' @param y_test Evaluation time series data period after `y` -#' @param method Method to solve linear equation system. 1: normal equation, 2: cholesky, 3: HouseholderQR. +#' @param num_chains Number of MCMC chains +#' @param num_iter Number of iteration for MCMC +#' @param num_burn Number of burn-in (warm-up) for MCMC +#' @param thinning Thinning +#' @param param_sv SV specification list +#' @param param_prior Prior specification list +#' @param param_intercept Intercept specification list +#' @param param_init Initialization specification list +#' @param get_lpl Compute LPL +#' @param seed_chain Seed for each window and chain in the form of matrix +#' @param seed_forecast Seed for each window forecast #' @param nthreads Number of threads for openmp -#' -#' @noRd -roll_vhar <- function(y, week, month, include_mean, step, y_test, method, nthreads) { - .Call(`_bvhar_roll_vhar`, y, week, month, include_mean, step, y_test, method, nthreads) -} - -#' Out-of-Sample Forecasting of BVAR based on Rolling Window -#' -#' This function conducts an rolling window forecasting of BVAR with Minnesota prior. -#' -#' @param y Time series data of which columns indicate the variables -#' @param lag BVAR order -#' @param bayes_spec List, BVAR specification -#' @param include_mean Add constant term +#' @param grp_id Unique group id +#' @param grp_mat Group matrix +#' @param include_mean Constant term +#' @param stable Filter stable draws #' @param step Integer, Step to forecast #' @param y_test Evaluation time series data period after `y` +#' @param nthreads Number of threads #' #' @noRd -roll_bvar <- function(y, lag, bayes_spec, include_mean, step, y_test, seed_forecast, nthreads) { - .Call(`_bvhar_roll_bvar`, y, lag, bayes_spec, include_mean, step, y_test, seed_forecast, nthreads) +expand_bvarldlt <- function(y, lag, num_chains, num_iter, num_burn, thinning, sparse, level, fit_record, param_reg, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads) { + .Call(`_bvhar_expand_bvarldlt`, y, lag, num_chains, num_iter, num_burn, thinning, sparse, level, fit_record, param_reg, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads) } -#' Out-of-Sample Forecasting of BVAR based on Rolling Window +#' Out-of-Sample Forecasting of VAR-SV based on Rolling Window #' -#' This function conducts an rolling window forecasting of BVAR with Flat prior. +#' This function conducts an rolling window forecasting of BVAR-SV. #' #' @param y Time series data of which columns indicate the variables -#' @param lag BVAR order -#' @param bayes_spec List, BVAR specification -#' @param include_mean Add constant term +#' @param lag VAR order +#' @param num_chains Number of MCMC chains +#' @param num_iter Number of iteration for MCMC +#' @param num_burn Number of burn-in (warm-up) for MCMC +#' @param thinning Thinning +#' @param param_sv SV specification list +#' @param param_prior Prior specification list +#' @param param_intercept Intercept specification list +#' @param param_init Initialization specification list +#' @param get_lpl Compute LPL +#' @param seed_chain Seed for each window and chain in the form of matrix +#' @param seed_forecast Seed for each window forecast +#' @param nthreads Number of threads for openmp +#' @param grp_id Unique group id +#' @param grp_mat Group matrix +#' @param include_mean Constant term +#' @param stable Filter stable draws #' @param step Integer, Step to forecast #' @param y_test Evaluation time series data period after `y` +#' @param nthreads Number of threads #' #' @noRd -roll_bvarflat <- function(y, lag, U, include_mean, step, y_test, seed_forecast, nthreads) { - .Call(`_bvhar_roll_bvarflat`, y, lag, U, include_mean, step, y_test, seed_forecast, nthreads) +expand_bvharldlt <- function(y, week, month, num_chains, num_iter, num_burn, thinning, sparse, level, fit_record, param_reg, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads) { + .Call(`_bvhar_expand_bvharldlt`, y, week, month, num_chains, num_iter, num_burn, thinning, sparse, level, fit_record, param_reg, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads) } -#' Out-of-Sample Forecasting of BVHAR based on Rolling Window +#' Out-of-Sample Forecasting of VAR-SV based on Rolling Window #' -#' This function conducts an rolling window forecasting of BVHAR with Minnesota prior. +#' This function conducts an rolling window forecasting of BVAR-SV. #' #' @param y Time series data of which columns indicate the variables -#' @param har `r lifecycle::badge("experimental")` Numeric vector for weekly and monthly order. -#' @param bayes_spec List, BVHAR specification -#' @param include_mean Add constant term +#' @param lag VAR order +#' @param num_chains Number of MCMC chains +#' @param num_iter Number of iteration for MCMC +#' @param num_burn Number of burn-in (warm-up) for MCMC +#' @param thinning Thinning +#' @param param_sv SV specification list +#' @param param_prior Prior specification list +#' @param param_intercept Intercept specification list +#' @param param_init Initialization specification list +#' @param get_lpl Compute LPL +#' @param seed_chain Seed for each window and chain in the form of matrix +#' @param seed_forecast Seed for each window forecast +#' @param nthreads Number of threads for openmp +#' @param grp_id Unique group id +#' @param grp_mat Group matrix +#' @param include_mean Constant term +#' @param stable Filter stable draws #' @param step Integer, Step to forecast #' @param y_test Evaluation time series data period after `y` +#' @param nthreads Number of threads #' #' @noRd -roll_bvhar <- function(y, week, month, bayes_spec, include_mean, step, y_test, seed_forecast, nthreads) { - .Call(`_bvhar_roll_bvhar`, y, week, month, bayes_spec, include_mean, step, y_test, seed_forecast, nthreads) +expand_bvarsv <- function(y, lag, num_chains, num_iter, num_burn, thinning, sv, sparse, level, fit_record, param_sv, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads) { + .Call(`_bvhar_expand_bvarsv`, y, lag, num_chains, num_iter, num_burn, thinning, sv, sparse, level, fit_record, param_sv, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads) } -#' Generalized Spillover of VAR -#' -#' @param object varlse or vharlse object. -#' @param step Step to forecast. +#' Out-of-Sample Forecasting of VAR-SV based on Rolling Window #' -#' @noRd -compute_ols_spillover <- function(object, step) { - .Call(`_bvhar_compute_ols_spillover`, object, step) -} - -#' Rolling-sample Total Spillover Index of VAR +#' This function conducts an rolling window forecasting of BVAR-SV. #' #' @param y Time series data of which columns indicate the variables -#' @param window Rolling window size -#' @param step forecast horizon for FEVD #' @param lag VAR order -#' @param include_mean Add constant term -#' -#' @noRd -dynamic_var_spillover <- function(y, window, step, lag, include_mean, method, nthreads) { - .Call(`_bvhar_dynamic_var_spillover`, y, window, step, lag, include_mean, method, nthreads) -} - -#' Rolling-sample Total Spillover Index of VHAR -#' -#' @param y Time series data of which columns indicate the variables -#' @param window Rolling window size -#' @param step forecast horizon for FEVD -#' @param har VHAR order -#' @param include_mean Add constant term -#' -#' @noRd -dynamic_vhar_spillover <- function(y, window, step, week, month, include_mean, method, nthreads) { - .Call(`_bvhar_dynamic_vhar_spillover`, y, window, step, week, month, include_mean, method, nthreads) -} - -#' Generalized Spillover of Minnesota prior -#' -#' @param object varlse or vharlse object. -#' @param step Step to forecast. -#' @param num_iter Number to sample MNIW distribution -#' @param num_burn Number of burn-in -#' @param thin Thinning -#' @param seed Random seed for boost library -#' -#' @noRd -compute_mn_spillover <- function(object, step, num_iter, num_burn, thin, seed) { - .Call(`_bvhar_compute_mn_spillover`, object, step, num_iter, num_burn, thin, seed) -} - -#' Rolling-sample Total Spillover Index of BVAR -#' -#' @param y Time series data of which columns indicate the variables -#' @param window Rolling window size -#' @param step forecast horizon for FEVD -#' @param num_iter Number to sample MNIW distribution -#' @param num_burn Number of burn-in -#' @param thin Thinning -#' @param lag BVAR order -#' @param bayes_spec BVAR specification -#' @param include_mean Add constant term -#' @param seed_chain Random seed for each window -#' @param nthreads Number of threads for openmp -#' -#' @noRd -dynamic_bvar_spillover <- function(y, window, step, num_iter, num_burn, thin, lag, bayes_spec, include_mean, seed_chain, nthreads) { - .Call(`_bvhar_dynamic_bvar_spillover`, y, window, step, num_iter, num_burn, thin, lag, bayes_spec, include_mean, seed_chain, nthreads) -} - -#' Rolling-sample Total Spillover Index of BVHAR -#' -#' @param y Time series data of which columns indicate the variables -#' @param window Rolling window size -#' @param step forecast horizon for FEVD -#' @param num_iter Number to sample MNIW distribution -#' @param num_burn Number of burn-in -#' @param thin Thinning -#' @param week Week order -#' @param month Month order -#' @param bayes_spec BVHAR specification -#' @param include_mean Add constant term -#' @param seed_chain Random seed for each window +#' @param num_chains Number of MCMC chains +#' @param num_iter Number of iteration for MCMC +#' @param num_burn Number of burn-in (warm-up) for MCMC +#' @param thinning Thinning +#' @param param_sv SV specification list +#' @param param_prior Prior specification list +#' @param param_intercept Intercept specification list +#' @param param_init Initialization specification list +#' @param get_lpl Compute LPL +#' @param seed_chain Seed for each window and chain in the form of matrix +#' @param seed_forecast Seed for each window forecast #' @param nthreads Number of threads for openmp +#' @param grp_id Unique group id +#' @param grp_mat Group matrix +#' @param include_mean Constant term +#' @param stable Filter stable draws +#' @param step Integer, Step to forecast +#' @param y_test Evaluation time series data period after `y` +#' @param nthreads Number of threads #' #' @noRd -dynamic_bvhar_spillover <- function(y, window, step, num_iter, num_burn, thin, week, month, bayes_spec, include_mean, seed_chain, nthreads) { - .Call(`_bvhar_dynamic_bvhar_spillover`, y, window, step, num_iter, num_burn, thin, week, month, bayes_spec, include_mean, seed_chain, nthreads) +expand_bvharsv <- function(y, week, month, num_chains, num_iter, num_burn, thinning, sv, sparse, level, fit_record, param_sv, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads) { + .Call(`_bvhar_expand_bvharsv`, y, week, month, num_chains, num_iter, num_burn, thinning, sv, sparse, level, fit_record, param_sv, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads) } compute_varldlt_spillover <- function(lag, step, fit_record, sparse) { diff --git a/inst/include/bvhar/bayesfit b/inst/include/bvhar/bayesfit deleted file mode 100644 index 35a8bfe6..00000000 --- a/inst/include/bvhar/bayesfit +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef BVHAR_BAYESFIT_H -#define BVHAR_BAYESFIT_H - -// MNIW -#include "src/bayes/mniw/minnesota.h" - -// CTA -#include "src/bayes/triangular/triangular.h" - -#endif // BVHAR_BAYESFIT_H \ No newline at end of file diff --git a/inst/include/bvhar/mniw b/inst/include/bvhar/mniw new file mode 100644 index 00000000..c41d6b67 --- /dev/null +++ b/inst/include/bvhar/mniw @@ -0,0 +1,10 @@ +#ifndef BVHAR_MNIW_H +#define BVHAR_MNIW_H + +#include "src/core/interrupt.h" + +#include "src/bayes/mniw/minnesota.h" +#include "src/bayes/mniw/forecaster.h" +#include "src/bayes/mniw/spillover.h" + +#endif // BVHAR_MNIW_H \ No newline at end of file diff --git a/inst/include/bvhar/triangular b/inst/include/bvhar/triangular new file mode 100644 index 00000000..ecf2bde8 --- /dev/null +++ b/inst/include/bvhar/triangular @@ -0,0 +1,8 @@ +#ifndef BVHAR_TRIANGULAR_H +#define BVHAR_TRIANGULAR_H + +#include "src/bayes/triangular/triangular.h" +#include "src/bayes/triangular/forecaster.h" +#include "src/bayes/triangular/spillover.h" + +#endif // BVHAR_TRIANGULAR_H \ No newline at end of file diff --git a/python/src/bvhar/_src/_bayes.cpp b/python/src/bvhar/_src/_bayes.cpp deleted file mode 100644 index 5aed53ff..00000000 --- a/python/src/bvhar/_src/_bayes.cpp +++ /dev/null @@ -1,39 +0,0 @@ -#include - -PYBIND11_MODULE(_bayes, m) { - py::class_>(m, "McmcLdlt") - .def( - py::init&, int, const Eigen::VectorXi&, const Eigen::VectorXi&, const Eigen::VectorXi&, - const Eigen::MatrixXi&, bool, const Eigen::VectorXi&, bool, int>() - ) - .def("returnRecords", &bvhar::McmcRun::returnRecords); - - py::class_>(m, "McmcLdltGrp") - .def( - py::init&, int, const Eigen::VectorXi&, const Eigen::VectorXi&, const Eigen::VectorXi&, - const Eigen::MatrixXi&, bool, const Eigen::VectorXi&, bool, int>() - ) - .def("returnRecords", &bvhar::McmcRun::returnRecords); - - py::class_>(m, "SvMcmc") - .def( - py::init&, int, const Eigen::VectorXi&, const Eigen::VectorXi&, const Eigen::VectorXi&, - const Eigen::MatrixXi&, bool, const Eigen::VectorXi&, bool, int>() - ) - .def("returnRecords", &bvhar::McmcRun::returnRecords); - - py::class_>(m, "SvGrpMcmc") - .def( - py::init&, int, const Eigen::VectorXi&, const Eigen::VectorXi&, const Eigen::VectorXi&, - const Eigen::MatrixXi&, bool, const Eigen::VectorXi&, bool, int>() - ) - .def("returnRecords", &bvhar::McmcRun::returnRecords); -} \ No newline at end of file diff --git a/python/src/bvhar/_src/_forecast.cpp b/python/src/bvhar/_src/_cta.cpp similarity index 76% rename from python/src/bvhar/_src/_forecast.cpp rename to python/src/bvhar/_src/_cta.cpp index 3171de1c..aa6425e0 100644 --- a/python/src/bvhar/_src/_forecast.cpp +++ b/python/src/bvhar/_src/_cta.cpp @@ -1,6 +1,42 @@ -#include +#include -PYBIND11_MODULE(_forecast, m) { +PYBIND11_MODULE(_cta, m) { + py::class_>(m, "McmcLdlt") + .def( + py::init&, int, const Eigen::VectorXi&, const Eigen::VectorXi&, const Eigen::VectorXi&, + const Eigen::MatrixXi&, bool, const Eigen::VectorXi&, bool, int>() + ) + .def("returnRecords", &bvhar::McmcRun::returnRecords); + + py::class_>(m, "McmcLdltGrp") + .def( + py::init&, int, const Eigen::VectorXi&, const Eigen::VectorXi&, const Eigen::VectorXi&, + const Eigen::MatrixXi&, bool, const Eigen::VectorXi&, bool, int>() + ) + .def("returnRecords", &bvhar::McmcRun::returnRecords); + + py::class_>(m, "SvMcmc") + .def( + py::init&, int, const Eigen::VectorXi&, const Eigen::VectorXi&, const Eigen::VectorXi&, + const Eigen::MatrixXi&, bool, const Eigen::VectorXi&, bool, int>() + ) + .def("returnRecords", &bvhar::McmcRun::returnRecords); + + py::class_>(m, "SvGrpMcmc") + .def( + py::init&, int, const Eigen::VectorXi&, const Eigen::VectorXi&, const Eigen::VectorXi&, + const Eigen::MatrixXi&, bool, const Eigen::VectorXi&, bool, int>() + ) + .def("returnRecords", &bvhar::McmcRun::returnRecords); + py::class_>(m, "LdltForecast") .def(py::init()) .def(py::init()) @@ -170,4 +206,34 @@ PYBIND11_MODULE(_forecast, m) { const Eigen::MatrixXi&, const Eigen::VectorXi&, bool, int, bool>() ) .def("returnForecast", &bvhar::McmcVharforecastRun::returnForecast); + + py::class_>(m, "LdltSpillover") + .def(py::init()) + .def(py::init()) + .def("returnSpillover", &bvhar::McmcSpilloverRun::returnSpillover); + + py::class_(m, "LdltDynamicSpillover") + .def( + py::init&, int, bool, + const Eigen::VectorXi&, const Eigen::VectorXi&, const Eigen::VectorXi&, const Eigen::MatrixXi&, + bool, const Eigen::MatrixXi&, int>() + ) + .def( + py::init&, int, bool, + const Eigen::VectorXi&, const Eigen::VectorXi&, const Eigen::VectorXi&, const Eigen::MatrixXi&, + bool, const Eigen::MatrixXi&, int>() + ) + .def("returnSpillover", &bvhar::DynamicLdltSpillover::returnSpillover); + + py::class_>(m, "SvSpillover") + .def(py::init()) + .def(py::init()) + .def("returnSpillover", &bvhar::McmcSpilloverRun::returnSpillover); + + py::class_(m, "SvDynamicSpillover") + .def(py::init()) + .def(py::init()) + .def("returnSpillover", &bvhar::DynamicSvSpillover::returnSpillover); } \ No newline at end of file diff --git a/python/src/bvhar/_src/_spillover.cpp b/python/src/bvhar/_src/_spillover.cpp deleted file mode 100644 index 8a4082e5..00000000 --- a/python/src/bvhar/_src/_spillover.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include - -PYBIND11_MODULE(_spillover, m) { - py::class_>(m, "LdltSpillover") - .def(py::init()) - .def(py::init()) - .def("returnSpillover", &bvhar::McmcSpilloverRun::returnSpillover); - - py::class_(m, "LdltDynamicSpillover") - .def( - py::init&, int, bool, - const Eigen::VectorXi&, const Eigen::VectorXi&, const Eigen::VectorXi&, const Eigen::MatrixXi&, - bool, const Eigen::MatrixXi&, int>() - ) - .def( - py::init&, int, bool, - const Eigen::VectorXi&, const Eigen::VectorXi&, const Eigen::VectorXi&, const Eigen::MatrixXi&, - bool, const Eigen::MatrixXi&, int>() - ) - .def("returnSpillover", &bvhar::DynamicLdltSpillover::returnSpillover); - - py::class_>(m, "SvSpillover") - .def(py::init()) - .def(py::init()) - .def("returnSpillover", &bvhar::McmcSpilloverRun::returnSpillover); - - py::class_(m, "SvDynamicSpillover") - .def(py::init()) - .def(py::init()) - .def("returnSpillover", &bvhar::DynamicSvSpillover::returnSpillover); -} \ No newline at end of file diff --git a/python/src/bvhar/model/_bayes.py b/python/src/bvhar/model/_bayes.py index f52e4e56..4e0042c3 100644 --- a/python/src/bvhar/model/_bayes.py +++ b/python/src/bvhar/model/_bayes.py @@ -1,12 +1,12 @@ from ..utils._misc import make_fortran_array, check_np, build_grpmat, process_record, concat_chain, concat_params, process_dens_forecast, process_dens_spillover, process_dens_vector_spillover, process_dens_list_spillover from ..utils.checkomp import get_maxomp from ..utils._utils import build_response, build_design -from .._src._bayes import McmcLdlt, McmcLdltGrp -from .._src._forecast import LdltForecast, LdltVarRoll, LdltVharRoll, LdltVarExpand, LdltVharExpand, LdltGrpVarRoll, LdltGrpVharRoll, LdltGrpVarExpand, LdltGrpVharExpand -from .._src._spillover import LdltSpillover, LdltDynamicSpillover -from .._src._bayes import SvMcmc, SvGrpMcmc -from .._src._forecast import SvForecast, SvVarRoll, SvVharRoll, SvVarExpand, SvVharExpand, SvGrpVarRoll, SvGrpVharRoll, SvGrpVarExpand, SvGrpVharExpand -from .._src._spillover import SvSpillover, SvDynamicSpillover +from .._src._cta import McmcLdlt, McmcLdltGrp +from .._src._cta import LdltForecast, LdltVarRoll, LdltVharRoll, LdltVarExpand, LdltVharExpand, LdltGrpVarRoll, LdltGrpVharRoll, LdltGrpVarExpand, LdltGrpVharExpand +from .._src._cta import LdltSpillover, LdltDynamicSpillover +from .._src._cta import SvMcmc, SvGrpMcmc +from .._src._cta import SvForecast, SvVarRoll, SvVharRoll, SvVarExpand, SvVharExpand, SvGrpVarRoll, SvGrpVharRoll, SvGrpVarExpand, SvGrpVharExpand +from .._src._cta import SvSpillover, SvDynamicSpillover from ._spec import LdltConfig, SvConfig, InterceptConfig from ._spec import _BayesConfig, SsvsConfig, HorseshoeConfig, MinnesotaConfig, DlConfig, NgConfig, GdpConfig import numpy as np diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index d8b64cdf..06703e15 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -98,33 +98,200 @@ BEGIN_RCPP return rcpp_result_gen; END_RCPP } -// estimate_sur -Rcpp::List estimate_sur(int num_chains, int num_iter, int num_burn, int thin, Eigen::MatrixXd x, Eigen::MatrixXd y, Rcpp::List param_reg, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, bool include_mean, Eigen::VectorXi seed_chain, bool display_progress, int nthreads); -RcppExport SEXP _bvhar_estimate_sur(SEXP num_chainsSEXP, SEXP num_iterSEXP, SEXP num_burnSEXP, SEXP thinSEXP, SEXP xSEXP, SEXP ySEXP, SEXP param_regSEXP, SEXP param_priorSEXP, SEXP param_interceptSEXP, SEXP param_initSEXP, SEXP prior_typeSEXP, SEXP gglSEXP, SEXP grp_idSEXP, SEXP own_idSEXP, SEXP cross_idSEXP, SEXP grp_matSEXP, SEXP include_meanSEXP, SEXP seed_chainSEXP, SEXP display_progressSEXP, SEXP nthreadsSEXP) { +// forecast_bvar +Rcpp::List forecast_bvar(Rcpp::List object, int step, int num_sim, unsigned int seed); +RcppExport SEXP _bvhar_forecast_bvar(SEXP objectSEXP, SEXP stepSEXP, SEXP num_simSEXP, SEXP seedSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< int >::type num_chains(num_chainsSEXP); + Rcpp::traits::input_parameter< Rcpp::List >::type object(objectSEXP); + Rcpp::traits::input_parameter< int >::type step(stepSEXP); + Rcpp::traits::input_parameter< int >::type num_sim(num_simSEXP); + Rcpp::traits::input_parameter< unsigned int >::type seed(seedSEXP); + rcpp_result_gen = Rcpp::wrap(forecast_bvar(object, step, num_sim, seed)); + return rcpp_result_gen; +END_RCPP +} +// forecast_bvharmn +Rcpp::List forecast_bvharmn(Rcpp::List object, int step, int num_sim, unsigned int seed); +RcppExport SEXP _bvhar_forecast_bvharmn(SEXP objectSEXP, SEXP stepSEXP, SEXP num_simSEXP, SEXP seedSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< Rcpp::List >::type object(objectSEXP); + Rcpp::traits::input_parameter< int >::type step(stepSEXP); + Rcpp::traits::input_parameter< int >::type num_sim(num_simSEXP); + Rcpp::traits::input_parameter< unsigned int >::type seed(seedSEXP); + rcpp_result_gen = Rcpp::wrap(forecast_bvharmn(object, step, num_sim, seed)); + return rcpp_result_gen; +END_RCPP +} +// roll_bvar +Eigen::MatrixXd roll_bvar(Eigen::MatrixXd y, int lag, Rcpp::List bayes_spec, bool include_mean, int step, Eigen::MatrixXd y_test, Eigen::VectorXi seed_forecast, int nthreads); +RcppExport SEXP _bvhar_roll_bvar(SEXP ySEXP, SEXP lagSEXP, SEXP bayes_specSEXP, SEXP include_meanSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP seed_forecastSEXP, SEXP nthreadsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); + Rcpp::traits::input_parameter< int >::type lag(lagSEXP); + Rcpp::traits::input_parameter< Rcpp::List >::type bayes_spec(bayes_specSEXP); + Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); + Rcpp::traits::input_parameter< int >::type step(stepSEXP); + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y_test(y_testSEXP); + Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_forecast(seed_forecastSEXP); + Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); + rcpp_result_gen = Rcpp::wrap(roll_bvar(y, lag, bayes_spec, include_mean, step, y_test, seed_forecast, nthreads)); + return rcpp_result_gen; +END_RCPP +} +// roll_bvarflat +Eigen::MatrixXd roll_bvarflat(Eigen::MatrixXd y, int lag, Eigen::MatrixXd U, bool include_mean, int step, Eigen::MatrixXd y_test, Eigen::VectorXi seed_forecast, int nthreads); +RcppExport SEXP _bvhar_roll_bvarflat(SEXP ySEXP, SEXP lagSEXP, SEXP USEXP, SEXP include_meanSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP seed_forecastSEXP, SEXP nthreadsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); + Rcpp::traits::input_parameter< int >::type lag(lagSEXP); + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type U(USEXP); + Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); + Rcpp::traits::input_parameter< int >::type step(stepSEXP); + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y_test(y_testSEXP); + Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_forecast(seed_forecastSEXP); + Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); + rcpp_result_gen = Rcpp::wrap(roll_bvarflat(y, lag, U, include_mean, step, y_test, seed_forecast, nthreads)); + return rcpp_result_gen; +END_RCPP +} +// roll_bvhar +Eigen::MatrixXd roll_bvhar(Eigen::MatrixXd y, int week, int month, Rcpp::List bayes_spec, bool include_mean, int step, Eigen::MatrixXd y_test, Eigen::VectorXi seed_forecast, int nthreads); +RcppExport SEXP _bvhar_roll_bvhar(SEXP ySEXP, SEXP weekSEXP, SEXP monthSEXP, SEXP bayes_specSEXP, SEXP include_meanSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP seed_forecastSEXP, SEXP nthreadsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); + Rcpp::traits::input_parameter< int >::type week(weekSEXP); + Rcpp::traits::input_parameter< int >::type month(monthSEXP); + Rcpp::traits::input_parameter< Rcpp::List >::type bayes_spec(bayes_specSEXP); + Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); + Rcpp::traits::input_parameter< int >::type step(stepSEXP); + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y_test(y_testSEXP); + Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_forecast(seed_forecastSEXP); + Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); + rcpp_result_gen = Rcpp::wrap(roll_bvhar(y, week, month, bayes_spec, include_mean, step, y_test, seed_forecast, nthreads)); + return rcpp_result_gen; +END_RCPP +} +// expand_bvar +Eigen::MatrixXd expand_bvar(Eigen::MatrixXd y, int lag, Rcpp::List bayes_spec, bool include_mean, int step, Eigen::MatrixXd y_test, Eigen::VectorXi seed_forecast, int nthreads); +RcppExport SEXP _bvhar_expand_bvar(SEXP ySEXP, SEXP lagSEXP, SEXP bayes_specSEXP, SEXP include_meanSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP seed_forecastSEXP, SEXP nthreadsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); + Rcpp::traits::input_parameter< int >::type lag(lagSEXP); + Rcpp::traits::input_parameter< Rcpp::List >::type bayes_spec(bayes_specSEXP); + Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); + Rcpp::traits::input_parameter< int >::type step(stepSEXP); + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y_test(y_testSEXP); + Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_forecast(seed_forecastSEXP); + Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); + rcpp_result_gen = Rcpp::wrap(expand_bvar(y, lag, bayes_spec, include_mean, step, y_test, seed_forecast, nthreads)); + return rcpp_result_gen; +END_RCPP +} +// expand_bvarflat +Eigen::MatrixXd expand_bvarflat(Eigen::MatrixXd y, int lag, Eigen::MatrixXd U, bool include_mean, int step, Eigen::MatrixXd y_test, Eigen::VectorXi seed_forecast, int nthreads); +RcppExport SEXP _bvhar_expand_bvarflat(SEXP ySEXP, SEXP lagSEXP, SEXP USEXP, SEXP include_meanSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP seed_forecastSEXP, SEXP nthreadsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); + Rcpp::traits::input_parameter< int >::type lag(lagSEXP); + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type U(USEXP); + Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); + Rcpp::traits::input_parameter< int >::type step(stepSEXP); + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y_test(y_testSEXP); + Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_forecast(seed_forecastSEXP); + Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); + rcpp_result_gen = Rcpp::wrap(expand_bvarflat(y, lag, U, include_mean, step, y_test, seed_forecast, nthreads)); + return rcpp_result_gen; +END_RCPP +} +// expand_bvhar +Eigen::MatrixXd expand_bvhar(Eigen::MatrixXd y, int week, int month, Rcpp::List bayes_spec, bool include_mean, int step, Eigen::MatrixXd y_test, Eigen::VectorXi seed_forecast, int nthreads); +RcppExport SEXP _bvhar_expand_bvhar(SEXP ySEXP, SEXP weekSEXP, SEXP monthSEXP, SEXP bayes_specSEXP, SEXP include_meanSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP seed_forecastSEXP, SEXP nthreadsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); + Rcpp::traits::input_parameter< int >::type week(weekSEXP); + Rcpp::traits::input_parameter< int >::type month(monthSEXP); + Rcpp::traits::input_parameter< Rcpp::List >::type bayes_spec(bayes_specSEXP); + Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); + Rcpp::traits::input_parameter< int >::type step(stepSEXP); + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y_test(y_testSEXP); + Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_forecast(seed_forecastSEXP); + Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); + rcpp_result_gen = Rcpp::wrap(expand_bvhar(y, week, month, bayes_spec, include_mean, step, y_test, seed_forecast, nthreads)); + return rcpp_result_gen; +END_RCPP +} +// compute_mn_spillover +Rcpp::List compute_mn_spillover(Rcpp::List object, int step, int num_iter, int num_burn, int thin, unsigned int seed); +RcppExport SEXP _bvhar_compute_mn_spillover(SEXP objectSEXP, SEXP stepSEXP, SEXP num_iterSEXP, SEXP num_burnSEXP, SEXP thinSEXP, SEXP seedSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< Rcpp::List >::type object(objectSEXP); + Rcpp::traits::input_parameter< int >::type step(stepSEXP); Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); Rcpp::traits::input_parameter< int >::type num_burn(num_burnSEXP); Rcpp::traits::input_parameter< int >::type thin(thinSEXP); - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type x(xSEXP); + Rcpp::traits::input_parameter< unsigned int >::type seed(seedSEXP); + rcpp_result_gen = Rcpp::wrap(compute_mn_spillover(object, step, num_iter, num_burn, thin, seed)); + return rcpp_result_gen; +END_RCPP +} +// dynamic_bvar_spillover +Rcpp::List dynamic_bvar_spillover(Eigen::MatrixXd y, int window, int step, int num_iter, int num_burn, int thin, int lag, Rcpp::List bayes_spec, bool include_mean, Eigen::VectorXi seed_chain, int nthreads); +RcppExport SEXP _bvhar_dynamic_bvar_spillover(SEXP ySEXP, SEXP windowSEXP, SEXP stepSEXP, SEXP num_iterSEXP, SEXP num_burnSEXP, SEXP thinSEXP, SEXP lagSEXP, SEXP bayes_specSEXP, SEXP include_meanSEXP, SEXP seed_chainSEXP, SEXP nthreadsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); - Rcpp::traits::input_parameter< Rcpp::List >::type param_reg(param_regSEXP); - Rcpp::traits::input_parameter< Rcpp::List >::type param_prior(param_priorSEXP); - Rcpp::traits::input_parameter< Rcpp::List >::type param_intercept(param_interceptSEXP); - Rcpp::traits::input_parameter< Rcpp::List >::type param_init(param_initSEXP); - Rcpp::traits::input_parameter< int >::type prior_type(prior_typeSEXP); - Rcpp::traits::input_parameter< bool >::type ggl(gglSEXP); - Rcpp::traits::input_parameter< Eigen::VectorXi >::type grp_id(grp_idSEXP); - Rcpp::traits::input_parameter< Eigen::VectorXi >::type own_id(own_idSEXP); - Rcpp::traits::input_parameter< Eigen::VectorXi >::type cross_id(cross_idSEXP); - Rcpp::traits::input_parameter< Eigen::MatrixXi >::type grp_mat(grp_matSEXP); + Rcpp::traits::input_parameter< int >::type window(windowSEXP); + Rcpp::traits::input_parameter< int >::type step(stepSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type num_burn(num_burnSEXP); + Rcpp::traits::input_parameter< int >::type thin(thinSEXP); + Rcpp::traits::input_parameter< int >::type lag(lagSEXP); + Rcpp::traits::input_parameter< Rcpp::List >::type bayes_spec(bayes_specSEXP); Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_chain(seed_chainSEXP); - Rcpp::traits::input_parameter< bool >::type display_progress(display_progressSEXP); Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); - rcpp_result_gen = Rcpp::wrap(estimate_sur(num_chains, num_iter, num_burn, thin, x, y, param_reg, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, seed_chain, display_progress, nthreads)); + rcpp_result_gen = Rcpp::wrap(dynamic_bvar_spillover(y, window, step, num_iter, num_burn, thin, lag, bayes_spec, include_mean, seed_chain, nthreads)); + return rcpp_result_gen; +END_RCPP +} +// dynamic_bvhar_spillover +Rcpp::List dynamic_bvhar_spillover(Eigen::MatrixXd y, int window, int step, int num_iter, int num_burn, int thin, int week, int month, Rcpp::List bayes_spec, bool include_mean, Eigen::VectorXi seed_chain, int nthreads); +RcppExport SEXP _bvhar_dynamic_bvhar_spillover(SEXP ySEXP, SEXP windowSEXP, SEXP stepSEXP, SEXP num_iterSEXP, SEXP num_burnSEXP, SEXP thinSEXP, SEXP weekSEXP, SEXP monthSEXP, SEXP bayes_specSEXP, SEXP include_meanSEXP, SEXP seed_chainSEXP, SEXP nthreadsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); + Rcpp::traits::input_parameter< int >::type window(windowSEXP); + Rcpp::traits::input_parameter< int >::type step(stepSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type num_burn(num_burnSEXP); + Rcpp::traits::input_parameter< int >::type thin(thinSEXP); + Rcpp::traits::input_parameter< int >::type week(weekSEXP); + Rcpp::traits::input_parameter< int >::type month(monthSEXP); + Rcpp::traits::input_parameter< Rcpp::List >::type bayes_spec(bayes_specSEXP); + Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); + Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_chain(seed_chainSEXP); + Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); + rcpp_result_gen = Rcpp::wrap(dynamic_bvhar_spillover(y, window, step, num_iter, num_burn, thin, week, month, bayes_spec, include_mean, seed_chain, nthreads)); return rcpp_result_gen; END_RCPP } @@ -192,6 +359,65 @@ BEGIN_RCPP return rcpp_result_gen; END_RCPP } +// forecast_var +Eigen::MatrixXd forecast_var(Rcpp::List object, int step); +RcppExport SEXP _bvhar_forecast_var(SEXP objectSEXP, SEXP stepSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< Rcpp::List >::type object(objectSEXP); + Rcpp::traits::input_parameter< int >::type step(stepSEXP); + rcpp_result_gen = Rcpp::wrap(forecast_var(object, step)); + return rcpp_result_gen; +END_RCPP +} +// forecast_vhar +Eigen::MatrixXd forecast_vhar(Rcpp::List object, int step); +RcppExport SEXP _bvhar_forecast_vhar(SEXP objectSEXP, SEXP stepSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< Rcpp::List >::type object(objectSEXP); + Rcpp::traits::input_parameter< int >::type step(stepSEXP); + rcpp_result_gen = Rcpp::wrap(forecast_vhar(object, step)); + return rcpp_result_gen; +END_RCPP +} +// roll_var +Eigen::MatrixXd roll_var(Eigen::MatrixXd y, int lag, bool include_mean, int step, Eigen::MatrixXd y_test, int method, int nthreads); +RcppExport SEXP _bvhar_roll_var(SEXP ySEXP, SEXP lagSEXP, SEXP include_meanSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP methodSEXP, SEXP nthreadsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); + Rcpp::traits::input_parameter< int >::type lag(lagSEXP); + Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); + Rcpp::traits::input_parameter< int >::type step(stepSEXP); + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y_test(y_testSEXP); + Rcpp::traits::input_parameter< int >::type method(methodSEXP); + Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); + rcpp_result_gen = Rcpp::wrap(roll_var(y, lag, include_mean, step, y_test, method, nthreads)); + return rcpp_result_gen; +END_RCPP +} +// roll_vhar +Eigen::MatrixXd roll_vhar(Eigen::MatrixXd y, int week, int month, bool include_mean, int step, Eigen::MatrixXd y_test, int method, int nthreads); +RcppExport SEXP _bvhar_roll_vhar(SEXP ySEXP, SEXP weekSEXP, SEXP monthSEXP, SEXP include_meanSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP methodSEXP, SEXP nthreadsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); + Rcpp::traits::input_parameter< int >::type week(weekSEXP); + Rcpp::traits::input_parameter< int >::type month(monthSEXP); + Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); + Rcpp::traits::input_parameter< int >::type step(stepSEXP); + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y_test(y_testSEXP); + Rcpp::traits::input_parameter< int >::type method(methodSEXP); + Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); + rcpp_result_gen = Rcpp::wrap(roll_vhar(y, week, month, include_mean, step, y_test, method, nthreads)); + return rcpp_result_gen; +END_RCPP +} // expand_var Eigen::MatrixXd expand_var(Eigen::MatrixXd y, int lag, bool include_mean, int step, Eigen::MatrixXd y_test, int method, int nthreads); RcppExport SEXP _bvhar_expand_var(SEXP ySEXP, SEXP lagSEXP, SEXP include_meanSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP methodSEXP, SEXP nthreadsSEXP) { @@ -227,76 +453,65 @@ BEGIN_RCPP return rcpp_result_gen; END_RCPP } -// expand_bvar -Eigen::MatrixXd expand_bvar(Eigen::MatrixXd y, int lag, Rcpp::List bayes_spec, bool include_mean, int step, Eigen::MatrixXd y_test, Eigen::VectorXi seed_forecast, int nthreads); -RcppExport SEXP _bvhar_expand_bvar(SEXP ySEXP, SEXP lagSEXP, SEXP bayes_specSEXP, SEXP include_meanSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP seed_forecastSEXP, SEXP nthreadsSEXP) { +// compute_ols_spillover +Rcpp::List compute_ols_spillover(Rcpp::List object, int step); +RcppExport SEXP _bvhar_compute_ols_spillover(SEXP objectSEXP, SEXP stepSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); - Rcpp::traits::input_parameter< int >::type lag(lagSEXP); - Rcpp::traits::input_parameter< Rcpp::List >::type bayes_spec(bayes_specSEXP); - Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); + Rcpp::traits::input_parameter< Rcpp::List >::type object(objectSEXP); Rcpp::traits::input_parameter< int >::type step(stepSEXP); - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y_test(y_testSEXP); - Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_forecast(seed_forecastSEXP); - Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); - rcpp_result_gen = Rcpp::wrap(expand_bvar(y, lag, bayes_spec, include_mean, step, y_test, seed_forecast, nthreads)); + rcpp_result_gen = Rcpp::wrap(compute_ols_spillover(object, step)); return rcpp_result_gen; END_RCPP } -// expand_bvarflat -Eigen::MatrixXd expand_bvarflat(Eigen::MatrixXd y, int lag, Eigen::MatrixXd U, bool include_mean, int step, Eigen::MatrixXd y_test, Eigen::VectorXi seed_forecast, int nthreads); -RcppExport SEXP _bvhar_expand_bvarflat(SEXP ySEXP, SEXP lagSEXP, SEXP USEXP, SEXP include_meanSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP seed_forecastSEXP, SEXP nthreadsSEXP) { +// dynamic_var_spillover +Rcpp::List dynamic_var_spillover(Eigen::MatrixXd y, int window, int step, int lag, bool include_mean, int method, int nthreads); +RcppExport SEXP _bvhar_dynamic_var_spillover(SEXP ySEXP, SEXP windowSEXP, SEXP stepSEXP, SEXP lagSEXP, SEXP include_meanSEXP, SEXP methodSEXP, SEXP nthreadsSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); + Rcpp::traits::input_parameter< int >::type window(windowSEXP); + Rcpp::traits::input_parameter< int >::type step(stepSEXP); Rcpp::traits::input_parameter< int >::type lag(lagSEXP); - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type U(USEXP); Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); - Rcpp::traits::input_parameter< int >::type step(stepSEXP); - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y_test(y_testSEXP); - Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_forecast(seed_forecastSEXP); + Rcpp::traits::input_parameter< int >::type method(methodSEXP); Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); - rcpp_result_gen = Rcpp::wrap(expand_bvarflat(y, lag, U, include_mean, step, y_test, seed_forecast, nthreads)); + rcpp_result_gen = Rcpp::wrap(dynamic_var_spillover(y, window, step, lag, include_mean, method, nthreads)); return rcpp_result_gen; END_RCPP } -// expand_bvhar -Eigen::MatrixXd expand_bvhar(Eigen::MatrixXd y, int week, int month, Rcpp::List bayes_spec, bool include_mean, int step, Eigen::MatrixXd y_test, Eigen::VectorXi seed_forecast, int nthreads); -RcppExport SEXP _bvhar_expand_bvhar(SEXP ySEXP, SEXP weekSEXP, SEXP monthSEXP, SEXP bayes_specSEXP, SEXP include_meanSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP seed_forecastSEXP, SEXP nthreadsSEXP) { +// dynamic_vhar_spillover +Rcpp::List dynamic_vhar_spillover(Eigen::MatrixXd y, int window, int step, int week, int month, bool include_mean, int method, int nthreads); +RcppExport SEXP _bvhar_dynamic_vhar_spillover(SEXP ySEXP, SEXP windowSEXP, SEXP stepSEXP, SEXP weekSEXP, SEXP monthSEXP, SEXP include_meanSEXP, SEXP methodSEXP, SEXP nthreadsSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); + Rcpp::traits::input_parameter< int >::type window(windowSEXP); + Rcpp::traits::input_parameter< int >::type step(stepSEXP); Rcpp::traits::input_parameter< int >::type week(weekSEXP); Rcpp::traits::input_parameter< int >::type month(monthSEXP); - Rcpp::traits::input_parameter< Rcpp::List >::type bayes_spec(bayes_specSEXP); Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); - Rcpp::traits::input_parameter< int >::type step(stepSEXP); - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y_test(y_testSEXP); - Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_forecast(seed_forecastSEXP); + Rcpp::traits::input_parameter< int >::type method(methodSEXP); Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); - rcpp_result_gen = Rcpp::wrap(expand_bvhar(y, week, month, bayes_spec, include_mean, step, y_test, seed_forecast, nthreads)); + rcpp_result_gen = Rcpp::wrap(dynamic_vhar_spillover(y, window, step, week, month, include_mean, method, nthreads)); return rcpp_result_gen; END_RCPP } -// expand_bvarldlt -Rcpp::List expand_bvarldlt(Eigen::MatrixXd y, int lag, int num_chains, int num_iter, int num_burn, int thinning, bool sparse, double level, Rcpp::List fit_record, Rcpp::List param_reg, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads); -RcppExport SEXP _bvhar_expand_bvarldlt(SEXP ySEXP, SEXP lagSEXP, SEXP num_chainsSEXP, SEXP num_iterSEXP, SEXP num_burnSEXP, SEXP thinningSEXP, SEXP sparseSEXP, SEXP levelSEXP, SEXP fit_recordSEXP, SEXP param_regSEXP, SEXP param_priorSEXP, SEXP param_interceptSEXP, SEXP param_initSEXP, SEXP prior_typeSEXP, SEXP gglSEXP, SEXP grp_idSEXP, SEXP own_idSEXP, SEXP cross_idSEXP, SEXP grp_matSEXP, SEXP include_meanSEXP, SEXP stableSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP get_lplSEXP, SEXP seed_chainSEXP, SEXP seed_forecastSEXP, SEXP display_progressSEXP, SEXP nthreadsSEXP) { +// estimate_sur +Rcpp::List estimate_sur(int num_chains, int num_iter, int num_burn, int thin, Eigen::MatrixXd x, Eigen::MatrixXd y, Rcpp::List param_reg, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, bool include_mean, Eigen::VectorXi seed_chain, bool display_progress, int nthreads); +RcppExport SEXP _bvhar_estimate_sur(SEXP num_chainsSEXP, SEXP num_iterSEXP, SEXP num_burnSEXP, SEXP thinSEXP, SEXP xSEXP, SEXP ySEXP, SEXP param_regSEXP, SEXP param_priorSEXP, SEXP param_interceptSEXP, SEXP param_initSEXP, SEXP prior_typeSEXP, SEXP gglSEXP, SEXP grp_idSEXP, SEXP own_idSEXP, SEXP cross_idSEXP, SEXP grp_matSEXP, SEXP include_meanSEXP, SEXP seed_chainSEXP, SEXP display_progressSEXP, SEXP nthreadsSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); - Rcpp::traits::input_parameter< int >::type lag(lagSEXP); Rcpp::traits::input_parameter< int >::type num_chains(num_chainsSEXP); Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); Rcpp::traits::input_parameter< int >::type num_burn(num_burnSEXP); - Rcpp::traits::input_parameter< int >::type thinning(thinningSEXP); - Rcpp::traits::input_parameter< bool >::type sparse(sparseSEXP); - Rcpp::traits::input_parameter< double >::type level(levelSEXP); - Rcpp::traits::input_parameter< Rcpp::List >::type fit_record(fit_recordSEXP); + Rcpp::traits::input_parameter< int >::type thin(thinSEXP); + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type x(xSEXP); + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); Rcpp::traits::input_parameter< Rcpp::List >::type param_reg(param_regSEXP); Rcpp::traits::input_parameter< Rcpp::List >::type param_prior(param_priorSEXP); Rcpp::traits::input_parameter< Rcpp::List >::type param_intercept(param_interceptSEXP); @@ -308,60 +523,61 @@ BEGIN_RCPP Rcpp::traits::input_parameter< Eigen::VectorXi >::type cross_id(cross_idSEXP); Rcpp::traits::input_parameter< Eigen::MatrixXi >::type grp_mat(grp_matSEXP); Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); - Rcpp::traits::input_parameter< bool >::type stable(stableSEXP); - Rcpp::traits::input_parameter< int >::type step(stepSEXP); - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y_test(y_testSEXP); - Rcpp::traits::input_parameter< bool >::type get_lpl(get_lplSEXP); - Rcpp::traits::input_parameter< Eigen::MatrixXi >::type seed_chain(seed_chainSEXP); - Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_forecast(seed_forecastSEXP); + Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_chain(seed_chainSEXP); Rcpp::traits::input_parameter< bool >::type display_progress(display_progressSEXP); Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); - rcpp_result_gen = Rcpp::wrap(expand_bvarldlt(y, lag, num_chains, num_iter, num_burn, thinning, sparse, level, fit_record, param_reg, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads)); + rcpp_result_gen = Rcpp::wrap(estimate_sur(num_chains, num_iter, num_burn, thin, x, y, param_reg, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, seed_chain, display_progress, nthreads)); return rcpp_result_gen; END_RCPP } -// expand_bvharldlt -Rcpp::List expand_bvharldlt(Eigen::MatrixXd y, int week, int month, int num_chains, int num_iter, int num_burn, int thinning, bool sparse, double level, Rcpp::List fit_record, Rcpp::List param_reg, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads); -RcppExport SEXP _bvhar_expand_bvharldlt(SEXP ySEXP, SEXP weekSEXP, SEXP monthSEXP, SEXP num_chainsSEXP, SEXP num_iterSEXP, SEXP num_burnSEXP, SEXP thinningSEXP, SEXP sparseSEXP, SEXP levelSEXP, SEXP fit_recordSEXP, SEXP param_regSEXP, SEXP param_priorSEXP, SEXP param_interceptSEXP, SEXP param_initSEXP, SEXP prior_typeSEXP, SEXP gglSEXP, SEXP grp_idSEXP, SEXP own_idSEXP, SEXP cross_idSEXP, SEXP grp_matSEXP, SEXP include_meanSEXP, SEXP stableSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP get_lplSEXP, SEXP seed_chainSEXP, SEXP seed_forecastSEXP, SEXP display_progressSEXP, SEXP nthreadsSEXP) { +// forecast_bvarldlt +Rcpp::List forecast_bvarldlt(int num_chains, int var_lag, int step, Eigen::MatrixXd response_mat, bool sparse, double level, Rcpp::List fit_record, int prior_type, Eigen::VectorXi seed_chain, bool include_mean, bool stable, int nthreads); +RcppExport SEXP _bvhar_forecast_bvarldlt(SEXP num_chainsSEXP, SEXP var_lagSEXP, SEXP stepSEXP, SEXP response_matSEXP, SEXP sparseSEXP, SEXP levelSEXP, SEXP fit_recordSEXP, SEXP prior_typeSEXP, SEXP seed_chainSEXP, SEXP include_meanSEXP, SEXP stableSEXP, SEXP nthreadsSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); - Rcpp::traits::input_parameter< int >::type week(weekSEXP); - Rcpp::traits::input_parameter< int >::type month(monthSEXP); Rcpp::traits::input_parameter< int >::type num_chains(num_chainsSEXP); - Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); - Rcpp::traits::input_parameter< int >::type num_burn(num_burnSEXP); - Rcpp::traits::input_parameter< int >::type thinning(thinningSEXP); + Rcpp::traits::input_parameter< int >::type var_lag(var_lagSEXP); + Rcpp::traits::input_parameter< int >::type step(stepSEXP); + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type response_mat(response_matSEXP); Rcpp::traits::input_parameter< bool >::type sparse(sparseSEXP); Rcpp::traits::input_parameter< double >::type level(levelSEXP); Rcpp::traits::input_parameter< Rcpp::List >::type fit_record(fit_recordSEXP); - Rcpp::traits::input_parameter< Rcpp::List >::type param_reg(param_regSEXP); - Rcpp::traits::input_parameter< Rcpp::List >::type param_prior(param_priorSEXP); - Rcpp::traits::input_parameter< Rcpp::List >::type param_intercept(param_interceptSEXP); - Rcpp::traits::input_parameter< Rcpp::List >::type param_init(param_initSEXP); Rcpp::traits::input_parameter< int >::type prior_type(prior_typeSEXP); - Rcpp::traits::input_parameter< bool >::type ggl(gglSEXP); - Rcpp::traits::input_parameter< Eigen::VectorXi >::type grp_id(grp_idSEXP); - Rcpp::traits::input_parameter< Eigen::VectorXi >::type own_id(own_idSEXP); - Rcpp::traits::input_parameter< Eigen::VectorXi >::type cross_id(cross_idSEXP); - Rcpp::traits::input_parameter< Eigen::MatrixXi >::type grp_mat(grp_matSEXP); + Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_chain(seed_chainSEXP); Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); Rcpp::traits::input_parameter< bool >::type stable(stableSEXP); + Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); + rcpp_result_gen = Rcpp::wrap(forecast_bvarldlt(num_chains, var_lag, step, response_mat, sparse, level, fit_record, prior_type, seed_chain, include_mean, stable, nthreads)); + return rcpp_result_gen; +END_RCPP +} +// forecast_bvharldlt +Rcpp::List forecast_bvharldlt(int num_chains, int month, int step, Eigen::MatrixXd response_mat, Eigen::MatrixXd HARtrans, bool sparse, double level, Rcpp::List fit_record, int prior_type, Eigen::VectorXi seed_chain, bool include_mean, bool stable, int nthreads); +RcppExport SEXP _bvhar_forecast_bvharldlt(SEXP num_chainsSEXP, SEXP monthSEXP, SEXP stepSEXP, SEXP response_matSEXP, SEXP HARtransSEXP, SEXP sparseSEXP, SEXP levelSEXP, SEXP fit_recordSEXP, SEXP prior_typeSEXP, SEXP seed_chainSEXP, SEXP include_meanSEXP, SEXP stableSEXP, SEXP nthreadsSEXP) { +BEGIN_RCPP + Rcpp::RObject rcpp_result_gen; + Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< int >::type num_chains(num_chainsSEXP); + Rcpp::traits::input_parameter< int >::type month(monthSEXP); Rcpp::traits::input_parameter< int >::type step(stepSEXP); - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y_test(y_testSEXP); - Rcpp::traits::input_parameter< bool >::type get_lpl(get_lplSEXP); - Rcpp::traits::input_parameter< Eigen::MatrixXi >::type seed_chain(seed_chainSEXP); - Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_forecast(seed_forecastSEXP); - Rcpp::traits::input_parameter< bool >::type display_progress(display_progressSEXP); + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type response_mat(response_matSEXP); + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type HARtrans(HARtransSEXP); + Rcpp::traits::input_parameter< bool >::type sparse(sparseSEXP); + Rcpp::traits::input_parameter< double >::type level(levelSEXP); + Rcpp::traits::input_parameter< Rcpp::List >::type fit_record(fit_recordSEXP); + Rcpp::traits::input_parameter< int >::type prior_type(prior_typeSEXP); + Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_chain(seed_chainSEXP); + Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); + Rcpp::traits::input_parameter< bool >::type stable(stableSEXP); Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); - rcpp_result_gen = Rcpp::wrap(expand_bvharldlt(y, week, month, num_chains, num_iter, num_burn, thinning, sparse, level, fit_record, param_reg, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads)); + rcpp_result_gen = Rcpp::wrap(forecast_bvharldlt(num_chains, month, step, response_mat, HARtrans, sparse, level, fit_record, prior_type, seed_chain, include_mean, stable, nthreads)); return rcpp_result_gen; END_RCPP } -// expand_bvarsv -Rcpp::List expand_bvarsv(Eigen::MatrixXd y, int lag, int num_chains, int num_iter, int num_burn, int thinning, bool sv, bool sparse, double level, Rcpp::List fit_record, Rcpp::List param_sv, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads); -RcppExport SEXP _bvhar_expand_bvarsv(SEXP ySEXP, SEXP lagSEXP, SEXP num_chainsSEXP, SEXP num_iterSEXP, SEXP num_burnSEXP, SEXP thinningSEXP, SEXP svSEXP, SEXP sparseSEXP, SEXP levelSEXP, SEXP fit_recordSEXP, SEXP param_svSEXP, SEXP param_priorSEXP, SEXP param_interceptSEXP, SEXP param_initSEXP, SEXP prior_typeSEXP, SEXP gglSEXP, SEXP grp_idSEXP, SEXP own_idSEXP, SEXP cross_idSEXP, SEXP grp_matSEXP, SEXP include_meanSEXP, SEXP stableSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP get_lplSEXP, SEXP seed_chainSEXP, SEXP seed_forecastSEXP, SEXP display_progressSEXP, SEXP nthreadsSEXP) { +// roll_bvarldlt +Rcpp::List roll_bvarldlt(Eigen::MatrixXd y, int lag, int num_chains, int num_iter, int num_burn, int thinning, bool sparse, double level, Rcpp::List fit_record, Rcpp::List param_reg, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads); +RcppExport SEXP _bvhar_roll_bvarldlt(SEXP ySEXP, SEXP lagSEXP, SEXP num_chainsSEXP, SEXP num_iterSEXP, SEXP num_burnSEXP, SEXP thinningSEXP, SEXP sparseSEXP, SEXP levelSEXP, SEXP fit_recordSEXP, SEXP param_regSEXP, SEXP param_priorSEXP, SEXP param_interceptSEXP, SEXP param_initSEXP, SEXP prior_typeSEXP, SEXP gglSEXP, SEXP grp_idSEXP, SEXP own_idSEXP, SEXP cross_idSEXP, SEXP grp_matSEXP, SEXP include_meanSEXP, SEXP stableSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP get_lplSEXP, SEXP seed_chainSEXP, SEXP seed_forecastSEXP, SEXP display_progressSEXP, SEXP nthreadsSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; @@ -371,11 +587,10 @@ BEGIN_RCPP Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); Rcpp::traits::input_parameter< int >::type num_burn(num_burnSEXP); Rcpp::traits::input_parameter< int >::type thinning(thinningSEXP); - Rcpp::traits::input_parameter< bool >::type sv(svSEXP); Rcpp::traits::input_parameter< bool >::type sparse(sparseSEXP); Rcpp::traits::input_parameter< double >::type level(levelSEXP); Rcpp::traits::input_parameter< Rcpp::List >::type fit_record(fit_recordSEXP); - Rcpp::traits::input_parameter< Rcpp::List >::type param_sv(param_svSEXP); + Rcpp::traits::input_parameter< Rcpp::List >::type param_reg(param_regSEXP); Rcpp::traits::input_parameter< Rcpp::List >::type param_prior(param_priorSEXP); Rcpp::traits::input_parameter< Rcpp::List >::type param_intercept(param_interceptSEXP); Rcpp::traits::input_parameter< Rcpp::List >::type param_init(param_initSEXP); @@ -394,13 +609,13 @@ BEGIN_RCPP Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_forecast(seed_forecastSEXP); Rcpp::traits::input_parameter< bool >::type display_progress(display_progressSEXP); Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); - rcpp_result_gen = Rcpp::wrap(expand_bvarsv(y, lag, num_chains, num_iter, num_burn, thinning, sv, sparse, level, fit_record, param_sv, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads)); + rcpp_result_gen = Rcpp::wrap(roll_bvarldlt(y, lag, num_chains, num_iter, num_burn, thinning, sparse, level, fit_record, param_reg, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads)); return rcpp_result_gen; END_RCPP } -// expand_bvharsv -Rcpp::List expand_bvharsv(Eigen::MatrixXd y, int week, int month, int num_chains, int num_iter, int num_burn, int thinning, bool sv, bool sparse, double level, Rcpp::List fit_record, Rcpp::List param_sv, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads); -RcppExport SEXP _bvhar_expand_bvharsv(SEXP ySEXP, SEXP weekSEXP, SEXP monthSEXP, SEXP num_chainsSEXP, SEXP num_iterSEXP, SEXP num_burnSEXP, SEXP thinningSEXP, SEXP svSEXP, SEXP sparseSEXP, SEXP levelSEXP, SEXP fit_recordSEXP, SEXP param_svSEXP, SEXP param_priorSEXP, SEXP param_interceptSEXP, SEXP param_initSEXP, SEXP prior_typeSEXP, SEXP gglSEXP, SEXP grp_idSEXP, SEXP own_idSEXP, SEXP cross_idSEXP, SEXP grp_matSEXP, SEXP include_meanSEXP, SEXP stableSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP get_lplSEXP, SEXP seed_chainSEXP, SEXP seed_forecastSEXP, SEXP display_progressSEXP, SEXP nthreadsSEXP) { +// roll_bvharldlt +Rcpp::List roll_bvharldlt(Eigen::MatrixXd y, int week, int month, int num_chains, int num_iter, int num_burn, int thinning, bool sparse, double level, Rcpp::List fit_record, Rcpp::List param_reg, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads); +RcppExport SEXP _bvhar_roll_bvharldlt(SEXP ySEXP, SEXP weekSEXP, SEXP monthSEXP, SEXP num_chainsSEXP, SEXP num_iterSEXP, SEXP num_burnSEXP, SEXP thinningSEXP, SEXP sparseSEXP, SEXP levelSEXP, SEXP fit_recordSEXP, SEXP param_regSEXP, SEXP param_priorSEXP, SEXP param_interceptSEXP, SEXP param_initSEXP, SEXP prior_typeSEXP, SEXP gglSEXP, SEXP grp_idSEXP, SEXP own_idSEXP, SEXP cross_idSEXP, SEXP grp_matSEXP, SEXP include_meanSEXP, SEXP stableSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP get_lplSEXP, SEXP seed_chainSEXP, SEXP seed_forecastSEXP, SEXP display_progressSEXP, SEXP nthreadsSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; @@ -411,11 +626,10 @@ BEGIN_RCPP Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); Rcpp::traits::input_parameter< int >::type num_burn(num_burnSEXP); Rcpp::traits::input_parameter< int >::type thinning(thinningSEXP); - Rcpp::traits::input_parameter< bool >::type sv(svSEXP); Rcpp::traits::input_parameter< bool >::type sparse(sparseSEXP); Rcpp::traits::input_parameter< double >::type level(levelSEXP); Rcpp::traits::input_parameter< Rcpp::List >::type fit_record(fit_recordSEXP); - Rcpp::traits::input_parameter< Rcpp::List >::type param_sv(param_svSEXP); + Rcpp::traits::input_parameter< Rcpp::List >::type param_reg(param_regSEXP); Rcpp::traits::input_parameter< Rcpp::List >::type param_prior(param_priorSEXP); Rcpp::traits::input_parameter< Rcpp::List >::type param_intercept(param_interceptSEXP); Rcpp::traits::input_parameter< Rcpp::List >::type param_init(param_initSEXP); @@ -434,65 +648,13 @@ BEGIN_RCPP Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_forecast(seed_forecastSEXP); Rcpp::traits::input_parameter< bool >::type display_progress(display_progressSEXP); Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); - rcpp_result_gen = Rcpp::wrap(expand_bvharsv(y, week, month, num_chains, num_iter, num_burn, thinning, sv, sparse, level, fit_record, param_sv, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads)); - return rcpp_result_gen; -END_RCPP -} -// forecast_var -Eigen::MatrixXd forecast_var(Rcpp::List object, int step); -RcppExport SEXP _bvhar_forecast_var(SEXP objectSEXP, SEXP stepSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< Rcpp::List >::type object(objectSEXP); - Rcpp::traits::input_parameter< int >::type step(stepSEXP); - rcpp_result_gen = Rcpp::wrap(forecast_var(object, step)); - return rcpp_result_gen; -END_RCPP -} -// forecast_vhar -Eigen::MatrixXd forecast_vhar(Rcpp::List object, int step); -RcppExport SEXP _bvhar_forecast_vhar(SEXP objectSEXP, SEXP stepSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< Rcpp::List >::type object(objectSEXP); - Rcpp::traits::input_parameter< int >::type step(stepSEXP); - rcpp_result_gen = Rcpp::wrap(forecast_vhar(object, step)); - return rcpp_result_gen; -END_RCPP -} -// forecast_bvar -Rcpp::List forecast_bvar(Rcpp::List object, int step, int num_sim, unsigned int seed); -RcppExport SEXP _bvhar_forecast_bvar(SEXP objectSEXP, SEXP stepSEXP, SEXP num_simSEXP, SEXP seedSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< Rcpp::List >::type object(objectSEXP); - Rcpp::traits::input_parameter< int >::type step(stepSEXP); - Rcpp::traits::input_parameter< int >::type num_sim(num_simSEXP); - Rcpp::traits::input_parameter< unsigned int >::type seed(seedSEXP); - rcpp_result_gen = Rcpp::wrap(forecast_bvar(object, step, num_sim, seed)); - return rcpp_result_gen; -END_RCPP -} -// forecast_bvharmn -Rcpp::List forecast_bvharmn(Rcpp::List object, int step, int num_sim, unsigned int seed); -RcppExport SEXP _bvhar_forecast_bvharmn(SEXP objectSEXP, SEXP stepSEXP, SEXP num_simSEXP, SEXP seedSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< Rcpp::List >::type object(objectSEXP); - Rcpp::traits::input_parameter< int >::type step(stepSEXP); - Rcpp::traits::input_parameter< int >::type num_sim(num_simSEXP); - Rcpp::traits::input_parameter< unsigned int >::type seed(seedSEXP); - rcpp_result_gen = Rcpp::wrap(forecast_bvharmn(object, step, num_sim, seed)); + rcpp_result_gen = Rcpp::wrap(roll_bvharldlt(y, week, month, num_chains, num_iter, num_burn, thinning, sparse, level, fit_record, param_reg, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads)); return rcpp_result_gen; END_RCPP } -// forecast_bvarldlt -Rcpp::List forecast_bvarldlt(int num_chains, int var_lag, int step, Eigen::MatrixXd response_mat, bool sparse, double level, Rcpp::List fit_record, int prior_type, Eigen::VectorXi seed_chain, bool include_mean, bool stable, int nthreads); -RcppExport SEXP _bvhar_forecast_bvarldlt(SEXP num_chainsSEXP, SEXP var_lagSEXP, SEXP stepSEXP, SEXP response_matSEXP, SEXP sparseSEXP, SEXP levelSEXP, SEXP fit_recordSEXP, SEXP prior_typeSEXP, SEXP seed_chainSEXP, SEXP include_meanSEXP, SEXP stableSEXP, SEXP nthreadsSEXP) { +// forecast_bvarsv +Rcpp::List forecast_bvarsv(int num_chains, int var_lag, int step, Eigen::MatrixXd response_mat, bool sv, bool sparse, double level, Rcpp::List fit_record, int prior_type, Eigen::VectorXi seed_chain, bool include_mean, bool stable, int nthreads); +RcppExport SEXP _bvhar_forecast_bvarsv(SEXP num_chainsSEXP, SEXP var_lagSEXP, SEXP stepSEXP, SEXP response_matSEXP, SEXP svSEXP, SEXP sparseSEXP, SEXP levelSEXP, SEXP fit_recordSEXP, SEXP prior_typeSEXP, SEXP seed_chainSEXP, SEXP include_meanSEXP, SEXP stableSEXP, SEXP nthreadsSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; @@ -500,6 +662,7 @@ BEGIN_RCPP Rcpp::traits::input_parameter< int >::type var_lag(var_lagSEXP); Rcpp::traits::input_parameter< int >::type step(stepSEXP); Rcpp::traits::input_parameter< Eigen::MatrixXd >::type response_mat(response_matSEXP); + Rcpp::traits::input_parameter< bool >::type sv(svSEXP); Rcpp::traits::input_parameter< bool >::type sparse(sparseSEXP); Rcpp::traits::input_parameter< double >::type level(levelSEXP); Rcpp::traits::input_parameter< Rcpp::List >::type fit_record(fit_recordSEXP); @@ -508,13 +671,13 @@ BEGIN_RCPP Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); Rcpp::traits::input_parameter< bool >::type stable(stableSEXP); Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); - rcpp_result_gen = Rcpp::wrap(forecast_bvarldlt(num_chains, var_lag, step, response_mat, sparse, level, fit_record, prior_type, seed_chain, include_mean, stable, nthreads)); + rcpp_result_gen = Rcpp::wrap(forecast_bvarsv(num_chains, var_lag, step, response_mat, sv, sparse, level, fit_record, prior_type, seed_chain, include_mean, stable, nthreads)); return rcpp_result_gen; END_RCPP } -// forecast_bvharldlt -Rcpp::List forecast_bvharldlt(int num_chains, int month, int step, Eigen::MatrixXd response_mat, Eigen::MatrixXd HARtrans, bool sparse, double level, Rcpp::List fit_record, int prior_type, Eigen::VectorXi seed_chain, bool include_mean, bool stable, int nthreads); -RcppExport SEXP _bvhar_forecast_bvharldlt(SEXP num_chainsSEXP, SEXP monthSEXP, SEXP stepSEXP, SEXP response_matSEXP, SEXP HARtransSEXP, SEXP sparseSEXP, SEXP levelSEXP, SEXP fit_recordSEXP, SEXP prior_typeSEXP, SEXP seed_chainSEXP, SEXP include_meanSEXP, SEXP stableSEXP, SEXP nthreadsSEXP) { +// forecast_bvharsv +Rcpp::List forecast_bvharsv(int num_chains, int month, int step, Eigen::MatrixXd response_mat, Eigen::MatrixXd HARtrans, bool sv, bool sparse, double level, Rcpp::List fit_record, int prior_type, Eigen::VectorXi seed_chain, bool include_mean, bool stable, int nthreads); +RcppExport SEXP _bvhar_forecast_bvharsv(SEXP num_chainsSEXP, SEXP monthSEXP, SEXP stepSEXP, SEXP response_matSEXP, SEXP HARtransSEXP, SEXP svSEXP, SEXP sparseSEXP, SEXP levelSEXP, SEXP fit_recordSEXP, SEXP prior_typeSEXP, SEXP seed_chainSEXP, SEXP include_meanSEXP, SEXP stableSEXP, SEXP nthreadsSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; @@ -523,6 +686,7 @@ BEGIN_RCPP Rcpp::traits::input_parameter< int >::type step(stepSEXP); Rcpp::traits::input_parameter< Eigen::MatrixXd >::type response_mat(response_matSEXP); Rcpp::traits::input_parameter< Eigen::MatrixXd >::type HARtrans(HARtransSEXP); + Rcpp::traits::input_parameter< bool >::type sv(svSEXP); Rcpp::traits::input_parameter< bool >::type sparse(sparseSEXP); Rcpp::traits::input_parameter< double >::type level(levelSEXP); Rcpp::traits::input_parameter< Rcpp::List >::type fit_record(fit_recordSEXP); @@ -531,13 +695,13 @@ BEGIN_RCPP Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); Rcpp::traits::input_parameter< bool >::type stable(stableSEXP); Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); - rcpp_result_gen = Rcpp::wrap(forecast_bvharldlt(num_chains, month, step, response_mat, HARtrans, sparse, level, fit_record, prior_type, seed_chain, include_mean, stable, nthreads)); + rcpp_result_gen = Rcpp::wrap(forecast_bvharsv(num_chains, month, step, response_mat, HARtrans, sv, sparse, level, fit_record, prior_type, seed_chain, include_mean, stable, nthreads)); return rcpp_result_gen; END_RCPP } -// roll_bvarldlt -Rcpp::List roll_bvarldlt(Eigen::MatrixXd y, int lag, int num_chains, int num_iter, int num_burn, int thinning, bool sparse, double level, Rcpp::List fit_record, Rcpp::List param_reg, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads); -RcppExport SEXP _bvhar_roll_bvarldlt(SEXP ySEXP, SEXP lagSEXP, SEXP num_chainsSEXP, SEXP num_iterSEXP, SEXP num_burnSEXP, SEXP thinningSEXP, SEXP sparseSEXP, SEXP levelSEXP, SEXP fit_recordSEXP, SEXP param_regSEXP, SEXP param_priorSEXP, SEXP param_interceptSEXP, SEXP param_initSEXP, SEXP prior_typeSEXP, SEXP gglSEXP, SEXP grp_idSEXP, SEXP own_idSEXP, SEXP cross_idSEXP, SEXP grp_matSEXP, SEXP include_meanSEXP, SEXP stableSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP get_lplSEXP, SEXP seed_chainSEXP, SEXP seed_forecastSEXP, SEXP display_progressSEXP, SEXP nthreadsSEXP) { +// roll_bvarsv +Rcpp::List roll_bvarsv(Eigen::MatrixXd y, int lag, int num_chains, int num_iter, int num_burn, int thinning, bool sv, bool sparse, double level, Rcpp::List fit_record, Rcpp::List param_sv, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads); +RcppExport SEXP _bvhar_roll_bvarsv(SEXP ySEXP, SEXP lagSEXP, SEXP num_chainsSEXP, SEXP num_iterSEXP, SEXP num_burnSEXP, SEXP thinningSEXP, SEXP svSEXP, SEXP sparseSEXP, SEXP levelSEXP, SEXP fit_recordSEXP, SEXP param_svSEXP, SEXP param_priorSEXP, SEXP param_interceptSEXP, SEXP param_initSEXP, SEXP prior_typeSEXP, SEXP gglSEXP, SEXP grp_idSEXP, SEXP own_idSEXP, SEXP cross_idSEXP, SEXP grp_matSEXP, SEXP include_meanSEXP, SEXP stableSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP get_lplSEXP, SEXP seed_chainSEXP, SEXP seed_forecastSEXP, SEXP display_progressSEXP, SEXP nthreadsSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; @@ -547,10 +711,11 @@ BEGIN_RCPP Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); Rcpp::traits::input_parameter< int >::type num_burn(num_burnSEXP); Rcpp::traits::input_parameter< int >::type thinning(thinningSEXP); + Rcpp::traits::input_parameter< bool >::type sv(svSEXP); Rcpp::traits::input_parameter< bool >::type sparse(sparseSEXP); Rcpp::traits::input_parameter< double >::type level(levelSEXP); Rcpp::traits::input_parameter< Rcpp::List >::type fit_record(fit_recordSEXP); - Rcpp::traits::input_parameter< Rcpp::List >::type param_reg(param_regSEXP); + Rcpp::traits::input_parameter< Rcpp::List >::type param_sv(param_svSEXP); Rcpp::traits::input_parameter< Rcpp::List >::type param_prior(param_priorSEXP); Rcpp::traits::input_parameter< Rcpp::List >::type param_intercept(param_interceptSEXP); Rcpp::traits::input_parameter< Rcpp::List >::type param_init(param_initSEXP); @@ -569,13 +734,13 @@ BEGIN_RCPP Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_forecast(seed_forecastSEXP); Rcpp::traits::input_parameter< bool >::type display_progress(display_progressSEXP); Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); - rcpp_result_gen = Rcpp::wrap(roll_bvarldlt(y, lag, num_chains, num_iter, num_burn, thinning, sparse, level, fit_record, param_reg, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads)); + rcpp_result_gen = Rcpp::wrap(roll_bvarsv(y, lag, num_chains, num_iter, num_burn, thinning, sv, sparse, level, fit_record, param_sv, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads)); return rcpp_result_gen; END_RCPP } -// roll_bvharldlt -Rcpp::List roll_bvharldlt(Eigen::MatrixXd y, int week, int month, int num_chains, int num_iter, int num_burn, int thinning, bool sparse, double level, Rcpp::List fit_record, Rcpp::List param_reg, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads); -RcppExport SEXP _bvhar_roll_bvharldlt(SEXP ySEXP, SEXP weekSEXP, SEXP monthSEXP, SEXP num_chainsSEXP, SEXP num_iterSEXP, SEXP num_burnSEXP, SEXP thinningSEXP, SEXP sparseSEXP, SEXP levelSEXP, SEXP fit_recordSEXP, SEXP param_regSEXP, SEXP param_priorSEXP, SEXP param_interceptSEXP, SEXP param_initSEXP, SEXP prior_typeSEXP, SEXP gglSEXP, SEXP grp_idSEXP, SEXP own_idSEXP, SEXP cross_idSEXP, SEXP grp_matSEXP, SEXP include_meanSEXP, SEXP stableSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP get_lplSEXP, SEXP seed_chainSEXP, SEXP seed_forecastSEXP, SEXP display_progressSEXP, SEXP nthreadsSEXP) { +// roll_bvharsv +Rcpp::List roll_bvharsv(Eigen::MatrixXd y, int week, int month, int num_chains, int num_iter, int num_burn, int thinning, bool sv, bool sparse, double level, Rcpp::List fit_record, Rcpp::List param_sv, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads); +RcppExport SEXP _bvhar_roll_bvharsv(SEXP ySEXP, SEXP weekSEXP, SEXP monthSEXP, SEXP num_chainsSEXP, SEXP num_iterSEXP, SEXP num_burnSEXP, SEXP thinningSEXP, SEXP svSEXP, SEXP sparseSEXP, SEXP levelSEXP, SEXP fit_recordSEXP, SEXP param_svSEXP, SEXP param_priorSEXP, SEXP param_interceptSEXP, SEXP param_initSEXP, SEXP prior_typeSEXP, SEXP gglSEXP, SEXP grp_idSEXP, SEXP own_idSEXP, SEXP cross_idSEXP, SEXP grp_matSEXP, SEXP include_meanSEXP, SEXP stableSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP get_lplSEXP, SEXP seed_chainSEXP, SEXP seed_forecastSEXP, SEXP display_progressSEXP, SEXP nthreadsSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; @@ -586,10 +751,11 @@ BEGIN_RCPP Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); Rcpp::traits::input_parameter< int >::type num_burn(num_burnSEXP); Rcpp::traits::input_parameter< int >::type thinning(thinningSEXP); + Rcpp::traits::input_parameter< bool >::type sv(svSEXP); Rcpp::traits::input_parameter< bool >::type sparse(sparseSEXP); Rcpp::traits::input_parameter< double >::type level(levelSEXP); Rcpp::traits::input_parameter< Rcpp::List >::type fit_record(fit_recordSEXP); - Rcpp::traits::input_parameter< Rcpp::List >::type param_reg(param_regSEXP); + Rcpp::traits::input_parameter< Rcpp::List >::type param_sv(param_svSEXP); Rcpp::traits::input_parameter< Rcpp::List >::type param_prior(param_priorSEXP); Rcpp::traits::input_parameter< Rcpp::List >::type param_intercept(param_interceptSEXP); Rcpp::traits::input_parameter< Rcpp::List >::type param_init(param_initSEXP); @@ -608,60 +774,90 @@ BEGIN_RCPP Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_forecast(seed_forecastSEXP); Rcpp::traits::input_parameter< bool >::type display_progress(display_progressSEXP); Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); - rcpp_result_gen = Rcpp::wrap(roll_bvharldlt(y, week, month, num_chains, num_iter, num_burn, thinning, sparse, level, fit_record, param_reg, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads)); + rcpp_result_gen = Rcpp::wrap(roll_bvharsv(y, week, month, num_chains, num_iter, num_burn, thinning, sv, sparse, level, fit_record, param_sv, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads)); return rcpp_result_gen; END_RCPP } -// forecast_bvarsv -Rcpp::List forecast_bvarsv(int num_chains, int var_lag, int step, Eigen::MatrixXd response_mat, bool sv, bool sparse, double level, Rcpp::List fit_record, int prior_type, Eigen::VectorXi seed_chain, bool include_mean, bool stable, int nthreads); -RcppExport SEXP _bvhar_forecast_bvarsv(SEXP num_chainsSEXP, SEXP var_lagSEXP, SEXP stepSEXP, SEXP response_matSEXP, SEXP svSEXP, SEXP sparseSEXP, SEXP levelSEXP, SEXP fit_recordSEXP, SEXP prior_typeSEXP, SEXP seed_chainSEXP, SEXP include_meanSEXP, SEXP stableSEXP, SEXP nthreadsSEXP) { +// expand_bvarldlt +Rcpp::List expand_bvarldlt(Eigen::MatrixXd y, int lag, int num_chains, int num_iter, int num_burn, int thinning, bool sparse, double level, Rcpp::List fit_record, Rcpp::List param_reg, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads); +RcppExport SEXP _bvhar_expand_bvarldlt(SEXP ySEXP, SEXP lagSEXP, SEXP num_chainsSEXP, SEXP num_iterSEXP, SEXP num_burnSEXP, SEXP thinningSEXP, SEXP sparseSEXP, SEXP levelSEXP, SEXP fit_recordSEXP, SEXP param_regSEXP, SEXP param_priorSEXP, SEXP param_interceptSEXP, SEXP param_initSEXP, SEXP prior_typeSEXP, SEXP gglSEXP, SEXP grp_idSEXP, SEXP own_idSEXP, SEXP cross_idSEXP, SEXP grp_matSEXP, SEXP include_meanSEXP, SEXP stableSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP get_lplSEXP, SEXP seed_chainSEXP, SEXP seed_forecastSEXP, SEXP display_progressSEXP, SEXP nthreadsSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); + Rcpp::traits::input_parameter< int >::type lag(lagSEXP); Rcpp::traits::input_parameter< int >::type num_chains(num_chainsSEXP); - Rcpp::traits::input_parameter< int >::type var_lag(var_lagSEXP); - Rcpp::traits::input_parameter< int >::type step(stepSEXP); - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type response_mat(response_matSEXP); - Rcpp::traits::input_parameter< bool >::type sv(svSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type num_burn(num_burnSEXP); + Rcpp::traits::input_parameter< int >::type thinning(thinningSEXP); Rcpp::traits::input_parameter< bool >::type sparse(sparseSEXP); Rcpp::traits::input_parameter< double >::type level(levelSEXP); Rcpp::traits::input_parameter< Rcpp::List >::type fit_record(fit_recordSEXP); + Rcpp::traits::input_parameter< Rcpp::List >::type param_reg(param_regSEXP); + Rcpp::traits::input_parameter< Rcpp::List >::type param_prior(param_priorSEXP); + Rcpp::traits::input_parameter< Rcpp::List >::type param_intercept(param_interceptSEXP); + Rcpp::traits::input_parameter< Rcpp::List >::type param_init(param_initSEXP); Rcpp::traits::input_parameter< int >::type prior_type(prior_typeSEXP); - Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_chain(seed_chainSEXP); + Rcpp::traits::input_parameter< bool >::type ggl(gglSEXP); + Rcpp::traits::input_parameter< Eigen::VectorXi >::type grp_id(grp_idSEXP); + Rcpp::traits::input_parameter< Eigen::VectorXi >::type own_id(own_idSEXP); + Rcpp::traits::input_parameter< Eigen::VectorXi >::type cross_id(cross_idSEXP); + Rcpp::traits::input_parameter< Eigen::MatrixXi >::type grp_mat(grp_matSEXP); Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); Rcpp::traits::input_parameter< bool >::type stable(stableSEXP); + Rcpp::traits::input_parameter< int >::type step(stepSEXP); + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y_test(y_testSEXP); + Rcpp::traits::input_parameter< bool >::type get_lpl(get_lplSEXP); + Rcpp::traits::input_parameter< Eigen::MatrixXi >::type seed_chain(seed_chainSEXP); + Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_forecast(seed_forecastSEXP); + Rcpp::traits::input_parameter< bool >::type display_progress(display_progressSEXP); Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); - rcpp_result_gen = Rcpp::wrap(forecast_bvarsv(num_chains, var_lag, step, response_mat, sv, sparse, level, fit_record, prior_type, seed_chain, include_mean, stable, nthreads)); + rcpp_result_gen = Rcpp::wrap(expand_bvarldlt(y, lag, num_chains, num_iter, num_burn, thinning, sparse, level, fit_record, param_reg, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads)); return rcpp_result_gen; END_RCPP } -// forecast_bvharsv -Rcpp::List forecast_bvharsv(int num_chains, int month, int step, Eigen::MatrixXd response_mat, Eigen::MatrixXd HARtrans, bool sv, bool sparse, double level, Rcpp::List fit_record, int prior_type, Eigen::VectorXi seed_chain, bool include_mean, bool stable, int nthreads); -RcppExport SEXP _bvhar_forecast_bvharsv(SEXP num_chainsSEXP, SEXP monthSEXP, SEXP stepSEXP, SEXP response_matSEXP, SEXP HARtransSEXP, SEXP svSEXP, SEXP sparseSEXP, SEXP levelSEXP, SEXP fit_recordSEXP, SEXP prior_typeSEXP, SEXP seed_chainSEXP, SEXP include_meanSEXP, SEXP stableSEXP, SEXP nthreadsSEXP) { +// expand_bvharldlt +Rcpp::List expand_bvharldlt(Eigen::MatrixXd y, int week, int month, int num_chains, int num_iter, int num_burn, int thinning, bool sparse, double level, Rcpp::List fit_record, Rcpp::List param_reg, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads); +RcppExport SEXP _bvhar_expand_bvharldlt(SEXP ySEXP, SEXP weekSEXP, SEXP monthSEXP, SEXP num_chainsSEXP, SEXP num_iterSEXP, SEXP num_burnSEXP, SEXP thinningSEXP, SEXP sparseSEXP, SEXP levelSEXP, SEXP fit_recordSEXP, SEXP param_regSEXP, SEXP param_priorSEXP, SEXP param_interceptSEXP, SEXP param_initSEXP, SEXP prior_typeSEXP, SEXP gglSEXP, SEXP grp_idSEXP, SEXP own_idSEXP, SEXP cross_idSEXP, SEXP grp_matSEXP, SEXP include_meanSEXP, SEXP stableSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP get_lplSEXP, SEXP seed_chainSEXP, SEXP seed_forecastSEXP, SEXP display_progressSEXP, SEXP nthreadsSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< int >::type num_chains(num_chainsSEXP); + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); + Rcpp::traits::input_parameter< int >::type week(weekSEXP); Rcpp::traits::input_parameter< int >::type month(monthSEXP); - Rcpp::traits::input_parameter< int >::type step(stepSEXP); - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type response_mat(response_matSEXP); - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type HARtrans(HARtransSEXP); - Rcpp::traits::input_parameter< bool >::type sv(svSEXP); + Rcpp::traits::input_parameter< int >::type num_chains(num_chainsSEXP); + Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); + Rcpp::traits::input_parameter< int >::type num_burn(num_burnSEXP); + Rcpp::traits::input_parameter< int >::type thinning(thinningSEXP); Rcpp::traits::input_parameter< bool >::type sparse(sparseSEXP); Rcpp::traits::input_parameter< double >::type level(levelSEXP); Rcpp::traits::input_parameter< Rcpp::List >::type fit_record(fit_recordSEXP); + Rcpp::traits::input_parameter< Rcpp::List >::type param_reg(param_regSEXP); + Rcpp::traits::input_parameter< Rcpp::List >::type param_prior(param_priorSEXP); + Rcpp::traits::input_parameter< Rcpp::List >::type param_intercept(param_interceptSEXP); + Rcpp::traits::input_parameter< Rcpp::List >::type param_init(param_initSEXP); Rcpp::traits::input_parameter< int >::type prior_type(prior_typeSEXP); - Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_chain(seed_chainSEXP); + Rcpp::traits::input_parameter< bool >::type ggl(gglSEXP); + Rcpp::traits::input_parameter< Eigen::VectorXi >::type grp_id(grp_idSEXP); + Rcpp::traits::input_parameter< Eigen::VectorXi >::type own_id(own_idSEXP); + Rcpp::traits::input_parameter< Eigen::VectorXi >::type cross_id(cross_idSEXP); + Rcpp::traits::input_parameter< Eigen::MatrixXi >::type grp_mat(grp_matSEXP); Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); Rcpp::traits::input_parameter< bool >::type stable(stableSEXP); + Rcpp::traits::input_parameter< int >::type step(stepSEXP); + Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y_test(y_testSEXP); + Rcpp::traits::input_parameter< bool >::type get_lpl(get_lplSEXP); + Rcpp::traits::input_parameter< Eigen::MatrixXi >::type seed_chain(seed_chainSEXP); + Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_forecast(seed_forecastSEXP); + Rcpp::traits::input_parameter< bool >::type display_progress(display_progressSEXP); Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); - rcpp_result_gen = Rcpp::wrap(forecast_bvharsv(num_chains, month, step, response_mat, HARtrans, sv, sparse, level, fit_record, prior_type, seed_chain, include_mean, stable, nthreads)); + rcpp_result_gen = Rcpp::wrap(expand_bvharldlt(y, week, month, num_chains, num_iter, num_burn, thinning, sparse, level, fit_record, param_reg, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads)); return rcpp_result_gen; END_RCPP -} -// roll_bvarsv -Rcpp::List roll_bvarsv(Eigen::MatrixXd y, int lag, int num_chains, int num_iter, int num_burn, int thinning, bool sv, bool sparse, double level, Rcpp::List fit_record, Rcpp::List param_sv, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads); -RcppExport SEXP _bvhar_roll_bvarsv(SEXP ySEXP, SEXP lagSEXP, SEXP num_chainsSEXP, SEXP num_iterSEXP, SEXP num_burnSEXP, SEXP thinningSEXP, SEXP svSEXP, SEXP sparseSEXP, SEXP levelSEXP, SEXP fit_recordSEXP, SEXP param_svSEXP, SEXP param_priorSEXP, SEXP param_interceptSEXP, SEXP param_initSEXP, SEXP prior_typeSEXP, SEXP gglSEXP, SEXP grp_idSEXP, SEXP own_idSEXP, SEXP cross_idSEXP, SEXP grp_matSEXP, SEXP include_meanSEXP, SEXP stableSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP get_lplSEXP, SEXP seed_chainSEXP, SEXP seed_forecastSEXP, SEXP display_progressSEXP, SEXP nthreadsSEXP) { +} +// expand_bvarsv +Rcpp::List expand_bvarsv(Eigen::MatrixXd y, int lag, int num_chains, int num_iter, int num_burn, int thinning, bool sv, bool sparse, double level, Rcpp::List fit_record, Rcpp::List param_sv, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads); +RcppExport SEXP _bvhar_expand_bvarsv(SEXP ySEXP, SEXP lagSEXP, SEXP num_chainsSEXP, SEXP num_iterSEXP, SEXP num_burnSEXP, SEXP thinningSEXP, SEXP svSEXP, SEXP sparseSEXP, SEXP levelSEXP, SEXP fit_recordSEXP, SEXP param_svSEXP, SEXP param_priorSEXP, SEXP param_interceptSEXP, SEXP param_initSEXP, SEXP prior_typeSEXP, SEXP gglSEXP, SEXP grp_idSEXP, SEXP own_idSEXP, SEXP cross_idSEXP, SEXP grp_matSEXP, SEXP include_meanSEXP, SEXP stableSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP get_lplSEXP, SEXP seed_chainSEXP, SEXP seed_forecastSEXP, SEXP display_progressSEXP, SEXP nthreadsSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; @@ -694,13 +890,13 @@ BEGIN_RCPP Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_forecast(seed_forecastSEXP); Rcpp::traits::input_parameter< bool >::type display_progress(display_progressSEXP); Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); - rcpp_result_gen = Rcpp::wrap(roll_bvarsv(y, lag, num_chains, num_iter, num_burn, thinning, sv, sparse, level, fit_record, param_sv, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads)); + rcpp_result_gen = Rcpp::wrap(expand_bvarsv(y, lag, num_chains, num_iter, num_burn, thinning, sv, sparse, level, fit_record, param_sv, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads)); return rcpp_result_gen; END_RCPP } -// roll_bvharsv -Rcpp::List roll_bvharsv(Eigen::MatrixXd y, int week, int month, int num_chains, int num_iter, int num_burn, int thinning, bool sv, bool sparse, double level, Rcpp::List fit_record, Rcpp::List param_sv, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads); -RcppExport SEXP _bvhar_roll_bvharsv(SEXP ySEXP, SEXP weekSEXP, SEXP monthSEXP, SEXP num_chainsSEXP, SEXP num_iterSEXP, SEXP num_burnSEXP, SEXP thinningSEXP, SEXP svSEXP, SEXP sparseSEXP, SEXP levelSEXP, SEXP fit_recordSEXP, SEXP param_svSEXP, SEXP param_priorSEXP, SEXP param_interceptSEXP, SEXP param_initSEXP, SEXP prior_typeSEXP, SEXP gglSEXP, SEXP grp_idSEXP, SEXP own_idSEXP, SEXP cross_idSEXP, SEXP grp_matSEXP, SEXP include_meanSEXP, SEXP stableSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP get_lplSEXP, SEXP seed_chainSEXP, SEXP seed_forecastSEXP, SEXP display_progressSEXP, SEXP nthreadsSEXP) { +// expand_bvharsv +Rcpp::List expand_bvharsv(Eigen::MatrixXd y, int week, int month, int num_chains, int num_iter, int num_burn, int thinning, bool sv, bool sparse, double level, Rcpp::List fit_record, Rcpp::List param_sv, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads); +RcppExport SEXP _bvhar_expand_bvharsv(SEXP ySEXP, SEXP weekSEXP, SEXP monthSEXP, SEXP num_chainsSEXP, SEXP num_iterSEXP, SEXP num_burnSEXP, SEXP thinningSEXP, SEXP svSEXP, SEXP sparseSEXP, SEXP levelSEXP, SEXP fit_recordSEXP, SEXP param_svSEXP, SEXP param_priorSEXP, SEXP param_interceptSEXP, SEXP param_initSEXP, SEXP prior_typeSEXP, SEXP gglSEXP, SEXP grp_idSEXP, SEXP own_idSEXP, SEXP cross_idSEXP, SEXP grp_matSEXP, SEXP include_meanSEXP, SEXP stableSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP get_lplSEXP, SEXP seed_chainSEXP, SEXP seed_forecastSEXP, SEXP display_progressSEXP, SEXP nthreadsSEXP) { BEGIN_RCPP Rcpp::RObject rcpp_result_gen; Rcpp::RNGScope rcpp_rngScope_gen; @@ -734,203 +930,7 @@ BEGIN_RCPP Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_forecast(seed_forecastSEXP); Rcpp::traits::input_parameter< bool >::type display_progress(display_progressSEXP); Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); - rcpp_result_gen = Rcpp::wrap(roll_bvharsv(y, week, month, num_chains, num_iter, num_burn, thinning, sv, sparse, level, fit_record, param_sv, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads)); - return rcpp_result_gen; -END_RCPP -} -// roll_var -Eigen::MatrixXd roll_var(Eigen::MatrixXd y, int lag, bool include_mean, int step, Eigen::MatrixXd y_test, int method, int nthreads); -RcppExport SEXP _bvhar_roll_var(SEXP ySEXP, SEXP lagSEXP, SEXP include_meanSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP methodSEXP, SEXP nthreadsSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); - Rcpp::traits::input_parameter< int >::type lag(lagSEXP); - Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); - Rcpp::traits::input_parameter< int >::type step(stepSEXP); - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y_test(y_testSEXP); - Rcpp::traits::input_parameter< int >::type method(methodSEXP); - Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); - rcpp_result_gen = Rcpp::wrap(roll_var(y, lag, include_mean, step, y_test, method, nthreads)); - return rcpp_result_gen; -END_RCPP -} -// roll_vhar -Eigen::MatrixXd roll_vhar(Eigen::MatrixXd y, int week, int month, bool include_mean, int step, Eigen::MatrixXd y_test, int method, int nthreads); -RcppExport SEXP _bvhar_roll_vhar(SEXP ySEXP, SEXP weekSEXP, SEXP monthSEXP, SEXP include_meanSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP methodSEXP, SEXP nthreadsSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); - Rcpp::traits::input_parameter< int >::type week(weekSEXP); - Rcpp::traits::input_parameter< int >::type month(monthSEXP); - Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); - Rcpp::traits::input_parameter< int >::type step(stepSEXP); - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y_test(y_testSEXP); - Rcpp::traits::input_parameter< int >::type method(methodSEXP); - Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); - rcpp_result_gen = Rcpp::wrap(roll_vhar(y, week, month, include_mean, step, y_test, method, nthreads)); - return rcpp_result_gen; -END_RCPP -} -// roll_bvar -Eigen::MatrixXd roll_bvar(Eigen::MatrixXd y, int lag, Rcpp::List bayes_spec, bool include_mean, int step, Eigen::MatrixXd y_test, Eigen::VectorXi seed_forecast, int nthreads); -RcppExport SEXP _bvhar_roll_bvar(SEXP ySEXP, SEXP lagSEXP, SEXP bayes_specSEXP, SEXP include_meanSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP seed_forecastSEXP, SEXP nthreadsSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); - Rcpp::traits::input_parameter< int >::type lag(lagSEXP); - Rcpp::traits::input_parameter< Rcpp::List >::type bayes_spec(bayes_specSEXP); - Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); - Rcpp::traits::input_parameter< int >::type step(stepSEXP); - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y_test(y_testSEXP); - Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_forecast(seed_forecastSEXP); - Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); - rcpp_result_gen = Rcpp::wrap(roll_bvar(y, lag, bayes_spec, include_mean, step, y_test, seed_forecast, nthreads)); - return rcpp_result_gen; -END_RCPP -} -// roll_bvarflat -Eigen::MatrixXd roll_bvarflat(Eigen::MatrixXd y, int lag, Eigen::MatrixXd U, bool include_mean, int step, Eigen::MatrixXd y_test, Eigen::VectorXi seed_forecast, int nthreads); -RcppExport SEXP _bvhar_roll_bvarflat(SEXP ySEXP, SEXP lagSEXP, SEXP USEXP, SEXP include_meanSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP seed_forecastSEXP, SEXP nthreadsSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); - Rcpp::traits::input_parameter< int >::type lag(lagSEXP); - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type U(USEXP); - Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); - Rcpp::traits::input_parameter< int >::type step(stepSEXP); - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y_test(y_testSEXP); - Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_forecast(seed_forecastSEXP); - Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); - rcpp_result_gen = Rcpp::wrap(roll_bvarflat(y, lag, U, include_mean, step, y_test, seed_forecast, nthreads)); - return rcpp_result_gen; -END_RCPP -} -// roll_bvhar -Eigen::MatrixXd roll_bvhar(Eigen::MatrixXd y, int week, int month, Rcpp::List bayes_spec, bool include_mean, int step, Eigen::MatrixXd y_test, Eigen::VectorXi seed_forecast, int nthreads); -RcppExport SEXP _bvhar_roll_bvhar(SEXP ySEXP, SEXP weekSEXP, SEXP monthSEXP, SEXP bayes_specSEXP, SEXP include_meanSEXP, SEXP stepSEXP, SEXP y_testSEXP, SEXP seed_forecastSEXP, SEXP nthreadsSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); - Rcpp::traits::input_parameter< int >::type week(weekSEXP); - Rcpp::traits::input_parameter< int >::type month(monthSEXP); - Rcpp::traits::input_parameter< Rcpp::List >::type bayes_spec(bayes_specSEXP); - Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); - Rcpp::traits::input_parameter< int >::type step(stepSEXP); - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y_test(y_testSEXP); - Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_forecast(seed_forecastSEXP); - Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); - rcpp_result_gen = Rcpp::wrap(roll_bvhar(y, week, month, bayes_spec, include_mean, step, y_test, seed_forecast, nthreads)); - return rcpp_result_gen; -END_RCPP -} -// compute_ols_spillover -Rcpp::List compute_ols_spillover(Rcpp::List object, int step); -RcppExport SEXP _bvhar_compute_ols_spillover(SEXP objectSEXP, SEXP stepSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< Rcpp::List >::type object(objectSEXP); - Rcpp::traits::input_parameter< int >::type step(stepSEXP); - rcpp_result_gen = Rcpp::wrap(compute_ols_spillover(object, step)); - return rcpp_result_gen; -END_RCPP -} -// dynamic_var_spillover -Rcpp::List dynamic_var_spillover(Eigen::MatrixXd y, int window, int step, int lag, bool include_mean, int method, int nthreads); -RcppExport SEXP _bvhar_dynamic_var_spillover(SEXP ySEXP, SEXP windowSEXP, SEXP stepSEXP, SEXP lagSEXP, SEXP include_meanSEXP, SEXP methodSEXP, SEXP nthreadsSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); - Rcpp::traits::input_parameter< int >::type window(windowSEXP); - Rcpp::traits::input_parameter< int >::type step(stepSEXP); - Rcpp::traits::input_parameter< int >::type lag(lagSEXP); - Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); - Rcpp::traits::input_parameter< int >::type method(methodSEXP); - Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); - rcpp_result_gen = Rcpp::wrap(dynamic_var_spillover(y, window, step, lag, include_mean, method, nthreads)); - return rcpp_result_gen; -END_RCPP -} -// dynamic_vhar_spillover -Rcpp::List dynamic_vhar_spillover(Eigen::MatrixXd y, int window, int step, int week, int month, bool include_mean, int method, int nthreads); -RcppExport SEXP _bvhar_dynamic_vhar_spillover(SEXP ySEXP, SEXP windowSEXP, SEXP stepSEXP, SEXP weekSEXP, SEXP monthSEXP, SEXP include_meanSEXP, SEXP methodSEXP, SEXP nthreadsSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); - Rcpp::traits::input_parameter< int >::type window(windowSEXP); - Rcpp::traits::input_parameter< int >::type step(stepSEXP); - Rcpp::traits::input_parameter< int >::type week(weekSEXP); - Rcpp::traits::input_parameter< int >::type month(monthSEXP); - Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); - Rcpp::traits::input_parameter< int >::type method(methodSEXP); - Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); - rcpp_result_gen = Rcpp::wrap(dynamic_vhar_spillover(y, window, step, week, month, include_mean, method, nthreads)); - return rcpp_result_gen; -END_RCPP -} -// compute_mn_spillover -Rcpp::List compute_mn_spillover(Rcpp::List object, int step, int num_iter, int num_burn, int thin, unsigned int seed); -RcppExport SEXP _bvhar_compute_mn_spillover(SEXP objectSEXP, SEXP stepSEXP, SEXP num_iterSEXP, SEXP num_burnSEXP, SEXP thinSEXP, SEXP seedSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< Rcpp::List >::type object(objectSEXP); - Rcpp::traits::input_parameter< int >::type step(stepSEXP); - Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); - Rcpp::traits::input_parameter< int >::type num_burn(num_burnSEXP); - Rcpp::traits::input_parameter< int >::type thin(thinSEXP); - Rcpp::traits::input_parameter< unsigned int >::type seed(seedSEXP); - rcpp_result_gen = Rcpp::wrap(compute_mn_spillover(object, step, num_iter, num_burn, thin, seed)); - return rcpp_result_gen; -END_RCPP -} -// dynamic_bvar_spillover -Rcpp::List dynamic_bvar_spillover(Eigen::MatrixXd y, int window, int step, int num_iter, int num_burn, int thin, int lag, Rcpp::List bayes_spec, bool include_mean, Eigen::VectorXi seed_chain, int nthreads); -RcppExport SEXP _bvhar_dynamic_bvar_spillover(SEXP ySEXP, SEXP windowSEXP, SEXP stepSEXP, SEXP num_iterSEXP, SEXP num_burnSEXP, SEXP thinSEXP, SEXP lagSEXP, SEXP bayes_specSEXP, SEXP include_meanSEXP, SEXP seed_chainSEXP, SEXP nthreadsSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); - Rcpp::traits::input_parameter< int >::type window(windowSEXP); - Rcpp::traits::input_parameter< int >::type step(stepSEXP); - Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); - Rcpp::traits::input_parameter< int >::type num_burn(num_burnSEXP); - Rcpp::traits::input_parameter< int >::type thin(thinSEXP); - Rcpp::traits::input_parameter< int >::type lag(lagSEXP); - Rcpp::traits::input_parameter< Rcpp::List >::type bayes_spec(bayes_specSEXP); - Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); - Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_chain(seed_chainSEXP); - Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); - rcpp_result_gen = Rcpp::wrap(dynamic_bvar_spillover(y, window, step, num_iter, num_burn, thin, lag, bayes_spec, include_mean, seed_chain, nthreads)); - return rcpp_result_gen; -END_RCPP -} -// dynamic_bvhar_spillover -Rcpp::List dynamic_bvhar_spillover(Eigen::MatrixXd y, int window, int step, int num_iter, int num_burn, int thin, int week, int month, Rcpp::List bayes_spec, bool include_mean, Eigen::VectorXi seed_chain, int nthreads); -RcppExport SEXP _bvhar_dynamic_bvhar_spillover(SEXP ySEXP, SEXP windowSEXP, SEXP stepSEXP, SEXP num_iterSEXP, SEXP num_burnSEXP, SEXP thinSEXP, SEXP weekSEXP, SEXP monthSEXP, SEXP bayes_specSEXP, SEXP include_meanSEXP, SEXP seed_chainSEXP, SEXP nthreadsSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< Eigen::MatrixXd >::type y(ySEXP); - Rcpp::traits::input_parameter< int >::type window(windowSEXP); - Rcpp::traits::input_parameter< int >::type step(stepSEXP); - Rcpp::traits::input_parameter< int >::type num_iter(num_iterSEXP); - Rcpp::traits::input_parameter< int >::type num_burn(num_burnSEXP); - Rcpp::traits::input_parameter< int >::type thin(thinSEXP); - Rcpp::traits::input_parameter< int >::type week(weekSEXP); - Rcpp::traits::input_parameter< int >::type month(monthSEXP); - Rcpp::traits::input_parameter< Rcpp::List >::type bayes_spec(bayes_specSEXP); - Rcpp::traits::input_parameter< bool >::type include_mean(include_meanSEXP); - Rcpp::traits::input_parameter< Eigen::VectorXi >::type seed_chain(seed_chainSEXP); - Rcpp::traits::input_parameter< int >::type nthreads(nthreadsSEXP); - rcpp_result_gen = Rcpp::wrap(dynamic_bvhar_spillover(y, window, step, num_iter, num_burn, thin, week, month, bayes_spec, include_mean, seed_chain, nthreads)); + rcpp_result_gen = Rcpp::wrap(expand_bvharsv(y, week, month, num_chains, num_iter, num_burn, thinning, sv, sparse, level, fit_record, param_sv, param_prior, param_intercept, param_init, prior_type, ggl, grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, get_lpl, seed_chain, seed_forecast, display_progress, nthreads)); return rcpp_result_gen; END_RCPP } @@ -1668,25 +1668,32 @@ static const R_CallMethodDef CallEntries[] = { {"_bvhar_estimate_bvar_mh", (DL_FUNC) &_bvhar_estimate_bvar_mh, 13}, {"_bvhar_estimate_mn_flat", (DL_FUNC) &_bvhar_estimate_mn_flat, 3}, {"_bvhar_estimate_mniw", (DL_FUNC) &_bvhar_estimate_mniw, 11}, - {"_bvhar_estimate_sur", (DL_FUNC) &_bvhar_estimate_sur, 20}, + {"_bvhar_forecast_bvar", (DL_FUNC) &_bvhar_forecast_bvar, 4}, + {"_bvhar_forecast_bvharmn", (DL_FUNC) &_bvhar_forecast_bvharmn, 4}, + {"_bvhar_roll_bvar", (DL_FUNC) &_bvhar_roll_bvar, 8}, + {"_bvhar_roll_bvarflat", (DL_FUNC) &_bvhar_roll_bvarflat, 8}, + {"_bvhar_roll_bvhar", (DL_FUNC) &_bvhar_roll_bvhar, 9}, + {"_bvhar_expand_bvar", (DL_FUNC) &_bvhar_expand_bvar, 8}, + {"_bvhar_expand_bvarflat", (DL_FUNC) &_bvhar_expand_bvarflat, 8}, + {"_bvhar_expand_bvhar", (DL_FUNC) &_bvhar_expand_bvhar, 9}, + {"_bvhar_compute_mn_spillover", (DL_FUNC) &_bvhar_compute_mn_spillover, 6}, + {"_bvhar_dynamic_bvar_spillover", (DL_FUNC) &_bvhar_dynamic_bvar_spillover, 11}, + {"_bvhar_dynamic_bvhar_spillover", (DL_FUNC) &_bvhar_dynamic_bvhar_spillover, 12}, {"_bvhar_estimate_var", (DL_FUNC) &_bvhar_estimate_var, 4}, {"_bvhar_estimate_har", (DL_FUNC) &_bvhar_estimate_har, 5}, {"_bvhar_compute_cov", (DL_FUNC) &_bvhar_compute_cov, 3}, {"_bvhar_infer_var", (DL_FUNC) &_bvhar_infer_var, 1}, {"_bvhar_infer_vhar", (DL_FUNC) &_bvhar_infer_vhar, 1}, - {"_bvhar_expand_var", (DL_FUNC) &_bvhar_expand_var, 7}, - {"_bvhar_expand_vhar", (DL_FUNC) &_bvhar_expand_vhar, 8}, - {"_bvhar_expand_bvar", (DL_FUNC) &_bvhar_expand_bvar, 8}, - {"_bvhar_expand_bvarflat", (DL_FUNC) &_bvhar_expand_bvarflat, 8}, - {"_bvhar_expand_bvhar", (DL_FUNC) &_bvhar_expand_bvhar, 9}, - {"_bvhar_expand_bvarldlt", (DL_FUNC) &_bvhar_expand_bvarldlt, 28}, - {"_bvhar_expand_bvharldlt", (DL_FUNC) &_bvhar_expand_bvharldlt, 29}, - {"_bvhar_expand_bvarsv", (DL_FUNC) &_bvhar_expand_bvarsv, 29}, - {"_bvhar_expand_bvharsv", (DL_FUNC) &_bvhar_expand_bvharsv, 30}, {"_bvhar_forecast_var", (DL_FUNC) &_bvhar_forecast_var, 2}, {"_bvhar_forecast_vhar", (DL_FUNC) &_bvhar_forecast_vhar, 2}, - {"_bvhar_forecast_bvar", (DL_FUNC) &_bvhar_forecast_bvar, 4}, - {"_bvhar_forecast_bvharmn", (DL_FUNC) &_bvhar_forecast_bvharmn, 4}, + {"_bvhar_roll_var", (DL_FUNC) &_bvhar_roll_var, 7}, + {"_bvhar_roll_vhar", (DL_FUNC) &_bvhar_roll_vhar, 8}, + {"_bvhar_expand_var", (DL_FUNC) &_bvhar_expand_var, 7}, + {"_bvhar_expand_vhar", (DL_FUNC) &_bvhar_expand_vhar, 8}, + {"_bvhar_compute_ols_spillover", (DL_FUNC) &_bvhar_compute_ols_spillover, 2}, + {"_bvhar_dynamic_var_spillover", (DL_FUNC) &_bvhar_dynamic_var_spillover, 7}, + {"_bvhar_dynamic_vhar_spillover", (DL_FUNC) &_bvhar_dynamic_vhar_spillover, 8}, + {"_bvhar_estimate_sur", (DL_FUNC) &_bvhar_estimate_sur, 20}, {"_bvhar_forecast_bvarldlt", (DL_FUNC) &_bvhar_forecast_bvarldlt, 12}, {"_bvhar_forecast_bvharldlt", (DL_FUNC) &_bvhar_forecast_bvharldlt, 13}, {"_bvhar_roll_bvarldlt", (DL_FUNC) &_bvhar_roll_bvarldlt, 28}, @@ -1695,17 +1702,10 @@ static const R_CallMethodDef CallEntries[] = { {"_bvhar_forecast_bvharsv", (DL_FUNC) &_bvhar_forecast_bvharsv, 14}, {"_bvhar_roll_bvarsv", (DL_FUNC) &_bvhar_roll_bvarsv, 29}, {"_bvhar_roll_bvharsv", (DL_FUNC) &_bvhar_roll_bvharsv, 30}, - {"_bvhar_roll_var", (DL_FUNC) &_bvhar_roll_var, 7}, - {"_bvhar_roll_vhar", (DL_FUNC) &_bvhar_roll_vhar, 8}, - {"_bvhar_roll_bvar", (DL_FUNC) &_bvhar_roll_bvar, 8}, - {"_bvhar_roll_bvarflat", (DL_FUNC) &_bvhar_roll_bvarflat, 8}, - {"_bvhar_roll_bvhar", (DL_FUNC) &_bvhar_roll_bvhar, 9}, - {"_bvhar_compute_ols_spillover", (DL_FUNC) &_bvhar_compute_ols_spillover, 2}, - {"_bvhar_dynamic_var_spillover", (DL_FUNC) &_bvhar_dynamic_var_spillover, 7}, - {"_bvhar_dynamic_vhar_spillover", (DL_FUNC) &_bvhar_dynamic_vhar_spillover, 8}, - {"_bvhar_compute_mn_spillover", (DL_FUNC) &_bvhar_compute_mn_spillover, 6}, - {"_bvhar_dynamic_bvar_spillover", (DL_FUNC) &_bvhar_dynamic_bvar_spillover, 11}, - {"_bvhar_dynamic_bvhar_spillover", (DL_FUNC) &_bvhar_dynamic_bvhar_spillover, 12}, + {"_bvhar_expand_bvarldlt", (DL_FUNC) &_bvhar_expand_bvarldlt, 28}, + {"_bvhar_expand_bvharldlt", (DL_FUNC) &_bvhar_expand_bvharldlt, 29}, + {"_bvhar_expand_bvarsv", (DL_FUNC) &_bvhar_expand_bvarsv, 29}, + {"_bvhar_expand_bvharsv", (DL_FUNC) &_bvhar_expand_bvharsv, 30}, {"_bvhar_compute_varldlt_spillover", (DL_FUNC) &_bvhar_compute_varldlt_spillover, 4}, {"_bvhar_compute_vharldlt_spillover", (DL_FUNC) &_bvhar_compute_vharldlt_spillover, 5}, {"_bvhar_dynamic_bvarldlt_spillover", (DL_FUNC) &_bvhar_dynamic_bvarldlt_spillover, 22}, diff --git a/src/estimate-bayes.cpp b/src/estimate-bayes.cpp deleted file mode 100644 index bef92e90..00000000 --- a/src/estimate-bayes.cpp +++ /dev/null @@ -1,234 +0,0 @@ -#include - -//' BVAR(p) Point Estimates based on Minnesota Prior -//' -//' Point estimates for posterior distribution -//' -//' @param y Time series data -//' @param lag VAR order -//' @param bayes_spec BVAR Minnesota specification -//' @param include_mean Constant term -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::List estimate_bvar_mn(Eigen::MatrixXd y, int lag, Rcpp::List bayes_spec, bool include_mean) { - bvhar::BvarSpec mn_spec(bayes_spec); - std::unique_ptr mn_obj(new bvhar::MinnBvar(y, lag, mn_spec, include_mean)); - return mn_obj->returnMinnRes(); -} - -//' BVHAR Point Estimates based on Minnesota Prior -//' -//' Point estimates for posterior distribution -//' -//' @param y Time series data -//' @param week VHAR week order -//' @param month VHAR month order -//' @param bayes_spec BVHAR Minnesota specification -//' @param include_mean Constant term -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::List estimate_bvhar_mn(Eigen::MatrixXd y, int week, int month, Rcpp::List bayes_spec, bool include_mean) { - std::unique_ptr mn_obj; - if (bayes_spec.containsElementNamed("delta")) { - bvhar::BvarSpec bvhar_spec(bayes_spec); - mn_obj.reset(new bvhar::MinnBvharS(y, week, month, bvhar_spec, include_mean)); - } else { - bvhar::BvharSpec bvhar_spec(bayes_spec); - mn_obj.reset(new bvhar::MinnBvharL(y, week, month, bvhar_spec, include_mean)); - } - return mn_obj->returnMinnRes(); -} - -//' @noRd -// [[Rcpp::export]] -Rcpp::List estimate_bvar_mh(int num_chains, int num_iter, int num_burn, int thin, - Eigen::MatrixXd x, Eigen::MatrixXd y, Eigen::MatrixXd x_dummy, Eigen::MatrixXd y_dummy, - Rcpp::List param_prior, Rcpp::List param_init, - Eigen::VectorXi seed_chain, bool display_progress, int nthreads) { - std::vector> mn_objs(num_chains); - std::vector res(num_chains); - Rcpp::List lambda_spec = param_prior["lambda"]; - Rcpp::List psi_spec = param_prior["sigma"]; - bvhar::MhMinnSpec mn_spec(lambda_spec, psi_spec); - for (int i = 0; i < num_chains; ++i) { - Rcpp::List init_spec = param_init[i]; - bvhar::MhMinnInits mn_init(init_spec); - mn_objs[i].reset(new bvhar::MhMinnesota(num_iter, mn_spec, mn_init, x, y, x_dummy, y_dummy, static_cast(seed_chain[i]))); - mn_objs[i]->computePosterior(); - } - auto run_mh = [&](int chain) { - bvhar::bvharprogress bar(num_iter, display_progress); - bvhar::bvharinterrupt(); - for (int i = 0; i < num_iter; i++) { - if (bvhar::bvharinterrupt::is_interrupted()) { - #ifdef _OPENMP - #pragma omp critical - #endif - { - res[chain] = mn_objs[chain]->returnRecords(0, 1); - } - break; - } - bar.increment(); - if (display_progress) { - bar.update(); - } - mn_objs[chain]->doPosteriorDraws(); - } - #ifdef _OPENMP - #pragma omp critical - #endif - { - res[chain] = mn_objs[chain]->returnRecords(num_burn, thin); - } - }; - if (num_chains == 1) { - run_mh(0); - } else { - #ifdef _OPENMP - #pragma omp parallel for num_threads(nthreads) - #endif - for (int chain = 0; chain < num_chains; chain++) { - run_mh(chain); - } - } - return Rcpp::wrap(res); -} - -//' BVAR(p) Point Estimates based on Nonhierarchical Matrix Normal Prior -//' -//' Point estimates for Ghosh et al. (2018) nonhierarchical model for BVAR. -//' -//' @param x Design matrix X0 -//' @param y Response matrix Y0 -//' @param U Positive definite matrix, covariance matrix corresponding to the column of the model parameter B -//' -//' @details -//' In Ghosh et al. (2018), there are many models for BVAR such as hierarchical or non-hierarchical. -//' Among these, this function chooses the most simple non-hierarchical matrix normal prior in Section 3.1. -//' -//' @references -//' Ghosh, S., Khare, K., & Michailidis, G. (2018). *High-Dimensional Posterior Consistency in Bayesian Vector Autoregressive Models*. Journal of the American Statistical Association, 114(526). [https://doi:10.1080/01621459.2018.1437043](https://doi:10.1080/01621459.2018.1437043) -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::List estimate_mn_flat(Eigen::MatrixXd x, Eigen::MatrixXd y, Eigen::MatrixXd U) { - if (U.rows() != x.cols()) { - Rcpp::stop("Wrong dimension: U"); - } - if (U.cols() != x.cols()) { - Rcpp::stop("Wrong dimension: U"); - } - std::unique_ptr mn_obj(new bvhar::MinnFlat(x, y, U)); - return mn_obj->returnMinnRes(); -} - -//' @noRd -// [[Rcpp::export]] -Rcpp::List estimate_mniw(int num_chains, int num_iter, int num_burn, int thin, - const Eigen::MatrixXd& mn_mean, const Eigen::MatrixXd& mn_prec, - const Eigen::MatrixXd& iw_scale, double iw_shape, - Eigen::VectorXi seed_chain, bool display_progress, int nthreads) { - std::vector> mn_objs(num_chains); - for (int i = 0; i < num_chains; ++i) { - bvhar::MinnFit mn_fit(mn_mean, mn_prec, iw_scale, iw_shape); - mn_objs[i].reset(new bvhar::McmcMniw(num_iter, mn_fit, static_cast(seed_chain[i]))); - } - std::vector res(num_chains); - auto run_conj = [&](int chain) { - bvhar::bvharprogress bar(num_iter, display_progress); - for (int i = 0; i < num_iter; ++i) { - bar.increment(); - bar.update(); - mn_objs[chain]->doPosteriorDraws(); - } - #ifdef _OPENMP - #pragma omp critical - #endif - { - res[chain] = mn_objs[chain]->returnRecords(num_burn, thin); - } - }; - if (num_chains == 1) { - run_conj(0); - } else { - #ifdef _OPENMP - #pragma omp parallel for num_threads(nthreads) - #endif - for (int chain = 0; chain < num_chains; ++chain) { - run_conj(chain); - } - } - return Rcpp::wrap(res); -} - -//' VAR with Shrinkage Priors -//' -//' This function generates parameters \eqn{\beta, a, \sigma_{h,i}^2, h_{0,i}} and log-volatilities \eqn{h_{i,1}, \ldots, h_{i, n}}. -//' -//' @param num_chain Number of MCMC chains -//' @param num_iter Number of iteration for MCMC -//' @param num_burn Number of burn-in (warm-up) for MCMC -//' @param thin Thinning -//' @param x Design matrix X0 -//' @param y Response matrix Y0 -//' @param param_reg Regression specification list -//' @param param_prior Prior specification list -//' @param param_intercept Intercept specification list -//' @param param_init Initialization specification list -//' @param grp_id Unique group id -//' @param grp_mat Group matrix -//' @param include_mean Constant term -//' @param seed_chain Seed for each chain -//' @param display_progress Progress bar -//' @param nthreads Number of threads for openmp -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::List estimate_sur(int num_chains, int num_iter, int num_burn, int thin, - Eigen::MatrixXd x, Eigen::MatrixXd y, - Rcpp::List param_reg, Rcpp::List param_prior, Rcpp::List param_intercept, - Rcpp::List param_init, int prior_type, bool ggl, - Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, - bool include_mean, Eigen::VectorXi seed_chain, bool display_progress, int nthreads) { - auto mcmc_run = [&]() -> std::unique_ptr { - if (param_reg.containsElementNamed("initial_mean")) { - if (ggl) { - return std::make_unique>( - num_chains, num_iter, num_burn, thin, x, y, - param_reg, param_prior, param_intercept, param_init, prior_type, - grp_id, own_id, cross_id, grp_mat, - include_mean, seed_chain, - display_progress, nthreads - ); - } - return std::make_unique>( - num_chains, num_iter, num_burn, thin, x, y, - param_reg, param_prior, param_intercept, param_init, prior_type, - grp_id, own_id, cross_id, grp_mat, - include_mean, seed_chain, - display_progress, nthreads - ); - } - if (ggl) { - return std::make_unique>( - num_chains, num_iter, num_burn, thin, x, y, - param_reg, param_prior, param_intercept, param_init, prior_type, - grp_id, own_id, cross_id, grp_mat, - include_mean, seed_chain, - display_progress, nthreads - ); - } - return std::make_unique>( - num_chains, num_iter, num_burn, thin, x, y, - param_reg, param_prior, param_intercept, param_init, prior_type, - grp_id, own_id, cross_id, grp_mat, - include_mean, seed_chain, - display_progress, nthreads - ); - }(); - // Start Gibbs sampling----------------------------------- - return mcmc_run->returnRecords(); -} diff --git a/src/estimate-ols.cpp b/src/estimate-ols.cpp deleted file mode 100644 index d01bc7dc..00000000 --- a/src/estimate-ols.cpp +++ /dev/null @@ -1,155 +0,0 @@ -#include - -//' Compute VAR(p) Coefficient Matrices and Fitted Values -//' -//' This function fits VAR(p) given response and design matrices of multivariate time series. -//' -//' @param x Design matrix X0 -//' @param y Response matrix Y0 -//' @param method Method to solve linear equation system. 1: normal equation, 2: cholesky, 3: HouseholderQR. -//' @details -//' Given Y0 and Y0, the function estimate least squares -//' Y0 = X0 A + Z -//' -//' @references Lütkepohl, H. (2007). *New Introduction to Multiple Time Series Analysis*. Springer Publishing. doi:[10.1007/978-3-540-27752-1](https://doi.org/10.1007/978-3-540-27752-1) -//' @noRd -// [[Rcpp::export]] -Rcpp::List estimate_var(Eigen::MatrixXd y, int lag, bool include_mean, int method) { - std::unique_ptr ols_obj(new bvhar::OlsVar(y, lag, include_mean, method)); - return ols_obj->returnOlsRes(); -} - -//' Compute Vector HAR Coefficient Matrices and Fitted Values -//' -//' This function fits VHAR given response and design matrices of multivariate time series. -//' -//' @param x Design matrix X0 -//' @param y Response matrix Y0 -//' @param week Integer, order for weekly term -//' @param month Integer, order for monthly term -//' @param include_mean bool, Add constant term (Default: `true`) or not (`false`) -//' @param method Method to solve linear equation system. 1: normal equation, 2: cholesky, 3: HouseholderQR. -//' @details -//' Given Y0 and Y0, the function estimate least squares -//' \deqn{Y_0 = X_1 \Phi + Z} -//' -//' @references -//' Baek, C. and Park, M. (2021). *Sparse vector heterogeneous autoregressive modeling for realized volatility*. J. Korean Stat. Soc. 50, 495-510. doi:[10.1007/s42952-020-00090-5](https://doi.org/10.1007/s42952-020-00090-5) -//' -//' Corsi, F. (2008). *A Simple Approximate Long-Memory Model of Realized Volatility*. Journal of Financial Econometrics, 7(2), 174-196. doi:[10.1093/jjfinec/nbp001](https://doi.org/10.1093/jjfinec/nbp001) -//' @noRd -// [[Rcpp::export]] -Rcpp::List estimate_har(Eigen::MatrixXd y, int week, int month, bool include_mean, int method) { - std::unique_ptr ols_obj(new bvhar::OlsVhar(y, week, month, include_mean, method)); - return ols_obj->returnOlsRes(); -} - -//' Covariance Estimate for Residual Covariance Matrix -//' -//' Compute ubiased estimator for residual covariance. -//' -//' @param z Matrix, residual -//' @param num_design Integer, Number of sample used (s = n - p) -//' @param dim_design Ingeger, Number of parameter for each dimension (k = mp + 1) -//' @details -//' See pp75 Lütkepohl (2007). -//' -//' * s = n - p: sample used (`num_design`) -//' * k = mp + 1 (m: dimension, p: VAR lag): number of parameter for each dimension (`dim_design`) -//' -//' Then an unbiased estimator for \eqn{\Sigma_e} is -//' -//' \deqn{\hat{\Sigma}_e = \frac{1}{s - k} (Y_0 - \hat{A} X_0)^T (Y_0 - \hat{A} X_0)} -//' -//' @references Lütkepohl, H. (2007). *New Introduction to Multiple Time Series Analysis*. Springer Publishing. -//' @noRd -// [[Rcpp::export]] -Eigen::MatrixXd compute_cov(Eigen::MatrixXd z, int num_design, int dim_design) { - Eigen::MatrixXd cov_mat(z.cols(), z.cols()); - cov_mat = z.transpose() * z / (num_design - dim_design); - return cov_mat; -} - -//' Statistic for VAR -//' -//' Compute partial t-statistics for inference in VAR model. -//' -//' @param object A `varlse` object -//' @details -//' Partial t-statistic for H0: aij = 0 -//' -//' * For each variable (e.g. 1st variable) -//' * Standard error = (1st) diagonal element of \eqn{\Sigma_e} estimator x diagonal elements of \eqn{(X_0^T X_0)^(-1)} -//' -//' @references Lütkepohl, H. (2007). *New Introduction to Multiple Time Series Analysis*. Springer Publishing. doi:[10.1007/978-3-540-27752-1](https://doi.org/10.1007/978-3-540-27752-1) -//' @noRd -// [[Rcpp::export]] -Rcpp::List infer_var(Rcpp::List object) { - if (!object.inherits("varlse")) { - Rcpp::stop("'object' must be varlse object."); - } - int dim = object["m"]; // dimension of time series - Eigen::MatrixXd cov_mat = object["covmat"]; // sigma - Eigen::MatrixXd coef_mat = object["coefficients"]; // Ahat(mp, m) = [A1^T, A2^T, ..., Ap^T, c^T]^T - Eigen::MatrixXd design_mat = object["design"]; // X0: n x mp - int num_design = object["obs"]; - int dim_design = coef_mat.rows(); // mp(+1) - int df = num_design - dim_design; - Eigen::VectorXd XtX = (design_mat.transpose() * design_mat).inverse().diagonal(); // diagonal element of (XtX)^(-1) - Eigen::MatrixXd res(dim_design * dim, 3); // stack estimate, std, and t stat - Eigen::ArrayXd st_err(dim_design); // save standard error in for loop - for (int i = 0; i < dim; i++) { - res.block(i * dim_design, 0, dim_design, 1) = coef_mat.col(i); - for (int j = 0; j < dim_design; j++) { - st_err[j] = sqrt(XtX[j] * cov_mat(i, i)); // variable-covariance matrix element - } - res.block(i * dim_design, 1, dim_design, 1) = st_err; - res.block(i * dim_design, 2, dim_design, 1) = coef_mat.col(i).array() / st_err; - } - return Rcpp::List::create( - Rcpp::Named("df") = df, - Rcpp::Named("summary_stat") = res - ); -} - -//' Statistic for VHAR -//' -//' Compute partial t-statistics for inference in VHAR model. -//' -//' @param object A `vharlse` object -//' @details -//' Partial t-statistic for H0: \eqn{\phi_{ij} = 0} -//' -//' * For each variable (e.g. 1st variable) -//' * Standard error = (1st) diagonal element of \eqn{\Sigma_e} estimator x diagonal elements of \eqn{(X_1^T X_1)^(-1)} -//' @noRd -// [[Rcpp::export]] -Rcpp::List infer_vhar(Rcpp::List object) { - if (!object.inherits("vharlse")) { - Rcpp::stop("'object' must be vharlse object."); - } - int dim = object["m"]; // dimension of time series - Eigen::MatrixXd cov_mat = object["covmat"]; // sigma - Eigen::MatrixXd coef_mat = object["coefficients"]; // Phihat(mp, m) = [Phi(daily), Phi(weekly), Phi(monthly), c^T]^T - Eigen::MatrixXd design_mat = object["design"]; // X0: n x mp - Eigen::MatrixXd HARtrans = object["HARtrans"]; // HAR transformation - Eigen::MatrixXd vhar_design = design_mat * HARtrans.transpose(); // X1 = X0 * C0^T - int num_design = object["obs"]; - int num_har = coef_mat.rows(); // 3m(+1) - int df = num_design - num_har; - Eigen::VectorXd XtX = (vhar_design.transpose() * vhar_design).inverse().diagonal(); // diagonal element of (XtX)^(-1) - Eigen::MatrixXd res(num_har * dim, 3); // stack estimate, std, and t stat - Eigen::ArrayXd st_err(num_har); // save standard error in for loop - for (int i = 0; i < dim; i++) { - res.block(i * num_har, 0, num_har, 1) = coef_mat.col(i); - for (int j = 0; j < num_har; j++) { - st_err[j] = sqrt(XtX[j] * cov_mat(i, i)); // variable-covariance matrix element - } - res.block(i * num_har, 1, num_har, 1) = st_err; - res.block(i * num_har, 2, num_har, 1) = coef_mat.col(i).array() / st_err; - } - return Rcpp::List::create( - Rcpp::Named("df") = df, - Rcpp::Named("summary_stat") = res - ); -} diff --git a/src/expand.cpp b/src/expand.cpp deleted file mode 100644 index 0cff0630..00000000 --- a/src/expand.cpp +++ /dev/null @@ -1,555 +0,0 @@ -#include - -//' Out-of-Sample Forecasting of VAR based on Expanding Window -//' -//' This function conducts an expanding window forecasting of VAR. -//' -//' @param y Time series data of which columns indicate the variables -//' @param lag VAR order -//' @param include_mean Add constant term -//' @param step Integer, Step to forecast -//' @param y_test Evaluation time series data period after `y` -//' @param method Method to solve linear equation system. 1: normal equation, 2: cholesky, 3: HouseholderQR. -//' @param nthreads Number of threads for openmp -//' -//' @noRd -// [[Rcpp::export]] -Eigen::MatrixXd expand_var(Eigen::MatrixXd y, int lag, bool include_mean, int step, Eigen::MatrixXd y_test, int method, int nthreads) { -#ifdef _OPENMP - Eigen::setNbThreads(nthreads); -#endif - int num_window = y.rows(); - int dim = y.cols(); - int num_test = y_test.rows(); - int num_horizon = num_test - step + 1; // longest forecast horizon - Eigen::MatrixXd tot_mat(num_window + num_test, dim); - tot_mat << y, - y_test; - std::vector expand_mat(num_horizon); - std::vector expand_y0(num_horizon); - for (int i = 0; i < num_horizon; i++) { - expand_mat[i] = tot_mat.topRows(num_window + i); - expand_y0[i] = bvhar::build_y0(expand_mat[i], lag, lag + 1); - } - std::vector> ols_objs(num_horizon); - switch(method) { - case 1: { - for (int i = 0; i < num_horizon; ++i) { - Eigen::MatrixXd design = bvhar::build_x0(expand_mat[i], lag, include_mean); - ols_objs[i] = std::unique_ptr(new bvhar::MultiOls(design, expand_y0[i])); - } - } - case 2: { - for (int i = 0; i < num_horizon; ++i) { - Eigen::MatrixXd design = bvhar::build_x0(expand_mat[i], lag, include_mean); - ols_objs[i] = std::unique_ptr(new bvhar::LltOls(design, expand_y0[i])); - } - } - case 3: { - for (int i = 0; i < num_horizon; ++i) { - Eigen::MatrixXd design = bvhar::build_x0(expand_mat[i], lag, include_mean); - ols_objs[i] = std::unique_ptr(new bvhar::QrOls(design, expand_y0[i])); - } - } - } - std::vector> forecaster(num_horizon); - std::vector res(num_horizon); -#ifdef _OPENMP - #pragma omp parallel for num_threads(nthreads) -#endif - for (int window = 0; window < num_horizon; window++) { - bvhar::OlsFit ols_fit = ols_objs[window]->returnOlsFit(lag); - forecaster[window].reset(new bvhar::VarForecaster(ols_fit, step, expand_y0[window], include_mean)); - res[window] = forecaster[window]->forecastPoint().bottomRows(1); - ols_objs[window].reset(); // free the memory by making nullptr - forecaster[window].reset(); // free the memory by making nullptr - } - return std::accumulate( - res.begin() + 1, res.end(), res[0], - [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { - Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); - concat_mat << acc, - curr; - return concat_mat; - } - ); -} - -//' Out-of-Sample Forecasting of VHAR based on Expanding Window -//' -//' This function conducts an expanding window forecasting of VHAR. -//' -//' @param y Time series data of which columns indicate the variables -//' @param week Integer, order for weekly term -//' @param month Integer, order for monthly term -//' @param include_mean Add constant term -//' @param step Integer, Step to forecast -//' @param y_test Evaluation time series data period after `y` -//' @param method Method to solve linear equation system. 1: normal equation, 2: cholesky, 3: HouseholderQR. -//' @param nthreads Number of threads for openmp -//' -//' @noRd -// [[Rcpp::export]] -Eigen::MatrixXd expand_vhar(Eigen::MatrixXd y, int week, int month, bool include_mean, int step, Eigen::MatrixXd y_test, int method, int nthreads) { -#ifdef _OPENMP - Eigen::setNbThreads(nthreads); -#endif - int num_window = y.rows(); - int dim = y.cols(); - int num_test = y_test.rows(); - int num_horizon = num_test - step + 1; // longest forecast horizon - Eigen::MatrixXd tot_mat(num_window + num_test, dim); - tot_mat << y, - y_test; - std::vector expand_mat(num_horizon); - std::vector expand_y0(num_horizon); - Eigen::MatrixXd har_trans = bvhar::build_vhar(dim, week, month, include_mean); - for (int i = 0; i < num_horizon; i++) { - expand_mat[i] = tot_mat.topRows(num_window + i); - expand_y0[i] = bvhar::build_y0(expand_mat[i], month, month + 1); - } - std::vector> ols_objs(num_horizon); - switch(method) { - case 1: { - for (int i = 0; i < num_horizon; ++i) { - Eigen::MatrixXd design = bvhar::build_x0(expand_mat[i], month, include_mean) * har_trans.transpose(); - ols_objs[i] = std::unique_ptr(new bvhar::MultiOls(design, expand_y0[i])); - } - } - case 2: { - for (int i = 0; i < num_horizon; ++i) { - Eigen::MatrixXd design = bvhar::build_x0(expand_mat[i], month, include_mean) * har_trans.transpose(); - ols_objs[i] = std::unique_ptr(new bvhar::LltOls(design, expand_y0[i])); - } - } - case 3: { - for (int i = 0; i < num_horizon; ++i) { - Eigen::MatrixXd design = bvhar::build_x0(expand_mat[i], month, include_mean) * har_trans.transpose(); - ols_objs[i] = std::unique_ptr(new bvhar::QrOls(design, expand_y0[i])); - } - } - } - std::vector> forecaster(num_horizon); - std::vector res(num_horizon); -#ifdef _OPENMP - #pragma omp parallel for num_threads(nthreads) -#endif - for (int window = 0; window < num_horizon; window++) { - bvhar::OlsFit ols_fit = ols_objs[window]->returnOlsFit(month); - forecaster[window].reset(new bvhar::VharForecaster(ols_fit, step, expand_y0[window], har_trans, include_mean)); - res[window] = forecaster[window]->forecastPoint().bottomRows(1); - ols_objs[window].reset(); // free the memory by making nullptr - forecaster[window].reset(); // free the memory by making nullptr - } - return std::accumulate( - res.begin() + 1, res.end(), res[0], - [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { - Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); - concat_mat << acc, - curr; - return concat_mat; - } - ); -} - -//' Out-of-Sample Forecasting of BVAR based on Expanding Window -//' -//' This function conducts an expanding window forecasting of BVAR with Minnesota prior. -//' -//' @param y Time series data of which columns indicate the variables -//' @param lag BVAR order -//' @param bayes_spec List, BVAR specification -//' @param include_mean Add constant term -//' @param step Integer, Step to forecast -//' @param y_test Evaluation time series data period after `y` -//' -//' @noRd -// [[Rcpp::export]] -Eigen::MatrixXd expand_bvar(Eigen::MatrixXd y, int lag, Rcpp::List bayes_spec, bool include_mean, int step, Eigen::MatrixXd y_test, Eigen::VectorXi seed_forecast, int nthreads) { - if (!bayes_spec.inherits("bvharspec")) { - Rcpp::stop("'object' must be bvharspec object."); - } - int num_window = y.rows(); - int dim = y.cols(); - int num_test = y_test.rows(); - int num_horizon = num_test - step + 1; - Eigen::MatrixXd tot_mat(num_window + num_test, dim); - tot_mat << y, - y_test; - std::vector expand_mat(num_horizon); - std::vector expand_y0(num_horizon); - for (int i = 0; i < num_horizon; i++) { - expand_mat[i] = tot_mat.topRows(num_window + i); - expand_y0[i] = bvhar::build_y0(expand_mat[i], lag, lag + 1); - } - tot_mat.resize(0, 0); // free the memory - std::vector> mn_objs(num_horizon); - for (int i = 0; i < num_horizon; ++i) { - bvhar::BvarSpec mn_spec(bayes_spec); - mn_objs[i].reset(new bvhar::MinnBvar(expand_mat[i], lag, mn_spec, include_mean)); - } - std::vector> forecaster(num_horizon); - std::vector res(num_horizon); -#ifdef _OPENMP - #pragma omp parallel for num_threads(nthreads) -#endif - for (int window = 0; window < num_horizon; window++) { - bvhar::MinnFit mn_fit = mn_objs[window]->returnMinnFit(); - forecaster[window].reset(new bvhar::BvarForecaster(mn_fit, step, expand_y0[window], lag, 1, include_mean, static_cast(seed_forecast[window]))); - res[window] = forecaster[window]->returnPoint().bottomRows(1); - mn_objs[window].reset(); // free the memory by making nullptr - forecaster[window].reset(); // free the memory by making nullptr - } - return std::accumulate( - res.begin() + 1, res.end(), res[0], - [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { - Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); - concat_mat << acc, - curr; - return concat_mat; - } - ); -} - -//' Out-of-Sample Forecasting of BVAR based on Expanding Window -//' -//' This function conducts an expanding window forecasting of BVAR with Flat prior. -//' -//' @param y Time series data of which columns indicate the variables -//' @param lag BVAR order -//' @param bayes_spec List, BVAR specification -//' @param include_mean Add constant term -//' @param step Integer, Step to forecast -//' @param y_test Evaluation time series data period after `y` -//' -//' @noRd -// [[Rcpp::export]] -Eigen::MatrixXd expand_bvarflat(Eigen::MatrixXd y, int lag, Eigen::MatrixXd U, bool include_mean, int step, Eigen::MatrixXd y_test, Eigen::VectorXi seed_forecast, int nthreads) { - int num_window = y.rows(); - int dim = y.cols(); - int num_test = y_test.rows(); - int num_horizon = num_test - step + 1; - Eigen::MatrixXd tot_mat(num_window + num_test, dim); - tot_mat << y, - y_test; - std::vector expand_mat(num_horizon); - std::vector expand_y0(num_horizon); - for (int i = 0; i < num_horizon; i++) { - expand_mat[i] = tot_mat.topRows(num_window + i); - expand_y0[i] = bvhar::build_y0(expand_mat[i], lag, lag + 1); - } - tot_mat.resize(0, 0); // free the memory - std::vector> mn_objs(num_horizon); - for (int i = 0; i < num_horizon; ++i) { - Eigen::MatrixXd x = bvhar::build_x0(expand_mat[i], lag, include_mean); - mn_objs[i].reset(new bvhar::MinnFlat(x, expand_y0[i], U)); - } - std::vector> forecaster(num_horizon); - std::vector res(num_horizon); -#ifdef _OPENMP - #pragma omp parallel for num_threads(nthreads) -#endif - for (int window = 0; window < num_horizon; window++) { - bvhar::MinnFit mn_fit = mn_objs[window]->returnMinnFit(); - forecaster[window].reset(new bvhar::BvarForecaster(mn_fit, step, expand_y0[window], lag, 1, include_mean, static_cast(seed_forecast[window]))); - res[window] = forecaster[window]->returnPoint().bottomRows(1); - mn_objs[window].reset(); // free the memory by making nullptr - forecaster[window].reset(); // free the memory by making nullptr - } - return std::accumulate( - res.begin() + 1, res.end(), res[0], - [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { - Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); - concat_mat << acc, - curr; - return concat_mat; - } - ); -} - -//' Out-of-Sample Forecasting of BVHAR based on Expanding Window -//' -//' This function conducts an expanding window forecasting of BVHAR with Minnesota prior. -//' -//' @param y Time series data of which columns indicate the variables -//' @param har `r lifecycle::badge("experimental")` Numeric vector for weekly and monthly order. -//' @param bayes_spec List, BVHAR specification -//' @param include_mean Add constant term -//' @param step Integer, Step to forecast -//' @param y_test Evaluation time series data period after `y` -//' -//' @noRd -// [[Rcpp::export]] -Eigen::MatrixXd expand_bvhar(Eigen::MatrixXd y, int week, int month, Rcpp::List bayes_spec, bool include_mean, int step, Eigen::MatrixXd y_test, Eigen::VectorXi seed_forecast, int nthreads) { - if (!bayes_spec.inherits("bvharspec")) { - Rcpp::stop("'object' must be bvharspec object."); - } - int num_window = y.rows(); - int dim = y.cols(); - int num_test = y_test.rows(); - int num_horizon = num_test - step + 1; - Eigen::MatrixXd tot_mat(num_window + num_test, dim); - tot_mat << y, - y_test; - std::vector expand_mat(num_horizon); - std::vector expand_y0(num_horizon); - Eigen::MatrixXd har_trans = bvhar::build_vhar(dim, week, month, include_mean); - for (int i = 0; i < num_horizon; i++) { - expand_mat[i] = tot_mat.topRows(num_window + i); - expand_y0[i] = bvhar::build_y0(expand_mat[i], month, month + 1); - } - tot_mat.resize(0, 0); // free the memory - std::vector> mn_objs(num_horizon); - if (bayes_spec.containsElementNamed("delta")) { - for (int i = 0; i < num_horizon; ++i) { - bvhar::BvarSpec mn_spec(bayes_spec); - mn_objs[i] = std::unique_ptr(new bvhar::MinnBvharS(expand_mat[i], week, month, mn_spec, include_mean)); - } - } else { - for (int i = 0; i < num_horizon; ++i) { - bvhar::BvharSpec mn_spec(bayes_spec); - mn_objs[i] = std::unique_ptr(new bvhar::MinnBvharL(expand_mat[i], week, month, mn_spec, include_mean)); - } - } - std::vector> forecaster(num_horizon); - std::vector res(num_horizon); -#ifdef _OPENMP - #pragma omp parallel for num_threads(nthreads) -#endif - for (int window = 0; window < num_horizon; window++) { - bvhar::MinnFit mn_fit = mn_objs[window]->returnMinnFit(); - forecaster[window].reset(new bvhar::BvharForecaster(mn_fit, step, expand_y0[window], har_trans, month, 1, include_mean, static_cast(seed_forecast[window]))); - res[window] = forecaster[window]->returnPoint().bottomRows(1); - mn_objs[window].reset(); // free the memory by making nullptr - forecaster[window].reset(); // free the memory by making nullptr - } - return std::accumulate( - res.begin() + 1, res.end(), res[0], - [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { - Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); - concat_mat << acc, - curr; - return concat_mat; - } - ); -} - -//' Out-of-Sample Forecasting of VAR-SV based on Rolling Window -//' -//' This function conducts an rolling window forecasting of BVAR-SV. -//' -//' @param y Time series data of which columns indicate the variables -//' @param lag VAR order -//' @param num_chains Number of MCMC chains -//' @param num_iter Number of iteration for MCMC -//' @param num_burn Number of burn-in (warm-up) for MCMC -//' @param thinning Thinning -//' @param param_sv SV specification list -//' @param param_prior Prior specification list -//' @param param_intercept Intercept specification list -//' @param param_init Initialization specification list -//' @param get_lpl Compute LPL -//' @param seed_chain Seed for each window and chain in the form of matrix -//' @param seed_forecast Seed for each window forecast -//' @param nthreads Number of threads for openmp -//' @param grp_id Unique group id -//' @param grp_mat Group matrix -//' @param include_mean Constant term -//' @param stable Filter stable draws -//' @param step Integer, Step to forecast -//' @param y_test Evaluation time series data period after `y` -//' @param nthreads Number of threads -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::List expand_bvarldlt(Eigen::MatrixXd y, int lag, int num_chains, int num_iter, int num_burn, int thinning, - bool sparse, double level, Rcpp::List fit_record, - Rcpp::List param_reg, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, - Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, - bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, - bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads) { - auto forecaster = [&]() -> std::unique_ptr> { - if (ggl) { - return std::make_unique>( - y, lag, num_chains, num_iter, num_burn, thinning, - sparse, level, fit_record, - param_reg, param_prior, param_intercept, param_init, prior_type, - grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, - get_lpl, seed_chain, seed_forecast, display_progress, nthreads, true - ); - } - return std::make_unique>( - y, lag, num_chains, num_iter, num_burn, thinning, - sparse, level, fit_record, - param_reg, param_prior, param_intercept, param_init, prior_type, - grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, - get_lpl, seed_chain, seed_forecast, display_progress, nthreads, true - ); - }(); - return forecaster->returnForecast(); -} - -//' Out-of-Sample Forecasting of VAR-SV based on Rolling Window -//' -//' This function conducts an rolling window forecasting of BVAR-SV. -//' -//' @param y Time series data of which columns indicate the variables -//' @param lag VAR order -//' @param num_chains Number of MCMC chains -//' @param num_iter Number of iteration for MCMC -//' @param num_burn Number of burn-in (warm-up) for MCMC -//' @param thinning Thinning -//' @param param_sv SV specification list -//' @param param_prior Prior specification list -//' @param param_intercept Intercept specification list -//' @param param_init Initialization specification list -//' @param get_lpl Compute LPL -//' @param seed_chain Seed for each window and chain in the form of matrix -//' @param seed_forecast Seed for each window forecast -//' @param nthreads Number of threads for openmp -//' @param grp_id Unique group id -//' @param grp_mat Group matrix -//' @param include_mean Constant term -//' @param stable Filter stable draws -//' @param step Integer, Step to forecast -//' @param y_test Evaluation time series data period after `y` -//' @param nthreads Number of threads -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::List expand_bvharldlt(Eigen::MatrixXd y, int week, int month, int num_chains, int num_iter, int num_burn, int thinning, - bool sparse, double level, Rcpp::List fit_record, - Rcpp::List param_reg, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, - Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, - bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, - bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads) { - auto forecaster = [&]() -> std::unique_ptr> { - if (ggl) { - return std::make_unique>( - y, week, month, num_chains, num_iter, num_burn, thinning, - sparse, level, fit_record, - param_reg, param_prior, param_intercept, param_init, prior_type, - grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, - get_lpl, seed_chain, seed_forecast, display_progress, nthreads, true - ); - } - return std::make_unique>( - y, week, month, num_chains, num_iter, num_burn, thinning, - sparse, level, fit_record, - param_reg, param_prior, param_intercept, param_init, prior_type, - grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, - get_lpl, seed_chain, seed_forecast, display_progress, nthreads, true - ); - }(); - return forecaster->returnForecast(); -} - -//' Out-of-Sample Forecasting of VAR-SV based on Rolling Window -//' -//' This function conducts an rolling window forecasting of BVAR-SV. -//' -//' @param y Time series data of which columns indicate the variables -//' @param lag VAR order -//' @param num_chains Number of MCMC chains -//' @param num_iter Number of iteration for MCMC -//' @param num_burn Number of burn-in (warm-up) for MCMC -//' @param thinning Thinning -//' @param param_sv SV specification list -//' @param param_prior Prior specification list -//' @param param_intercept Intercept specification list -//' @param param_init Initialization specification list -//' @param get_lpl Compute LPL -//' @param seed_chain Seed for each window and chain in the form of matrix -//' @param seed_forecast Seed for each window forecast -//' @param nthreads Number of threads for openmp -//' @param grp_id Unique group id -//' @param grp_mat Group matrix -//' @param include_mean Constant term -//' @param stable Filter stable draws -//' @param step Integer, Step to forecast -//' @param y_test Evaluation time series data period after `y` -//' @param nthreads Number of threads -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::List expand_bvarsv(Eigen::MatrixXd y, int lag, int num_chains, int num_iter, int num_burn, int thinning, - bool sv, bool sparse, double level, Rcpp::List fit_record, - Rcpp::List param_sv, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, - Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, - bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, - bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads) { - auto forecaster = [&]() -> std::unique_ptr> { - if (ggl) { - return std::make_unique>( - y, lag, num_chains, num_iter, num_burn, thinning, - sparse, level, fit_record, - param_sv, param_prior, param_intercept, param_init, prior_type, - grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, - get_lpl, seed_chain, seed_forecast, display_progress, nthreads, sv - ); - } - return std::make_unique>( - y, lag, num_chains, num_iter, num_burn, thinning, - sparse, level, fit_record, - param_sv, param_prior, param_intercept, param_init, prior_type, - grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, - get_lpl, seed_chain, seed_forecast, display_progress, nthreads, sv - ); - }(); - return forecaster->returnForecast(); -} - -//' Out-of-Sample Forecasting of VAR-SV based on Rolling Window -//' -//' This function conducts an rolling window forecasting of BVAR-SV. -//' -//' @param y Time series data of which columns indicate the variables -//' @param lag VAR order -//' @param num_chains Number of MCMC chains -//' @param num_iter Number of iteration for MCMC -//' @param num_burn Number of burn-in (warm-up) for MCMC -//' @param thinning Thinning -//' @param param_sv SV specification list -//' @param param_prior Prior specification list -//' @param param_intercept Intercept specification list -//' @param param_init Initialization specification list -//' @param get_lpl Compute LPL -//' @param seed_chain Seed for each window and chain in the form of matrix -//' @param seed_forecast Seed for each window forecast -//' @param nthreads Number of threads for openmp -//' @param grp_id Unique group id -//' @param grp_mat Group matrix -//' @param include_mean Constant term -//' @param stable Filter stable draws -//' @param step Integer, Step to forecast -//' @param y_test Evaluation time series data period after `y` -//' @param nthreads Number of threads -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::List expand_bvharsv(Eigen::MatrixXd y, int week, int month, int num_chains, int num_iter, int num_burn, int thinning, - bool sv, bool sparse, double level, Rcpp::List fit_record, - Rcpp::List param_sv, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, - Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, - bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, - bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads) { - auto forecaster = [&]() -> std::unique_ptr> { - if (ggl) { - return std::make_unique>( - y, week, month, num_chains, num_iter, num_burn, thinning, - sparse, level, fit_record, - param_sv, param_prior, param_intercept, param_init, prior_type, - grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, - get_lpl, seed_chain, seed_forecast, display_progress, nthreads, sv - ); - } - return std::make_unique>( - y, week, month, num_chains, num_iter, num_burn, thinning, - sparse, level, fit_record, - param_sv, param_prior, param_intercept, param_init, prior_type, - grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, - get_lpl, seed_chain, seed_forecast, display_progress, nthreads, sv - ); - }(); - return forecaster->returnForecast(); -} diff --git a/src/forecast.cpp b/src/forecast.cpp deleted file mode 100644 index b021bab4..00000000 --- a/src/forecast.cpp +++ /dev/null @@ -1,472 +0,0 @@ -#include - -//' Forecasting Vector Autoregression -//' -//' @param object A `varlse` object -//' @param step Integer, Step to forecast -//' @details -//' n-step ahead forecasting using VAR(p) recursively, based on pp35 of Lütkepohl (2007). -//' -//' @references Lütkepohl, H. (2007). *New Introduction to Multiple Time Series Analysis*. Springer Publishing. [https://doi.org/10.1007/978-3-540-27752-1](https://doi.org/10.1007/978-3-540-27752-1) -//' @noRd -// [[Rcpp::export]] -Eigen::MatrixXd forecast_var(Rcpp::List object, int step) { - if (! object.inherits("varlse")) { - Rcpp::stop("'object' must be varlse object."); - } - Eigen::MatrixXd response_mat = object["y0"]; // Y0 - Eigen::MatrixXd coef_mat = object["coefficients"]; // bhat - int var_lag = object["p"]; // VAR(p) - bool include_mean = Rcpp::as(object["type"]) == "const"; - bvhar::OlsFit ols_fit(coef_mat, var_lag); - std::unique_ptr forecaster(new bvhar::VarForecaster(ols_fit, step, response_mat, include_mean)); - return forecaster->forecastPoint(); -} - -//' Forecasting Vector HAR -//' -//' @param object A `vharlse` object -//' @param step Integer, Step to forecast -//' @details -//' n-step ahead forecasting using VHAR recursively. -//' -//' @noRd -// [[Rcpp::export]] -Eigen::MatrixXd forecast_vhar(Rcpp::List object, int step) { - if (!object.inherits("vharlse")) { - Rcpp::stop("'object' must be vharlse object."); - } - Eigen::MatrixXd response_mat = object["y0"]; // Y0 - Eigen::MatrixXd coef_mat = object["coefficients"]; // bhat - Eigen::MatrixXd HARtrans = object["HARtrans"]; // HAR transformation - int month = object["month"]; - bool include_mean = Rcpp::as(object["type"]) == "const"; - bvhar::OlsFit ols_fit(coef_mat, month); - std::unique_ptr forecaster(new bvhar::VharForecaster(ols_fit, step, response_mat, HARtrans, include_mean)); - return forecaster->forecastPoint(); -} - -//' Forecasting BVAR(p) -//' -//' @param object A `bvarmn` or `bvarflat` object -//' @param step Integer, Step to forecast -//' @param num_sim Integer, number to simulate parameters from posterior distribution -//' @details -//' n-step ahead forecasting using BVAR(p) recursively. -//' -//' For given number of simulation (`num_sim`), -//' -//' 1. Generate \eqn{(A^{(b)}, \Sigma_e^{(b)}) \sim MIW} (posterior) -//' 2. Recursively, \eqn{j = 1, \ldots, h} (`step`) -//' - Point forecast: Use \eqn{\hat{A}} -//' - Predictive distribution: Again generate \eqn{\tilde{Y}_{n + j}^{(b)} \sim A^{(b)}, \Sigma_e^{(b)} \sim MN} -//' - tilde notation indicates simulated ones -//' -//' @references -//' Lütkepohl, H. (2007). *New Introduction to Multiple Time Series Analysis*. Springer Publishing. [https://doi.org/10.1007/978-3-540-27752-1](https://doi.org/10.1007/978-3-540-27752-1) -//' -//' Litterman, R. B. (1986). *Forecasting with Bayesian Vector Autoregressions: Five Years of Experience*. Journal of Business & Economic Statistics, 4(1), 25. [https://doi:10.2307/1391384](https://doi:10.2307/1391384) -//' -//' Bańbura, M., Giannone, D., & Reichlin, L. (2010). *Large Bayesian vector auto regressions*. Journal of Applied Econometrics, 25(1). [https://doi:10.1002/jae.1137](https://doi:10.1002/jae.1137) -//' -//' Ghosh, S., Khare, K., & Michailidis, G. (2018). *High-Dimensional Posterior Consistency in Bayesian Vector Autoregressive Models*. Journal of the American Statistical Association, 114(526). [https://doi:10.1080/01621459.2018.1437043](https://doi:10.1080/01621459.2018.1437043) -//' -//' Karlsson, S. (2013). *Chapter 15 Forecasting with Bayesian Vector Autoregression*. Handbook of Economic Forecasting, 2, 791-897. doi:[10.1016/b978-0-444-62731-5.00015-4](https://doi.org/10.1016/B978-0-444-62731-5.00015-4) -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::List forecast_bvar(Rcpp::List object, int step, int num_sim, unsigned int seed) { - if (!object.inherits("bvarmn") && !object.inherits("bvarflat")) { - Rcpp::stop("'object' must be bvarmn or bvarflat object."); - } - Eigen::MatrixXd response_mat = object["y0"]; // Y0 - Eigen::MatrixXd posterior_mean_mat = object["coefficients"]; // Ahat = posterior mean of MN - Eigen::MatrixXd posterior_prec_mat = object["mn_prec"]; // vhat = posterior precision of MN to compute SE - Eigen::MatrixXd posterior_scale = object["covmat"]; // Sighat = posterior scale of IW - double posterior_shape = object["iw_shape"]; // posterior shape of IW - int var_lag = object["p"]; // VAR(p) - bool include_mean = Rcpp::as(object["type"]) == "const"; - bvhar::MinnFit mn_fit(posterior_mean_mat, posterior_prec_mat, posterior_scale, posterior_shape); - std::unique_ptr forecaster(new bvhar::BvarForecaster(mn_fit, step, response_mat, var_lag, num_sim, include_mean, seed)); - forecaster->forecastDensity(); - return forecaster->returnForecast(); -} - -//' Forecasting Bayesian VHAR -//' -//' @param object A `bvharmn` object -//' @param step Integer, Step to forecast -//' @param num_sim Integer, number to simulate parameters from posterior distribution -//' @details -//' n-step ahead forecasting using VHAR recursively. -//' -//' For given number of simulation (`num_sim`), -//' -//' 1. Generate \eqn{(\Phi^{(b)}, \Sigma_e^{(b)}) \sim MIW} (posterior) -//' 2. Recursively, \eqn{j = 1, \ldots, h} (`step`) -//' - Point forecast: Use \eqn{\hat\Phi} -//' - Predictive distribution: Again generate \eqn{\tilde{Y}_{n + j}^{(b)} \sim \Phi^{(b)}, \Sigma_e^{(b)} \sim MN} -//' - tilde notation indicates simulated ones -//' -//' @references Kim, Y. G., and Baek, C. (2024). *Bayesian vector heterogeneous autoregressive modeling*. Journal of Statistical Computation and Simulation, 94(6), 1139-1157. -//' @noRd -// [[Rcpp::export]] -Rcpp::List forecast_bvharmn(Rcpp::List object, int step, int num_sim, unsigned int seed) { - if (!object.inherits("bvharmn")) { - Rcpp::stop("'object' must be bvharmn object."); - } - Eigen::MatrixXd response_mat = object["y0"]; // Y0 - Eigen::MatrixXd posterior_mean_mat = object["coefficients"]; // Phihat = posterior mean of MN: h x m, h = 3m (+ 1) - Eigen::MatrixXd posterior_prec_mat = object["mn_prec"]; // Psihat = posterior precision of MN to compute SE: h x h - Eigen::MatrixXd posterior_mn_scale_u = posterior_prec_mat.inverse(); - Eigen::MatrixXd posterior_scale = object["covmat"]; // Sighat = posterior scale of IW: m x m - double posterior_shape = object["iw_shape"]; // posterior shape of IW - Eigen::MatrixXd HARtrans = object["HARtrans"]; // HAR transformation: h x k0, k0 = 22m (+ 1) - Eigen::MatrixXd transformed_prec_mat = HARtrans.transpose() * posterior_prec_mat.inverse() * HARtrans; // to compute SE: play a role V in BVAR - int month = object["month"]; - bool include_mean = Rcpp::as(object["type"]) == "const"; - bvhar::MinnFit mn_fit(posterior_mean_mat, posterior_prec_mat, posterior_scale, posterior_shape); - std::unique_ptr forecaster(new bvhar::BvharForecaster(mn_fit, step, response_mat, HARtrans, month, num_sim, include_mean, seed)); - forecaster->forecastDensity(); - return forecaster->returnForecast(); -} - -//' Forecasting predictive density of BVAR -//' -//' @param num_chains Number of chains -//' @param var_lag VAR order. -//' @param step Integer, Step to forecast. -//' @param response_mat Response matrix. -//' @param sv Use Innovation? -//' @param sparse Use restricted model? -//' @param level CI level to give sparsity. Valid when `prior_type` is 0. -//' @param fit_record MCMC records list -//' @param prior_type Prior type. If 0, use CI. Valid when sparse is true. -//' @param seed_chain Seed for each chain -//' @param stable Filter stable draws -//' @param include_mean Include constant term? -//' @param nthreads OpenMP number of threads -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::List forecast_bvarldlt(int num_chains, int var_lag, int step, Eigen::MatrixXd response_mat, - bool sparse, double level, Rcpp::List fit_record, int prior_type, - Eigen::VectorXi seed_chain, bool include_mean, bool stable, int nthreads) { - auto forecaster = std::make_unique>( - num_chains, var_lag, step, response_mat, - sparse, level, fit_record, - seed_chain, include_mean, stable, nthreads - ); - return Rcpp::wrap(forecaster->returnForecast()); -} - -//' Forecasting Predictive Density of BVHAR -//' -//' @param num_chains Number of MCMC chains -//' @param month VHAR month order. -//' @param step Integer, Step to forecast. -//' @param response_mat Response matrix. -//' @param HARtrans VHAR linear transformation matrix -//' @param sv Use Innovation? -//' @param sparse Use restricted model? -//' @param level CI level to give sparsity. Valid when `prior_type` is 0. -//' @param fit_record MCMC records list -//' @param prior_type Prior type. If 0, use CI. Valid when sparse is true. -//' @param seed_chain Seed for each chain -//' @param include_mean Include constant term? -//' @param stable Filter stable draws -//' @param nthreads OpenMP number of threads -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::List forecast_bvharldlt(int num_chains, int month, int step, Eigen::MatrixXd response_mat, Eigen::MatrixXd HARtrans, - bool sparse, double level, Rcpp::List fit_record, int prior_type, - Eigen::VectorXi seed_chain, bool include_mean, bool stable, int nthreads) { - auto forecaster = std::make_unique>( - num_chains, month, step, response_mat, HARtrans, - sparse, level, fit_record, - seed_chain, include_mean, stable, nthreads - ); - return Rcpp::wrap(forecaster->returnForecast()); -} - -//' Out-of-Sample Forecasting of VAR-SV based on Rolling Window -//' -//' This function conducts an rolling window forecasting of BVAR-SV. -//' -//' @param y Time series data of which columns indicate the variables -//' @param lag VAR order -//' @param num_chains Number of MCMC chains -//' @param num_iter Number of iteration for MCMC -//' @param num_burn Number of burn-in (warm-up) for MCMC -//' @param thinning Thinning -//' @param param_reg SV specification list -//' @param param_prior Prior specification list -//' @param param_intercept Intercept specification list -//' @param param_init Initialization specification list -//' @param get_lpl Compute LPL -//' @param seed_chain Seed for each window and chain in the form of matrix -//' @param seed_forecast Seed for each window forecast -//' @param nthreads Number of threads for openmp -//' @param grp_id Unique group id -//' @param grp_mat Group matrix -//' @param include_mean Constant term -//' @param stable Filter stable draws -//' @param step Integer, Step to forecast -//' @param y_test Evaluation time series data period after `y` -//' @param nthreads Number of threads -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::List roll_bvarldlt(Eigen::MatrixXd y, int lag, int num_chains, int num_iter, int num_burn, int thinning, - bool sparse, double level, Rcpp::List fit_record, - Rcpp::List param_reg, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, - Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, - bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, - bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads) { - auto forecaster = [&]() -> std::unique_ptr> { - if (ggl) { - return std::make_unique>( - y, lag, num_chains, num_iter, num_burn, thinning, - sparse, level, fit_record, - param_reg, param_prior, param_intercept, param_init, prior_type, - grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, - get_lpl, seed_chain, seed_forecast, display_progress, nthreads, true - ); - } - return std::make_unique>( - y, lag, num_chains, num_iter, num_burn, thinning, - sparse, level, fit_record, - param_reg, param_prior, param_intercept, param_init, prior_type, - grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, - get_lpl, seed_chain, seed_forecast, display_progress, nthreads, true - ); - }(); - return forecaster->returnForecast(); -} - -//' Out-of-Sample Forecasting of VAR-SV based on Rolling Window -//' -//' This function conducts an rolling window forecasting of BVAR-SV. -//' -//' @param y Time series data of which columns indicate the variables -//' @param lag VAR order -//' @param num_chains Number of MCMC chains -//' @param num_iter Number of iteration for MCMC -//' @param num_burn Number of burn-in (warm-up) for MCMC -//' @param thinning Thinning -//' @param param_sv SV specification list -//' @param param_prior Prior specification list -//' @param param_intercept Intercept specification list -//' @param param_init Initialization specification list -//' @param get_lpl Compute LPL -//' @param seed_chain Seed for each window and chain in the form of matrix -//' @param seed_forecast Seed for each window forecast -//' @param nthreads Number of threads for openmp -//' @param grp_id Unique group id -//' @param grp_mat Group matrix -//' @param include_mean Constant term -//' @param stable Filter stable draws -//' @param step Integer, Step to forecast -//' @param y_test Evaluation time series data period after `y` -//' @param nthreads Number of threads -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::List roll_bvharldlt(Eigen::MatrixXd y, int week, int month, int num_chains, int num_iter, int num_burn, int thinning, - bool sparse, double level, Rcpp::List fit_record, - Rcpp::List param_reg, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, - Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, - bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, - bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads) { - auto forecaster = [&]() -> std::unique_ptr> { - if (ggl) { - return std::make_unique>( - y, week, month, num_chains, num_iter, num_burn, thinning, - sparse, level, fit_record, - param_reg, param_prior, param_intercept, param_init, prior_type, - grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, - get_lpl, seed_chain, seed_forecast, display_progress, nthreads, true - ); - } - return std::make_unique>( - y, week, month, num_chains, num_iter, num_burn, thinning, - sparse, level, fit_record, - param_reg, param_prior, param_intercept, param_init, prior_type, - grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, - get_lpl, seed_chain, seed_forecast, display_progress, nthreads, true - ); - }(); - return forecaster->returnForecast(); -} - -//' Forecasting predictive density of VAR-SV -//' -//' @param num_chains Number of chains -//' @param var_lag VAR order. -//' @param step Integer, Step to forecast. -//' @param response_mat Response matrix. -//' @param sv Use Innovation? -//' @param sparse Use restricted model? -//' @param level CI level to give sparsity. Valid when `prior_type` is 0. -//' @param fit_record MCMC records list -//' @param prior_type Prior type. If 0, use CI. Valid when sparse is true. -//' @param seed_chain Seed for each chain -//' @param stable Filter stable draws -//' @param include_mean Include constant term? -//' @param nthreads OpenMP number of threads -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::List forecast_bvarsv(int num_chains, int var_lag, int step, Eigen::MatrixXd response_mat, - bool sv, bool sparse, double level, Rcpp::List fit_record, int prior_type, - Eigen::VectorXi seed_chain, bool include_mean, bool stable, int nthreads) { - auto forecaster = std::make_unique>( - num_chains, var_lag, step, response_mat, - sparse, level, fit_record, - seed_chain, include_mean, stable, nthreads, - sv - ); - return Rcpp::wrap(forecaster->returnForecast()); -} - -//' Forecasting Predictive Density of VHAR-SV -//' -//' @param num_chains Number of MCMC chains -//' @param month VHAR month order. -//' @param step Integer, Step to forecast. -//' @param response_mat Response matrix. -//' @param HARtrans VHAR linear transformation matrix -//' @param sv Use Innovation? -//' @param sparse Use restricted model? -//' @param level CI level to give sparsity. Valid when `prior_type` is 0. -//' @param fit_record MCMC records list -//' @param prior_type Prior type. If 0, use CI. Valid when sparse is true. -//' @param seed_chain Seed for each chain -//' @param include_mean Include constant term? -//' @param stable Filter stable draws -//' @param nthreads OpenMP number of threads -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::List forecast_bvharsv(int num_chains, int month, int step, Eigen::MatrixXd response_mat, Eigen::MatrixXd HARtrans, - bool sv, bool sparse, double level, Rcpp::List fit_record, int prior_type, - Eigen::VectorXi seed_chain, bool include_mean, bool stable, int nthreads) { - auto forecaster = std::make_unique>( - num_chains, month, step, response_mat, HARtrans, - sparse, level, fit_record, - seed_chain, include_mean, stable, nthreads, - sv - ); - return Rcpp::wrap(forecaster->returnForecast()); -} - -//' Out-of-Sample Forecasting of VAR-SV based on Rolling Window -//' -//' This function conducts an rolling window forecasting of BVAR-SV. -//' -//' @param y Time series data of which columns indicate the variables -//' @param lag VAR order -//' @param num_chains Number of MCMC chains -//' @param num_iter Number of iteration for MCMC -//' @param num_burn Number of burn-in (warm-up) for MCMC -//' @param thinning Thinning -//' @param param_sv SV specification list -//' @param param_prior Prior specification list -//' @param param_intercept Intercept specification list -//' @param param_init Initialization specification list -//' @param get_lpl Compute LPL -//' @param seed_chain Seed for each window and chain in the form of matrix -//' @param seed_forecast Seed for each window forecast -//' @param nthreads Number of threads for openmp -//' @param grp_id Unique group id -//' @param grp_mat Group matrix -//' @param include_mean Constant term -//' @param stable Filter stable draws -//' @param step Integer, Step to forecast -//' @param y_test Evaluation time series data period after `y` -//' @param nthreads Number of threads -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::List roll_bvarsv(Eigen::MatrixXd y, int lag, int num_chains, int num_iter, int num_burn, int thinning, - bool sv, bool sparse, double level, Rcpp::List fit_record, - Rcpp::List param_sv, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, - Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, - bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, - bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads) { - auto forecaster = [&]() -> std::unique_ptr> { - if (ggl) { - return std::make_unique>( - y, lag, num_chains, num_iter, num_burn, thinning, - sparse, level, fit_record, - param_sv, param_prior, param_intercept, param_init, prior_type, - grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, - get_lpl, seed_chain, seed_forecast, display_progress, nthreads, sv - ); - } - return std::make_unique>( - y, lag, num_chains, num_iter, num_burn, thinning, - sparse, level, fit_record, - param_sv, param_prior, param_intercept, param_init, prior_type, - grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, - get_lpl, seed_chain, seed_forecast, display_progress, nthreads, sv - ); - }(); - return forecaster->returnForecast(); -} - -//' Out-of-Sample Forecasting of VAR-SV based on Rolling Window -//' -//' This function conducts an rolling window forecasting of BVAR-SV. -//' -//' @param y Time series data of which columns indicate the variables -//' @param lag VAR order -//' @param num_chains Number of MCMC chains -//' @param num_iter Number of iteration for MCMC -//' @param num_burn Number of burn-in (warm-up) for MCMC -//' @param thinning Thinning -//' @param param_sv SV specification list -//' @param param_prior Prior specification list -//' @param param_intercept Intercept specification list -//' @param param_init Initialization specification list -//' @param get_lpl Compute LPL -//' @param seed_chain Seed for each window and chain in the form of matrix -//' @param seed_forecast Seed for each window forecast -//' @param nthreads Number of threads for openmp -//' @param grp_id Unique group id -//' @param grp_mat Group matrix -//' @param include_mean Constant term -//' @param stable Filter stable draws -//' @param step Integer, Step to forecast -//' @param y_test Evaluation time series data period after `y` -//' @param nthreads Number of threads -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::List roll_bvharsv(Eigen::MatrixXd y, int week, int month, int num_chains, int num_iter, int num_burn, int thinning, - bool sv, bool sparse, double level, Rcpp::List fit_record, - Rcpp::List param_sv, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, - Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, - bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, - bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads) { - auto forecaster = [&]() -> std::unique_ptr> { - if (ggl) { - return std::make_unique>( - y, week, month, num_chains, num_iter, num_burn, thinning, - sparse, level, fit_record, - param_sv, param_prior, param_intercept, param_init, prior_type, - grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, - get_lpl, seed_chain, seed_forecast, display_progress, nthreads, sv - ); - } - return std::make_unique>( - y, week, month, num_chains, num_iter, num_burn, thinning, - sparse, level, fit_record, - param_sv, param_prior, param_intercept, param_init, prior_type, - grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, - get_lpl, seed_chain, seed_forecast, display_progress, nthreads, sv - ); - }(); - return forecaster->returnForecast(); -} diff --git a/src/mniw.cpp b/src/mniw.cpp new file mode 100644 index 00000000..ebcd5624 --- /dev/null +++ b/src/mniw.cpp @@ -0,0 +1,999 @@ +#include + +//' BVAR(p) Point Estimates based on Minnesota Prior +//' +//' Point estimates for posterior distribution +//' +//' @param y Time series data +//' @param lag VAR order +//' @param bayes_spec BVAR Minnesota specification +//' @param include_mean Constant term +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::List estimate_bvar_mn(Eigen::MatrixXd y, int lag, Rcpp::List bayes_spec, bool include_mean) { + bvhar::BvarSpec mn_spec(bayes_spec); + std::unique_ptr mn_obj(new bvhar::MinnBvar(y, lag, mn_spec, include_mean)); + return mn_obj->returnMinnRes(); +} + +//' BVHAR Point Estimates based on Minnesota Prior +//' +//' Point estimates for posterior distribution +//' +//' @param y Time series data +//' @param week VHAR week order +//' @param month VHAR month order +//' @param bayes_spec BVHAR Minnesota specification +//' @param include_mean Constant term +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::List estimate_bvhar_mn(Eigen::MatrixXd y, int week, int month, Rcpp::List bayes_spec, bool include_mean) { + std::unique_ptr mn_obj; + if (bayes_spec.containsElementNamed("delta")) { + bvhar::BvarSpec bvhar_spec(bayes_spec); + mn_obj.reset(new bvhar::MinnBvharS(y, week, month, bvhar_spec, include_mean)); + } else { + bvhar::BvharSpec bvhar_spec(bayes_spec); + mn_obj.reset(new bvhar::MinnBvharL(y, week, month, bvhar_spec, include_mean)); + } + return mn_obj->returnMinnRes(); +} + +//' @noRd +// [[Rcpp::export]] +Rcpp::List estimate_bvar_mh(int num_chains, int num_iter, int num_burn, int thin, + Eigen::MatrixXd x, Eigen::MatrixXd y, Eigen::MatrixXd x_dummy, Eigen::MatrixXd y_dummy, + Rcpp::List param_prior, Rcpp::List param_init, + Eigen::VectorXi seed_chain, bool display_progress, int nthreads) { + std::vector> mn_objs(num_chains); + std::vector res(num_chains); + Rcpp::List lambda_spec = param_prior["lambda"]; + Rcpp::List psi_spec = param_prior["sigma"]; + bvhar::MhMinnSpec mn_spec(lambda_spec, psi_spec); + for (int i = 0; i < num_chains; ++i) { + Rcpp::List init_spec = param_init[i]; + bvhar::MhMinnInits mn_init(init_spec); + mn_objs[i].reset(new bvhar::MhMinnesota(num_iter, mn_spec, mn_init, x, y, x_dummy, y_dummy, static_cast(seed_chain[i]))); + mn_objs[i]->computePosterior(); + } + auto run_mh = [&](int chain) { + bvhar::bvharprogress bar(num_iter, display_progress); + bvhar::bvharinterrupt(); + for (int i = 0; i < num_iter; i++) { + if (bvhar::bvharinterrupt::is_interrupted()) { + #ifdef _OPENMP + #pragma omp critical + #endif + { + res[chain] = mn_objs[chain]->returnRecords(0, 1); + } + break; + } + bar.increment(); + if (display_progress) { + bar.update(); + } + mn_objs[chain]->doPosteriorDraws(); + } + #ifdef _OPENMP + #pragma omp critical + #endif + { + res[chain] = mn_objs[chain]->returnRecords(num_burn, thin); + } + }; + if (num_chains == 1) { + run_mh(0); + } else { + #ifdef _OPENMP + #pragma omp parallel for num_threads(nthreads) + #endif + for (int chain = 0; chain < num_chains; chain++) { + run_mh(chain); + } + } + return Rcpp::wrap(res); +} + +//' BVAR(p) Point Estimates based on Nonhierarchical Matrix Normal Prior +//' +//' Point estimates for Ghosh et al. (2018) nonhierarchical model for BVAR. +//' +//' @param x Design matrix X0 +//' @param y Response matrix Y0 +//' @param U Positive definite matrix, covariance matrix corresponding to the column of the model parameter B +//' +//' @details +//' In Ghosh et al. (2018), there are many models for BVAR such as hierarchical or non-hierarchical. +//' Among these, this function chooses the most simple non-hierarchical matrix normal prior in Section 3.1. +//' +//' @references +//' Ghosh, S., Khare, K., & Michailidis, G. (2018). *High-Dimensional Posterior Consistency in Bayesian Vector Autoregressive Models*. Journal of the American Statistical Association, 114(526). [https://doi:10.1080/01621459.2018.1437043](https://doi:10.1080/01621459.2018.1437043) +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::List estimate_mn_flat(Eigen::MatrixXd x, Eigen::MatrixXd y, Eigen::MatrixXd U) { + if (U.rows() != x.cols()) { + Rcpp::stop("Wrong dimension: U"); + } + if (U.cols() != x.cols()) { + Rcpp::stop("Wrong dimension: U"); + } + std::unique_ptr mn_obj(new bvhar::MinnFlat(x, y, U)); + return mn_obj->returnMinnRes(); +} + +//' @noRd +// [[Rcpp::export]] +Rcpp::List estimate_mniw(int num_chains, int num_iter, int num_burn, int thin, + const Eigen::MatrixXd& mn_mean, const Eigen::MatrixXd& mn_prec, + const Eigen::MatrixXd& iw_scale, double iw_shape, + Eigen::VectorXi seed_chain, bool display_progress, int nthreads) { + std::vector> mn_objs(num_chains); + for (int i = 0; i < num_chains; ++i) { + bvhar::MinnFit mn_fit(mn_mean, mn_prec, iw_scale, iw_shape); + mn_objs[i].reset(new bvhar::McmcMniw(num_iter, mn_fit, static_cast(seed_chain[i]))); + } + std::vector res(num_chains); + auto run_conj = [&](int chain) { + bvhar::bvharprogress bar(num_iter, display_progress); + for (int i = 0; i < num_iter; ++i) { + bar.increment(); + bar.update(); + mn_objs[chain]->doPosteriorDraws(); + } + #ifdef _OPENMP + #pragma omp critical + #endif + { + res[chain] = mn_objs[chain]->returnRecords(num_burn, thin); + } + }; + if (num_chains == 1) { + run_conj(0); + } else { + #ifdef _OPENMP + #pragma omp parallel for num_threads(nthreads) + #endif + for (int chain = 0; chain < num_chains; ++chain) { + run_conj(chain); + } + } + return Rcpp::wrap(res); +} + +//' Forecasting BVAR(p) +//' +//' @param object A `bvarmn` or `bvarflat` object +//' @param step Integer, Step to forecast +//' @param num_sim Integer, number to simulate parameters from posterior distribution +//' @details +//' n-step ahead forecasting using BVAR(p) recursively. +//' +//' For given number of simulation (`num_sim`), +//' +//' 1. Generate \eqn{(A^{(b)}, \Sigma_e^{(b)}) \sim MIW} (posterior) +//' 2. Recursively, \eqn{j = 1, \ldots, h} (`step`) +//' - Point forecast: Use \eqn{\hat{A}} +//' - Predictive distribution: Again generate \eqn{\tilde{Y}_{n + j}^{(b)} \sim A^{(b)}, \Sigma_e^{(b)} \sim MN} +//' - tilde notation indicates simulated ones +//' +//' @references +//' Lütkepohl, H. (2007). *New Introduction to Multiple Time Series Analysis*. Springer Publishing. [https://doi.org/10.1007/978-3-540-27752-1](https://doi.org/10.1007/978-3-540-27752-1) +//' +//' Litterman, R. B. (1986). *Forecasting with Bayesian Vector Autoregressions: Five Years of Experience*. Journal of Business & Economic Statistics, 4(1), 25. [https://doi:10.2307/1391384](https://doi:10.2307/1391384) +//' +//' Bańbura, M., Giannone, D., & Reichlin, L. (2010). *Large Bayesian vector auto regressions*. Journal of Applied Econometrics, 25(1). [https://doi:10.1002/jae.1137](https://doi:10.1002/jae.1137) +//' +//' Ghosh, S., Khare, K., & Michailidis, G. (2018). *High-Dimensional Posterior Consistency in Bayesian Vector Autoregressive Models*. Journal of the American Statistical Association, 114(526). [https://doi:10.1080/01621459.2018.1437043](https://doi:10.1080/01621459.2018.1437043) +//' +//' Karlsson, S. (2013). *Chapter 15 Forecasting with Bayesian Vector Autoregression*. Handbook of Economic Forecasting, 2, 791-897. doi:[10.1016/b978-0-444-62731-5.00015-4](https://doi.org/10.1016/B978-0-444-62731-5.00015-4) +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::List forecast_bvar(Rcpp::List object, int step, int num_sim, unsigned int seed) { + if (!object.inherits("bvarmn") && !object.inherits("bvarflat")) { + Rcpp::stop("'object' must be bvarmn or bvarflat object."); + } + Eigen::MatrixXd response_mat = object["y0"]; // Y0 + Eigen::MatrixXd posterior_mean_mat = object["coefficients"]; // Ahat = posterior mean of MN + Eigen::MatrixXd posterior_prec_mat = object["mn_prec"]; // vhat = posterior precision of MN to compute SE + Eigen::MatrixXd posterior_scale = object["covmat"]; // Sighat = posterior scale of IW + double posterior_shape = object["iw_shape"]; // posterior shape of IW + int var_lag = object["p"]; // VAR(p) + bool include_mean = Rcpp::as(object["type"]) == "const"; + bvhar::MinnFit mn_fit(posterior_mean_mat, posterior_prec_mat, posterior_scale, posterior_shape); + std::unique_ptr forecaster(new bvhar::BvarForecaster(mn_fit, step, response_mat, var_lag, num_sim, include_mean, seed)); + forecaster->forecastDensity(); + return forecaster->returnForecast(); +} + +//' Forecasting Bayesian VHAR +//' +//' @param object A `bvharmn` object +//' @param step Integer, Step to forecast +//' @param num_sim Integer, number to simulate parameters from posterior distribution +//' @details +//' n-step ahead forecasting using VHAR recursively. +//' +//' For given number of simulation (`num_sim`), +//' +//' 1. Generate \eqn{(\Phi^{(b)}, \Sigma_e^{(b)}) \sim MIW} (posterior) +//' 2. Recursively, \eqn{j = 1, \ldots, h} (`step`) +//' - Point forecast: Use \eqn{\hat\Phi} +//' - Predictive distribution: Again generate \eqn{\tilde{Y}_{n + j}^{(b)} \sim \Phi^{(b)}, \Sigma_e^{(b)} \sim MN} +//' - tilde notation indicates simulated ones +//' +//' @references Kim, Y. G., and Baek, C. (2024). *Bayesian vector heterogeneous autoregressive modeling*. Journal of Statistical Computation and Simulation, 94(6), 1139-1157. +//' @noRd +// [[Rcpp::export]] +Rcpp::List forecast_bvharmn(Rcpp::List object, int step, int num_sim, unsigned int seed) { + if (!object.inherits("bvharmn")) { + Rcpp::stop("'object' must be bvharmn object."); + } + Eigen::MatrixXd response_mat = object["y0"]; // Y0 + Eigen::MatrixXd posterior_mean_mat = object["coefficients"]; // Phihat = posterior mean of MN: h x m, h = 3m (+ 1) + Eigen::MatrixXd posterior_prec_mat = object["mn_prec"]; // Psihat = posterior precision of MN to compute SE: h x h + Eigen::MatrixXd posterior_mn_scale_u = posterior_prec_mat.inverse(); + Eigen::MatrixXd posterior_scale = object["covmat"]; // Sighat = posterior scale of IW: m x m + double posterior_shape = object["iw_shape"]; // posterior shape of IW + Eigen::MatrixXd HARtrans = object["HARtrans"]; // HAR transformation: h x k0, k0 = 22m (+ 1) + Eigen::MatrixXd transformed_prec_mat = HARtrans.transpose() * posterior_prec_mat.inverse() * HARtrans; // to compute SE: play a role V in BVAR + int month = object["month"]; + bool include_mean = Rcpp::as(object["type"]) == "const"; + bvhar::MinnFit mn_fit(posterior_mean_mat, posterior_prec_mat, posterior_scale, posterior_shape); + std::unique_ptr forecaster(new bvhar::BvharForecaster(mn_fit, step, response_mat, HARtrans, month, num_sim, include_mean, seed)); + forecaster->forecastDensity(); + return forecaster->returnForecast(); +} + +//' Out-of-Sample Forecasting of BVAR based on Rolling Window +//' +//' This function conducts an rolling window forecasting of BVAR with Minnesota prior. +//' +//' @param y Time series data of which columns indicate the variables +//' @param lag BVAR order +//' @param bayes_spec List, BVAR specification +//' @param include_mean Add constant term +//' @param step Integer, Step to forecast +//' @param y_test Evaluation time series data period after `y` +//' +//' @noRd +// [[Rcpp::export]] +Eigen::MatrixXd roll_bvar(Eigen::MatrixXd y, int lag, Rcpp::List bayes_spec, bool include_mean, int step, Eigen::MatrixXd y_test, Eigen::VectorXi seed_forecast, int nthreads) { + if (!bayes_spec.inherits("bvharspec")) { + Rcpp::stop("'object' must be bvharspec object."); + } + int num_window = y.rows(); + int dim = y.cols(); + int num_test = y_test.rows(); + int num_horizon = num_test - step + 1; // longest forecast horizon + Eigen::MatrixXd tot_mat(num_window + num_test, dim); + tot_mat << y, + y_test; + std::vector roll_mat(num_horizon); + std::vector roll_y0(num_horizon); + for (int i = 0; i < num_horizon; i++) { + roll_mat[i] = tot_mat.middleRows(i, num_window); + roll_y0[i] = bvhar::build_y0(roll_mat[i], lag, lag + 1); + } + tot_mat.resize(0, 0); // free the memory + std::vector> mn_objs(num_horizon); + for (int i = 0; i < num_horizon; ++i) { + bvhar::BvarSpec mn_spec(bayes_spec); + mn_objs[i] = std::unique_ptr(new bvhar::MinnBvar(roll_mat[i], lag, mn_spec, include_mean)); + } + std::vector> forecaster(num_horizon); + std::vector res(num_horizon); +#ifdef _OPENMP + #pragma omp parallel for num_threads(nthreads) +#endif + for (int window = 0; window < num_horizon; window++) { + bvhar::MinnFit mn_fit = mn_objs[window]->returnMinnFit(); + forecaster[window].reset(new bvhar::BvarForecaster(mn_fit, step, roll_y0[window], lag, 1, include_mean, static_cast(seed_forecast[window]))); + res[window] = forecaster[window]->returnPoint().bottomRows(1); + mn_objs[window].reset(); // free the memory by making nullptr + forecaster[window].reset(); // free the memory by making nullptr + } + return std::accumulate( + res.begin() + 1, res.end(), res[0], + [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { + Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); + concat_mat << acc, + curr; + return concat_mat; + } + ); +} + +//' Out-of-Sample Forecasting of BVAR based on Rolling Window +//' +//' This function conducts an rolling window forecasting of BVAR with Flat prior. +//' +//' @param y Time series data of which columns indicate the variables +//' @param lag BVAR order +//' @param bayes_spec List, BVAR specification +//' @param include_mean Add constant term +//' @param step Integer, Step to forecast +//' @param y_test Evaluation time series data period after `y` +//' +//' @noRd +// [[Rcpp::export]] +Eigen::MatrixXd roll_bvarflat(Eigen::MatrixXd y, int lag, Eigen::MatrixXd U, bool include_mean, int step, Eigen::MatrixXd y_test, Eigen::VectorXi seed_forecast, int nthreads) { + int num_window = y.rows(); + int dim = y.cols(); + int num_test = y_test.rows(); + int num_horizon = num_test - step + 1; // longest forecast horizon + Eigen::MatrixXd tot_mat(num_window + num_test, dim); + tot_mat << y, + y_test; + std::vector roll_mat(num_horizon); + std::vector roll_y0(num_horizon); + for (int i = 0; i < num_horizon; i++) { + roll_mat[i] = tot_mat.middleRows(i, num_window); + roll_y0[i] = bvhar::build_y0(roll_mat[i], lag, lag + 1); + } + tot_mat.resize(0, 0); // free the memory + std::vector> mn_objs(num_horizon); + for (int i = 0; i < num_horizon; ++i) { + Eigen::MatrixXd x = bvhar::build_x0(roll_mat[i], lag, include_mean); + mn_objs[i].reset(new bvhar::MinnFlat(x, roll_y0[i], U)); + } + std::vector> forecaster(num_horizon); + std::vector res(num_horizon); + #ifdef _OPENMP + #pragma omp parallel for num_threads(nthreads) +#endif + for (int window = 0; window < num_horizon; window++) { + bvhar::MinnFit mn_fit = mn_objs[window]->returnMinnFit(); + forecaster[window].reset(new bvhar::BvarForecaster(mn_fit, step, roll_y0[window], lag, 1, include_mean, static_cast(seed_forecast[window]))); + res[window] = forecaster[window]->returnPoint().bottomRows(1); + mn_objs[window].reset(); // free the memory by making nullptr + forecaster[window].reset(); // free the memory by making nullptr + } + return std::accumulate( + res.begin() + 1, res.end(), res[0], + [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { + Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); + concat_mat << acc, + curr; + return concat_mat; + } + ); +} + +//' Out-of-Sample Forecasting of BVHAR based on Rolling Window +//' +//' This function conducts an rolling window forecasting of BVHAR with Minnesota prior. +//' +//' @param y Time series data of which columns indicate the variables +//' @param har `r lifecycle::badge("experimental")` Numeric vector for weekly and monthly order. +//' @param bayes_spec List, BVHAR specification +//' @param include_mean Add constant term +//' @param step Integer, Step to forecast +//' @param y_test Evaluation time series data period after `y` +//' +//' @noRd +// [[Rcpp::export]] +Eigen::MatrixXd roll_bvhar(Eigen::MatrixXd y, int week, int month, Rcpp::List bayes_spec, bool include_mean, int step, Eigen::MatrixXd y_test, Eigen::VectorXi seed_forecast, int nthreads) { + if (!bayes_spec.inherits("bvharspec")) { + Rcpp::stop("'object' must be bvharspec object."); + } + int num_window = y.rows(); + int dim = y.cols(); + int num_test = y_test.rows(); + int num_horizon = num_test - step + 1; // longest forecast horizon + Eigen::MatrixXd tot_mat(num_window + num_test, dim); + tot_mat << y, + y_test; + std::vector roll_mat(num_horizon); + std::vector roll_y0(num_horizon); + Eigen::MatrixXd har_trans = bvhar::build_vhar(dim, week, month, include_mean); + for (int i = 0; i < num_horizon; i++) { + roll_mat[i] = tot_mat.middleRows(i, num_window); + roll_y0[i] = bvhar::build_y0(roll_mat[i], month, month + 1); + } + tot_mat.resize(0, 0); // free the memory + std::vector> mn_objs(num_horizon); + if (bayes_spec.containsElementNamed("delta")) { + bvhar::BvarSpec mn_spec(bayes_spec); + for (int i = 0; i < num_horizon; ++i) { + mn_objs[i].reset(new bvhar::MinnBvharS(roll_mat[i], week, month, mn_spec, include_mean)); + } + } else { + bvhar::BvharSpec mn_spec(bayes_spec); + for (int i = 0; i < num_horizon; ++i) { + bvhar::BvharSpec mn_spec(bayes_spec); + mn_objs[i].reset(new bvhar::MinnBvharL(roll_mat[i], week, month, mn_spec, include_mean)); + } + } + std::vector> forecaster(num_horizon); + std::vector res(num_horizon); +#ifdef _OPENMP + #pragma omp parallel for num_threads(nthreads) +#endif + for (int window = 0; window < num_horizon; window++) { + bvhar::MinnFit mn_fit = mn_objs[window]->returnMinnFit(); + forecaster[window].reset(new bvhar::BvharForecaster(mn_fit, step, roll_y0[window], har_trans, month, 1, include_mean, static_cast(seed_forecast[window]))); + res[window] = forecaster[window]->returnPoint().bottomRows(1); + mn_objs[window].reset(); // free the memory by making nullptr + forecaster[window].reset(); // free the memory by making nullptr + } + return std::accumulate( + res.begin() + 1, res.end(), res[0], + [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { + Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); + concat_mat << acc, + curr; + return concat_mat; + } + ); +} + +//' Out-of-Sample Forecasting of BVAR based on Expanding Window +//' +//' This function conducts an expanding window forecasting of BVAR with Minnesota prior. +//' +//' @param y Time series data of which columns indicate the variables +//' @param lag BVAR order +//' @param bayes_spec List, BVAR specification +//' @param include_mean Add constant term +//' @param step Integer, Step to forecast +//' @param y_test Evaluation time series data period after `y` +//' +//' @noRd +// [[Rcpp::export]] +Eigen::MatrixXd expand_bvar(Eigen::MatrixXd y, int lag, Rcpp::List bayes_spec, bool include_mean, int step, Eigen::MatrixXd y_test, Eigen::VectorXi seed_forecast, int nthreads) { + if (!bayes_spec.inherits("bvharspec")) { + Rcpp::stop("'object' must be bvharspec object."); + } + int num_window = y.rows(); + int dim = y.cols(); + int num_test = y_test.rows(); + int num_horizon = num_test - step + 1; + Eigen::MatrixXd tot_mat(num_window + num_test, dim); + tot_mat << y, + y_test; + std::vector expand_mat(num_horizon); + std::vector expand_y0(num_horizon); + for (int i = 0; i < num_horizon; i++) { + expand_mat[i] = tot_mat.topRows(num_window + i); + expand_y0[i] = bvhar::build_y0(expand_mat[i], lag, lag + 1); + } + tot_mat.resize(0, 0); // free the memory + std::vector> mn_objs(num_horizon); + for (int i = 0; i < num_horizon; ++i) { + bvhar::BvarSpec mn_spec(bayes_spec); + mn_objs[i].reset(new bvhar::MinnBvar(expand_mat[i], lag, mn_spec, include_mean)); + } + std::vector> forecaster(num_horizon); + std::vector res(num_horizon); +#ifdef _OPENMP + #pragma omp parallel for num_threads(nthreads) +#endif + for (int window = 0; window < num_horizon; window++) { + bvhar::MinnFit mn_fit = mn_objs[window]->returnMinnFit(); + forecaster[window].reset(new bvhar::BvarForecaster(mn_fit, step, expand_y0[window], lag, 1, include_mean, static_cast(seed_forecast[window]))); + res[window] = forecaster[window]->returnPoint().bottomRows(1); + mn_objs[window].reset(); // free the memory by making nullptr + forecaster[window].reset(); // free the memory by making nullptr + } + return std::accumulate( + res.begin() + 1, res.end(), res[0], + [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { + Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); + concat_mat << acc, + curr; + return concat_mat; + } + ); +} + +//' Out-of-Sample Forecasting of BVAR based on Expanding Window +//' +//' This function conducts an expanding window forecasting of BVAR with Flat prior. +//' +//' @param y Time series data of which columns indicate the variables +//' @param lag BVAR order +//' @param bayes_spec List, BVAR specification +//' @param include_mean Add constant term +//' @param step Integer, Step to forecast +//' @param y_test Evaluation time series data period after `y` +//' +//' @noRd +// [[Rcpp::export]] +Eigen::MatrixXd expand_bvarflat(Eigen::MatrixXd y, int lag, Eigen::MatrixXd U, bool include_mean, int step, Eigen::MatrixXd y_test, Eigen::VectorXi seed_forecast, int nthreads) { + int num_window = y.rows(); + int dim = y.cols(); + int num_test = y_test.rows(); + int num_horizon = num_test - step + 1; + Eigen::MatrixXd tot_mat(num_window + num_test, dim); + tot_mat << y, + y_test; + std::vector expand_mat(num_horizon); + std::vector expand_y0(num_horizon); + for (int i = 0; i < num_horizon; i++) { + expand_mat[i] = tot_mat.topRows(num_window + i); + expand_y0[i] = bvhar::build_y0(expand_mat[i], lag, lag + 1); + } + tot_mat.resize(0, 0); // free the memory + std::vector> mn_objs(num_horizon); + for (int i = 0; i < num_horizon; ++i) { + Eigen::MatrixXd x = bvhar::build_x0(expand_mat[i], lag, include_mean); + mn_objs[i].reset(new bvhar::MinnFlat(x, expand_y0[i], U)); + } + std::vector> forecaster(num_horizon); + std::vector res(num_horizon); +#ifdef _OPENMP + #pragma omp parallel for num_threads(nthreads) +#endif + for (int window = 0; window < num_horizon; window++) { + bvhar::MinnFit mn_fit = mn_objs[window]->returnMinnFit(); + forecaster[window].reset(new bvhar::BvarForecaster(mn_fit, step, expand_y0[window], lag, 1, include_mean, static_cast(seed_forecast[window]))); + res[window] = forecaster[window]->returnPoint().bottomRows(1); + mn_objs[window].reset(); // free the memory by making nullptr + forecaster[window].reset(); // free the memory by making nullptr + } + return std::accumulate( + res.begin() + 1, res.end(), res[0], + [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { + Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); + concat_mat << acc, + curr; + return concat_mat; + } + ); +} + +//' Out-of-Sample Forecasting of BVHAR based on Expanding Window +//' +//' This function conducts an expanding window forecasting of BVHAR with Minnesota prior. +//' +//' @param y Time series data of which columns indicate the variables +//' @param har `r lifecycle::badge("experimental")` Numeric vector for weekly and monthly order. +//' @param bayes_spec List, BVHAR specification +//' @param include_mean Add constant term +//' @param step Integer, Step to forecast +//' @param y_test Evaluation time series data period after `y` +//' +//' @noRd +// [[Rcpp::export]] +Eigen::MatrixXd expand_bvhar(Eigen::MatrixXd y, int week, int month, Rcpp::List bayes_spec, bool include_mean, int step, Eigen::MatrixXd y_test, Eigen::VectorXi seed_forecast, int nthreads) { + if (!bayes_spec.inherits("bvharspec")) { + Rcpp::stop("'object' must be bvharspec object."); + } + int num_window = y.rows(); + int dim = y.cols(); + int num_test = y_test.rows(); + int num_horizon = num_test - step + 1; + Eigen::MatrixXd tot_mat(num_window + num_test, dim); + tot_mat << y, + y_test; + std::vector expand_mat(num_horizon); + std::vector expand_y0(num_horizon); + Eigen::MatrixXd har_trans = bvhar::build_vhar(dim, week, month, include_mean); + for (int i = 0; i < num_horizon; i++) { + expand_mat[i] = tot_mat.topRows(num_window + i); + expand_y0[i] = bvhar::build_y0(expand_mat[i], month, month + 1); + } + tot_mat.resize(0, 0); // free the memory + std::vector> mn_objs(num_horizon); + if (bayes_spec.containsElementNamed("delta")) { + for (int i = 0; i < num_horizon; ++i) { + bvhar::BvarSpec mn_spec(bayes_spec); + mn_objs[i] = std::unique_ptr(new bvhar::MinnBvharS(expand_mat[i], week, month, mn_spec, include_mean)); + } + } else { + for (int i = 0; i < num_horizon; ++i) { + bvhar::BvharSpec mn_spec(bayes_spec); + mn_objs[i] = std::unique_ptr(new bvhar::MinnBvharL(expand_mat[i], week, month, mn_spec, include_mean)); + } + } + std::vector> forecaster(num_horizon); + std::vector res(num_horizon); +#ifdef _OPENMP + #pragma omp parallel for num_threads(nthreads) +#endif + for (int window = 0; window < num_horizon; window++) { + bvhar::MinnFit mn_fit = mn_objs[window]->returnMinnFit(); + forecaster[window].reset(new bvhar::BvharForecaster(mn_fit, step, expand_y0[window], har_trans, month, 1, include_mean, static_cast(seed_forecast[window]))); + res[window] = forecaster[window]->returnPoint().bottomRows(1); + mn_objs[window].reset(); // free the memory by making nullptr + forecaster[window].reset(); // free the memory by making nullptr + } + return std::accumulate( + res.begin() + 1, res.end(), res[0], + [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { + Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); + concat_mat << acc, + curr; + return concat_mat; + } + ); +} + +//' Generalized Spillover of Minnesota prior +//' +//' @param object varlse or vharlse object. +//' @param step Step to forecast. +//' @param num_iter Number to sample MNIW distribution +//' @param num_burn Number of burn-in +//' @param thin Thinning +//' @param seed Random seed for boost library +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::List compute_mn_spillover(Rcpp::List object, int step, int num_iter, int num_burn, int thin, unsigned int seed) { + if (!(object.inherits("bvarmn") || object.inherits("bvharmn"))) { + Rcpp::stop("'object' must be bvarmn or bvharmn object."); + } + std::unique_ptr spillover; + if (object.inherits("bvharmn")) { + bvhar::MinnFit fit(Rcpp::as(object["coefficients"]), Rcpp::as(object["mn_prec"]), Rcpp::as(object["covmat"]), object["iw_shape"]); + spillover.reset(new bvhar::BvharSpillover(fit, step, num_iter, num_burn, thin, object["month"], Rcpp::as(object["HARtrans"]), seed)); + } else { + bvhar::MinnFit fit(Rcpp::as(object["coefficients"]), Rcpp::as(object["mn_prec"]), Rcpp::as(object["covmat"]), object["iw_shape"]); + spillover.reset(new bvhar::MinnSpillover(fit, step, num_iter, num_burn, thin, object["p"], seed)); + } + spillover->updateMniw(); + spillover->computeSpillover(); + Eigen::VectorXd to_sp = spillover->returnTo(); + Eigen::VectorXd from_sp = spillover->returnFrom(); + return Rcpp::List::create( + Rcpp::Named("connect") = spillover->returnSpillover(), + Rcpp::Named("to") = to_sp, + Rcpp::Named("from") = from_sp, + Rcpp::Named("tot") = spillover->returnTot(), + Rcpp::Named("net") = to_sp - from_sp, + Rcpp::Named("net_pairwise") = spillover->returnNet() + ); +} + +// Rcpp::List compute_bvarmn_spillover(int lag, int step, Eigen::MatrixXd alpha_record, Eigen::MatrixXd sig_record) { +// // if (!(object.inherits("bvarmn") || object.inherits("bvharmn"))) { +// // Rcpp::stop("'object' must be bvarmn or bvharmn object."); +// // } +// bvhar::MinnRecords mn_record(alpha_record, sig_record); +// std::unique_ptr spillover(new bvhar::MinnSpillover(mn_record, step, lag)); +// // std::unique_ptr spillover; +// // if (object.inherits("bvharmn")) { +// // bvhar::MinnFit fit(Rcpp::as(object["coefficients"]), Rcpp::as(object["mn_prec"]), Rcpp::as(object["iw_scale"]), object["iw_shape"]); +// // spillover.reset(new bvhar::BvharSpillover(fit, step, num_iter, num_burn, thin, object["month"], Rcpp::as(object["HARtrans"]), seed)); +// // } else { +// // bvhar::MinnFit fit(Rcpp::as(object["coefficients"]), Rcpp::as(object["mn_prec"]), Rcpp::as(object["iw_scale"]), object["iw_shape"]); +// // spillover.reset(new bvhar::MinnSpillover(fit, step, num_iter, num_burn, thin, object["p"], seed)); +// // } +// // spillover->updateMniw(); +// spillover->computeSpillover(); +// Eigen::VectorXd to_sp = spillover->returnTo(); +// Eigen::VectorXd from_sp = spillover->returnFrom(); +// return Rcpp::List::create( +// Rcpp::Named("connect") = spillover->returnSpillover(), +// Rcpp::Named("to") = to_sp, +// Rcpp::Named("from") = from_sp, +// Rcpp::Named("tot") = spillover->returnTot(), +// Rcpp::Named("net") = to_sp - from_sp, +// Rcpp::Named("net_pairwise") = spillover->returnNet() +// ); +// } + +// Rcpp::List compute_bvharmn_spillover(int month, int step, Eigen::MatrixXd har_trans, Eigen::MatrixXd phi_record, Eigen::MatrixXd sig_record) { +// // if (!(object.inherits("bvarmn") || object.inherits("bvharmn"))) { +// // Rcpp::stop("'object' must be bvarmn or bvharmn object."); +// // } +// bvhar::MinnRecords mn_record(phi_record, sig_record); +// std::unique_ptr spillover(new bvhar::BvharSpillover(mn_record, step, month, har_trans)); +// // std::unique_ptr spillover; +// // if (object.inherits("bvharmn")) { +// // bvhar::MinnFit fit(Rcpp::as(object["coefficients"]), Rcpp::as(object["mn_prec"]), Rcpp::as(object["iw_scale"]), object["iw_shape"]); +// // spillover.reset(new bvhar::BvharSpillover(fit, step, num_iter, num_burn, thin, object["month"], Rcpp::as(object["HARtrans"]), seed)); +// // } else { +// // bvhar::MinnFit fit(Rcpp::as(object["coefficients"]), Rcpp::as(object["mn_prec"]), Rcpp::as(object["iw_scale"]), object["iw_shape"]); +// // spillover.reset(new bvhar::MinnSpillover(fit, step, num_iter, num_burn, thin, object["p"], seed)); +// // } +// // spillover->updateMniw(); +// spillover->computeSpillover(); +// Eigen::VectorXd to_sp = spillover->returnTo(); +// Eigen::VectorXd from_sp = spillover->returnFrom(); +// return Rcpp::List::create( +// Rcpp::Named("connect") = spillover->returnSpillover(), +// Rcpp::Named("to") = to_sp, +// Rcpp::Named("from") = from_sp, +// Rcpp::Named("tot") = spillover->returnTot(), +// Rcpp::Named("net") = to_sp - from_sp, +// Rcpp::Named("net_pairwise") = spillover->returnNet() +// ); +// } + +//' Rolling-sample Total Spillover Index of BVAR +//' +//' @param y Time series data of which columns indicate the variables +//' @param window Rolling window size +//' @param step forecast horizon for FEVD +//' @param num_iter Number to sample MNIW distribution +//' @param num_burn Number of burn-in +//' @param thin Thinning +//' @param lag BVAR order +//' @param bayes_spec BVAR specification +//' @param include_mean Add constant term +//' @param seed_chain Random seed for each window +//' @param nthreads Number of threads for openmp +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::List dynamic_bvar_spillover(Eigen::MatrixXd y, int window, int step, int num_iter, int num_burn, int thin, + int lag, Rcpp::List bayes_spec, bool include_mean, Eigen::VectorXi seed_chain, int nthreads) { +// Rcpp::List dynamic_bvar_spillover(Eigen::MatrixXd y, int window, int step, int num_chains, int num_iter, int num_burn, int thin, +// int lag, Rcpp::List bayes_spec, bool include_mean, Eigen::MatrixXi seed_chain, int nthreads) { + int num_horizon = y.rows() - window + 1; // number of windows = T - win + 1 + if (num_horizon <= 0) { + Rcpp::stop("Window size is too large."); + } + // std::vector>> mn_objs(num_horizon); + // for (auto &mn_chain : mn_objs) { + // mn_chain.resize(num_chains); + // for (auto &ptr : mn_chain) { + // ptr = nullptr; + // } + // } + std::vector> mn_objs(num_horizon); + int dim = y.cols(); + for (int i = 0; i < num_horizon; ++i) { + Eigen::MatrixXd roll_mat = y.middleRows(i, window); + // Eigen::MatrixXd roll_y0 = bvhar::build_y0(roll_mat, lag, lag + 1); + // Eigen::MatrixXd roll_x0 = bvhar::build_x0(roll_mat, lag, include_mean); + bvhar::BvarSpec mn_spec(bayes_spec); + mn_objs[i] = std::unique_ptr(new bvhar::MinnBvar(roll_mat, lag, mn_spec, include_mean)); + // Eigen::MatrixXd y_dummy = bvhar::build_ydummy( + // lag, mn_spec._sigma, mn_spec._lambda, + // mn_spec._delta, Eigen::VectorXd::Zero(dim), Eigen::VectorXd::Zero(dim), + // include_mean + // ); + // Eigen::MatrixXd x_dummy = bvhar::build_xdummy( + // Eigen::VectorXd::LinSpaced(lag, 1, lag), + // mn_spec._lambda, mn_spec._sigma, mn_spec._eps, include_mean + // ); + // for (int j = 0; j < num_chains; ++j) { + // mn_objs[i][j].reset(new bvhar::Minnesota(num_iter, roll_x0, roll_y0, x_dummy, y_dummy, static_cast(seed_chain(i, j)))); + // mn_objs[i][j]->computePosterior(); + // } + } + // std::vector> mn_recs(num_horizon); + // for (auto &rec_chain : mn_recs) { + // rec_chain.resize(num_chains); + // for (auto &ptr : rec_chain) { + // ptr = nullptr; + // } + // } + // std::vector mn_rec(num_chains); + std::vector> spillover(num_horizon); + // std::vector mn_recs(num_chains); + // std::vector coef_record(num_chains); + // std::vector sig_record(num_chains); + Eigen::VectorXd tot(num_horizon); + Eigen::MatrixXd to_sp(num_horizon, dim); + Eigen::MatrixXd from_sp(num_horizon, dim); +#ifdef _OPENMP + #pragma omp parallel for num_threads(nthreads) +#endif + for (int i = 0; i < num_horizon; ++i) { + // for (int win = 0; win < num_horizon; ++win) { + bvhar::MinnFit mn_fit = mn_objs[i]->returnMinnFit(); + spillover[i].reset(new bvhar::MinnSpillover(mn_fit, step, num_iter, num_burn, thin, lag, static_cast(seed_chain[i]))); + spillover[i]->updateMniw(); + spillover[i]->computeSpillover(); + to_sp.row(i) = spillover[i]->returnTo(); + from_sp.row(i) = spillover[i]->returnFrom(); + tot[i] = spillover[i]->returnTot(); + mn_objs[i].reset(); // free the memory by making nullptr + spillover[i].reset(); // free the memory by making nullptr + + // std::vector coef_record(num_chains); + // std::vector sig_record(num_chains); + // for (int chain = 0; chain < num_chains; ++chain) { + // for (int i = 0; i < num_iter; ++i) { + // mn_objs[win][chain]->doPosteriorDraws(); + // } + // // mn_recs[i][j] = mn_objs[i][j]->returnMinnRecords(num_burn, thin); + // bvhar::MinnRecords mn_rec = mn_objs[win][chain]->returnMinnRecords(num_burn, thin); + // // mn_recs[j] = mn_objs[i][j]->returnMinnRecords(num_burn, thin); + // coef_record[chain] = mn_rec.coef_record; + // sig_record[chain] = mn_rec.sig_record; + // mn_objs[win][chain].reset(); + // } + // Eigen::MatrixXd coef = std::accumulate( + // coef_record.begin() + 1, coef_record.end(), coef_record[0], + // [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { + // Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); + // concat_mat << acc, + // curr; + // return concat_mat; + // } + // ); + // Eigen::MatrixXd sig = std::accumulate( + // sig_record.begin() + 1, sig_record.end(), sig_record[0], + // [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { + // Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); + // concat_mat << acc, + // curr; + // return concat_mat; + // } + // ); + // bvhar::MinnRecords mn_record(coef, sig); + // spillover[win].reset(new bvhar::MinnSpillover(mn_record, step, lag)); + // // bvhar::MinnFit mn_fit = mn_objs[win]->returnMinnFit(); + // // spillover[win].reset(new bvhar::MinnSpillover(mn_fit, step, num_iter, num_burn, thin, lag, static_cast(seed_chain[win]))); + // // spillover[win]->updateMniw(); + // spillover[win]->computeSpillover(); + // to_sp.row(win) = spillover[win]->returnTo(); + // from_sp.row(win) = spillover[win]->returnFrom(); + // tot[win] = spillover[win]->returnTot(); + // // mn_objs[win].reset(); // free the memory by making nullptr + // spillover[win].reset(); // free the memory by making nullptr + } + return Rcpp::List::create( + Rcpp::Named("to") = to_sp, + Rcpp::Named("from") = from_sp, + Rcpp::Named("tot") = tot, + Rcpp::Named("net") = to_sp - from_sp + ); +} + +//' Rolling-sample Total Spillover Index of BVHAR +//' +//' @param y Time series data of which columns indicate the variables +//' @param window Rolling window size +//' @param step forecast horizon for FEVD +//' @param num_iter Number to sample MNIW distribution +//' @param num_burn Number of burn-in +//' @param thin Thinning +//' @param week Week order +//' @param month Month order +//' @param bayes_spec BVHAR specification +//' @param include_mean Add constant term +//' @param seed_chain Random seed for each window +//' @param nthreads Number of threads for openmp +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::List dynamic_bvhar_spillover(Eigen::MatrixXd y, int window, int step, int num_iter, int num_burn, int thin, + int week, int month, Rcpp::List bayes_spec, bool include_mean, Eigen::VectorXi seed_chain, int nthreads) { +// Rcpp::List dynamic_bvhar_spillover(Eigen::MatrixXd y, int window, int step, int num_chains, int num_iter, int num_burn, int thin, +// int week, int month, Rcpp::List bayes_spec, bool include_mean, Eigen::MatrixXi seed_chain, int nthreads) { + int num_horizon = y.rows() - window + 1; // number of windows = T - win + 1 + if (num_horizon <= 0) { + Rcpp::stop("Window size is too large."); + } + int dim = y.cols(); + Eigen::MatrixXd har_trans = bvhar::build_vhar(dim, week, month, include_mean); + // std::vector>> mn_objs(num_horizon); + // for (auto &mn_chain : mn_objs) { + // mn_chain.resize(num_chains); + // for (auto &ptr : mn_chain) { + // ptr = nullptr; + // } + // } + // for (int i = 0; i < num_horizon; ++i) { + // Eigen::MatrixXd roll_mat = y.middleRows(i, window); + // Eigen::MatrixXd roll_y0 = bvhar::build_y0(roll_mat, month, month + 1); + // Eigen::MatrixXd roll_x0 = bvhar::build_x0(roll_mat, month, include_mean) * har_trans.transpose(); + // bvhar::BvarSpec mn_spec(bayes_spec); + // Eigen::MatrixXd y_dummy = bvhar::build_ydummy( + // lag, mn_spec._sigma, mn_spec._lambda, + // mn_spec._delta, Eigen::VectorXd::Zero(dim), Eigen::VectorXd::Zero(dim), + // include_mean + // ); + // Eigen::MatrixXd x_dummy = bvhar::build_xdummy( + // Eigen::VectorXd::LinSpaced(lag, 1, lag), + // mn_spec._lambda, mn_spec._sigma, mn_spec._eps, include_mean + // ); + // if (bayes_spec.containsElementNamed("delta")) { + // // bvhar::BvarSpec bvhar_spec(bayes_spec); + // bvhar::BvarSpec mn_spec(bayes_spec); + // Eigen::MatrixXd y_dummy = bvhar::build_ydummy( + // 3, mn_spec._sigma, mn_spec._lambda, + // mn_spec._delta, Eigen::VectorXd::Zero(dim), Eigen::VectorXd::Zero(dim), + // include_mean + // ); + // Eigen::MatrixXd x_dummy = bvhar::build_xdummy( + // Eigen::VectorXd::LinSpaced(3, 1, 3), + // mn_spec._lambda, mn_spec._sigma, mn_spec._eps, include_mean + // ); + // // mn_objs[i] = std::unique_ptr(new bvhar::MinnBvharS(roll_mat, week, month, bvhar_spec, include_mean)); + // for (int j = 0; j < num_chains; ++j) { + // mn_objs[i][j].reset(new bvhar::Minnesota(num_iter, roll_x0, roll_y0, x_dummy, y_dummy, static_cast(seed_chain(i, j)))); + // mn_objs[i][j]->computePosterior(); + // } + // } else { + // // bvhar::BvharSpec bvhar_spec(bayes_spec); + // bvhar::BvharSpec mn_spec(bayes_spec); + // Eigen::MatrixXd y_dummy = bvhar::build_ydummy( + // 3, mn_spec._sigma, mn_spec._lambda, + // mn_spec._daily, mn_spec._weekly, mn_spec._monthly, + // include_mean + // ); + // Eigen::MatrixXd x_dummy = bvhar::build_xdummy( + // Eigen::VectorXd::LinSpaced(3, 1, 3), + // mn_spec._lambda, mn_spec._sigma, mn_spec._eps, include_mean + // ); + // // mn_objs[i] = std::unique_ptr(new bvhar::MinnBvharL(roll_mat, week, month, bvhar_spec, include_mean)); + // for (int j = 0; j < num_chains; ++j) { + // mn_objs[i][j].reset(new bvhar::Minnesota(num_iter, roll_x0, roll_y0, x_dummy, y_dummy, static_cast(seed_chain(i, j)))); + // mn_objs[i][j]->computePosterior(); + // } + // } + // } + std::vector> mn_objs(num_horizon); + for (int i = 0; i < num_horizon; ++i) { + Eigen::MatrixXd roll_mat = y.middleRows(i, window); + if (bayes_spec.containsElementNamed("delta")) { + bvhar::BvarSpec bvhar_spec(bayes_spec); + mn_objs[i] = std::unique_ptr(new bvhar::MinnBvharS(roll_mat, week, month, bvhar_spec, include_mean)); + } else { + bvhar::BvharSpec bvhar_spec(bayes_spec); + mn_objs[i] = std::unique_ptr(new bvhar::MinnBvharL(roll_mat, week, month, bvhar_spec, include_mean)); + } + } + std::vector> spillover(num_horizon); + // std::vector mn_recs(num_chains); + // std::vector coef_record(num_chains); + // std::vector sig_record(num_chains); + Eigen::VectorXd tot(num_horizon); + Eigen::MatrixXd to_sp(num_horizon, y.cols()); + Eigen::MatrixXd from_sp(num_horizon, y.cols()); +#ifdef _OPENMP + #pragma omp parallel for num_threads(nthreads) +#endif + for (int i = 0; i < num_horizon; ++i) { + // for (int win = 0; win < num_horizon; ++win) { + // std::vector coef_record(num_chains); + // std::vector sig_record(num_chains); + // for (int chain = 0; chain < num_chains; ++chain) { + // for (int i = 0; i < num_iter; ++i) { + // mn_objs[win][chain]->doPosteriorDraws(); + // } + // bvhar::MinnRecords mn_rec = mn_objs[win][chain]->returnMinnRecords(num_burn, thin); + // coef_record[chain] = mn_rec.coef_record; + // sig_record[chain] = mn_rec.sig_record; + // mn_objs[win][chain].reset(); + // } + // Eigen::MatrixXd coef = std::accumulate( + // coef_record.begin() + 1, coef_record.end(), coef_record[0], + // [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { + // Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); + // concat_mat << acc, + // curr; + // return concat_mat; + // } + // ); + // Eigen::MatrixXd sig = std::accumulate( + // sig_record.begin() + 1, sig_record.end(), sig_record[0], + // [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { + // Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); + // concat_mat << acc, + // curr; + // return concat_mat; + // } + // ); + // bvhar::MinnRecords mn_record(coef, sig); + bvhar::MinnFit mn_fit = mn_objs[i]->returnMinnFit(); + // spillover[i].reset(new bvhar::BvharSpillover(mn_fit, step, num_iter, num_burn, thin, month, har_trans, static_cast(seed_chain[win]))); + spillover[i].reset(new bvhar::BvharSpillover(mn_fit, step, num_iter, num_burn, thin, month, har_trans, static_cast(seed_chain[i]))); + spillover[i]->updateMniw(); + // spillover[win].reset(new bvhar::BvharSpillover(mn_record, step, month, har_trans)); + spillover[i]->computeSpillover(); + to_sp.row(i) = spillover[i]->returnTo(); + from_sp.row(i) = spillover[i]->returnFrom(); + tot[i] = spillover[i]->returnTot(); + mn_objs[i].reset(); // free the memory by making nullptr + spillover[i].reset(); // free the memory by making nullptr + } + return Rcpp::List::create( + Rcpp::Named("to") = to_sp, + Rcpp::Named("from") = from_sp, + Rcpp::Named("tot") = tot, + Rcpp::Named("net") = to_sp - from_sp + ); +} diff --git a/src/ols.cpp b/src/ols.cpp new file mode 100644 index 00000000..3128d1cc --- /dev/null +++ b/src/ols.cpp @@ -0,0 +1,630 @@ +#include + +//' Compute VAR(p) Coefficient Matrices and Fitted Values +//' +//' This function fits VAR(p) given response and design matrices of multivariate time series. +//' +//' @param x Design matrix X0 +//' @param y Response matrix Y0 +//' @param method Method to solve linear equation system. 1: normal equation, 2: cholesky, 3: HouseholderQR. +//' @details +//' Given Y0 and Y0, the function estimate least squares +//' Y0 = X0 A + Z +//' +//' @references Lütkepohl, H. (2007). *New Introduction to Multiple Time Series Analysis*. Springer Publishing. doi:[10.1007/978-3-540-27752-1](https://doi.org/10.1007/978-3-540-27752-1) +//' @noRd +// [[Rcpp::export]] +Rcpp::List estimate_var(Eigen::MatrixXd y, int lag, bool include_mean, int method) { + std::unique_ptr ols_obj(new bvhar::OlsVar(y, lag, include_mean, method)); + return ols_obj->returnOlsRes(); +} + +//' Compute Vector HAR Coefficient Matrices and Fitted Values +//' +//' This function fits VHAR given response and design matrices of multivariate time series. +//' +//' @param x Design matrix X0 +//' @param y Response matrix Y0 +//' @param week Integer, order for weekly term +//' @param month Integer, order for monthly term +//' @param include_mean bool, Add constant term (Default: `true`) or not (`false`) +//' @param method Method to solve linear equation system. 1: normal equation, 2: cholesky, 3: HouseholderQR. +//' @details +//' Given Y0 and Y0, the function estimate least squares +//' \deqn{Y_0 = X_1 \Phi + Z} +//' +//' @references +//' Baek, C. and Park, M. (2021). *Sparse vector heterogeneous autoregressive modeling for realized volatility*. J. Korean Stat. Soc. 50, 495-510. doi:[10.1007/s42952-020-00090-5](https://doi.org/10.1007/s42952-020-00090-5) +//' +//' Corsi, F. (2008). *A Simple Approximate Long-Memory Model of Realized Volatility*. Journal of Financial Econometrics, 7(2), 174-196. doi:[10.1093/jjfinec/nbp001](https://doi.org/10.1093/jjfinec/nbp001) +//' @noRd +// [[Rcpp::export]] +Rcpp::List estimate_har(Eigen::MatrixXd y, int week, int month, bool include_mean, int method) { + std::unique_ptr ols_obj(new bvhar::OlsVhar(y, week, month, include_mean, method)); + return ols_obj->returnOlsRes(); +} + +//' Covariance Estimate for Residual Covariance Matrix +//' +//' Compute ubiased estimator for residual covariance. +//' +//' @param z Matrix, residual +//' @param num_design Integer, Number of sample used (s = n - p) +//' @param dim_design Ingeger, Number of parameter for each dimension (k = mp + 1) +//' @details +//' See pp75 Lütkepohl (2007). +//' +//' * s = n - p: sample used (`num_design`) +//' * k = mp + 1 (m: dimension, p: VAR lag): number of parameter for each dimension (`dim_design`) +//' +//' Then an unbiased estimator for \eqn{\Sigma_e} is +//' +//' \deqn{\hat{\Sigma}_e = \frac{1}{s - k} (Y_0 - \hat{A} X_0)^T (Y_0 - \hat{A} X_0)} +//' +//' @references Lütkepohl, H. (2007). *New Introduction to Multiple Time Series Analysis*. Springer Publishing. +//' @noRd +// [[Rcpp::export]] +Eigen::MatrixXd compute_cov(Eigen::MatrixXd z, int num_design, int dim_design) { + Eigen::MatrixXd cov_mat(z.cols(), z.cols()); + cov_mat = z.transpose() * z / (num_design - dim_design); + return cov_mat; +} + +//' Statistic for VAR +//' +//' Compute partial t-statistics for inference in VAR model. +//' +//' @param object A `varlse` object +//' @details +//' Partial t-statistic for H0: aij = 0 +//' +//' * For each variable (e.g. 1st variable) +//' * Standard error = (1st) diagonal element of \eqn{\Sigma_e} estimator x diagonal elements of \eqn{(X_0^T X_0)^(-1)} +//' +//' @references Lütkepohl, H. (2007). *New Introduction to Multiple Time Series Analysis*. Springer Publishing. doi:[10.1007/978-3-540-27752-1](https://doi.org/10.1007/978-3-540-27752-1) +//' @noRd +// [[Rcpp::export]] +Rcpp::List infer_var(Rcpp::List object) { + if (!object.inherits("varlse")) { + Rcpp::stop("'object' must be varlse object."); + } + int dim = object["m"]; // dimension of time series + Eigen::MatrixXd cov_mat = object["covmat"]; // sigma + Eigen::MatrixXd coef_mat = object["coefficients"]; // Ahat(mp, m) = [A1^T, A2^T, ..., Ap^T, c^T]^T + Eigen::MatrixXd design_mat = object["design"]; // X0: n x mp + int num_design = object["obs"]; + int dim_design = coef_mat.rows(); // mp(+1) + int df = num_design - dim_design; + Eigen::VectorXd XtX = (design_mat.transpose() * design_mat).inverse().diagonal(); // diagonal element of (XtX)^(-1) + Eigen::MatrixXd res(dim_design * dim, 3); // stack estimate, std, and t stat + Eigen::ArrayXd st_err(dim_design); // save standard error in for loop + for (int i = 0; i < dim; i++) { + res.block(i * dim_design, 0, dim_design, 1) = coef_mat.col(i); + for (int j = 0; j < dim_design; j++) { + st_err[j] = sqrt(XtX[j] * cov_mat(i, i)); // variable-covariance matrix element + } + res.block(i * dim_design, 1, dim_design, 1) = st_err; + res.block(i * dim_design, 2, dim_design, 1) = coef_mat.col(i).array() / st_err; + } + return Rcpp::List::create( + Rcpp::Named("df") = df, + Rcpp::Named("summary_stat") = res + ); +} + +//' Statistic for VHAR +//' +//' Compute partial t-statistics for inference in VHAR model. +//' +//' @param object A `vharlse` object +//' @details +//' Partial t-statistic for H0: \eqn{\phi_{ij} = 0} +//' +//' * For each variable (e.g. 1st variable) +//' * Standard error = (1st) diagonal element of \eqn{\Sigma_e} estimator x diagonal elements of \eqn{(X_1^T X_1)^(-1)} +//' @noRd +// [[Rcpp::export]] +Rcpp::List infer_vhar(Rcpp::List object) { + if (!object.inherits("vharlse")) { + Rcpp::stop("'object' must be vharlse object."); + } + int dim = object["m"]; // dimension of time series + Eigen::MatrixXd cov_mat = object["covmat"]; // sigma + Eigen::MatrixXd coef_mat = object["coefficients"]; // Phihat(mp, m) = [Phi(daily), Phi(weekly), Phi(monthly), c^T]^T + Eigen::MatrixXd design_mat = object["design"]; // X0: n x mp + Eigen::MatrixXd HARtrans = object["HARtrans"]; // HAR transformation + Eigen::MatrixXd vhar_design = design_mat * HARtrans.transpose(); // X1 = X0 * C0^T + int num_design = object["obs"]; + int num_har = coef_mat.rows(); // 3m(+1) + int df = num_design - num_har; + Eigen::VectorXd XtX = (vhar_design.transpose() * vhar_design).inverse().diagonal(); // diagonal element of (XtX)^(-1) + Eigen::MatrixXd res(num_har * dim, 3); // stack estimate, std, and t stat + Eigen::ArrayXd st_err(num_har); // save standard error in for loop + for (int i = 0; i < dim; i++) { + res.block(i * num_har, 0, num_har, 1) = coef_mat.col(i); + for (int j = 0; j < num_har; j++) { + st_err[j] = sqrt(XtX[j] * cov_mat(i, i)); // variable-covariance matrix element + } + res.block(i * num_har, 1, num_har, 1) = st_err; + res.block(i * num_har, 2, num_har, 1) = coef_mat.col(i).array() / st_err; + } + return Rcpp::List::create( + Rcpp::Named("df") = df, + Rcpp::Named("summary_stat") = res + ); +} + +//' Forecasting Vector Autoregression +//' +//' @param object A `varlse` object +//' @param step Integer, Step to forecast +//' @details +//' n-step ahead forecasting using VAR(p) recursively, based on pp35 of Lütkepohl (2007). +//' +//' @references Lütkepohl, H. (2007). *New Introduction to Multiple Time Series Analysis*. Springer Publishing. [https://doi.org/10.1007/978-3-540-27752-1](https://doi.org/10.1007/978-3-540-27752-1) +//' @noRd +// [[Rcpp::export]] +Eigen::MatrixXd forecast_var(Rcpp::List object, int step) { + if (! object.inherits("varlse")) { + Rcpp::stop("'object' must be varlse object."); + } + Eigen::MatrixXd response_mat = object["y0"]; // Y0 + Eigen::MatrixXd coef_mat = object["coefficients"]; // bhat + int var_lag = object["p"]; // VAR(p) + bool include_mean = Rcpp::as(object["type"]) == "const"; + bvhar::OlsFit ols_fit(coef_mat, var_lag); + std::unique_ptr forecaster(new bvhar::VarForecaster(ols_fit, step, response_mat, include_mean)); + return forecaster->forecastPoint(); +} + +//' Forecasting Vector HAR +//' +//' @param object A `vharlse` object +//' @param step Integer, Step to forecast +//' @details +//' n-step ahead forecasting using VHAR recursively. +//' +//' @noRd +// [[Rcpp::export]] +Eigen::MatrixXd forecast_vhar(Rcpp::List object, int step) { + if (!object.inherits("vharlse")) { + Rcpp::stop("'object' must be vharlse object."); + } + Eigen::MatrixXd response_mat = object["y0"]; // Y0 + Eigen::MatrixXd coef_mat = object["coefficients"]; // bhat + Eigen::MatrixXd HARtrans = object["HARtrans"]; // HAR transformation + int month = object["month"]; + bool include_mean = Rcpp::as(object["type"]) == "const"; + bvhar::OlsFit ols_fit(coef_mat, month); + std::unique_ptr forecaster(new bvhar::VharForecaster(ols_fit, step, response_mat, HARtrans, include_mean)); + return forecaster->forecastPoint(); +} + +//' Out-of-Sample Forecasting of VAR based on Rolling Window +//' +//' This function conducts an rolling window forecasting of VAR. +//' +//' @param y Time series data of which columns indicate the variables +//' @param lag VAR order +//' @param include_mean Add constant term +//' @param step Integer, Step to forecast +//' @param y_test Evaluation time series data period after `y` +//' @param method Method to solve linear equation system. 1: normal equation, 2: cholesky, 3: HouseholderQR. +//' @param nthreads Number of threads for openmp +//' +//' @noRd +// [[Rcpp::export]] +Eigen::MatrixXd roll_var(Eigen::MatrixXd y, int lag, bool include_mean, int step, Eigen::MatrixXd y_test, int method, int nthreads) { +#ifdef _OPENMP + Eigen::setNbThreads(nthreads); +#endif + int num_window = y.rows(); + int dim = y.cols(); + int num_test = y_test.rows(); + int num_horizon = num_test - step + 1; // longest forecast horizon + Eigen::MatrixXd tot_mat(num_window + num_test, dim); + tot_mat << y, + y_test; + std::vector roll_mat(num_horizon); + std::vector roll_y0(num_horizon); + for (int i = 0; i < num_horizon; i++) { + roll_mat[i] = tot_mat.middleRows(i, num_window); + roll_y0[i] = bvhar::build_y0(roll_mat[i], lag, lag + 1); + } + std::vector> ols_objs(num_horizon); + switch(method) { + case 1: { + for (int i = 0; i < num_horizon; ++i) { + Eigen::MatrixXd design = bvhar::build_x0(roll_mat[i], lag, include_mean); + ols_objs[i] = std::unique_ptr(new bvhar::MultiOls(design, roll_y0[i])); + } + } + case 2: { + for (int i = 0; i < num_horizon; ++i) { + Eigen::MatrixXd design = bvhar::build_x0(roll_mat[i], lag, include_mean); + ols_objs[i] = std::unique_ptr(new bvhar::LltOls(design, roll_y0[i])); + } + } + case 3: { + for (int i = 0; i < num_horizon; ++i) { + Eigen::MatrixXd design = bvhar::build_x0(roll_mat[i], lag, include_mean); + ols_objs[i] = std::unique_ptr(new bvhar::QrOls(design, roll_y0[i])); + } + } + } + std::vector> forecaster(num_horizon); + std::vector res(num_horizon); +#ifdef _OPENMP + #pragma omp parallel for num_threads(nthreads) +#endif + for (int window = 0; window < num_horizon; window++) { + bvhar::OlsFit ols_fit = ols_objs[window]->returnOlsFit(lag); + forecaster[window].reset(new bvhar::VarForecaster(ols_fit, step, roll_y0[window], include_mean)); + res[window] = forecaster[window]->forecastPoint().bottomRows(1); + ols_objs[window].reset(); // free the memory by making nullptr + forecaster[window].reset(); // free the memory by making nullptr + } + return std::accumulate( + res.begin() + 1, res.end(), res[0], + [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { + Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); + concat_mat << acc, + curr; + return concat_mat; + } + ); +} + +//' Out-of-Sample Forecasting of VHAR based on Rolling Window +//' +//' This function conducts an rolling window forecasting of VHAR. +//' +//' @param y Time series data of which columns indicate the variables +//' @param week Integer, order for weekly term +//' @param month Integer, order for monthly term +//' @param include_mean Add constant term +//' @param step Integer, Step to forecast +//' @param y_test Evaluation time series data period after `y` +//' @param method Method to solve linear equation system. 1: normal equation, 2: cholesky, 3: HouseholderQR. +//' @param nthreads Number of threads for openmp +//' +//' @noRd +// [[Rcpp::export]] +Eigen::MatrixXd roll_vhar(Eigen::MatrixXd y, int week, int month, bool include_mean, int step, Eigen::MatrixXd y_test, int method, int nthreads) { +#ifdef _OPENMP + Eigen::setNbThreads(nthreads); +#endif + int num_window = y.rows(); + int dim = y.cols(); + int num_test = y_test.rows(); + int num_horizon = num_test - step + 1; // longest forecast horizon + Eigen::MatrixXd tot_mat(num_window + num_test, dim); + tot_mat << y, + y_test; + std::vector roll_mat(num_horizon); + std::vector roll_y0(num_horizon); + Eigen::MatrixXd har_trans = bvhar::build_vhar(dim, week, month, include_mean); + for (int i = 0; i < num_horizon; i++) { + roll_mat[i] = tot_mat.middleRows(i, num_window); + roll_y0[i] = bvhar::build_y0(roll_mat[i], month, month + 1); + } + std::vector> ols_objs(num_horizon); + switch(method) { + case 1: { + for (int i = 0; i < num_horizon; ++i) { + Eigen::MatrixXd design = bvhar::build_x0(roll_mat[i], month, include_mean) * har_trans.transpose(); + ols_objs[i] = std::unique_ptr(new bvhar::MultiOls(design, roll_y0[i])); + } + } + case 2: { + for (int i = 0; i < num_horizon; ++i) { + Eigen::MatrixXd design = bvhar::build_x0(roll_mat[i], month, include_mean) * har_trans.transpose(); + ols_objs[i] = std::unique_ptr(new bvhar::LltOls(design, roll_y0[i])); + } + } + case 3: { + for (int i = 0; i < num_horizon; ++i) { + Eigen::MatrixXd design = bvhar::build_x0(roll_mat[i], month, include_mean) * har_trans.transpose(); + ols_objs[i] = std::unique_ptr(new bvhar::QrOls(design, roll_y0[i])); + } + } + } + std::vector> forecaster(num_horizon); + std::vector res(num_horizon); +#ifdef _OPENMP + #pragma omp parallel for num_threads(nthreads) +#endif + for (int window = 0; window < num_horizon; window++) { + bvhar::OlsFit ols_fit = ols_objs[window]->returnOlsFit(month); + forecaster[window].reset(new bvhar::VharForecaster(ols_fit, step, roll_y0[window], har_trans, include_mean)); + res[window] = forecaster[window]->forecastPoint().bottomRows(1); + ols_objs[window].reset(); // free the memory by making nullptr + forecaster[window].reset(); // free the memory by making nullptr + } + return std::accumulate( + res.begin() + 1, res.end(), res[0], + [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { + Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); + concat_mat << acc, + curr; + return concat_mat; + } + ); +} + +//' Out-of-Sample Forecasting of VAR based on Expanding Window +//' +//' This function conducts an expanding window forecasting of VAR. +//' +//' @param y Time series data of which columns indicate the variables +//' @param lag VAR order +//' @param include_mean Add constant term +//' @param step Integer, Step to forecast +//' @param y_test Evaluation time series data period after `y` +//' @param method Method to solve linear equation system. 1: normal equation, 2: cholesky, 3: HouseholderQR. +//' @param nthreads Number of threads for openmp +//' +//' @noRd +// [[Rcpp::export]] +Eigen::MatrixXd expand_var(Eigen::MatrixXd y, int lag, bool include_mean, int step, Eigen::MatrixXd y_test, int method, int nthreads) { +#ifdef _OPENMP + Eigen::setNbThreads(nthreads); +#endif + int num_window = y.rows(); + int dim = y.cols(); + int num_test = y_test.rows(); + int num_horizon = num_test - step + 1; // longest forecast horizon + Eigen::MatrixXd tot_mat(num_window + num_test, dim); + tot_mat << y, + y_test; + std::vector expand_mat(num_horizon); + std::vector expand_y0(num_horizon); + for (int i = 0; i < num_horizon; i++) { + expand_mat[i] = tot_mat.topRows(num_window + i); + expand_y0[i] = bvhar::build_y0(expand_mat[i], lag, lag + 1); + } + std::vector> ols_objs(num_horizon); + switch(method) { + case 1: { + for (int i = 0; i < num_horizon; ++i) { + Eigen::MatrixXd design = bvhar::build_x0(expand_mat[i], lag, include_mean); + ols_objs[i] = std::unique_ptr(new bvhar::MultiOls(design, expand_y0[i])); + } + } + case 2: { + for (int i = 0; i < num_horizon; ++i) { + Eigen::MatrixXd design = bvhar::build_x0(expand_mat[i], lag, include_mean); + ols_objs[i] = std::unique_ptr(new bvhar::LltOls(design, expand_y0[i])); + } + } + case 3: { + for (int i = 0; i < num_horizon; ++i) { + Eigen::MatrixXd design = bvhar::build_x0(expand_mat[i], lag, include_mean); + ols_objs[i] = std::unique_ptr(new bvhar::QrOls(design, expand_y0[i])); + } + } + } + std::vector> forecaster(num_horizon); + std::vector res(num_horizon); +#ifdef _OPENMP + #pragma omp parallel for num_threads(nthreads) +#endif + for (int window = 0; window < num_horizon; window++) { + bvhar::OlsFit ols_fit = ols_objs[window]->returnOlsFit(lag); + forecaster[window].reset(new bvhar::VarForecaster(ols_fit, step, expand_y0[window], include_mean)); + res[window] = forecaster[window]->forecastPoint().bottomRows(1); + ols_objs[window].reset(); // free the memory by making nullptr + forecaster[window].reset(); // free the memory by making nullptr + } + return std::accumulate( + res.begin() + 1, res.end(), res[0], + [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { + Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); + concat_mat << acc, + curr; + return concat_mat; + } + ); +} + +//' Out-of-Sample Forecasting of VHAR based on Expanding Window +//' +//' This function conducts an expanding window forecasting of VHAR. +//' +//' @param y Time series data of which columns indicate the variables +//' @param week Integer, order for weekly term +//' @param month Integer, order for monthly term +//' @param include_mean Add constant term +//' @param step Integer, Step to forecast +//' @param y_test Evaluation time series data period after `y` +//' @param method Method to solve linear equation system. 1: normal equation, 2: cholesky, 3: HouseholderQR. +//' @param nthreads Number of threads for openmp +//' +//' @noRd +// [[Rcpp::export]] +Eigen::MatrixXd expand_vhar(Eigen::MatrixXd y, int week, int month, bool include_mean, int step, Eigen::MatrixXd y_test, int method, int nthreads) { +#ifdef _OPENMP + Eigen::setNbThreads(nthreads); +#endif + int num_window = y.rows(); + int dim = y.cols(); + int num_test = y_test.rows(); + int num_horizon = num_test - step + 1; // longest forecast horizon + Eigen::MatrixXd tot_mat(num_window + num_test, dim); + tot_mat << y, + y_test; + std::vector expand_mat(num_horizon); + std::vector expand_y0(num_horizon); + Eigen::MatrixXd har_trans = bvhar::build_vhar(dim, week, month, include_mean); + for (int i = 0; i < num_horizon; i++) { + expand_mat[i] = tot_mat.topRows(num_window + i); + expand_y0[i] = bvhar::build_y0(expand_mat[i], month, month + 1); + } + std::vector> ols_objs(num_horizon); + switch(method) { + case 1: { + for (int i = 0; i < num_horizon; ++i) { + Eigen::MatrixXd design = bvhar::build_x0(expand_mat[i], month, include_mean) * har_trans.transpose(); + ols_objs[i] = std::unique_ptr(new bvhar::MultiOls(design, expand_y0[i])); + } + } + case 2: { + for (int i = 0; i < num_horizon; ++i) { + Eigen::MatrixXd design = bvhar::build_x0(expand_mat[i], month, include_mean) * har_trans.transpose(); + ols_objs[i] = std::unique_ptr(new bvhar::LltOls(design, expand_y0[i])); + } + } + case 3: { + for (int i = 0; i < num_horizon; ++i) { + Eigen::MatrixXd design = bvhar::build_x0(expand_mat[i], month, include_mean) * har_trans.transpose(); + ols_objs[i] = std::unique_ptr(new bvhar::QrOls(design, expand_y0[i])); + } + } + } + std::vector> forecaster(num_horizon); + std::vector res(num_horizon); +#ifdef _OPENMP + #pragma omp parallel for num_threads(nthreads) +#endif + for (int window = 0; window < num_horizon; window++) { + bvhar::OlsFit ols_fit = ols_objs[window]->returnOlsFit(month); + forecaster[window].reset(new bvhar::VharForecaster(ols_fit, step, expand_y0[window], har_trans, include_mean)); + res[window] = forecaster[window]->forecastPoint().bottomRows(1); + ols_objs[window].reset(); // free the memory by making nullptr + forecaster[window].reset(); // free the memory by making nullptr + } + return std::accumulate( + res.begin() + 1, res.end(), res[0], + [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { + Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); + concat_mat << acc, + curr; + return concat_mat; + } + ); +} + +//' Generalized Spillover of VAR +//' +//' @param object varlse or vharlse object. +//' @param step Step to forecast. +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::List compute_ols_spillover(Rcpp::List object, int step) { + if (!(object.inherits("varlse") || object.inherits("vharlse"))) { + Rcpp::stop("'object' must be varlse or vharlse object."); + } + std::unique_ptr coef_mat; + int ord; + if (object.inherits("vharlse")) { + coef_mat.reset(new Eigen::MatrixXd(Rcpp::as(object["HARtrans"]).transpose() * Rcpp::as(object["coefficients"]))); + ord = object["month"]; + } else { + coef_mat.reset(new Eigen::MatrixXd(Rcpp::as(object["coefficients"]))); + ord = object["p"]; + } + bvhar::StructuralFit fit(*coef_mat, ord, step - 1, Rcpp::as(object["covmat"])); + std::unique_ptr spillover(new bvhar::OlsSpillover(fit)); + spillover->computeSpillover(); + Eigen::VectorXd to_sp = spillover->returnTo(); + Eigen::VectorXd from_sp = spillover->returnFrom(); + return Rcpp::List::create( + Rcpp::Named("connect") = spillover->returnSpillover(), + Rcpp::Named("to") = to_sp, + Rcpp::Named("from") = from_sp, + Rcpp::Named("tot") = spillover->returnTot(), + Rcpp::Named("net") = to_sp - from_sp, + Rcpp::Named("net_pairwise") = spillover->returnNet() + ); +} + +//' Rolling-sample Total Spillover Index of VAR +//' +//' @param y Time series data of which columns indicate the variables +//' @param window Rolling window size +//' @param step forecast horizon for FEVD +//' @param lag VAR order +//' @param include_mean Add constant term +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::List dynamic_var_spillover(Eigen::MatrixXd y, int window, int step, int lag, bool include_mean, int method, int nthreads) { + int num_horizon = y.rows() - window + 1; // number of windows = T - win + 1 + if (num_horizon <= 0) { + Rcpp::stop("Window size is too large."); + } + std::vector> ols_objs(num_horizon); + for (int i = 0; i < num_horizon; ++i) { + Eigen::MatrixXd roll_mat = y.middleRows(i, window); + ols_objs[i] = std::unique_ptr(new bvhar::OlsVar(roll_mat, lag, include_mean, method)); + } + std::vector> spillover(num_horizon); + Eigen::VectorXd tot(num_horizon); + Eigen::MatrixXd to_sp(num_horizon, y.cols()); + Eigen::MatrixXd from_sp(num_horizon, y.cols()); +#ifdef _OPENMP + #pragma omp parallel for num_threads(nthreads) +#endif + for (int i = 0; i < num_horizon; ++i) { + bvhar::StructuralFit ols_fit = ols_objs[i]->returnStructuralFit(step - 1); + spillover[i].reset(new bvhar::OlsSpillover(ols_fit)); + spillover[i]->computeSpillover(); + to_sp.row(i) = spillover[i]->returnTo(); + from_sp.row(i) = spillover[i]->returnFrom(); + tot[i] = spillover[i]->returnTot(); + ols_objs[i].reset(); // free the memory by making nullptr + spillover[i].reset(); // free the memory by making nullptr + } + return Rcpp::List::create( + Rcpp::Named("to") = to_sp, + Rcpp::Named("from") = from_sp, + Rcpp::Named("tot") = tot, + Rcpp::Named("net") = to_sp - from_sp + ); +} + +//' Rolling-sample Total Spillover Index of VHAR +//' +//' @param y Time series data of which columns indicate the variables +//' @param window Rolling window size +//' @param step forecast horizon for FEVD +//' @param har VHAR order +//' @param include_mean Add constant term +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::List dynamic_vhar_spillover(Eigen::MatrixXd y, int window, int step, int week, int month, bool include_mean, int method, int nthreads) { + int num_horizon = y.rows() - window + 1; // number of windows = T - win + 1 + if (num_horizon <= 0) { + Rcpp::stop("Window size is too large."); + } + std::vector> ols_objs(num_horizon); + for (int i = 0; i < num_horizon; ++i) { + Eigen::MatrixXd roll_mat = y.middleRows(i, window); + ols_objs[i] = std::unique_ptr(new bvhar::OlsVhar(roll_mat, week, month, include_mean, method)); + } + std::vector> spillover(num_horizon); + Eigen::VectorXd tot(num_horizon); + Eigen::MatrixXd to_sp(num_horizon, y.cols()); + Eigen::MatrixXd from_sp(num_horizon, y.cols()); +#ifdef _OPENMP + #pragma omp parallel for num_threads(nthreads) +#endif + for (int i = 0; i < num_horizon; ++i) { + bvhar::StructuralFit ols_fit = ols_objs[i]->returnStructuralFit(step - 1); + spillover[i].reset(new bvhar::OlsSpillover(ols_fit)); + spillover[i]->computeSpillover(); + to_sp.row(i) = spillover[i]->returnTo(); + from_sp.row(i) = spillover[i]->returnFrom(); + tot[i] = spillover[i]->returnTot(); + ols_objs[i].reset(); // free the memory by making nullptr + spillover[i].reset(); // free the memory by making nullptr + } + return Rcpp::List::create( + Rcpp::Named("to") = to_sp, + Rcpp::Named("from") = from_sp, + Rcpp::Named("tot") = tot, + Rcpp::Named("net") = to_sp - from_sp + ); +} diff --git a/src/roll.cpp b/src/roll.cpp deleted file mode 100644 index 7a5e7540..00000000 --- a/src/roll.cpp +++ /dev/null @@ -1,336 +0,0 @@ -#include - -//' Out-of-Sample Forecasting of VAR based on Rolling Window -//' -//' This function conducts an rolling window forecasting of VAR. -//' -//' @param y Time series data of which columns indicate the variables -//' @param lag VAR order -//' @param include_mean Add constant term -//' @param step Integer, Step to forecast -//' @param y_test Evaluation time series data period after `y` -//' @param method Method to solve linear equation system. 1: normal equation, 2: cholesky, 3: HouseholderQR. -//' @param nthreads Number of threads for openmp -//' -//' @noRd -// [[Rcpp::export]] -Eigen::MatrixXd roll_var(Eigen::MatrixXd y, int lag, bool include_mean, int step, Eigen::MatrixXd y_test, int method, int nthreads) { -#ifdef _OPENMP - Eigen::setNbThreads(nthreads); -#endif - int num_window = y.rows(); - int dim = y.cols(); - int num_test = y_test.rows(); - int num_horizon = num_test - step + 1; // longest forecast horizon - Eigen::MatrixXd tot_mat(num_window + num_test, dim); - tot_mat << y, - y_test; - std::vector roll_mat(num_horizon); - std::vector roll_y0(num_horizon); - for (int i = 0; i < num_horizon; i++) { - roll_mat[i] = tot_mat.middleRows(i, num_window); - roll_y0[i] = bvhar::build_y0(roll_mat[i], lag, lag + 1); - } - std::vector> ols_objs(num_horizon); - switch(method) { - case 1: { - for (int i = 0; i < num_horizon; ++i) { - Eigen::MatrixXd design = bvhar::build_x0(roll_mat[i], lag, include_mean); - ols_objs[i] = std::unique_ptr(new bvhar::MultiOls(design, roll_y0[i])); - } - } - case 2: { - for (int i = 0; i < num_horizon; ++i) { - Eigen::MatrixXd design = bvhar::build_x0(roll_mat[i], lag, include_mean); - ols_objs[i] = std::unique_ptr(new bvhar::LltOls(design, roll_y0[i])); - } - } - case 3: { - for (int i = 0; i < num_horizon; ++i) { - Eigen::MatrixXd design = bvhar::build_x0(roll_mat[i], lag, include_mean); - ols_objs[i] = std::unique_ptr(new bvhar::QrOls(design, roll_y0[i])); - } - } - } - std::vector> forecaster(num_horizon); - std::vector res(num_horizon); -#ifdef _OPENMP - #pragma omp parallel for num_threads(nthreads) -#endif - for (int window = 0; window < num_horizon; window++) { - bvhar::OlsFit ols_fit = ols_objs[window]->returnOlsFit(lag); - forecaster[window].reset(new bvhar::VarForecaster(ols_fit, step, roll_y0[window], include_mean)); - res[window] = forecaster[window]->forecastPoint().bottomRows(1); - ols_objs[window].reset(); // free the memory by making nullptr - forecaster[window].reset(); // free the memory by making nullptr - } - return std::accumulate( - res.begin() + 1, res.end(), res[0], - [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { - Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); - concat_mat << acc, - curr; - return concat_mat; - } - ); -} - -//' Out-of-Sample Forecasting of VHAR based on Rolling Window -//' -//' This function conducts an rolling window forecasting of VHAR. -//' -//' @param y Time series data of which columns indicate the variables -//' @param week Integer, order for weekly term -//' @param month Integer, order for monthly term -//' @param include_mean Add constant term -//' @param step Integer, Step to forecast -//' @param y_test Evaluation time series data period after `y` -//' @param method Method to solve linear equation system. 1: normal equation, 2: cholesky, 3: HouseholderQR. -//' @param nthreads Number of threads for openmp -//' -//' @noRd -// [[Rcpp::export]] -Eigen::MatrixXd roll_vhar(Eigen::MatrixXd y, int week, int month, bool include_mean, int step, Eigen::MatrixXd y_test, int method, int nthreads) { -#ifdef _OPENMP - Eigen::setNbThreads(nthreads); -#endif - int num_window = y.rows(); - int dim = y.cols(); - int num_test = y_test.rows(); - int num_horizon = num_test - step + 1; // longest forecast horizon - Eigen::MatrixXd tot_mat(num_window + num_test, dim); - tot_mat << y, - y_test; - std::vector roll_mat(num_horizon); - std::vector roll_y0(num_horizon); - Eigen::MatrixXd har_trans = bvhar::build_vhar(dim, week, month, include_mean); - for (int i = 0; i < num_horizon; i++) { - roll_mat[i] = tot_mat.middleRows(i, num_window); - roll_y0[i] = bvhar::build_y0(roll_mat[i], month, month + 1); - } - std::vector> ols_objs(num_horizon); - switch(method) { - case 1: { - for (int i = 0; i < num_horizon; ++i) { - Eigen::MatrixXd design = bvhar::build_x0(roll_mat[i], month, include_mean) * har_trans.transpose(); - ols_objs[i] = std::unique_ptr(new bvhar::MultiOls(design, roll_y0[i])); - } - } - case 2: { - for (int i = 0; i < num_horizon; ++i) { - Eigen::MatrixXd design = bvhar::build_x0(roll_mat[i], month, include_mean) * har_trans.transpose(); - ols_objs[i] = std::unique_ptr(new bvhar::LltOls(design, roll_y0[i])); - } - } - case 3: { - for (int i = 0; i < num_horizon; ++i) { - Eigen::MatrixXd design = bvhar::build_x0(roll_mat[i], month, include_mean) * har_trans.transpose(); - ols_objs[i] = std::unique_ptr(new bvhar::QrOls(design, roll_y0[i])); - } - } - } - std::vector> forecaster(num_horizon); - std::vector res(num_horizon); -#ifdef _OPENMP - #pragma omp parallel for num_threads(nthreads) -#endif - for (int window = 0; window < num_horizon; window++) { - bvhar::OlsFit ols_fit = ols_objs[window]->returnOlsFit(month); - forecaster[window].reset(new bvhar::VharForecaster(ols_fit, step, roll_y0[window], har_trans, include_mean)); - res[window] = forecaster[window]->forecastPoint().bottomRows(1); - ols_objs[window].reset(); // free the memory by making nullptr - forecaster[window].reset(); // free the memory by making nullptr - } - return std::accumulate( - res.begin() + 1, res.end(), res[0], - [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { - Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); - concat_mat << acc, - curr; - return concat_mat; - } - ); -} - -//' Out-of-Sample Forecasting of BVAR based on Rolling Window -//' -//' This function conducts an rolling window forecasting of BVAR with Minnesota prior. -//' -//' @param y Time series data of which columns indicate the variables -//' @param lag BVAR order -//' @param bayes_spec List, BVAR specification -//' @param include_mean Add constant term -//' @param step Integer, Step to forecast -//' @param y_test Evaluation time series data period after `y` -//' -//' @noRd -// [[Rcpp::export]] -Eigen::MatrixXd roll_bvar(Eigen::MatrixXd y, int lag, Rcpp::List bayes_spec, bool include_mean, int step, Eigen::MatrixXd y_test, Eigen::VectorXi seed_forecast, int nthreads) { - if (!bayes_spec.inherits("bvharspec")) { - Rcpp::stop("'object' must be bvharspec object."); - } - int num_window = y.rows(); - int dim = y.cols(); - int num_test = y_test.rows(); - int num_horizon = num_test - step + 1; // longest forecast horizon - Eigen::MatrixXd tot_mat(num_window + num_test, dim); - tot_mat << y, - y_test; - std::vector roll_mat(num_horizon); - std::vector roll_y0(num_horizon); - for (int i = 0; i < num_horizon; i++) { - roll_mat[i] = tot_mat.middleRows(i, num_window); - roll_y0[i] = bvhar::build_y0(roll_mat[i], lag, lag + 1); - } - tot_mat.resize(0, 0); // free the memory - std::vector> mn_objs(num_horizon); - for (int i = 0; i < num_horizon; ++i) { - bvhar::BvarSpec mn_spec(bayes_spec); - mn_objs[i] = std::unique_ptr(new bvhar::MinnBvar(roll_mat[i], lag, mn_spec, include_mean)); - } - std::vector> forecaster(num_horizon); - std::vector res(num_horizon); -#ifdef _OPENMP - #pragma omp parallel for num_threads(nthreads) -#endif - for (int window = 0; window < num_horizon; window++) { - bvhar::MinnFit mn_fit = mn_objs[window]->returnMinnFit(); - forecaster[window].reset(new bvhar::BvarForecaster(mn_fit, step, roll_y0[window], lag, 1, include_mean, static_cast(seed_forecast[window]))); - res[window] = forecaster[window]->returnPoint().bottomRows(1); - mn_objs[window].reset(); // free the memory by making nullptr - forecaster[window].reset(); // free the memory by making nullptr - } - return std::accumulate( - res.begin() + 1, res.end(), res[0], - [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { - Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); - concat_mat << acc, - curr; - return concat_mat; - } - ); -} - -//' Out-of-Sample Forecasting of BVAR based on Rolling Window -//' -//' This function conducts an rolling window forecasting of BVAR with Flat prior. -//' -//' @param y Time series data of which columns indicate the variables -//' @param lag BVAR order -//' @param bayes_spec List, BVAR specification -//' @param include_mean Add constant term -//' @param step Integer, Step to forecast -//' @param y_test Evaluation time series data period after `y` -//' -//' @noRd -// [[Rcpp::export]] -Eigen::MatrixXd roll_bvarflat(Eigen::MatrixXd y, int lag, Eigen::MatrixXd U, bool include_mean, int step, Eigen::MatrixXd y_test, Eigen::VectorXi seed_forecast, int nthreads) { - int num_window = y.rows(); - int dim = y.cols(); - int num_test = y_test.rows(); - int num_horizon = num_test - step + 1; // longest forecast horizon - Eigen::MatrixXd tot_mat(num_window + num_test, dim); - tot_mat << y, - y_test; - std::vector roll_mat(num_horizon); - std::vector roll_y0(num_horizon); - for (int i = 0; i < num_horizon; i++) { - roll_mat[i] = tot_mat.middleRows(i, num_window); - roll_y0[i] = bvhar::build_y0(roll_mat[i], lag, lag + 1); - } - tot_mat.resize(0, 0); // free the memory - std::vector> mn_objs(num_horizon); - for (int i = 0; i < num_horizon; ++i) { - Eigen::MatrixXd x = bvhar::build_x0(roll_mat[i], lag, include_mean); - mn_objs[i].reset(new bvhar::MinnFlat(x, roll_y0[i], U)); - } - std::vector> forecaster(num_horizon); - std::vector res(num_horizon); - #ifdef _OPENMP - #pragma omp parallel for num_threads(nthreads) -#endif - for (int window = 0; window < num_horizon; window++) { - bvhar::MinnFit mn_fit = mn_objs[window]->returnMinnFit(); - forecaster[window].reset(new bvhar::BvarForecaster(mn_fit, step, roll_y0[window], lag, 1, include_mean, static_cast(seed_forecast[window]))); - res[window] = forecaster[window]->returnPoint().bottomRows(1); - mn_objs[window].reset(); // free the memory by making nullptr - forecaster[window].reset(); // free the memory by making nullptr - } - return std::accumulate( - res.begin() + 1, res.end(), res[0], - [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { - Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); - concat_mat << acc, - curr; - return concat_mat; - } - ); -} - -//' Out-of-Sample Forecasting of BVHAR based on Rolling Window -//' -//' This function conducts an rolling window forecasting of BVHAR with Minnesota prior. -//' -//' @param y Time series data of which columns indicate the variables -//' @param har `r lifecycle::badge("experimental")` Numeric vector for weekly and monthly order. -//' @param bayes_spec List, BVHAR specification -//' @param include_mean Add constant term -//' @param step Integer, Step to forecast -//' @param y_test Evaluation time series data period after `y` -//' -//' @noRd -// [[Rcpp::export]] -Eigen::MatrixXd roll_bvhar(Eigen::MatrixXd y, int week, int month, Rcpp::List bayes_spec, bool include_mean, int step, Eigen::MatrixXd y_test, Eigen::VectorXi seed_forecast, int nthreads) { - if (!bayes_spec.inherits("bvharspec")) { - Rcpp::stop("'object' must be bvharspec object."); - } - int num_window = y.rows(); - int dim = y.cols(); - int num_test = y_test.rows(); - int num_horizon = num_test - step + 1; // longest forecast horizon - Eigen::MatrixXd tot_mat(num_window + num_test, dim); - tot_mat << y, - y_test; - std::vector roll_mat(num_horizon); - std::vector roll_y0(num_horizon); - Eigen::MatrixXd har_trans = bvhar::build_vhar(dim, week, month, include_mean); - for (int i = 0; i < num_horizon; i++) { - roll_mat[i] = tot_mat.middleRows(i, num_window); - roll_y0[i] = bvhar::build_y0(roll_mat[i], month, month + 1); - } - tot_mat.resize(0, 0); // free the memory - std::vector> mn_objs(num_horizon); - if (bayes_spec.containsElementNamed("delta")) { - bvhar::BvarSpec mn_spec(bayes_spec); - for (int i = 0; i < num_horizon; ++i) { - mn_objs[i].reset(new bvhar::MinnBvharS(roll_mat[i], week, month, mn_spec, include_mean)); - } - } else { - bvhar::BvharSpec mn_spec(bayes_spec); - for (int i = 0; i < num_horizon; ++i) { - bvhar::BvharSpec mn_spec(bayes_spec); - mn_objs[i].reset(new bvhar::MinnBvharL(roll_mat[i], week, month, mn_spec, include_mean)); - } - } - std::vector> forecaster(num_horizon); - std::vector res(num_horizon); -#ifdef _OPENMP - #pragma omp parallel for num_threads(nthreads) -#endif - for (int window = 0; window < num_horizon; window++) { - bvhar::MinnFit mn_fit = mn_objs[window]->returnMinnFit(); - forecaster[window].reset(new bvhar::BvharForecaster(mn_fit, step, roll_y0[window], har_trans, month, 1, include_mean, static_cast(seed_forecast[window]))); - res[window] = forecaster[window]->returnPoint().bottomRows(1); - mn_objs[window].reset(); // free the memory by making nullptr - forecaster[window].reset(); // free the memory by making nullptr - } - return std::accumulate( - res.begin() + 1, res.end(), res[0], - [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { - Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); - concat_mat << acc, - curr; - return concat_mat; - } - ); -} diff --git a/src/spillover.cpp b/src/spillover.cpp deleted file mode 100644 index ba7a55fb..00000000 --- a/src/spillover.cpp +++ /dev/null @@ -1,589 +0,0 @@ -#include - -//' Generalized Spillover of VAR -//' -//' @param object varlse or vharlse object. -//' @param step Step to forecast. -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::List compute_ols_spillover(Rcpp::List object, int step) { - if (!(object.inherits("varlse") || object.inherits("vharlse"))) { - Rcpp::stop("'object' must be varlse or vharlse object."); - } - std::unique_ptr coef_mat; - int ord; - if (object.inherits("vharlse")) { - coef_mat.reset(new Eigen::MatrixXd(Rcpp::as(object["HARtrans"]).transpose() * Rcpp::as(object["coefficients"]))); - ord = object["month"]; - } else { - coef_mat.reset(new Eigen::MatrixXd(Rcpp::as(object["coefficients"]))); - ord = object["p"]; - } - bvhar::StructuralFit fit(*coef_mat, ord, step - 1, Rcpp::as(object["covmat"])); - std::unique_ptr spillover(new bvhar::OlsSpillover(fit)); - spillover->computeSpillover(); - Eigen::VectorXd to_sp = spillover->returnTo(); - Eigen::VectorXd from_sp = spillover->returnFrom(); - return Rcpp::List::create( - Rcpp::Named("connect") = spillover->returnSpillover(), - Rcpp::Named("to") = to_sp, - Rcpp::Named("from") = from_sp, - Rcpp::Named("tot") = spillover->returnTot(), - Rcpp::Named("net") = to_sp - from_sp, - Rcpp::Named("net_pairwise") = spillover->returnNet() - ); -} - -//' Rolling-sample Total Spillover Index of VAR -//' -//' @param y Time series data of which columns indicate the variables -//' @param window Rolling window size -//' @param step forecast horizon for FEVD -//' @param lag VAR order -//' @param include_mean Add constant term -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::List dynamic_var_spillover(Eigen::MatrixXd y, int window, int step, int lag, bool include_mean, int method, int nthreads) { - int num_horizon = y.rows() - window + 1; // number of windows = T - win + 1 - if (num_horizon <= 0) { - Rcpp::stop("Window size is too large."); - } - std::vector> ols_objs(num_horizon); - for (int i = 0; i < num_horizon; ++i) { - Eigen::MatrixXd roll_mat = y.middleRows(i, window); - ols_objs[i] = std::unique_ptr(new bvhar::OlsVar(roll_mat, lag, include_mean, method)); - } - std::vector> spillover(num_horizon); - Eigen::VectorXd tot(num_horizon); - Eigen::MatrixXd to_sp(num_horizon, y.cols()); - Eigen::MatrixXd from_sp(num_horizon, y.cols()); -#ifdef _OPENMP - #pragma omp parallel for num_threads(nthreads) -#endif - for (int i = 0; i < num_horizon; ++i) { - bvhar::StructuralFit ols_fit = ols_objs[i]->returnStructuralFit(step - 1); - spillover[i].reset(new bvhar::OlsSpillover(ols_fit)); - spillover[i]->computeSpillover(); - to_sp.row(i) = spillover[i]->returnTo(); - from_sp.row(i) = spillover[i]->returnFrom(); - tot[i] = spillover[i]->returnTot(); - ols_objs[i].reset(); // free the memory by making nullptr - spillover[i].reset(); // free the memory by making nullptr - } - return Rcpp::List::create( - Rcpp::Named("to") = to_sp, - Rcpp::Named("from") = from_sp, - Rcpp::Named("tot") = tot, - Rcpp::Named("net") = to_sp - from_sp - ); -} - -//' Rolling-sample Total Spillover Index of VHAR -//' -//' @param y Time series data of which columns indicate the variables -//' @param window Rolling window size -//' @param step forecast horizon for FEVD -//' @param har VHAR order -//' @param include_mean Add constant term -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::List dynamic_vhar_spillover(Eigen::MatrixXd y, int window, int step, int week, int month, bool include_mean, int method, int nthreads) { - int num_horizon = y.rows() - window + 1; // number of windows = T - win + 1 - if (num_horizon <= 0) { - Rcpp::stop("Window size is too large."); - } - std::vector> ols_objs(num_horizon); - for (int i = 0; i < num_horizon; ++i) { - Eigen::MatrixXd roll_mat = y.middleRows(i, window); - ols_objs[i] = std::unique_ptr(new bvhar::OlsVhar(roll_mat, week, month, include_mean, method)); - } - std::vector> spillover(num_horizon); - Eigen::VectorXd tot(num_horizon); - Eigen::MatrixXd to_sp(num_horizon, y.cols()); - Eigen::MatrixXd from_sp(num_horizon, y.cols()); -#ifdef _OPENMP - #pragma omp parallel for num_threads(nthreads) -#endif - for (int i = 0; i < num_horizon; ++i) { - bvhar::StructuralFit ols_fit = ols_objs[i]->returnStructuralFit(step - 1); - spillover[i].reset(new bvhar::OlsSpillover(ols_fit)); - spillover[i]->computeSpillover(); - to_sp.row(i) = spillover[i]->returnTo(); - from_sp.row(i) = spillover[i]->returnFrom(); - tot[i] = spillover[i]->returnTot(); - ols_objs[i].reset(); // free the memory by making nullptr - spillover[i].reset(); // free the memory by making nullptr - } - return Rcpp::List::create( - Rcpp::Named("to") = to_sp, - Rcpp::Named("from") = from_sp, - Rcpp::Named("tot") = tot, - Rcpp::Named("net") = to_sp - from_sp - ); -} - -//' Generalized Spillover of Minnesota prior -//' -//' @param object varlse or vharlse object. -//' @param step Step to forecast. -//' @param num_iter Number to sample MNIW distribution -//' @param num_burn Number of burn-in -//' @param thin Thinning -//' @param seed Random seed for boost library -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::List compute_mn_spillover(Rcpp::List object, int step, int num_iter, int num_burn, int thin, unsigned int seed) { - if (!(object.inherits("bvarmn") || object.inherits("bvharmn"))) { - Rcpp::stop("'object' must be bvarmn or bvharmn object."); - } - std::unique_ptr spillover; - if (object.inherits("bvharmn")) { - bvhar::MinnFit fit(Rcpp::as(object["coefficients"]), Rcpp::as(object["mn_prec"]), Rcpp::as(object["covmat"]), object["iw_shape"]); - spillover.reset(new bvhar::BvharSpillover(fit, step, num_iter, num_burn, thin, object["month"], Rcpp::as(object["HARtrans"]), seed)); - } else { - bvhar::MinnFit fit(Rcpp::as(object["coefficients"]), Rcpp::as(object["mn_prec"]), Rcpp::as(object["covmat"]), object["iw_shape"]); - spillover.reset(new bvhar::MinnSpillover(fit, step, num_iter, num_burn, thin, object["p"], seed)); - } - spillover->updateMniw(); - spillover->computeSpillover(); - Eigen::VectorXd to_sp = spillover->returnTo(); - Eigen::VectorXd from_sp = spillover->returnFrom(); - return Rcpp::List::create( - Rcpp::Named("connect") = spillover->returnSpillover(), - Rcpp::Named("to") = to_sp, - Rcpp::Named("from") = from_sp, - Rcpp::Named("tot") = spillover->returnTot(), - Rcpp::Named("net") = to_sp - from_sp, - Rcpp::Named("net_pairwise") = spillover->returnNet() - ); -} - -// Rcpp::List compute_bvarmn_spillover(int lag, int step, Eigen::MatrixXd alpha_record, Eigen::MatrixXd sig_record) { -// // if (!(object.inherits("bvarmn") || object.inherits("bvharmn"))) { -// // Rcpp::stop("'object' must be bvarmn or bvharmn object."); -// // } -// bvhar::MinnRecords mn_record(alpha_record, sig_record); -// std::unique_ptr spillover(new bvhar::MinnSpillover(mn_record, step, lag)); -// // std::unique_ptr spillover; -// // if (object.inherits("bvharmn")) { -// // bvhar::MinnFit fit(Rcpp::as(object["coefficients"]), Rcpp::as(object["mn_prec"]), Rcpp::as(object["iw_scale"]), object["iw_shape"]); -// // spillover.reset(new bvhar::BvharSpillover(fit, step, num_iter, num_burn, thin, object["month"], Rcpp::as(object["HARtrans"]), seed)); -// // } else { -// // bvhar::MinnFit fit(Rcpp::as(object["coefficients"]), Rcpp::as(object["mn_prec"]), Rcpp::as(object["iw_scale"]), object["iw_shape"]); -// // spillover.reset(new bvhar::MinnSpillover(fit, step, num_iter, num_burn, thin, object["p"], seed)); -// // } -// // spillover->updateMniw(); -// spillover->computeSpillover(); -// Eigen::VectorXd to_sp = spillover->returnTo(); -// Eigen::VectorXd from_sp = spillover->returnFrom(); -// return Rcpp::List::create( -// Rcpp::Named("connect") = spillover->returnSpillover(), -// Rcpp::Named("to") = to_sp, -// Rcpp::Named("from") = from_sp, -// Rcpp::Named("tot") = spillover->returnTot(), -// Rcpp::Named("net") = to_sp - from_sp, -// Rcpp::Named("net_pairwise") = spillover->returnNet() -// ); -// } - -// Rcpp::List compute_bvharmn_spillover(int month, int step, Eigen::MatrixXd har_trans, Eigen::MatrixXd phi_record, Eigen::MatrixXd sig_record) { -// // if (!(object.inherits("bvarmn") || object.inherits("bvharmn"))) { -// // Rcpp::stop("'object' must be bvarmn or bvharmn object."); -// // } -// bvhar::MinnRecords mn_record(phi_record, sig_record); -// std::unique_ptr spillover(new bvhar::BvharSpillover(mn_record, step, month, har_trans)); -// // std::unique_ptr spillover; -// // if (object.inherits("bvharmn")) { -// // bvhar::MinnFit fit(Rcpp::as(object["coefficients"]), Rcpp::as(object["mn_prec"]), Rcpp::as(object["iw_scale"]), object["iw_shape"]); -// // spillover.reset(new bvhar::BvharSpillover(fit, step, num_iter, num_burn, thin, object["month"], Rcpp::as(object["HARtrans"]), seed)); -// // } else { -// // bvhar::MinnFit fit(Rcpp::as(object["coefficients"]), Rcpp::as(object["mn_prec"]), Rcpp::as(object["iw_scale"]), object["iw_shape"]); -// // spillover.reset(new bvhar::MinnSpillover(fit, step, num_iter, num_burn, thin, object["p"], seed)); -// // } -// // spillover->updateMniw(); -// spillover->computeSpillover(); -// Eigen::VectorXd to_sp = spillover->returnTo(); -// Eigen::VectorXd from_sp = spillover->returnFrom(); -// return Rcpp::List::create( -// Rcpp::Named("connect") = spillover->returnSpillover(), -// Rcpp::Named("to") = to_sp, -// Rcpp::Named("from") = from_sp, -// Rcpp::Named("tot") = spillover->returnTot(), -// Rcpp::Named("net") = to_sp - from_sp, -// Rcpp::Named("net_pairwise") = spillover->returnNet() -// ); -// } - -//' Rolling-sample Total Spillover Index of BVAR -//' -//' @param y Time series data of which columns indicate the variables -//' @param window Rolling window size -//' @param step forecast horizon for FEVD -//' @param num_iter Number to sample MNIW distribution -//' @param num_burn Number of burn-in -//' @param thin Thinning -//' @param lag BVAR order -//' @param bayes_spec BVAR specification -//' @param include_mean Add constant term -//' @param seed_chain Random seed for each window -//' @param nthreads Number of threads for openmp -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::List dynamic_bvar_spillover(Eigen::MatrixXd y, int window, int step, int num_iter, int num_burn, int thin, - int lag, Rcpp::List bayes_spec, bool include_mean, Eigen::VectorXi seed_chain, int nthreads) { -// Rcpp::List dynamic_bvar_spillover(Eigen::MatrixXd y, int window, int step, int num_chains, int num_iter, int num_burn, int thin, -// int lag, Rcpp::List bayes_spec, bool include_mean, Eigen::MatrixXi seed_chain, int nthreads) { - int num_horizon = y.rows() - window + 1; // number of windows = T - win + 1 - if (num_horizon <= 0) { - Rcpp::stop("Window size is too large."); - } - // std::vector>> mn_objs(num_horizon); - // for (auto &mn_chain : mn_objs) { - // mn_chain.resize(num_chains); - // for (auto &ptr : mn_chain) { - // ptr = nullptr; - // } - // } - std::vector> mn_objs(num_horizon); - int dim = y.cols(); - for (int i = 0; i < num_horizon; ++i) { - Eigen::MatrixXd roll_mat = y.middleRows(i, window); - // Eigen::MatrixXd roll_y0 = bvhar::build_y0(roll_mat, lag, lag + 1); - // Eigen::MatrixXd roll_x0 = bvhar::build_x0(roll_mat, lag, include_mean); - bvhar::BvarSpec mn_spec(bayes_spec); - mn_objs[i] = std::unique_ptr(new bvhar::MinnBvar(roll_mat, lag, mn_spec, include_mean)); - // Eigen::MatrixXd y_dummy = bvhar::build_ydummy( - // lag, mn_spec._sigma, mn_spec._lambda, - // mn_spec._delta, Eigen::VectorXd::Zero(dim), Eigen::VectorXd::Zero(dim), - // include_mean - // ); - // Eigen::MatrixXd x_dummy = bvhar::build_xdummy( - // Eigen::VectorXd::LinSpaced(lag, 1, lag), - // mn_spec._lambda, mn_spec._sigma, mn_spec._eps, include_mean - // ); - // for (int j = 0; j < num_chains; ++j) { - // mn_objs[i][j].reset(new bvhar::Minnesota(num_iter, roll_x0, roll_y0, x_dummy, y_dummy, static_cast(seed_chain(i, j)))); - // mn_objs[i][j]->computePosterior(); - // } - } - // std::vector> mn_recs(num_horizon); - // for (auto &rec_chain : mn_recs) { - // rec_chain.resize(num_chains); - // for (auto &ptr : rec_chain) { - // ptr = nullptr; - // } - // } - // std::vector mn_rec(num_chains); - std::vector> spillover(num_horizon); - // std::vector mn_recs(num_chains); - // std::vector coef_record(num_chains); - // std::vector sig_record(num_chains); - Eigen::VectorXd tot(num_horizon); - Eigen::MatrixXd to_sp(num_horizon, dim); - Eigen::MatrixXd from_sp(num_horizon, dim); -#ifdef _OPENMP - #pragma omp parallel for num_threads(nthreads) -#endif - for (int i = 0; i < num_horizon; ++i) { - // for (int win = 0; win < num_horizon; ++win) { - bvhar::MinnFit mn_fit = mn_objs[i]->returnMinnFit(); - spillover[i].reset(new bvhar::MinnSpillover(mn_fit, step, num_iter, num_burn, thin, lag, static_cast(seed_chain[i]))); - spillover[i]->updateMniw(); - spillover[i]->computeSpillover(); - to_sp.row(i) = spillover[i]->returnTo(); - from_sp.row(i) = spillover[i]->returnFrom(); - tot[i] = spillover[i]->returnTot(); - mn_objs[i].reset(); // free the memory by making nullptr - spillover[i].reset(); // free the memory by making nullptr - - // std::vector coef_record(num_chains); - // std::vector sig_record(num_chains); - // for (int chain = 0; chain < num_chains; ++chain) { - // for (int i = 0; i < num_iter; ++i) { - // mn_objs[win][chain]->doPosteriorDraws(); - // } - // // mn_recs[i][j] = mn_objs[i][j]->returnMinnRecords(num_burn, thin); - // bvhar::MinnRecords mn_rec = mn_objs[win][chain]->returnMinnRecords(num_burn, thin); - // // mn_recs[j] = mn_objs[i][j]->returnMinnRecords(num_burn, thin); - // coef_record[chain] = mn_rec.coef_record; - // sig_record[chain] = mn_rec.sig_record; - // mn_objs[win][chain].reset(); - // } - // Eigen::MatrixXd coef = std::accumulate( - // coef_record.begin() + 1, coef_record.end(), coef_record[0], - // [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { - // Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); - // concat_mat << acc, - // curr; - // return concat_mat; - // } - // ); - // Eigen::MatrixXd sig = std::accumulate( - // sig_record.begin() + 1, sig_record.end(), sig_record[0], - // [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { - // Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); - // concat_mat << acc, - // curr; - // return concat_mat; - // } - // ); - // bvhar::MinnRecords mn_record(coef, sig); - // spillover[win].reset(new bvhar::MinnSpillover(mn_record, step, lag)); - // // bvhar::MinnFit mn_fit = mn_objs[win]->returnMinnFit(); - // // spillover[win].reset(new bvhar::MinnSpillover(mn_fit, step, num_iter, num_burn, thin, lag, static_cast(seed_chain[win]))); - // // spillover[win]->updateMniw(); - // spillover[win]->computeSpillover(); - // to_sp.row(win) = spillover[win]->returnTo(); - // from_sp.row(win) = spillover[win]->returnFrom(); - // tot[win] = spillover[win]->returnTot(); - // // mn_objs[win].reset(); // free the memory by making nullptr - // spillover[win].reset(); // free the memory by making nullptr - } - return Rcpp::List::create( - Rcpp::Named("to") = to_sp, - Rcpp::Named("from") = from_sp, - Rcpp::Named("tot") = tot, - Rcpp::Named("net") = to_sp - from_sp - ); -} - -//' Rolling-sample Total Spillover Index of BVHAR -//' -//' @param y Time series data of which columns indicate the variables -//' @param window Rolling window size -//' @param step forecast horizon for FEVD -//' @param num_iter Number to sample MNIW distribution -//' @param num_burn Number of burn-in -//' @param thin Thinning -//' @param week Week order -//' @param month Month order -//' @param bayes_spec BVHAR specification -//' @param include_mean Add constant term -//' @param seed_chain Random seed for each window -//' @param nthreads Number of threads for openmp -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::List dynamic_bvhar_spillover(Eigen::MatrixXd y, int window, int step, int num_iter, int num_burn, int thin, - int week, int month, Rcpp::List bayes_spec, bool include_mean, Eigen::VectorXi seed_chain, int nthreads) { -// Rcpp::List dynamic_bvhar_spillover(Eigen::MatrixXd y, int window, int step, int num_chains, int num_iter, int num_burn, int thin, -// int week, int month, Rcpp::List bayes_spec, bool include_mean, Eigen::MatrixXi seed_chain, int nthreads) { - int num_horizon = y.rows() - window + 1; // number of windows = T - win + 1 - if (num_horizon <= 0) { - Rcpp::stop("Window size is too large."); - } - int dim = y.cols(); - Eigen::MatrixXd har_trans = bvhar::build_vhar(dim, week, month, include_mean); - // std::vector>> mn_objs(num_horizon); - // for (auto &mn_chain : mn_objs) { - // mn_chain.resize(num_chains); - // for (auto &ptr : mn_chain) { - // ptr = nullptr; - // } - // } - // for (int i = 0; i < num_horizon; ++i) { - // Eigen::MatrixXd roll_mat = y.middleRows(i, window); - // Eigen::MatrixXd roll_y0 = bvhar::build_y0(roll_mat, month, month + 1); - // Eigen::MatrixXd roll_x0 = bvhar::build_x0(roll_mat, month, include_mean) * har_trans.transpose(); - // bvhar::BvarSpec mn_spec(bayes_spec); - // Eigen::MatrixXd y_dummy = bvhar::build_ydummy( - // lag, mn_spec._sigma, mn_spec._lambda, - // mn_spec._delta, Eigen::VectorXd::Zero(dim), Eigen::VectorXd::Zero(dim), - // include_mean - // ); - // Eigen::MatrixXd x_dummy = bvhar::build_xdummy( - // Eigen::VectorXd::LinSpaced(lag, 1, lag), - // mn_spec._lambda, mn_spec._sigma, mn_spec._eps, include_mean - // ); - // if (bayes_spec.containsElementNamed("delta")) { - // // bvhar::BvarSpec bvhar_spec(bayes_spec); - // bvhar::BvarSpec mn_spec(bayes_spec); - // Eigen::MatrixXd y_dummy = bvhar::build_ydummy( - // 3, mn_spec._sigma, mn_spec._lambda, - // mn_spec._delta, Eigen::VectorXd::Zero(dim), Eigen::VectorXd::Zero(dim), - // include_mean - // ); - // Eigen::MatrixXd x_dummy = bvhar::build_xdummy( - // Eigen::VectorXd::LinSpaced(3, 1, 3), - // mn_spec._lambda, mn_spec._sigma, mn_spec._eps, include_mean - // ); - // // mn_objs[i] = std::unique_ptr(new bvhar::MinnBvharS(roll_mat, week, month, bvhar_spec, include_mean)); - // for (int j = 0; j < num_chains; ++j) { - // mn_objs[i][j].reset(new bvhar::Minnesota(num_iter, roll_x0, roll_y0, x_dummy, y_dummy, static_cast(seed_chain(i, j)))); - // mn_objs[i][j]->computePosterior(); - // } - // } else { - // // bvhar::BvharSpec bvhar_spec(bayes_spec); - // bvhar::BvharSpec mn_spec(bayes_spec); - // Eigen::MatrixXd y_dummy = bvhar::build_ydummy( - // 3, mn_spec._sigma, mn_spec._lambda, - // mn_spec._daily, mn_spec._weekly, mn_spec._monthly, - // include_mean - // ); - // Eigen::MatrixXd x_dummy = bvhar::build_xdummy( - // Eigen::VectorXd::LinSpaced(3, 1, 3), - // mn_spec._lambda, mn_spec._sigma, mn_spec._eps, include_mean - // ); - // // mn_objs[i] = std::unique_ptr(new bvhar::MinnBvharL(roll_mat, week, month, bvhar_spec, include_mean)); - // for (int j = 0; j < num_chains; ++j) { - // mn_objs[i][j].reset(new bvhar::Minnesota(num_iter, roll_x0, roll_y0, x_dummy, y_dummy, static_cast(seed_chain(i, j)))); - // mn_objs[i][j]->computePosterior(); - // } - // } - // } - std::vector> mn_objs(num_horizon); - for (int i = 0; i < num_horizon; ++i) { - Eigen::MatrixXd roll_mat = y.middleRows(i, window); - if (bayes_spec.containsElementNamed("delta")) { - bvhar::BvarSpec bvhar_spec(bayes_spec); - mn_objs[i] = std::unique_ptr(new bvhar::MinnBvharS(roll_mat, week, month, bvhar_spec, include_mean)); - } else { - bvhar::BvharSpec bvhar_spec(bayes_spec); - mn_objs[i] = std::unique_ptr(new bvhar::MinnBvharL(roll_mat, week, month, bvhar_spec, include_mean)); - } - } - std::vector> spillover(num_horizon); - // std::vector mn_recs(num_chains); - // std::vector coef_record(num_chains); - // std::vector sig_record(num_chains); - Eigen::VectorXd tot(num_horizon); - Eigen::MatrixXd to_sp(num_horizon, y.cols()); - Eigen::MatrixXd from_sp(num_horizon, y.cols()); -#ifdef _OPENMP - #pragma omp parallel for num_threads(nthreads) -#endif - for (int i = 0; i < num_horizon; ++i) { - // for (int win = 0; win < num_horizon; ++win) { - // std::vector coef_record(num_chains); - // std::vector sig_record(num_chains); - // for (int chain = 0; chain < num_chains; ++chain) { - // for (int i = 0; i < num_iter; ++i) { - // mn_objs[win][chain]->doPosteriorDraws(); - // } - // bvhar::MinnRecords mn_rec = mn_objs[win][chain]->returnMinnRecords(num_burn, thin); - // coef_record[chain] = mn_rec.coef_record; - // sig_record[chain] = mn_rec.sig_record; - // mn_objs[win][chain].reset(); - // } - // Eigen::MatrixXd coef = std::accumulate( - // coef_record.begin() + 1, coef_record.end(), coef_record[0], - // [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { - // Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); - // concat_mat << acc, - // curr; - // return concat_mat; - // } - // ); - // Eigen::MatrixXd sig = std::accumulate( - // sig_record.begin() + 1, sig_record.end(), sig_record[0], - // [](const Eigen::MatrixXd& acc, const Eigen::MatrixXd& curr) { - // Eigen::MatrixXd concat_mat(acc.rows() + curr.rows(), acc.cols()); - // concat_mat << acc, - // curr; - // return concat_mat; - // } - // ); - // bvhar::MinnRecords mn_record(coef, sig); - bvhar::MinnFit mn_fit = mn_objs[i]->returnMinnFit(); - // spillover[i].reset(new bvhar::BvharSpillover(mn_fit, step, num_iter, num_burn, thin, month, har_trans, static_cast(seed_chain[win]))); - spillover[i].reset(new bvhar::BvharSpillover(mn_fit, step, num_iter, num_burn, thin, month, har_trans, static_cast(seed_chain[i]))); - spillover[i]->updateMniw(); - // spillover[win].reset(new bvhar::BvharSpillover(mn_record, step, month, har_trans)); - spillover[i]->computeSpillover(); - to_sp.row(i) = spillover[i]->returnTo(); - from_sp.row(i) = spillover[i]->returnFrom(); - tot[i] = spillover[i]->returnTot(); - mn_objs[i].reset(); // free the memory by making nullptr - spillover[i].reset(); // free the memory by making nullptr - } - return Rcpp::List::create( - Rcpp::Named("to") = to_sp, - Rcpp::Named("from") = from_sp, - Rcpp::Named("tot") = tot, - Rcpp::Named("net") = to_sp - from_sp - ); -} - -// [[Rcpp::export]] -Rcpp::List compute_varldlt_spillover(int lag, int step, Rcpp::List fit_record, bool sparse) { - // auto spillover = bvhar::initialize_spillover(0, lag, step, fit_record, sparse, 0); - // return spillover->returnSpilloverDensity(); - auto spillover = std::make_unique>(lag, step, fit_record, sparse); - return spillover->returnSpillover(); -} - -// [[Rcpp::export]] -Rcpp::List compute_vharldlt_spillover(int week, int month, int step, Rcpp::List fit_record, bool sparse) { - // auto spillover = bvhar::initialize_spillover(0, month, step, fit_record, sparse, 0, NULLOPT, week); - // return spillover->returnSpilloverDensity(); - auto spillover = std::make_unique>(week, month, step, fit_record, sparse); - return spillover->returnSpillover(); -} - -// [[Rcpp::export]] -Rcpp::List dynamic_bvarldlt_spillover(Eigen::MatrixXd y, int window, int step, int num_chains, int num_iter, int num_burn, int thin, bool sparse, - int lag, Rcpp::List param_reg, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, - int prior_type, bool ggl, Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, - bool include_mean, Eigen::MatrixXi seed_chain, int nthreads) { - auto spillover = std::make_unique( - y, window, step, lag, num_chains, num_iter, num_burn, thin, sparse, - param_reg, param_prior, param_intercept, param_init, prior_type, ggl, - grp_id, own_id, cross_id, grp_mat, - include_mean, seed_chain, nthreads - ); - return spillover->returnSpillover(); -} - -// [[Rcpp::export]] -Rcpp::List dynamic_bvharldlt_spillover(Eigen::MatrixXd y, int window, int step, int num_chains, int num_iter, int num_burn, int thin, bool sparse, - int week, int month, Rcpp::List param_reg, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, - int prior_type, bool ggl, Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, - bool include_mean, Eigen::MatrixXi seed_chain, int nthreads) { - auto spillover = std::make_unique( - y, window, step, week, month, num_chains, num_iter, num_burn, thin, sparse, - param_reg, param_prior, param_intercept, param_init, prior_type, ggl, - grp_id, own_id, cross_id, grp_mat, - include_mean, seed_chain, nthreads - ); - return spillover->returnSpillover(); -} - -//' Dynamic Total Spillover Index of BVAR-SV -//' -//' @param lag VAR lag. -//' @param window Rolling window size -//' @param step forecast horizon for FEVD -//' @param response_mat Response matrix. -//' @param phi_record Coefficients MCMC record -//' @param h_record log volatility MCMC record -//' @param a_record Contemporaneous coefficients MCMC record -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::List dynamic_bvarsv_spillover(int lag, int step, int num_design, Rcpp::List fit_record, bool sparse, bool include_mean, int nthreads) { - auto spillover = std::make_unique(lag, step, num_design, fit_record, include_mean, sparse, nthreads); - return spillover->returnSpillover(); -} - -//' Dynamic Total Spillover Index of BVHAR-SV -//' -//' @param month VHAR month order. -//' @param window Rolling window size -//' @param step forecast horizon for FEVD -//' @param response_mat Response matrix. -//' @param HARtrans VHAR linear transformation matrix -//' @param phi_record Coefficients MCMC record -//' @param h_record log volatility MCMC record -//' @param a_record Contemporaneous coefficients MCMC record -//' -//' @noRd -// [[Rcpp::export]] -Rcpp::List dynamic_bvharsv_spillover(int week, int month, int step, int num_design, Rcpp::List fit_record, bool sparse, bool include_mean, int nthreads) { - auto spillover = std::make_unique(week, month, step, num_design, fit_record, include_mean, sparse, nthreads); - return spillover->returnSpillover(); -} diff --git a/src/triangular.cpp b/src/triangular.cpp new file mode 100644 index 00000000..4a210e03 --- /dev/null +++ b/src/triangular.cpp @@ -0,0 +1,709 @@ +#include + +//' VAR with Shrinkage Priors +//' +//' This function generates parameters \eqn{\beta, a, \sigma_{h,i}^2, h_{0,i}} and log-volatilities \eqn{h_{i,1}, \ldots, h_{i, n}}. +//' +//' @param num_chain Number of MCMC chains +//' @param num_iter Number of iteration for MCMC +//' @param num_burn Number of burn-in (warm-up) for MCMC +//' @param thin Thinning +//' @param x Design matrix X0 +//' @param y Response matrix Y0 +//' @param param_reg Regression specification list +//' @param param_prior Prior specification list +//' @param param_intercept Intercept specification list +//' @param param_init Initialization specification list +//' @param grp_id Unique group id +//' @param grp_mat Group matrix +//' @param include_mean Constant term +//' @param seed_chain Seed for each chain +//' @param display_progress Progress bar +//' @param nthreads Number of threads for openmp +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::List estimate_sur(int num_chains, int num_iter, int num_burn, int thin, + Eigen::MatrixXd x, Eigen::MatrixXd y, + Rcpp::List param_reg, Rcpp::List param_prior, Rcpp::List param_intercept, + Rcpp::List param_init, int prior_type, bool ggl, + Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, + bool include_mean, Eigen::VectorXi seed_chain, bool display_progress, int nthreads) { + auto mcmc_run = [&]() -> std::unique_ptr { + if (param_reg.containsElementNamed("initial_mean")) { + if (ggl) { + return std::make_unique>( + num_chains, num_iter, num_burn, thin, x, y, + param_reg, param_prior, param_intercept, param_init, prior_type, + grp_id, own_id, cross_id, grp_mat, + include_mean, seed_chain, + display_progress, nthreads + ); + } + return std::make_unique>( + num_chains, num_iter, num_burn, thin, x, y, + param_reg, param_prior, param_intercept, param_init, prior_type, + grp_id, own_id, cross_id, grp_mat, + include_mean, seed_chain, + display_progress, nthreads + ); + } + if (ggl) { + return std::make_unique>( + num_chains, num_iter, num_burn, thin, x, y, + param_reg, param_prior, param_intercept, param_init, prior_type, + grp_id, own_id, cross_id, grp_mat, + include_mean, seed_chain, + display_progress, nthreads + ); + } + return std::make_unique>( + num_chains, num_iter, num_burn, thin, x, y, + param_reg, param_prior, param_intercept, param_init, prior_type, + grp_id, own_id, cross_id, grp_mat, + include_mean, seed_chain, + display_progress, nthreads + ); + }(); + // Start Gibbs sampling----------------------------------- + return mcmc_run->returnRecords(); +} + +//' Forecasting predictive density of BVAR +//' +//' @param num_chains Number of chains +//' @param var_lag VAR order. +//' @param step Integer, Step to forecast. +//' @param response_mat Response matrix. +//' @param sv Use Innovation? +//' @param sparse Use restricted model? +//' @param level CI level to give sparsity. Valid when `prior_type` is 0. +//' @param fit_record MCMC records list +//' @param prior_type Prior type. If 0, use CI. Valid when sparse is true. +//' @param seed_chain Seed for each chain +//' @param stable Filter stable draws +//' @param include_mean Include constant term? +//' @param nthreads OpenMP number of threads +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::List forecast_bvarldlt(int num_chains, int var_lag, int step, Eigen::MatrixXd response_mat, + bool sparse, double level, Rcpp::List fit_record, int prior_type, + Eigen::VectorXi seed_chain, bool include_mean, bool stable, int nthreads) { + auto forecaster = std::make_unique>( + num_chains, var_lag, step, response_mat, + sparse, level, fit_record, + seed_chain, include_mean, stable, nthreads + ); + return Rcpp::wrap(forecaster->returnForecast()); +} + +//' Forecasting Predictive Density of BVHAR +//' +//' @param num_chains Number of MCMC chains +//' @param month VHAR month order. +//' @param step Integer, Step to forecast. +//' @param response_mat Response matrix. +//' @param HARtrans VHAR linear transformation matrix +//' @param sv Use Innovation? +//' @param sparse Use restricted model? +//' @param level CI level to give sparsity. Valid when `prior_type` is 0. +//' @param fit_record MCMC records list +//' @param prior_type Prior type. If 0, use CI. Valid when sparse is true. +//' @param seed_chain Seed for each chain +//' @param include_mean Include constant term? +//' @param stable Filter stable draws +//' @param nthreads OpenMP number of threads +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::List forecast_bvharldlt(int num_chains, int month, int step, Eigen::MatrixXd response_mat, Eigen::MatrixXd HARtrans, + bool sparse, double level, Rcpp::List fit_record, int prior_type, + Eigen::VectorXi seed_chain, bool include_mean, bool stable, int nthreads) { + auto forecaster = std::make_unique>( + num_chains, month, step, response_mat, HARtrans, + sparse, level, fit_record, + seed_chain, include_mean, stable, nthreads + ); + return Rcpp::wrap(forecaster->returnForecast()); +} + +//' Out-of-Sample Forecasting of VAR-SV based on Rolling Window +//' +//' This function conducts an rolling window forecasting of BVAR-SV. +//' +//' @param y Time series data of which columns indicate the variables +//' @param lag VAR order +//' @param num_chains Number of MCMC chains +//' @param num_iter Number of iteration for MCMC +//' @param num_burn Number of burn-in (warm-up) for MCMC +//' @param thinning Thinning +//' @param param_reg SV specification list +//' @param param_prior Prior specification list +//' @param param_intercept Intercept specification list +//' @param param_init Initialization specification list +//' @param get_lpl Compute LPL +//' @param seed_chain Seed for each window and chain in the form of matrix +//' @param seed_forecast Seed for each window forecast +//' @param nthreads Number of threads for openmp +//' @param grp_id Unique group id +//' @param grp_mat Group matrix +//' @param include_mean Constant term +//' @param stable Filter stable draws +//' @param step Integer, Step to forecast +//' @param y_test Evaluation time series data period after `y` +//' @param nthreads Number of threads +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::List roll_bvarldlt(Eigen::MatrixXd y, int lag, int num_chains, int num_iter, int num_burn, int thinning, + bool sparse, double level, Rcpp::List fit_record, + Rcpp::List param_reg, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, + Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, + bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, + bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads) { + auto forecaster = [&]() -> std::unique_ptr> { + if (ggl) { + return std::make_unique>( + y, lag, num_chains, num_iter, num_burn, thinning, + sparse, level, fit_record, + param_reg, param_prior, param_intercept, param_init, prior_type, + grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, + get_lpl, seed_chain, seed_forecast, display_progress, nthreads, true + ); + } + return std::make_unique>( + y, lag, num_chains, num_iter, num_burn, thinning, + sparse, level, fit_record, + param_reg, param_prior, param_intercept, param_init, prior_type, + grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, + get_lpl, seed_chain, seed_forecast, display_progress, nthreads, true + ); + }(); + return forecaster->returnForecast(); +} + +//' Out-of-Sample Forecasting of VAR-SV based on Rolling Window +//' +//' This function conducts an rolling window forecasting of BVAR-SV. +//' +//' @param y Time series data of which columns indicate the variables +//' @param lag VAR order +//' @param num_chains Number of MCMC chains +//' @param num_iter Number of iteration for MCMC +//' @param num_burn Number of burn-in (warm-up) for MCMC +//' @param thinning Thinning +//' @param param_sv SV specification list +//' @param param_prior Prior specification list +//' @param param_intercept Intercept specification list +//' @param param_init Initialization specification list +//' @param get_lpl Compute LPL +//' @param seed_chain Seed for each window and chain in the form of matrix +//' @param seed_forecast Seed for each window forecast +//' @param nthreads Number of threads for openmp +//' @param grp_id Unique group id +//' @param grp_mat Group matrix +//' @param include_mean Constant term +//' @param stable Filter stable draws +//' @param step Integer, Step to forecast +//' @param y_test Evaluation time series data period after `y` +//' @param nthreads Number of threads +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::List roll_bvharldlt(Eigen::MatrixXd y, int week, int month, int num_chains, int num_iter, int num_burn, int thinning, + bool sparse, double level, Rcpp::List fit_record, + Rcpp::List param_reg, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, + Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, + bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, + bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads) { + auto forecaster = [&]() -> std::unique_ptr> { + if (ggl) { + return std::make_unique>( + y, week, month, num_chains, num_iter, num_burn, thinning, + sparse, level, fit_record, + param_reg, param_prior, param_intercept, param_init, prior_type, + grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, + get_lpl, seed_chain, seed_forecast, display_progress, nthreads, true + ); + } + return std::make_unique>( + y, week, month, num_chains, num_iter, num_burn, thinning, + sparse, level, fit_record, + param_reg, param_prior, param_intercept, param_init, prior_type, + grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, + get_lpl, seed_chain, seed_forecast, display_progress, nthreads, true + ); + }(); + return forecaster->returnForecast(); +} + +//' Forecasting predictive density of VAR-SV +//' +//' @param num_chains Number of chains +//' @param var_lag VAR order. +//' @param step Integer, Step to forecast. +//' @param response_mat Response matrix. +//' @param sv Use Innovation? +//' @param sparse Use restricted model? +//' @param level CI level to give sparsity. Valid when `prior_type` is 0. +//' @param fit_record MCMC records list +//' @param prior_type Prior type. If 0, use CI. Valid when sparse is true. +//' @param seed_chain Seed for each chain +//' @param stable Filter stable draws +//' @param include_mean Include constant term? +//' @param nthreads OpenMP number of threads +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::List forecast_bvarsv(int num_chains, int var_lag, int step, Eigen::MatrixXd response_mat, + bool sv, bool sparse, double level, Rcpp::List fit_record, int prior_type, + Eigen::VectorXi seed_chain, bool include_mean, bool stable, int nthreads) { + auto forecaster = std::make_unique>( + num_chains, var_lag, step, response_mat, + sparse, level, fit_record, + seed_chain, include_mean, stable, nthreads, + sv + ); + return Rcpp::wrap(forecaster->returnForecast()); +} + +//' Forecasting Predictive Density of VHAR-SV +//' +//' @param num_chains Number of MCMC chains +//' @param month VHAR month order. +//' @param step Integer, Step to forecast. +//' @param response_mat Response matrix. +//' @param HARtrans VHAR linear transformation matrix +//' @param sv Use Innovation? +//' @param sparse Use restricted model? +//' @param level CI level to give sparsity. Valid when `prior_type` is 0. +//' @param fit_record MCMC records list +//' @param prior_type Prior type. If 0, use CI. Valid when sparse is true. +//' @param seed_chain Seed for each chain +//' @param include_mean Include constant term? +//' @param stable Filter stable draws +//' @param nthreads OpenMP number of threads +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::List forecast_bvharsv(int num_chains, int month, int step, Eigen::MatrixXd response_mat, Eigen::MatrixXd HARtrans, + bool sv, bool sparse, double level, Rcpp::List fit_record, int prior_type, + Eigen::VectorXi seed_chain, bool include_mean, bool stable, int nthreads) { + auto forecaster = std::make_unique>( + num_chains, month, step, response_mat, HARtrans, + sparse, level, fit_record, + seed_chain, include_mean, stable, nthreads, + sv + ); + return Rcpp::wrap(forecaster->returnForecast()); +} + +//' Out-of-Sample Forecasting of VAR-SV based on Rolling Window +//' +//' This function conducts an rolling window forecasting of BVAR-SV. +//' +//' @param y Time series data of which columns indicate the variables +//' @param lag VAR order +//' @param num_chains Number of MCMC chains +//' @param num_iter Number of iteration for MCMC +//' @param num_burn Number of burn-in (warm-up) for MCMC +//' @param thinning Thinning +//' @param param_sv SV specification list +//' @param param_prior Prior specification list +//' @param param_intercept Intercept specification list +//' @param param_init Initialization specification list +//' @param get_lpl Compute LPL +//' @param seed_chain Seed for each window and chain in the form of matrix +//' @param seed_forecast Seed for each window forecast +//' @param nthreads Number of threads for openmp +//' @param grp_id Unique group id +//' @param grp_mat Group matrix +//' @param include_mean Constant term +//' @param stable Filter stable draws +//' @param step Integer, Step to forecast +//' @param y_test Evaluation time series data period after `y` +//' @param nthreads Number of threads +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::List roll_bvarsv(Eigen::MatrixXd y, int lag, int num_chains, int num_iter, int num_burn, int thinning, + bool sv, bool sparse, double level, Rcpp::List fit_record, + Rcpp::List param_sv, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, + Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, + bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, + bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads) { + auto forecaster = [&]() -> std::unique_ptr> { + if (ggl) { + return std::make_unique>( + y, lag, num_chains, num_iter, num_burn, thinning, + sparse, level, fit_record, + param_sv, param_prior, param_intercept, param_init, prior_type, + grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, + get_lpl, seed_chain, seed_forecast, display_progress, nthreads, sv + ); + } + return std::make_unique>( + y, lag, num_chains, num_iter, num_burn, thinning, + sparse, level, fit_record, + param_sv, param_prior, param_intercept, param_init, prior_type, + grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, + get_lpl, seed_chain, seed_forecast, display_progress, nthreads, sv + ); + }(); + return forecaster->returnForecast(); +} + +//' Out-of-Sample Forecasting of VAR-SV based on Rolling Window +//' +//' This function conducts an rolling window forecasting of BVAR-SV. +//' +//' @param y Time series data of which columns indicate the variables +//' @param lag VAR order +//' @param num_chains Number of MCMC chains +//' @param num_iter Number of iteration for MCMC +//' @param num_burn Number of burn-in (warm-up) for MCMC +//' @param thinning Thinning +//' @param param_sv SV specification list +//' @param param_prior Prior specification list +//' @param param_intercept Intercept specification list +//' @param param_init Initialization specification list +//' @param get_lpl Compute LPL +//' @param seed_chain Seed for each window and chain in the form of matrix +//' @param seed_forecast Seed for each window forecast +//' @param nthreads Number of threads for openmp +//' @param grp_id Unique group id +//' @param grp_mat Group matrix +//' @param include_mean Constant term +//' @param stable Filter stable draws +//' @param step Integer, Step to forecast +//' @param y_test Evaluation time series data period after `y` +//' @param nthreads Number of threads +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::List roll_bvharsv(Eigen::MatrixXd y, int week, int month, int num_chains, int num_iter, int num_burn, int thinning, + bool sv, bool sparse, double level, Rcpp::List fit_record, + Rcpp::List param_sv, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, + Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, + bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, + bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads) { + auto forecaster = [&]() -> std::unique_ptr> { + if (ggl) { + return std::make_unique>( + y, week, month, num_chains, num_iter, num_burn, thinning, + sparse, level, fit_record, + param_sv, param_prior, param_intercept, param_init, prior_type, + grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, + get_lpl, seed_chain, seed_forecast, display_progress, nthreads, sv + ); + } + return std::make_unique>( + y, week, month, num_chains, num_iter, num_burn, thinning, + sparse, level, fit_record, + param_sv, param_prior, param_intercept, param_init, prior_type, + grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, + get_lpl, seed_chain, seed_forecast, display_progress, nthreads, sv + ); + }(); + return forecaster->returnForecast(); +} + +//' Out-of-Sample Forecasting of VAR-SV based on Rolling Window +//' +//' This function conducts an rolling window forecasting of BVAR-SV. +//' +//' @param y Time series data of which columns indicate the variables +//' @param lag VAR order +//' @param num_chains Number of MCMC chains +//' @param num_iter Number of iteration for MCMC +//' @param num_burn Number of burn-in (warm-up) for MCMC +//' @param thinning Thinning +//' @param param_sv SV specification list +//' @param param_prior Prior specification list +//' @param param_intercept Intercept specification list +//' @param param_init Initialization specification list +//' @param get_lpl Compute LPL +//' @param seed_chain Seed for each window and chain in the form of matrix +//' @param seed_forecast Seed for each window forecast +//' @param nthreads Number of threads for openmp +//' @param grp_id Unique group id +//' @param grp_mat Group matrix +//' @param include_mean Constant term +//' @param stable Filter stable draws +//' @param step Integer, Step to forecast +//' @param y_test Evaluation time series data period after `y` +//' @param nthreads Number of threads +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::List expand_bvarldlt(Eigen::MatrixXd y, int lag, int num_chains, int num_iter, int num_burn, int thinning, + bool sparse, double level, Rcpp::List fit_record, + Rcpp::List param_reg, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, + Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, + bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, + bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads) { + auto forecaster = [&]() -> std::unique_ptr> { + if (ggl) { + return std::make_unique>( + y, lag, num_chains, num_iter, num_burn, thinning, + sparse, level, fit_record, + param_reg, param_prior, param_intercept, param_init, prior_type, + grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, + get_lpl, seed_chain, seed_forecast, display_progress, nthreads, true + ); + } + return std::make_unique>( + y, lag, num_chains, num_iter, num_burn, thinning, + sparse, level, fit_record, + param_reg, param_prior, param_intercept, param_init, prior_type, + grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, + get_lpl, seed_chain, seed_forecast, display_progress, nthreads, true + ); + }(); + return forecaster->returnForecast(); +} + +//' Out-of-Sample Forecasting of VAR-SV based on Rolling Window +//' +//' This function conducts an rolling window forecasting of BVAR-SV. +//' +//' @param y Time series data of which columns indicate the variables +//' @param lag VAR order +//' @param num_chains Number of MCMC chains +//' @param num_iter Number of iteration for MCMC +//' @param num_burn Number of burn-in (warm-up) for MCMC +//' @param thinning Thinning +//' @param param_sv SV specification list +//' @param param_prior Prior specification list +//' @param param_intercept Intercept specification list +//' @param param_init Initialization specification list +//' @param get_lpl Compute LPL +//' @param seed_chain Seed for each window and chain in the form of matrix +//' @param seed_forecast Seed for each window forecast +//' @param nthreads Number of threads for openmp +//' @param grp_id Unique group id +//' @param grp_mat Group matrix +//' @param include_mean Constant term +//' @param stable Filter stable draws +//' @param step Integer, Step to forecast +//' @param y_test Evaluation time series data period after `y` +//' @param nthreads Number of threads +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::List expand_bvharldlt(Eigen::MatrixXd y, int week, int month, int num_chains, int num_iter, int num_burn, int thinning, + bool sparse, double level, Rcpp::List fit_record, + Rcpp::List param_reg, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, + Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, + bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, + bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads) { + auto forecaster = [&]() -> std::unique_ptr> { + if (ggl) { + return std::make_unique>( + y, week, month, num_chains, num_iter, num_burn, thinning, + sparse, level, fit_record, + param_reg, param_prior, param_intercept, param_init, prior_type, + grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, + get_lpl, seed_chain, seed_forecast, display_progress, nthreads, true + ); + } + return std::make_unique>( + y, week, month, num_chains, num_iter, num_burn, thinning, + sparse, level, fit_record, + param_reg, param_prior, param_intercept, param_init, prior_type, + grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, + get_lpl, seed_chain, seed_forecast, display_progress, nthreads, true + ); + }(); + return forecaster->returnForecast(); +} + +//' Out-of-Sample Forecasting of VAR-SV based on Rolling Window +//' +//' This function conducts an rolling window forecasting of BVAR-SV. +//' +//' @param y Time series data of which columns indicate the variables +//' @param lag VAR order +//' @param num_chains Number of MCMC chains +//' @param num_iter Number of iteration for MCMC +//' @param num_burn Number of burn-in (warm-up) for MCMC +//' @param thinning Thinning +//' @param param_sv SV specification list +//' @param param_prior Prior specification list +//' @param param_intercept Intercept specification list +//' @param param_init Initialization specification list +//' @param get_lpl Compute LPL +//' @param seed_chain Seed for each window and chain in the form of matrix +//' @param seed_forecast Seed for each window forecast +//' @param nthreads Number of threads for openmp +//' @param grp_id Unique group id +//' @param grp_mat Group matrix +//' @param include_mean Constant term +//' @param stable Filter stable draws +//' @param step Integer, Step to forecast +//' @param y_test Evaluation time series data period after `y` +//' @param nthreads Number of threads +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::List expand_bvarsv(Eigen::MatrixXd y, int lag, int num_chains, int num_iter, int num_burn, int thinning, + bool sv, bool sparse, double level, Rcpp::List fit_record, + Rcpp::List param_sv, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, + Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, + bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, + bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads) { + auto forecaster = [&]() -> std::unique_ptr> { + if (ggl) { + return std::make_unique>( + y, lag, num_chains, num_iter, num_burn, thinning, + sparse, level, fit_record, + param_sv, param_prior, param_intercept, param_init, prior_type, + grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, + get_lpl, seed_chain, seed_forecast, display_progress, nthreads, sv + ); + } + return std::make_unique>( + y, lag, num_chains, num_iter, num_burn, thinning, + sparse, level, fit_record, + param_sv, param_prior, param_intercept, param_init, prior_type, + grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, + get_lpl, seed_chain, seed_forecast, display_progress, nthreads, sv + ); + }(); + return forecaster->returnForecast(); +} + +//' Out-of-Sample Forecasting of VAR-SV based on Rolling Window +//' +//' This function conducts an rolling window forecasting of BVAR-SV. +//' +//' @param y Time series data of which columns indicate the variables +//' @param lag VAR order +//' @param num_chains Number of MCMC chains +//' @param num_iter Number of iteration for MCMC +//' @param num_burn Number of burn-in (warm-up) for MCMC +//' @param thinning Thinning +//' @param param_sv SV specification list +//' @param param_prior Prior specification list +//' @param param_intercept Intercept specification list +//' @param param_init Initialization specification list +//' @param get_lpl Compute LPL +//' @param seed_chain Seed for each window and chain in the form of matrix +//' @param seed_forecast Seed for each window forecast +//' @param nthreads Number of threads for openmp +//' @param grp_id Unique group id +//' @param grp_mat Group matrix +//' @param include_mean Constant term +//' @param stable Filter stable draws +//' @param step Integer, Step to forecast +//' @param y_test Evaluation time series data period after `y` +//' @param nthreads Number of threads +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::List expand_bvharsv(Eigen::MatrixXd y, int week, int month, int num_chains, int num_iter, int num_burn, int thinning, + bool sv, bool sparse, double level, Rcpp::List fit_record, + Rcpp::List param_sv, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, int prior_type, bool ggl, + Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, + bool include_mean, bool stable, int step, Eigen::MatrixXd y_test, + bool get_lpl, Eigen::MatrixXi seed_chain, Eigen::VectorXi seed_forecast, bool display_progress, int nthreads) { + auto forecaster = [&]() -> std::unique_ptr> { + if (ggl) { + return std::make_unique>( + y, week, month, num_chains, num_iter, num_burn, thinning, + sparse, level, fit_record, + param_sv, param_prior, param_intercept, param_init, prior_type, + grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, + get_lpl, seed_chain, seed_forecast, display_progress, nthreads, sv + ); + } + return std::make_unique>( + y, week, month, num_chains, num_iter, num_burn, thinning, + sparse, level, fit_record, + param_sv, param_prior, param_intercept, param_init, prior_type, + grp_id, own_id, cross_id, grp_mat, include_mean, stable, step, y_test, + get_lpl, seed_chain, seed_forecast, display_progress, nthreads, sv + ); + }(); + return forecaster->returnForecast(); +} + +// [[Rcpp::export]] +Rcpp::List compute_varldlt_spillover(int lag, int step, Rcpp::List fit_record, bool sparse) { + // auto spillover = bvhar::initialize_spillover(0, lag, step, fit_record, sparse, 0); + // return spillover->returnSpilloverDensity(); + auto spillover = std::make_unique>(lag, step, fit_record, sparse); + return spillover->returnSpillover(); +} + +// [[Rcpp::export]] +Rcpp::List compute_vharldlt_spillover(int week, int month, int step, Rcpp::List fit_record, bool sparse) { + // auto spillover = bvhar::initialize_spillover(0, month, step, fit_record, sparse, 0, NULLOPT, week); + // return spillover->returnSpilloverDensity(); + auto spillover = std::make_unique>(week, month, step, fit_record, sparse); + return spillover->returnSpillover(); +} + +// [[Rcpp::export]] +Rcpp::List dynamic_bvarldlt_spillover(Eigen::MatrixXd y, int window, int step, int num_chains, int num_iter, int num_burn, int thin, bool sparse, + int lag, Rcpp::List param_reg, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, + int prior_type, bool ggl, Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, + bool include_mean, Eigen::MatrixXi seed_chain, int nthreads) { + auto spillover = std::make_unique( + y, window, step, lag, num_chains, num_iter, num_burn, thin, sparse, + param_reg, param_prior, param_intercept, param_init, prior_type, ggl, + grp_id, own_id, cross_id, grp_mat, + include_mean, seed_chain, nthreads + ); + return spillover->returnSpillover(); +} + +// [[Rcpp::export]] +Rcpp::List dynamic_bvharldlt_spillover(Eigen::MatrixXd y, int window, int step, int num_chains, int num_iter, int num_burn, int thin, bool sparse, + int week, int month, Rcpp::List param_reg, Rcpp::List param_prior, Rcpp::List param_intercept, Rcpp::List param_init, + int prior_type, bool ggl, Eigen::VectorXi grp_id, Eigen::VectorXi own_id, Eigen::VectorXi cross_id, Eigen::MatrixXi grp_mat, + bool include_mean, Eigen::MatrixXi seed_chain, int nthreads) { + auto spillover = std::make_unique( + y, window, step, week, month, num_chains, num_iter, num_burn, thin, sparse, + param_reg, param_prior, param_intercept, param_init, prior_type, ggl, + grp_id, own_id, cross_id, grp_mat, + include_mean, seed_chain, nthreads + ); + return spillover->returnSpillover(); +} + +//' Dynamic Total Spillover Index of BVAR-SV +//' +//' @param lag VAR lag. +//' @param window Rolling window size +//' @param step forecast horizon for FEVD +//' @param response_mat Response matrix. +//' @param phi_record Coefficients MCMC record +//' @param h_record log volatility MCMC record +//' @param a_record Contemporaneous coefficients MCMC record +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::List dynamic_bvarsv_spillover(int lag, int step, int num_design, Rcpp::List fit_record, bool sparse, bool include_mean, int nthreads) { + auto spillover = std::make_unique(lag, step, num_design, fit_record, include_mean, sparse, nthreads); + return spillover->returnSpillover(); +} + +//' Dynamic Total Spillover Index of BVHAR-SV +//' +//' @param month VHAR month order. +//' @param window Rolling window size +//' @param step forecast horizon for FEVD +//' @param response_mat Response matrix. +//' @param HARtrans VHAR linear transformation matrix +//' @param phi_record Coefficients MCMC record +//' @param h_record log volatility MCMC record +//' @param a_record Contemporaneous coefficients MCMC record +//' +//' @noRd +// [[Rcpp::export]] +Rcpp::List dynamic_bvharsv_spillover(int week, int month, int step, int num_design, Rcpp::List fit_record, bool sparse, bool include_mean, int nthreads) { + auto spillover = std::make_unique(week, month, step, num_design, fit_record, include_mean, sparse, nthreads); + return spillover->returnSpillover(); +}