R/zerosign_restr.default.R

Defines functions zerosign_restr.default

zerosign_restr.default <- function(B, Sigma, p, n, draws, restr_matrix, LR=FALSE,
                                   has_const=TRUE, tries=300, var_names=NULL, shock_names=NULL){
  restr_matrix_stacked = NULL
  for(period in 1:dim(restr_matrix)[3]){
    restr_matrix_stacked = rbind(restr_matrix_stacked, restr_matrix[,,period])
  }
  #calculate horizon
  if(LR){
    horiz = dim(restr_matrix)[3]-2
  }
  else{
    horiz = dim(restr_matrix)[3]-1
  }
  #sr_sign_matrix_stacked = matrix(nrow=5, ncol=5)
  #diag(sr_sign_matrix_stacked) = 1
  #sr_sign_matrix_stacked[5, 1:4] <- 0
  #DEBUG!
  cat("\n")
  cat("Restrictions horizon: ",
      ifelse(LR, paste0(ifelse(horiz < 0, "Only", paste0(horiz, " +")), " long-run"), horiz), "\n", sep="")

  satisfying_models = list()
  for(draw in 1:draws){
    cat("\rDraw ", draw, " of ", draws,". Accepted draws: ", length(satisfying_models), ".", sep="")
    bayesian_ortho_irfs = irf_ala_arias(B=B[draw,,], Sigma=Sigma[draw,,],
                                        p=p, n=n, horizon=(dim(restr_matrix)[3]-1), LR=LR)
    succ_models_from_draw = zerosign_restr_ala_arias(irfs=bayesian_ortho_irfs, B=B[draw,,], Sigma=Sigma[draw,,],
                                                     zero_sign_matrix = restr_matrix_stacked,
                                                     tries = tries)
    if(length(succ_models_from_draw) != 0){
      satisfying_models[[length(satisfying_models) + 1]] = succ_models_from_draw
    }
  }
  cat("\n", sep="")
  if(length(satisfying_models) == 0){
    cat("No staisfying models are found!")
  }
  # create var_names
  n_anon_vars = bvar_model$meta$M - length(var_names)
  for(i in 1:n_anon_vars){
    var_names <- c(var_names, sprintf('Variable %d', i))
  }
  # create shock_names
  n_anon_shocks = bvar_model$meta$M - length(shock_names)
  for(i in 1:n_anon_shocks){
    shock_names <- c(shock_names, sprintf('Shock %d', i))
  }
  toreturn <- list("models" = satisfying_models)
  class(toreturn) <- "ZerosignR.result"
  toreturn$meta$var_names <- var_names
  toreturn$meta$shock_names <- shock_names
  toreturn$meta$modelclass <- "bvar"
  toreturn$meta$restr_matrix <- restr_matrix
  toreturn$meta$lags <- bvar_model$meta$lags
  toreturn$meta$nvars <- bvar_model$meta$M
  toreturn$meta$LR <- LR
  toreturn$meta$tries <- tries
  toreturn$meta$models_checked <- tries*draws
  toreturn$meta$accepted <- length(satisfying_models)
  return(toreturn)
}
roootra/ZerosignR documentation built on May 23, 2022, 2:06 p.m.