Skip to content

Commit

Permalink
Merge pull request #36 from bsvars/31-more-user-friendly-things
Browse files Browse the repository at this point in the history
31 more user friendly things
  • Loading branch information
adamwang15 authored Jul 16, 2024
2 parents 719c2db + b140100 commit 77d7e85
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 85 deletions.
51 changes: 23 additions & 28 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,35 @@ devtools::install_github("bsvars/bsvarSIGNs")

# Example

A repliaction of Antolín-Díaz and Rubio-Ramírez (2018).
A replication of Arias, Rubio-Ramírez and Waggoner (2018).

```r
data(optimism)

# optimism shock
# no effect on productivity + positive effect on stock prices
sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5)

specification = specify_bsvarSIGN$new(optimism * 100,
p = 4,
sign_irf = sign_irf)

posterior = estimate(specification, S = 100)
irf = compute_impulse_responses(posterior, horizon = 40)
plot(irf, probability = 0.68)
```

A replication of Antolín-Díaz and Rubio-Ramírez (2018).

```r
data(monetary)

# contractionary monetary policy shock
sign_irf = matrix(0, 6, 6)
sign_irf[, 1] = c(0, -1, -1, 0, -1, 1)
sign_irf = array(sign_irf, dim = c(6, 6, 5))
sign_irf = matrix(NA, 6, 6)
sign_irf[, 1] = c(NA, -1, -1, NA, -1, 1)
sign_irf = array(sign_irf, dim = c(6, 6, 6))

# in October 1979 the shock
# in October 1979
sign_narrative = rbind(c(1, 1, NA, 1, 166, 0), # is positive
c(3, 1, 6, 1, 166, 0)) # greatest historical decomposition

Expand All @@ -53,26 +71,3 @@ irf = compute_impulse_responses(posterior, horizon = 60)
plot(irf, probability = 0.68)
```

A repliaction of Arias, Rubio-Ramírez and Waggoner (2018).

```r
data(optimism)

# optimism shock
# no contemporaneous effect on productivity
zero_irf = matrix(0, nrow = 5, ncol = 5)
zero_irf[1, 1] = 1
# positive contemporaneous effect on stock prices
sign_irf = array(0, dim = c(5, 5, 1))
sign_irf[2, 1, 1] = 1

specification = specify_bsvarSIGN$new(optimism * 100,
p = 4,
sign_irf = sign_irf,
zero_irf = zero_irf)

posterior = estimate(specification, S = 100)
irf = compute_impulse_responses(posterior, horizon = 40)
plot(irf, probability = 0.68)
```

51 changes: 23 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,35 @@ devtools::install_github("bsvars/bsvarSIGNs")

# Example

A repliaction of Antolín-Díaz and Rubio-Ramírez (2018).
A replication of Arias, Rubio-Ramírez and Waggoner (2018).

``` r
data(optimism)

# optimism shock
# no effect on productivity + positive effect on stock prices
sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5)

specification = specify_bsvarSIGN$new(optimism * 100,
p = 4,
sign_irf = sign_irf)

posterior = estimate(specification, S = 100)
irf = compute_impulse_responses(posterior, horizon = 40)
plot(irf, probability = 0.68)
```

A replication of Antolín-Díaz and Rubio-Ramírez (2018).

``` r
data(monetary)

# contractionary monetary policy shock
sign_irf = matrix(0, 6, 6)
sign_irf[, 1] = c(0, -1, -1, 0, -1, 1)
sign_irf = array(sign_irf, dim = c(6, 6, 5))
sign_irf = matrix(NA, 6, 6)
sign_irf[, 1] = c(NA, -1, -1, NA, -1, 1)
sign_irf = array(sign_irf, dim = c(6, 6, 6))

# in October 1979 the shock
# in October 1979
sign_narrative = rbind(c(1, 1, NA, 1, 166, 0), # is positive
c(3, 1, 6, 1, 166, 0)) # greatest historical decomposition

Expand All @@ -41,26 +59,3 @@ posterior = estimate(specification, S = 100)
irf = compute_impulse_responses(posterior, horizon = 60)
plot(irf, probability = 0.68)
```

A repliaction of Arias, Rubio-Ramírez and Waggoner (2018).

