Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when mixing zero and sign restrictions #56

Open
giannilmbd opened this issue Nov 21, 2024 · 7 comments
Open

Error when mixing zero and sign restrictions #56

giannilmbd opened this issue Nov 21, 2024 · 7 comments
Assignees

Comments

@giannilmbd
Copy link

Hi,

I get this error

**************************************************|
0%   10   20   30   40   50   60   70   80   90   100%
[----|----|----|----|----|----|----|----|----|----|
**************************************************|
Error in estimate.BSVARSIGN(specification, S = n_draws) : 
  matrix multiplication: incompatible matrix dimensions: 0x0 and 6x1

when I mix sign and zero restrictions eg like

sr <- matrix(c(
#"House.Price","CPI","ois3mrg","GDP",'YCslope','CHTWI'
  1 ,   NA, NA,  NA,-1,NA,#"House.Price"
  NA,   1, 0, NA,-1,NA,# "CPI'
  NA,   NA,  1,  NA,NA,NA,# ois3mrg
  NA,   NA, 0,  1,-1,-NA,# "GDP"
  NA,   1,  -1,  1,1,NA,# "YCslope
   NA,   NA,  1,  NA,1,1 # CHTWI
), nrow = n_var, byrow = TRUE)

Note that I also have narrative restrictions, in case it matters.

When I replace the 0s with NA, the program works fine.

I'm using

> packageVersion("bsvarSIGNs")
[1] ‘1.0.1’
> packageVersion("bsvars")
[1] ‘3.1’

Thanks and best
Gianni

@giannilmbd
Copy link
Author

Hi,
I've experimented and if the column (and corresponding row) with the zeros is moved first (i.e. order of variables and shocks changed) the code works as expected.
Cheers
Gianni

@giannilmbd
Copy link
Author

Sorry for this sequence of messages... Just learning how the code works.

It appears that while putting the column with zeros first makes the code run without error messages, the IRFs produced under this constellation have no confidence bands. Reverting to NA in place of the 0s produces a whole non-degenerate distribution of the posterior.

I understand from this experiment that indeed there is some problem in the mixture of zeros and sign restrictions in certain cases.

Cheers

Gianni

@JPablo1203
Copy link

Hi, I am facing a smiliar issue when trying to model the global market of aluminium. My work is in line with Kilian & Murphy (2014) and Boer, et al. (2024) .

My endogenous variables are a global real economic activity index (REA), the percentage growth of production, log real prices and the difference of log production. I additionally add a set of deterministics as exogenous variables:

## Needed packages
if (!require("pacman")) install.packages("pacman"); library(pacman)
p_load(rstudioapi, dplyr, tidyr, bsvars, bsvarSIGNs)

## Load the data for estimation
est_data <- read.csv("example_data.csv")
endog_vars <- c("kilian_rea", # Real Economic Activity
                "pctg_growth_production", # Production growth
                "log_real_price", # Real prices
                "growth_log_lme_stocks") # Inventories' change

exog_vars <- c("lin_trend",
               "great_recession",
               "covid_pandemic",
               "ukraine_war")

# Isolate endogenous into separate time-series objects
endog_ts <- est_data %>% 
  select(all_of(endog_vars)) %>% 
  ts()

# Isolate the deterministics into a separate matrix object
exog_ts <- est_data %>% 
  select(all_of(exog_vars)) %>% 
  as.matrix()

I hypothesize four structural shocks: Aggregate demand, flow supply, contemporaneous metal-specific demand and expectational metal-specific demand. Moreover, I hypothesize that their respective effects on impact are:

  • Aggregate demand shock: positive on REA, unknown on supply, positive on prices and unknown on inventories.
  • Supply shock: 0 on REA, positive on supply, negative on prices, unknown on inventories.
  • Contemporaneous demand: positive on REA, unknown on supply, positive on prices and negative on inventories.
  • Expectational demand: positive on REA, unknown on supply, positive on prices and positive on inventories.

This therefore translates into the sign-restrictions matrix:

sign_irf <- matrix(c(1, 0, 1, 1,
                     NA, 1, NA, NA,
                     1, -1, 1, 1,
                     NA, NA, -1, 1),
                   nrow = 4, byrow = T)

However I get a similar error when trying to estimate the model:

# Esimate the models with deterministics
# Real economic activity and prices' priors are set to Random Walks
# Production and inventories' priors are set to White Noise 
specification_w_dummy  <- specify_bsvarSIGN$new(data = endog_ts,
                                                p = 12,
                                                sign_irf = sign_irf, 
                                                exogenous = exog_ts,
                                                stationary = c(T, F, T, F))

posterior <- estimate(specification_w_dummy, S = 10)


**************************************************|
 bsvarSIGNs: Bayesian Structural VAR with sign,   |
             zero and narrative restrictions      |
**************************************************|
 Progress of simulation for 10 independent draws
 Press Esc to interrupt the computations
**************************************************|
0%   10   20   30   40   50   60   70   80   90   100%
[----|----|----|----|----|----|----|----|----|----|
**************************************************|
Error in estimate.BSVARSIGN(specification_w_dummy, S = 10) : 
  matrix multiplication: incompatible matrix dimensions: 0x0 and 4x1

Should we try to reorder the variables as to ensure that some of the identification restrictions in the original papers hold?
This is the dataset I am using at the moment.
Thanks in advance for your help.

@donotdespair
Copy link
Member

Hey @adamwang15

Could you please have a look at this?

Cheers, T

@adamwang15
Copy link
Collaborator

Hi @giannilmbd @JPablo1203 @donotdespair

Thanks for sharing your experience using the package!

Yes, I think we should reorder the shocks so that those with zero restrictions are placed at the beginning. If you take a look at Arias, Rubio-Ramírez & Waggoner (2018), on page 689, especially in footnote 7, the authors implicitly suggest we should order zero restricted shocks first. A more technical reason is, in Algorithm 2 Step 3 if the first shock has no zero restrictions, then the dimension of $M_j$ is $0\times n$ which has no well-defined null space.

For confidence bands, I suspect you have only one effective draw, therefore the confidence band is equal to the single draw. Increasing the total number of samples S should solve this.

@JPablo1203
Copy link

JPablo1203 commented Dec 5, 2024

Hi,

I kept experimenting with the package and decided to add narrative sign-restrictions, in top of some 0 restrictions on the impact matrix.

I once again am trying to model the supply of a commodity with monthly data, I assume that, in the short run, the supply is inelastic, which would translate into a bunch of 0 restrictions. I reorganised the variables and impact matrix as to have the 0's on the first columns. Moreover, I added a series of narrative sign-restrictions for the 2008 crisis and the Ukraine war:

## Needed packages
if (!require("pacman")) install.packages("pacman"); library(pacman)
p_load(rstudioapi, dplyr, tidyr, bsvars, bsvarSIGNs)

## Loading the data for estimation
est_data <- read.csv("narrative_sign_example.csv")

## Define endogeneous and exogeneous variables
endog_vars <- c("kilian_rea", # Real Economic Activity
                "pctg_growth_production", # Production growth
                "log_real_price", # Real prices
                "growth_log_lme_stocks") # Inventories' change

exog_vars <- c("lin_trend",
               "great_recession",
               "covid_pandemic",
               "ukraine_war")

# Set the sign restrictions and shocks titles
sign_irf <- matrix(c(1, 1, 1, 1,
                     0, 0, 0, 1,
                     1, 1, 1, -1,
                     NA, 1, -1, NA),
                   nrow = 4, byrow = T)

rownames(sign_irf) <- endog_vars
colnames(sign_irf) <- shocks_titles
sign_irf

#                            Agg. Demand Shock | Contemp. Demand Shock | Expect. Demand Shock | Flow Supply Shock
# kilian_rea                             1                     1                    1                 1
# pctg_growth_production                 0                     0                    0                 1
# log_real_price                         1                     1                    1                -1
# growth_log_lme_stocks                 NA                     1                   -1                NA

# Isolate the estimation matrices
endog_ts <- est_data %>%
  select(all_of(endog_vars)) %>%
  ts()

exog_ts <- est_data %>%
  select(all_of(exog_vars)) %>%
  as.matrix()

sign_narrative <- list(
  # Lehman brothers' pick - agg demand on agg demand
  specify_narrative(start = 146, periods = 1, type = "S", sign = -1, shock = 1, var = 1),
  # Ukraine war
  specify_narrative(start = 313, periods = 1, type = "S", sign = 1, shock = 3, var = 3)
)

If I run the model without the narrative sign-restrictions, everything works properly. However, when adding the narrative sign-restrictions, I get this error:

set.seed(123)
specification <- specify_bsvarSIGN$new(data = endog_ts,
                                       p = 12,
                                       sign_irf = sign_irf,
                                       sign_narrative = sign_narrative, 
                                       exogenous = exog_ts,
                                       stationary = c(T, T, F, F))

posterior_nar <- estimate(specification, S = n_iter)

**************************************************|
 bsvarSIGNs: Bayesian Structural VAR with sign,   |
             zero and narrative restrictions      |
**************************************************|
 Progress of simulation for 100 independent draws
 Press Esc to interrupt the computations
**************************************************|
0%   10   20   30   40   50   60   70   80   90   100%
[----|----|----|----|----|----|----|----|----|----|
**************************************************|
Error in estimate.BSVARSIGN(specification, S = 100) : 
  null(): svd failed

Do you know why this may be the case? Could it be explained by the fact I added to many exclusion restrictions?
Thanks in advance for your help!

@adamwang15
Copy link
Collaborator

Hi @JPablo1203

Thanks for sharing your code and output!

I suspect this is a numerical problem that is specific to your dataset, could you try the following:

  1. Try removing one of the narrative restrictions or zero restrictions, you can be right that the combination is too strict.
  2. Set max_tries = 1 in specify_bsvarSIGN$new, this may prevent the algorithm from getting stuck.
  3. Try another order of the shocks as long as the zero restrictions are at the beginning.

We will also try to fix this in the algorithm regarding the computation of null space:

  • Resample rotation matrix when svd fails?
  • Make the algorithm robust to the order of shocks, so that it is not necessary to have zero restrictions at the beginning?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants