From de567377c3b11fc948e3e9b9e09796a2ff6b69f6 Mon Sep 17 00:00:00 2001 From: Michel Juillard Date: Sun, 10 Apr 2016 18:55:17 +0200 Subject: [PATCH] adding groups of shocks for shock_decomposition consistency checks, documentation and test case are still missing --- matlab/global_initialization.m | 2 ++ matlab/graph_decomp.m | 2 +- matlab/shock_decomposition.m | 19 ++++++++++++++++++- preprocessor/DynareBison.yy | 28 ++++++++++++++++++++++++++-- preprocessor/DynareFlex.ll | 3 +++ preprocessor/ParsingDriver.cc | 33 +++++++++++++++++++++++++++++++++ preprocessor/ParsingDriver.hh | 12 +++++++++++- preprocessor/Shocks.cc | 19 +++++++++++++++++++ preprocessor/Shocks.hh | 18 ++++++++++++++++++ 9 files changed, 131 insertions(+), 5 deletions(-) diff --git a/matlab/global_initialization.m b/matlab/global_initialization.m index 80d9e5731..188952cb2 100644 --- a/matlab/global_initialization.m +++ b/matlab/global_initialization.m @@ -662,6 +662,8 @@ options_.discretionary_tol = 1e-7; % Shock decomposition options_.parameter_set = []; +options_.use_shock_groups = ''; +options_.colormap = ''; % Nonlinearfilters options_.nonlinear_filter = []; diff --git a/matlab/graph_decomp.m b/matlab/graph_decomp.m index f1ef56e47..743865a7a 100644 --- a/matlab/graph_decomp.m +++ b/matlab/graph_decomp.m @@ -88,7 +88,7 @@ for j=1:nvar hold on; y1 = 0; height = 1/comp_nbr; - labels = char(shock_names,'Initial values'); + labels = char(char(shock_names),'Initial values'); for i=1:comp_nbr fill([0 0 0.2 0.2],[y1 y1+0.7*height y1+0.7*height y1],i); diff --git a/matlab/shock_decomposition.m b/matlab/shock_decomposition.m index 84ef91937..c2ad99784 100644 --- a/matlab/shock_decomposition.m +++ b/matlab/shock_decomposition.m @@ -112,4 +112,21 @@ end oo_.shock_decomposition = z; -graph_decomp(z,M_.exo_names,M_.endo_names,i_var,options_.initial_date,M_,options_) +if options_.use_shock_groups + shock_groups = M_.shock_groups.(options_.use_shock_groups); + shock_names = fieldnames(shock_groups); + ngroups = length(shock_names); + zz = zeros(endo_nbr,ngroups+2,gend); + for i=1:length(shock_names) + for j = shock_groups.(shock_names{i}) + k = find(strcmp(j,cellstr(M_.exo_names))); + zz(:,i,:) = zz(:,i,:) + z(:,k,:); + end + end + zz(:,ngroups+(1:2),:) = z(:,nshocks+(1:2),:); + z = zz; +else + shock_names = M_.exo_names; +end + +graph_decomp(z,shock_names,M_.endo_names,i_var,options_.initial_date,M_,options_) diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy index dbf7ae6f2..c6ac9ea0f 100644 --- a/preprocessor/DynareBison.yy +++ b/preprocessor/DynareBison.yy @@ -120,7 +120,7 @@ class ParsingDriver; %token QUOTED_STRING %token QZ_CRITERIUM QZ_ZERO_THRESHOLD FULL DSGE_VAR DSGE_VARLAG DSGE_PRIOR_WEIGHT TRUNCATE %token RELATIVE_IRF REPLIC SIMUL_REPLIC RPLOT SAVE_PARAMS_AND_STEADY_STATE PARAMETER_UNCERTAINTY -%token SHOCKS SHOCK_DECOMPOSITION SIGMA_E SIMUL SIMUL_ALGO SIMUL_SEED ENDOGENOUS_TERMINAL_PERIOD +%token SHOCKS SHOCK_DECOMPOSITION SHOCK_GROUPS USE_SHOCK_GROUPS SIGMA_E SIMUL SIMUL_ALGO SIMUL_SEED ENDOGENOUS_TERMINAL_PERIOD %token SMOOTHER SMOOTHER2HISTVAL SQUARE_ROOT_SOLVER STACK_SOLVE_ALGO STEADY_STATE_MODEL SOLVE_ALGO SOLVER_PERIODS %token STDERR STEADY STOCH_SIMUL SURPRISE SYLVESTER SYLVESTER_FIXED_POINT_TOL REGIMES REGIME %token TEX RAMSEY_MODEL RAMSEY_POLICY RAMSEY_CONSTRAINTS PLANNER_DISCOUNT DISCRETIONARY_POLICY DISCRETIONARY_TOL @@ -128,7 +128,7 @@ class ParsingDriver; %token UNIFORM_PDF UNIT_ROOT_VARS USE_DLL USEAUTOCORR GSA_SAMPLE_FILE USE_UNIVARIATE_FILTERS_IF_SINGULARITY_IS_DETECTED %token VALUES VAR VAREXO VAREXO_DET VAROBS PREDETERMINED_VARIABLES %token WRITE_LATEX_DYNAMIC_MODEL WRITE_LATEX_STATIC_MODEL WRITE_LATEX_ORIGINAL_MODEL -%token XLS_SHEET XLS_RANGE LMMCP OCCBIN BANDPASS_FILTER +%token XLS_SHEET XLS_RANGE LMMCP OCCBIN BANDPASS_FILTER COLORMAP %left COMMA %left EQUAL_EQUAL EXCLAMATION_EQUAL %left LESS GREATER LESS_EQUAL GREATER_EQUAL @@ -285,6 +285,7 @@ statement : parameters | perfect_foresight_solver | prior_function | posterior_function + | shock_groups ; dsample : DSAMPLE INT_NUMBER ';' @@ -2382,6 +2383,8 @@ shock_decomposition_options_list : shock_decomposition_option COMMA shock_decomp shock_decomposition_option : o_parameter_set | o_datafile + | o_use_shock_groups + | o_colormap ; homotopy_setup: HOMOTOPY_SETUP ';' homotopy_list END ';' @@ -2583,6 +2586,23 @@ smoother2histval_option : o_infile | o_outvars ; +shock_groups : SHOCK_GROUPS ';' shock_group_list END ';'{driver.end_shock_groups(new string("default"));} + | SHOCK_GROUPS '(' NAME EQUAL symbol ')' ';' shock_group_list END ';' + {driver.end_shock_groups($5);} + ; + +shock_group_list : shock_group_list shock_group_element + | shock_group_element + ; + +shock_group_element : symbol EQUAL shock_name_list ';' {driver.add_shock_group($1);} + ; + +shock_name_list : shock_name_list COMMA symbol {driver.add_shock_group_element($3);} + | shock_name_list symbol {driver.add_shock_group_element($2);} + | symbol {driver.add_shock_group_element($1);} + ; + o_dr_algo : DR_ALGO EQUAL INT_NUMBER { if (*$3 == string("0")) driver.warning("dr_algo option is now deprecated, and may be removed in a future version of Dynare"); @@ -3094,6 +3114,10 @@ o_lmmcp : LMMCP {driver.option_num("lmmcp", "1"); }; o_occbin : OCCBIN {driver.option_num("occbin", "1"); }; o_function : FUNCTION EQUAL filename { driver.option_str("function", $3); }; o_sampling_draws : SAMPLING_DRAWS EQUAL INT_NUMBER { driver.option_num("sampling_draws",$3); }; +o_use_shock_groups : USE_SHOCK_GROUPS { driver.option_str("use_shock_groups","default"); } + | USE_SHOCK_GROUPS EQUAL symbol { driver.option_str("use_shock_groups", $3); } + ; +o_colormap : COLORMAP EQUAL symbol { driver.option_str("colormap",$3); }; range : symbol ':' symbol { diff --git a/preprocessor/DynareFlex.ll b/preprocessor/DynareFlex.ll index dedf87dca..2e695633f 100644 --- a/preprocessor/DynareFlex.ll +++ b/preprocessor/DynareFlex.ll @@ -187,6 +187,7 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2 endval {BEGIN DYNARE_BLOCK; return token::ENDVAL;} histval {BEGIN DYNARE_BLOCK; return token::HISTVAL;} shocks {BEGIN DYNARE_BLOCK; return token::SHOCKS;} +shock_groups {BEGIN DYNARE_BLOCK; return token::SHOCK_GROUPS;} mshocks {BEGIN DYNARE_BLOCK; return token::MSHOCKS;} estimated_params {BEGIN DYNARE_BLOCK; return token::ESTIMATED_PARAMS;} /* priors is an alias for estimated_params */ @@ -793,6 +794,8 @@ DATE -?[0-9]+([YyAa]|[Mm]([1-9]|1[0-2])|[Qq][1-4]|[Ww]([1-9]{1}|[1-4][0-9]|5[0-2 max_dim_cova_group {return token::MAX_DIM_COVA_GROUP;} gsa_sample_file {return token::GSA_SAMPLE_FILE;} +use_shock_groups {return token::USE_SHOCK_GROUPS;} + [A-Za-z_][A-Za-z0-9_]* { yylval->string_val = new string(yytext); return token::NAME; diff --git a/preprocessor/ParsingDriver.cc b/preprocessor/ParsingDriver.cc index 15194cb18..fa23d8529 100644 --- a/preprocessor/ParsingDriver.cc +++ b/preprocessor/ParsingDriver.cc @@ -2872,3 +2872,36 @@ ParsingDriver::add_ramsey_constraint(const string *name, BinaryOpcode op_code, c delete name; } +void +ParsingDriver::add_shock_group_element(string *name) +{ + check_symbol_existence(*name); + int symb_id = mod_file->symbol_table.getID(*name); + SymbolType type = mod_file->symbol_table.getType(symb_id); + + if (type != eExogenous) + error("shock_groups: " + *name + " should be an exogenous variable"); + + shock_group.push_back(*name); + + delete name; +} + + +void +ParsingDriver::add_shock_group(string *name) +{ + ShockGroupsStatement::Group G; + G.name = *name; + G.list = shock_group; + shock_groups.push_back(G); + + shock_group.clear(); +} + +void +ParsingDriver::end_shock_groups(const string *name) +{ + mod_file->addStatement(new ShockGroupsStatement(shock_groups, *name)); + shock_groups.clear(); +} diff --git a/preprocessor/ParsingDriver.hh b/preprocessor/ParsingDriver.hh index 0885f5a69..32096308f 100644 --- a/preprocessor/ParsingDriver.hh +++ b/preprocessor/ParsingDriver.hh @@ -205,6 +205,10 @@ private: //! Temporary storage for subsample statement: map>, subsample_declaration_map > typedef map, SubsamplesStatement::subsample_declaration_map_t > subsample_declarations_t; subsample_declarations_t subsample_declarations; + //! Temporary storage for shock_groups + vector shock_group; + vector shock_groups; + //! reset the values for temporary storage void reset_current_external_function_options(); //! Adds a model lagged variable to ModelTree and VariableTable @@ -698,7 +702,13 @@ public: void add_irf_calibration_item(string *endo, string *periods, string *exo, vector *range); //! End a moment_calibration statement void end_irf_calibration(); - + //! Add a shock to a group + void add_shock_group_element(string *name); + //! Add a set of shock groups + void add_shock_group(string *name); + //! End shock groups declaration + void end_shock_groups(const string *name); + void smoother2histval(); void histval_file(string *filename); void perfect_foresight_setup(); diff --git a/preprocessor/Shocks.cc b/preprocessor/Shocks.cc index 85ace03e6..64e5feac6 100644 --- a/preprocessor/Shocks.cc +++ b/preprocessor/Shocks.cc @@ -459,3 +459,22 @@ IrfCalibration::writeOutput(ostream &output, const string &basename, bool minima } output << "};" << endl; } + +ShockGroupsStatement::ShockGroupsStatement(const group_t &shock_groups_arg, const string &name_arg) + : shock_groups(shock_groups_arg), name(name_arg) +{ +} + +void +ShockGroupsStatement::writeOutput(ostream &output, const string &basename, bool minimal_workspace) const +{ + for (vector::const_iterator it = shock_groups.begin(); it != shock_groups.end(); it++) + { + output << "M_.shock_groups." << name + << "." << it->name << " = {"; + for ( vector::const_iterator it1 = it->list.begin(); it1 != it->list.end(); it1++) + output << " '" << *it1 << "'"; + output << "};" << endl; + } +} + diff --git a/preprocessor/Shocks.hh b/preprocessor/Shocks.hh index a78a154bf..7af10e2a3 100644 --- a/preprocessor/Shocks.hh +++ b/preprocessor/Shocks.hh @@ -141,4 +141,22 @@ public: virtual void writeOutput(ostream &output, const string &basename, bool minimal_workspace) const; }; +class ShockGroupsStatement : public Statement +{ +public: + struct Group + { + string name; + vector list; + }; + typedef vector group_t; +private: + group_t shock_groups; + vector group_names; + string name; +public: + ShockGroupsStatement(const group_t &shock_groups_arg, const string &name_arg); + virtual void writeOutput(ostream &output, const string &basename, bool minimal_workspace) const; +}; + #endif