``` r
data(optimism)

# optimism shock
# no contemporaneous effect on productivity
zero_irf = matrix(0, nrow = 5, ncol = 5)
zero_irf[1, 1] = 1
# positive contemporaneous effect on stock prices
sign_irf = array(0, dim = c(5, 5, 1))
sign_irf[2, 1, 1] = 1

specification = specify_bsvarSIGN$new(optimism * 100,
p = 4,
sign_irf = sign_irf,
zero_irf = zero_irf)

posterior = estimate(specification, S = 100)
irf = compute_impulse_responses(posterior, horizon = 40)
plot(irf, probability = 0.68)
```
65 changes: 37 additions & 28 deletions src/bsvars_sign.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ using namespace Rcpp;
using namespace arma;


/***
All notations in the C++ code except for compute.cpp and forecast_bsvarSIGNs.cpp
are consistent with the notations in the papers:
Antolín-Díaz and Rubio-Ramírez (2018) and Arias, Rubio-Ramírez and Waggoner (2018)
which are different from the notations in the R code.
***/


// [[Rcpp::interfaces(cpp)]]
// [[Rcpp::export]]
Rcpp::List bsvar_sign_cpp(
Expand Down Expand Up @@ -47,7 +55,7 @@ Rcpp::List bsvar_sign_cpp(
if (show_progress) {
Rcout << "**************************************************|" << endl;
Rcout << " bsvarSIGNs: Bayesian Structural VAR with zero, |" << endl;
Rcout << " sign and narrative restrictions |" << endl;
Rcout << " sign and narrative restrictions |" << endl;
Rcout << "**************************************************|" << endl;
// Rcout << " Gibbs sampler for the SVAR model |" << endl;
// Rcout << "**************************************************|" << endl;
Expand Down Expand Up @@ -96,32 +104,36 @@ Rcpp::List bsvar_sign_cpp(
// #pragma omp parallel for private(hyper, mu, delta, lambda, psi, prior_V, prior_S, Ystar, Xstar, Yplus, Xplus, result, post_B, post_V, post_S, Sigma, chol_Sigma, B, h_invp, Q, shocks, w)
for (int s = 0; s < S; s++) {

w = 0;
hyper = hypers.col(randi(distr_param(0, S_hyper)));
mu = hyper(0);
delta = hyper(1);
lambda = hyper(2);
psi = hyper.rows(3, N + 2);

// update Minnesota prior
prior_V = diagmat(prior_v %
join_vert(lambda * lambda * repmat(1 / psi, p, 1),
ones<vec>(K - N * p)));
prior_S = diagmat(psi);

// update dummy observation prior
Ystar = join_vert(Ysoc / mu, Ysur / delta);
Xstar = join_vert(Xsoc / mu, Xsur / delta);
Yplus = join_vert(Ystar, Y);
Xplus = join_vert(Xstar, X);

// posterior parameters
result = niw_cpp(Yplus, Xplus, prior_B, prior_V, prior_S, prior_nu);
post_B = result(0);
post_V = result(1);
post_S = result(2);
post_nu = as_scalar(result(3));

w = 0;

while (w == 0) {
hyper = hypers.col(randi(distr_param(0, S_hyper)));
mu = hyper(0);
delta = hyper(1);
lambda = hyper(2);
psi = hyper.rows(3, N + 2);

// update Minnesota prior
prior_V = diagmat(prior_v %
join_vert(lambda * lambda * repmat(1 / psi, p, 1),
ones<vec>(K - N * p)));
prior_S = diagmat(psi);

// update dummy observation prior
Ystar = join_vert(Ysoc / mu, Ysur / delta);
Xstar = join_vert(Xsoc / mu, Xsur / delta);
Yplus = join_vert(Ystar, Y);
Xplus = join_vert(Xstar, X);

// posterior parameters
result = niw_cpp(Yplus, Xplus, prior_B, prior_V, prior_S, prior_nu);
post_B = result(0);
post_V = result(1);
post_S = result(2);
post_nu = as_scalar(result(3));
checkUserInterrupt();

// sample reduced-form parameters
Sigma = iwishrnd(post_S, post_nu);
Expand All @@ -145,9 +157,6 @@ Rcpp::List bsvar_sign_cpp(
posterior_Theta0.slice(s) = chol_Sigma * Q;
posterior_shocks.slice(s) = shocks;

// Check for user interrupts
if (s % 200 == 0) checkUserInterrupt();

// Increment progress bar
if (any(prog_rep_points == s)) bar.increment();

Expand Down
2 changes: 1 addition & 1 deletion src/restrictions_narrative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ double weight_narrative(
const arma::cube& irf
) {

const int M = 1e+04; // number of draws to approximate normal distribution
const int M = 1e+3; // number of draws to approximate normal distribution

double n_success = 1.0e-15;

Expand Down

0 comments on commit 77d7e85

Please sign in to comment.