Skip to content

Commit

Permalink
Merge pull request #4 from bsvars/2-gibbs-sampler-for-the-basic-model
Browse files Browse the repository at this point in the history
2 gibbs sampler for the basic model
  • Loading branch information
donotdespair authored May 11, 2024
2 parents 774ae93 + b7a7d34 commit 58c8d1f
Show file tree
Hide file tree
Showing 22 changed files with 2,403 additions and 6 deletions.
11 changes: 8 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@ Title: Forecasting with Bayesian Hierarchical Panel Vector Autoregressions
Description: Forecasting a multi-country time series panel data using Bayesian Vector Autoregressions with a three-level country-global hierarchical prior structure.
Version: 0.0.1.9000
Date: 2024-04-27
Author: c(person(given="Tomasz", family="Woźniak", email="wozniak.tom@pm.me", role = c("aut", "cre"), comment = c(ORCID = "0000-0003-2212-2378")), person(given="Miguel", family="Sanchez-Martinez", role = "ctb"), person(given="International Labour Organization", role = "cph"))
Author: c(person(given="Tomasz", family="Woźniak", email="wozniak.tom@pm.me", role =
c("aut", "cre"), comment = c(ORCID = "0000-0003-2212-2378")),
person(given="Miguel", family="Sanchez-Martinez", role = "ctb"),
person(given="International Labour Organization", role = "cph"))
Maintainer: Tomasz Woźniak <wozniak.tom@pm.me>
License: GPL (>= 3)
Depends:
R (>= 2.10),
bsvars
Imports:
R6,
Rcpp (>= 1.0.12),
RcppProgress,
RcppTN
LinkingTo: bsvars, Rcpp, RcppArmadillo, RcppProgress, RcppTN
Suggests: tinytest
LazyData: true
Encoding: UTF-8
RoxygenNote: 7.3.1
Depends:
R (>= 2.10)
11 changes: 11 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Generated by roxygen2: do not edit by hand

S3method(estimate,BVARPANEL)
S3method(estimate,PosteriorBVARPANEL)
export(specify_bvarPANEL)
export(specify_panel_data_matrices)
export(specify_posterior_bvarPANEL)
export(specify_prior_bvarPANEL)
export(specify_starting_values_bvarPANEL)
import(RcppProgress)
importFrom(R6,R6Class)
importFrom(Rcpp,sourceCpp)
importFrom(RcppTN,dtn)
importFrom(RcppTN,rtn)
importFrom(bsvars,estimate)
useDynLib(bvarPANELs, .registration = TRUE)
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# bvarPANELs 0.0.1.9000

1. This package is alive! [#1](https://github.com/bsvars/bvarPANELs/issues/1)
2. The package includes the Gibbs sampler for a Bayesian Hierarchical Panel Vector Autoregression model. [#2](https://github.com/bsvars/bvarPANELs/issues/2)
3. The package includes labour market dynamic panel data. [#3](https://github.com/bsvars/bvarPANELs/issues/3)
36 changes: 36 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,43 @@
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

bvarPANEL <- function(S, Y, X, prior, starting_values, thin = 100L, show_progress = TRUE) {
.Call(`_bvarPANELs_bvarPANEL`, S, Y, X, prior, starting_values, thin, show_progress)
}

rmniw1 <- function(A, V, S, nu) {
.Call(`_bvarPANELs_rmniw1`, A, V, S, nu)
}

sample_m <- function(aux_A, aux_V, aux_s, aux_w, prior) {
.Call(`_bvarPANELs_sample_m`, aux_A, aux_V, aux_s, aux_w, prior)
}

sample_w <- function(aux_V, prior) {
.Call(`_bvarPANELs_sample_w`, aux_V, prior)
}

sample_s <- function(aux_A, aux_V, aux_Sigma, aux_m, prior) {
.Call(`_bvarPANELs_sample_s`, aux_A, aux_V, aux_Sigma, aux_m, prior)
}

log_kernel_nu <- function(aux_nu, aux_Sigma_c, aux_Sigma, prior_lambda, C, N, K) {
.Call(`_bvarPANELs_log_kernel_nu`, aux_nu, aux_Sigma_c, aux_Sigma, prior_lambda, C, N, K)
}

sample_nu <- function(aux_nu, aux_Sigma_c, aux_Sigma, prior) {
.Call(`_bvarPANELs_sample_nu`, aux_nu, aux_Sigma_c, aux_Sigma, prior)
}

sample_Sigma <- function(aux_Sigma_c_inv, aux_s, aux_nu, prior) {
.Call(`_bvarPANELs_sample_Sigma`, aux_Sigma_c_inv, aux_s, aux_nu, prior)
}

sample_AV <- function(aux_A_c, aux_Sigma_c_inv, aux_s, aux_m, aux_w, prior) {
.Call(`_bvarPANELs_sample_AV`, aux_A_c, aux_Sigma_c_inv, aux_s, aux_m, aux_w, prior)
}

sample_A_c_Sigma_c <- function(Y_c, X_c, aux_A, aux_V, aux_Sigma, aux_nu) {
.Call(`_bvarPANELs_sample_A_c_Sigma_c`, Y_c, X_c, aux_A, aux_V, aux_Sigma, aux_nu)
}

9 changes: 9 additions & 0 deletions R/bvarPANELs-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,18 @@
#' @aliases bvarPANELs-package bvarPANELs
#' @docType package
#' @useDynLib bvarPANELs, .registration = TRUE
#' @importFrom bsvars estimate
#' @importFrom Rcpp sourceCpp
#' @importFrom R6 R6Class
#' @importFrom RcppTN rtn dtn
#' @import RcppProgress
#' @note This package is currently in active development.
#' @author Tomasz Woźniak \email{wozniak.tom@pm.me}
#' @keywords package models ts
#' #' @examples
#' data(ilo_cubic_panel) # load the data
#' set.seed(123)
#' specification = specify_bvarPANEL$new(ilo_cubic_panel) # specify the model
#' burn_in = estimate(specification, 10) # run the burn-in
#' posterior = estimate(burn_in, 10) # estimate the model
"_PACKAGE"
94 changes: 94 additions & 0 deletions R/estimate.bvarPANEL.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@

#' @title Bayesian estimation of a Bayesian Hierarchical Panel Vector
#' Autoregression using Gibbs sampler
#'
#' @description Estimates the Bayesian Hierarchical Panel VAR using the Gibbs
#' sampler proposed by Woźniak (2024).
#'
#' @details
#' The homoskedastic SVAR model is given by the reduced form equation:
#' \deqn{Y_c = A_cX_c + E_c}
#' where \eqn{Y_c} is an \code{T_c x N} matrix of dependent variables for
#' country \code{c}, \eqn{X_c} is a \code{T_c x K} matrix of explanatory
#' variables, \eqn{E_c} is an \code{T_c x N} matrix of error terms, and
#' \eqn{A_c} is an \code{NxK} matrix of country-specific autoregressive slope
#' coefficients and parameters on deterministic terms in \eqn{X_c}.
#'
#' @param specification an object of class BVARPANEL generated using the
#' \code{specify_bvarPANEL$new()} function.
#' @param S a positive integer, the number of posterior draws to be generated
#' @param thin a positive integer, specifying the frequency of MCMC output thinning
#' @param show_progress a logical value, if \code{TRUE} the estimation progress
#' bar is visible
#'
#' @return An object of class PosteriorBVARPANEL containing the Bayesian
#' estimation output and containing two elements:
#'
#' \code{posterior} a list with a collection of \code{S} draws from the
#' posterior distribution generated via Gibbs sampler.
#' \code{last_draw} an object of class BVARPANEL with the last draw of the
#' current MCMC run as the starting value to be passed to the continuation of
#' the MCMC estimation using the \code{estimate()} method.
#'
#' @seealso \code{\link{specify_bvarPANEL}}, \code{\link{specify_posterior_bvarPANEL}}
#'
#' @author Tomasz Woźniak \email{wozniak.tom@pm.me}
#'
#' @method estimate BVARPANEL
#'
#' @examples
#' data(ilo_cubic_panel) # load the data
#' set.seed(123)
#' specification = specify_bvarPANEL$new(ilo_cubic_panel) # specify the model
#' burn_in = estimate(specification, 10) # run the burn-in
#' posterior = estimate(burn_in, 10) # estimate the model
#'
#' @export
estimate.BVARPANEL <- function(
specification,
S,
thin = 10L,
show_progress = TRUE
) {

# get the inputs to estimation
prior = specification$prior$get_prior()
starting_values = specification$starting_values$get_starting_values()
data_matrices = specification$data_matrices$get_data_matrices()

# estimation
qqq = .Call(`_bvarPANELs_bvarPANEL`, S, data_matrices$Y, data_matrices$X, prior, starting_values, thin, show_progress)

specification$starting_values$set_starting_values(qqq$last_draw)
output = specify_posterior_bvarPANEL$new(specification, qqq$posterior)

return(output)
} # END estimate.BVARPANEL



#' @inherit estimate.BVARPANEL
#'
#' @method estimate PosteriorBVARPANEL
#'
#' @param specification an object of class PosteriorBVARPANEL generated using
#' the \code{estimate.BVARPANEL()} function. This setup facilitates the
#' continuation of the MCMC sampling starting from the last draw of the previous
#' run.
#'
#' @export
estimate.PosteriorBVARPANEL <- function(specification, S, thin = 10, show_progress = TRUE) {

# get the inputs to estimation
prior = specification$last_draw$prior$get_prior()
starting_values = specification$last_draw$starting_values$get_starting_values()
data_matrices = specification$last_draw$data_matrices$get_data_matrices()

# estimation
qqq = .Call(`_bvarPANELs_bvarPANEL`, S, data_matrices$Y, data_matrices$X, prior, starting_values, thin, show_progress)

specification$last_draw$starting_values$set_starting_values(qqq$last_draw)
output = specify_posterior_bvarPANEL$new(specification$last_draw, qqq$posterior)

return(output)
} # END estimate.PosteriorBSVAR
Loading

0 comments on commit 58c8d1f

Please sign in to comment.