From 31f501ab811e6a77e98bc252c9d9be35d8d60bdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Wo=C5=BAniak?= Date: Fri, 2 Aug 2024 01:50:49 +1000 Subject: [PATCH] the last --- index-speaker.html | 120 +++++++++++++++++++++++++---------------- index.html | 120 +++++++++++++++++++++++++---------------- index.qmd | 23 ++++++++ w4UKR_bsvars_Granger.R | 19 +++++++ 4 files changed, 188 insertions(+), 94 deletions(-) diff --git a/index-speaker.html b/index-speaker.html index 6f1e40a..0d737d4 100644 --- a/index-speaker.html +++ b/index-speaker.html @@ -536,6 +536,23 @@

b

- progress bar

+
+

My first steps with bsvars

+

Exercise.

+
    +
  1. Download R script w4UKR_bsvars.R
  2. +
  3. Open the file in your RStudio
  4. +
  5. Execute the code line by line
  6. +
+

You have just estimated your first model using the bsvars package! YAY!

+
    +
  1. Modify the line specifying the model to
  2. +
+
specify_bsvar_t$new(p = 4) |>
+
    +
  1. Run the code line by line
  2. +
+

bsvars models and identification

@@ -829,6 +846,15 @@

…for autoregressive parameters.

  • use the verify_autoregression() function
  • +
    +

    bsvars hypothesis verification

    +

    …for autoregressive parameters.

    +

    Exercise.

    +
      +
    1. Download the R script w4UKR_bsvars_Granger.R
    2. +
    3. Verify the hypothesis of no Granger causality from ttr to gdp
    4. +
    +

    bsvars modeling of monetary policy

    @@ -903,36 +929,36 @@

    Identified shocks.

    bsvars modeling of monetary policy

    Specify and estimate a lower-triangular SVAR-SV.

    -
    library(bsvars)
    -load("soe.rda")
    -soe           = as.matrix(soe)
    -
    -TT            = nrow(soe)
    -lag_order     = 8
    -lag_exogenous = 4
    -T             = TT - max(lag_order, lag_exogenous)
    -
    -exogenous     = matrix(NA, TT - lag_exogenous, 0)
    -for (i in 0:lag_exogenous) {
    -  exogenous   = cbind(exogenous, as.matrix(soe[(lag_exogenous - i + 1):(TT - i), 6:8]))
    -}
    -
    -set.seed(1234)
    -spec          = specify_bsvar_sv$new(
    -  data        = tail(soe[,1:5], T),
    -  p           = lag_order,
    -  exogenous   = tail(exogenous, T)
    -)
    -
    -burn          = estimate(spec, 1e4)
    -post          = estimate(burn, 1e4)
    +
    library(bsvars)
    +load("soe.rda")
    +soe           = as.matrix(soe)
    +
    +TT            = nrow(soe)
    +lag_order     = 8
    +lag_exogenous = 4
    +T             = TT - max(lag_order, lag_exogenous)
    +
    +exogenous     = matrix(NA, TT - lag_exogenous, 0)
    +for (i in 0:lag_exogenous) {
    +  exogenous   = cbind(exogenous, as.matrix(soe[(lag_exogenous - i + 1):(TT - i), 6:8]))
    +}
    +
    +set.seed(1234)
    +spec          = specify_bsvar_sv$new(
    +  data        = tail(soe[,1:5], T),
    +  p           = lag_order,
    +  exogenous   = tail(exogenous, T)
    +)
    +
    +burn          = estimate(spec, 1e4)
    +post          = estimate(burn, 1e4)

    bsvars modeling of monetary policy

    SVAR-SV: Compute and plot impulse responses.

    -
    post |> compute_impulse_responses(horizon = 60) |> plot(probability = 0.68, col = "#0056B9")
    +
    post |> compute_impulse_responses(horizon = 60) |> plot(probability = 0.68, col = "#0056B9")
    @@ -940,7 +966,7 @@

    SVAR-SV: Compute and plot i

    bsvars modeling of monetary policy

    SVAR-SV: Forecast error variance decompositions.

    -
    post |> compute_variance_decompositions(horizon = 60) |> plot(col = bsvars_grad)
    +
    post |> compute_variance_decompositions(horizon = 60) |> plot(col = bsvars_grad)
    @@ -948,7 +974,7 @@

    SVAR-SV: Forecast error

    bsvars modeling of monetary policy

    SVAR-SV: Structural shocks.

    -
    post |> compute_structural_shocks() |> plot(col = "#0056B9")
    +
    post |> compute_structural_shocks() |> plot(col = "#0056B9")
    @@ -956,7 +982,7 @@

    SVAR-SV: Structural shocks.

    bsvars modeling of monetary policy

    SVAR-SV: Shocks’ conditional standard deviations.

    -
    post |> compute_conditional_sd() |> plot(col = "#0056B9")
    +
    post |> compute_conditional_sd() |> plot(col = "#0056B9")
    @@ -964,7 +990,7 @@

    SVAR-SV: Shocks’ cond

    bsvars modeling of monetary policy

    SVAR-SV: Homoskedasticity verification.

    -
    post |> verify_identification() |> summary()
    +
    post |> verify_identification() |> summary()
     **************************************************|
      bsvars: Bayesian Structural Vector Autoregressions|
    @@ -988,7 +1014,7 @@ 

    SVAR-SV: Homoskedasticity verifi

    bsvars modeling of monetary policy

    SVAR-SV: Fitted Values.

    -
    post |> compute_fitted_values() |> plot(col = "#0056B9")
    +
    post |> compute_fitted_values() |> plot(col = "#0056B9")
    @@ -996,9 +1022,9 @@

    SVAR-SV: Fitted Values.

    bsvars modeling of monetary policy

    SVAR-SV: Does foreign sector matter?

    -
    A0 = matrix(NA, 5, 56)
    -A0[,45:56] = 0
    -post |> verify_autoregression(hypothesis = A0) |> summary()
    +
    A0 = matrix(NA, 5, 56)
    +A0[,45:56] = 0
    +post |> verify_autoregression(hypothesis = A0) |> summary()
     **************************************************|
      bsvars: Bayesian Structural Vector Autoregressions|
    @@ -1023,22 +1049,22 @@ 

    SVAR-SV: Does foreign sector matter?

    bsvars modeling of monetary policy

    Specify and estimate a lower-triangular SVAR-t.

    -
    set.seed(1234)
    -spec          = specify_bsvar_t$new(
    -  data        = tail(soe[,1:5], T),
    -  p           = lag_order,
    -  exogenous   = tail(exogenous, T)
    -)
    -
    -burn          = estimate(spec, 1e4)
    -post          = estimate(burn, 1e4)
    +
    set.seed(1234)
    +spec          = specify_bsvar_t$new(
    +  data        = tail(soe[,1:5], T),
    +  p           = lag_order,
    +  exogenous   = tail(exogenous, T)
    +)
    +
    +burn          = estimate(spec, 1e4)
    +post          = estimate(burn, 1e4)

    bsvars modeling of monetary policy

    SVAR-t: Compute and plot impulse responses.

    -
    post |> compute_impulse_responses(horizon = 60) |> plot(probability = 0.68, col = "#0056B9" )
    +
    post |> compute_impulse_responses(horizon = 60) |> plot(probability = 0.68, col = "#0056B9" )
    @@ -1046,7 +1072,7 @@

    SVAR-t: Compute and plot imp

    bsvars modeling of monetary policy

    SVAR-t: Forecast error variance decompositions.

    -
    post |> compute_variance_decompositions(horizon = 60) |> plot(col = bsvars_grad )
    +
    post |> compute_variance_decompositions(horizon = 60) |> plot(col = bsvars_grad )
    @@ -1054,7 +1080,7 @@

    SVAR-t: Forecast error v

    bsvars modeling of monetary policy

    SVAR-t: Structural shocks.

    -
    post |> compute_structural_shocks() |> plot(col = "#0056B9" )
    +
    post |> compute_structural_shocks() |> plot(col = "#0056B9" )
    @@ -1062,7 +1088,7 @@

    SVAR-t: Structural shocks.

    bsvars modeling of monetary policy

    SVAR-t: Normality verification.

    -
    post |> verify_identification() |> summary( )
    +
    post |> verify_identification() |> summary( )
     **************************************************|
      bsvars: Bayesian Structural Vector Autoregressions|
    @@ -1086,7 +1112,7 @@ 

    SVAR-t: Normality verification.

    bsvars modeling of monetary policy

    SVAR-t: Degrees-of-freedom posterior density.

    -
    post$posterior$df |> hist(breaks = 100, col = "#0056B9", border = "#0056B9", bty = "n" )
    +
    post$posterior$df |> hist(breaks = 100, col = "#0056B9", border = "#0056B9", bty = "n" )
    @@ -1094,7 +1120,7 @@

    SVAR-t: Degrees-of-freedom

    bsvars modeling of monetary policy

    SVAR-t: Fitted Values.

    -
    post |> compute_fitted_values() |> plot(col = "#0056B9")
    +
    post |> compute_fitted_values() |> plot(col = "#0056B9")
    diff --git a/index.html b/index.html index f47f63d..f916cc3 100644 --- a/index.html +++ b/index.html @@ -536,6 +536,23 @@

    b

    - progress bar

    +
    +

    My first steps with bsvars

    +

    Exercise.

    +
      +
    1. Download R script w4UKR_bsvars.R
    2. +
    3. Open the file in your RStudio
    4. +
    5. Execute the code line by line
    6. +
    +

    You have just estimated your first model using the bsvars package! YAY!

    +
      +
    1. Modify the line specifying the model to
    2. +
    +
    specify_bsvar_t$new(p = 4) |>
    +
      +
    1. Run the code line by line
    2. +
    +

    bsvars models and identification

    @@ -829,6 +846,15 @@

    …for autoregressive parameters.

  • use the verify_autoregression() function
  • +
    +

    bsvars hypothesis verification

    +

    …for autoregressive parameters.

    +

    Exercise.

    +
      +
    1. Download the R script w4UKR_bsvars_Granger.R
    2. +
    3. Verify the hypothesis of no Granger causality from ttr to gdp
    4. +
    +

    bsvars modeling of monetary policy

    @@ -903,36 +929,36 @@

    Identified shocks.

    bsvars modeling of monetary policy

    Specify and estimate a lower-triangular SVAR-SV.

    -
    library(bsvars)
    -load("soe.rda")
    -soe           = as.matrix(soe)
    -
    -TT            = nrow(soe)
    -lag_order     = 8
    -lag_exogenous = 4
    -T             = TT - max(lag_order, lag_exogenous)
    -
    -exogenous     = matrix(NA, TT - lag_exogenous, 0)
    -for (i in 0:lag_exogenous) {
    -  exogenous   = cbind(exogenous, as.matrix(soe[(lag_exogenous - i + 1):(TT - i), 6:8]))
    -}
    -
    -set.seed(1234)
    -spec          = specify_bsvar_sv$new(
    -  data        = tail(soe[,1:5], T),
    -  p           = lag_order,
    -  exogenous   = tail(exogenous, T)
    -)
    -
    -burn          = estimate(spec, 1e4)
    -post          = estimate(burn, 1e4)
    +
    library(bsvars)
    +load("soe.rda")
    +soe           = as.matrix(soe)
    +
    +TT            = nrow(soe)
    +lag_order     = 8
    +lag_exogenous = 4
    +T             = TT - max(lag_order, lag_exogenous)
    +
    +exogenous     = matrix(NA, TT - lag_exogenous, 0)
    +for (i in 0:lag_exogenous) {
    +  exogenous   = cbind(exogenous, as.matrix(soe[(lag_exogenous - i + 1):(TT - i), 6:8]))
    +}
    +
    +set.seed(1234)
    +spec          = specify_bsvar_sv$new(
    +  data        = tail(soe[,1:5], T),
    +  p           = lag_order,
    +  exogenous   = tail(exogenous, T)
    +)
    +
    +burn          = estimate(spec, 1e4)
    +post          = estimate(burn, 1e4)

    bsvars modeling of monetary policy

    SVAR-SV: Compute and plot impulse responses.

    -
    post |> compute_impulse_responses(horizon = 60) |> plot(probability = 0.68, col = "#0056B9")
    +
    post |> compute_impulse_responses(horizon = 60) |> plot(probability = 0.68, col = "#0056B9")
    @@ -940,7 +966,7 @@

    SVAR-SV: Compute and plot i

    bsvars modeling of monetary policy

    SVAR-SV: Forecast error variance decompositions.

    -
    post |> compute_variance_decompositions(horizon = 60) |> plot(col = bsvars_grad)
    +
    post |> compute_variance_decompositions(horizon = 60) |> plot(col = bsvars_grad)
    @@ -948,7 +974,7 @@

    SVAR-SV: Forecast error

    bsvars modeling of monetary policy

    SVAR-SV: Structural shocks.

    -
    post |> compute_structural_shocks() |> plot(col = "#0056B9")
    +
    post |> compute_structural_shocks() |> plot(col = "#0056B9")
    @@ -956,7 +982,7 @@

    SVAR-SV: Structural shocks.

    bsvars modeling of monetary policy

    SVAR-SV: Shocks’ conditional standard deviations.

    -
    post |> compute_conditional_sd() |> plot(col = "#0056B9")
    +
    post |> compute_conditional_sd() |> plot(col = "#0056B9")
    @@ -964,7 +990,7 @@

    SVAR-SV: Shocks’ cond

    bsvars modeling of monetary policy

    SVAR-SV: Homoskedasticity verification.

    -
    post |> verify_identification() |> summary()
    +
    post |> verify_identification() |> summary()
     **************************************************|
      bsvars: Bayesian Structural Vector Autoregressions|
    @@ -988,7 +1014,7 @@ 

    SVAR-SV: Homoskedasticity verifi

    bsvars modeling of monetary policy

    SVAR-SV: Fitted Values.

    -
    post |> compute_fitted_values() |> plot(col = "#0056B9")
    +
    post |> compute_fitted_values() |> plot(col = "#0056B9")
    @@ -996,9 +1022,9 @@

    SVAR-SV: Fitted Values.

    bsvars modeling of monetary policy

    SVAR-SV: Does foreign sector matter?

    -
    A0 = matrix(NA, 5, 56)
    -A0[,45:56] = 0
    -post |> verify_autoregression(hypothesis = A0) |> summary()
    +
    A0 = matrix(NA, 5, 56)
    +A0[,45:56] = 0
    +post |> verify_autoregression(hypothesis = A0) |> summary()
     **************************************************|
      bsvars: Bayesian Structural Vector Autoregressions|
    @@ -1023,22 +1049,22 @@ 

    SVAR-SV: Does foreign sector matter?

    bsvars modeling of monetary policy

    Specify and estimate a lower-triangular SVAR-t.

    -
    set.seed(1234)
    -spec          = specify_bsvar_t$new(
    -  data        = tail(soe[,1:5], T),
    -  p           = lag_order,
    -  exogenous   = tail(exogenous, T)
    -)
    -
    -burn          = estimate(spec, 1e4)
    -post          = estimate(burn, 1e4)
    +
    set.seed(1234)
    +spec          = specify_bsvar_t$new(
    +  data        = tail(soe[,1:5], T),
    +  p           = lag_order,
    +  exogenous   = tail(exogenous, T)
    +)
    +
    +burn          = estimate(spec, 1e4)
    +post          = estimate(burn, 1e4)

    bsvars modeling of monetary policy

    SVAR-t: Compute and plot impulse responses.

    -
    post |> compute_impulse_responses(horizon = 60) |> plot(probability = 0.68, col = "#0056B9" )
    +
    post |> compute_impulse_responses(horizon = 60) |> plot(probability = 0.68, col = "#0056B9" )
    @@ -1046,7 +1072,7 @@

    SVAR-t: Compute and plot imp

    bsvars modeling of monetary policy

    SVAR-t: Forecast error variance decompositions.

    -
    post |> compute_variance_decompositions(horizon = 60) |> plot(col = bsvars_grad )
    +
    post |> compute_variance_decompositions(horizon = 60) |> plot(col = bsvars_grad )
    @@ -1054,7 +1080,7 @@

    SVAR-t: Forecast error v

    bsvars modeling of monetary policy

    SVAR-t: Structural shocks.

    -
    post |> compute_structural_shocks() |> plot(col = "#0056B9" )
    +
    post |> compute_structural_shocks() |> plot(col = "#0056B9" )
    @@ -1062,7 +1088,7 @@

    SVAR-t: Structural shocks.

    bsvars modeling of monetary policy

    SVAR-t: Normality verification.

    -
    post |> verify_identification() |> summary( )
    +
    post |> verify_identification() |> summary( )
     **************************************************|
      bsvars: Bayesian Structural Vector Autoregressions|
    @@ -1086,7 +1112,7 @@ 

    SVAR-t: Normality verification.

    bsvars modeling of monetary policy

    SVAR-t: Degrees-of-freedom posterior density.

    -
    post$posterior$df |> hist(breaks = 100, col = "#0056B9", border = "#0056B9", bty = "n" )
    +
    post$posterior$df |> hist(breaks = 100, col = "#0056B9", border = "#0056B9", bty = "n" )
    @@ -1094,7 +1120,7 @@

    SVAR-t: Degrees-of-freedom

    bsvars modeling of monetary policy

    SVAR-t: Fitted Values.

    -
    post |> compute_fitted_values() |> plot(col = "#0056B9")
    +
    post |> compute_fitted_values() |> plot(col = "#0056B9")
    diff --git a/index.qmd b/index.qmd index 3ca2f0e..30ef3fd 100644 --- a/index.qmd +++ b/index.qmd @@ -259,6 +259,21 @@ spec2 |> +## My first steps with [bsvars](https://cran.r-project.org/package=bsvars){style="color:#FFD800;"} + +### Exercise. + +1. Download **R** script [w4UKR_bsvars.R](https://github.com/bsvars/2024-08-bsvars-w4UKR/blob/main/w4UKR_bsvars.R) +2. Open the file in your **RStudio** +3. Execute the code line by line + +#### You have just estimated your first model using the **bsvars** package! YAY! + +4. Modify the line specifying the model to +``` +specify_bsvar_t$new(p = 4) |> +``` +5. Run the code line by line @@ -736,6 +751,14 @@ $$ $$ +## [bsvars](https://cran.r-project.org/package=bsvars){style="color:#FFD800;"} hypothesis verification + +### ...for autoregressive parameters. + +### Exercise. + +1. Download the **R** script [w4UKR_bsvars_Granger.R](https://github.com/bsvars/2024-08-bsvars-w4UKR/blob/main/w4UKR_bsvars_Granger.R) +2. Verify the hypothesis of no Granger causality from ttr to gdp ## [bsvars](https://cran.r-project.org/package=bsvars){style="color:#FFD800;"} modeling of monetary policy {background-color="#0056B9"} diff --git a/w4UKR_bsvars_Granger.R b/w4UKR_bsvars_Granger.R index e69de29..4a8c1b0 100644 --- a/w4UKR_bsvars_Granger.R +++ b/w4UKR_bsvars_Granger.R @@ -0,0 +1,19 @@ + + +library(bsvars) + +# upload data +data(us_fiscal_lsuw) # upload dependent variables + +set.seed(123) +us_fiscal_lsuw |> + specify_bsvar$new() |> + estimate(S = 1000) |> + estimate(S = 2000) -> post + +post$last_draw$starting_values$A # study the structure of the A matrix + +# verify no Granger causality from ttr to gdp +H0 = matrix(NA, 3, 4) +H0[,] = 0 # modify this line to set appropriate restriction +post |> verify_autoregression(hypothesis = H0) |> summary()