-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from bsvars/23-update-the-forecast-method
23 update the forecast method
- Loading branch information
Showing
8 changed files
with
409 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
|
||
#' @title Forecasting using Structural Vector Autoregression | ||
#' | ||
#' @description Samples from the joint predictive density of all of the dependent | ||
#' variables for models from packages \pkg{bsvars}, \pkg{bsvarSIGNs} or | ||
#' \pkg{bvarPANELs} at forecast horizons from 1 to \code{horizon} specified as | ||
#' an argument of the function. | ||
#' | ||
#' @method forecast PosteriorBSVARSIGN | ||
#' @param posterior posterior estimation outcome - an object of class | ||
#' \code{PosteriorBSVARSIGN} obtained by running the \code{estimate} function. | ||
#' @param horizon a positive integer, specifying the forecasting horizon. | ||
#' @param exogenous_forecast a matrix of dimension \code{horizon x d} containing | ||
#' forecasted values of the exogenous variables. | ||
#' @param conditional_forecast a \code{horizon x N} matrix with forecasted values | ||
#' for selected variables. It should only contain \code{numeric} or \code{NA} | ||
#' values. The entries with \code{NA} values correspond to the values that are | ||
#' forecasted conditionally on the realisations provided as \code{numeric} values. | ||
#' | ||
#' @return A list of class \code{Forecasts} containing the | ||
#' draws from the predictive density and data. The output list includes element: | ||
#' | ||
#' \describe{ | ||
#' \item{forecasts}{an \code{NxhorizonxS} array with the draws from predictive density} | ||
#' \item{Y}{an \eqn{NxT} matrix with the data on dependent variables} | ||
#' } | ||
#' | ||
#' @author Tomasz Woźniak \email{[email protected]} and Xiaolei Wang \email{[email protected]} | ||
#' | ||
#' @examples | ||
#' # upload data | ||
#' data(oil) | ||
#' | ||
#' # specify the model and set seed | ||
#' set.seed(123) | ||
#' sign_irf = array(matrix(c(-1, -1, 1, rep(0, 6)), nrow = 3), dim = c(3, 3, 1)) | ||
#' specification = specify_bsvarSIGN$new(oil, p = 12, sign_irf = sign_irf) | ||
#' | ||
#' # estimate the model | ||
#' posterior = estimate(specification, 20) | ||
#' | ||
#' # sample from predictive density 1 year ahead | ||
#' predictive = forecast(posterior, 4) | ||
#' | ||
#' # workflow with the pipe |> | ||
#' ############################################################ | ||
#' set.seed(123) | ||
#' oil |> | ||
#' specify_bsvarSIGN$new(p = 12, sign_irf = sign_irf) |> | ||
#' estimate(S = 20) |> | ||
#' forecast(horizon = 4) -> predictive | ||
#' | ||
#' # conditional forecasting 2 quarters ahead conditioning on | ||
#' # provided future values for the Gross Domestic Product | ||
#' ############################################################ | ||
#' cf = matrix(NA , 2, 3) | ||
#' cf[,3] = tail(oil, 1)[3] # conditional forecasts equal to the last gdp observation | ||
#' predictive = forecast(posterior, 2, conditional_forecast = cf) | ||
#' | ||
#' # workflow with the pipe |> | ||
#' ############################################################ | ||
#' set.seed(123) | ||
#' oil |> | ||
#' specify_bsvarSIGN$new(p = 12, sign_irf = sign_irf) |> | ||
#' estimate(S = 10) |> | ||
#' forecast(horizon = 2, conditional_forecast = cf) -> predictive | ||
#' | ||
#' @export | ||
forecast.PosteriorBSVARSIGN = function( | ||
posterior, | ||
horizon = 1, | ||
exogenous_forecast = NULL, | ||
conditional_forecast = NULL | ||
) { | ||
|
||
posterior_Sigma = posterior$posterior$Sigma | ||
posterior_A = posterior$posterior$A | ||
T = nrow(posterior$last_draw$data_matrices$X) | ||
X_T = posterior$last_draw$data_matrices$X[T,] | ||
Y = posterior$last_draw$data_matrices$Y | ||
|
||
N = nrow(posterior_Sigma) | ||
K = length(X_T) | ||
d = K - N * posterior$last_draw$p - 1 | ||
S = dim(posterior_Sigma)[3] | ||
|
||
# prepare forecasting with exogenous variables | ||
if (d == 0 ) { | ||
exogenous_forecast = matrix(NA, horizon, 1) | ||
} else { | ||
stopifnot("Forecasted values of exogenous variables are missing." = (d > 0) & !is.null(exogenous_forecast)) | ||
stopifnot("The matrix of exogenous_forecast does not have a correct number of columns." = ncol(exogenous_forecast) == d) | ||
stopifnot("Provide exogenous_forecast for all forecast periods specified by argument horizon." = nrow(exogenous_forecast) == horizon) | ||
stopifnot("Argument exogenous has to be a matrix." = is.matrix(exogenous_forecast) & is.numeric(exogenous_forecast)) | ||
stopifnot("Argument exogenous cannot include missing values." = sum(is.na(exogenous_forecast)) == 0 ) | ||
} | ||
|
||
# prepare forecasting with conditional forecasts | ||
if ( is.null(conditional_forecast) ) { | ||
# this will not be used for forecasting, but needs to be provided | ||
conditional_forecast = matrix(NA, horizon, N) | ||
} else { | ||
stopifnot("Argument conditional_forecast must be a matrix with numeric values." | ||
= is.matrix(conditional_forecast) & is.numeric(conditional_forecast) | ||
) | ||
stopifnot("Argument conditional_forecast must have the number of rows equal to | ||
the value of argument horizon." | ||
= nrow(conditional_forecast) == horizon | ||
) | ||
stopifnot("Argument conditional_forecast must have the number of columns | ||
equal to the number of columns in the used data." | ||
= ncol(conditional_forecast) == N | ||
) | ||
} | ||
|
||
# perform forecasting | ||
for_y = .Call(`_bsvarSIGNs_forecast_bsvarSIGNs`, | ||
posterior_Sigma, | ||
posterior_A, | ||
X_T, | ||
exogenous_forecast, | ||
conditional_forecast, | ||
horizon | ||
) # END .Call | ||
|
||
fore = list() | ||
fore$forecasts = for_y | ||
fore$Y = Y | ||
class(fore) = "Forecasts" | ||
|
||
return(fore) | ||
} # END forecast.PosteriorBSVARSIGN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
data(oil) | ||
|
||
# for bsvar | ||
set.seed(1) | ||
suppressMessages( | ||
specification_no1 <- specify_bsvarSIGN$new(oil, p = 1) | ||
) | ||
run_no1 <- estimate(specification_no1, 3, 1, show_progress = FALSE) | ||
ff <- forecast(run_no1, horizon = 2) | ||
|
||
set.seed(1) | ||
suppressMessages( | ||
ff2 <- oil |> | ||
specify_bsvarSIGN$new(p = 1) |> | ||
estimate(S = 3, thin = 1, show_progress = FALSE) |> | ||
forecast(horizon = 2) | ||
) | ||
|
||
|
||
expect_identical( | ||
ff$forecasts[1,1,1], ff2$forecasts[1,1,1], | ||
info = "forecast: forecast identical for normal and pipe workflow." | ||
) | ||
|
||
expect_true( | ||
is.numeric(ff$forecasts) & is.array(ff$forecasts), | ||
info = "forecast: returns numeric array." | ||
) | ||
|
||
|
||
expect_error( | ||
specify_bsvar$new(us_fiscal_lsuw) |> forecast(horizon = 3), | ||
info = "forecast: wrong input provided." | ||
) | ||
|
||
expect_error( | ||
forecast(run_no1, horizon = 1.5), | ||
info = "forecast: specify horizon as integer." | ||
) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.