From 632fd01836ae82aa43261231cfa3a04d0739a7c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org> Date: Mon, 17 Mar 2014 18:10:44 +0100 Subject: [PATCH] Allow ranges in lags/periods specification of {irf,moment}_calibration. Ref #267 --- preprocessor/DynareBison.yy | 30 ++++++++++++++++++++++++++++++ preprocessor/ParsingDriver.cc | 12 ++++++------ preprocessor/ParsingDriver.hh | 4 ++-- preprocessor/Shocks.cc | 4 ++-- preprocessor/Shocks.hh | 6 +++--- 5 files changed, 43 insertions(+), 13 deletions(-) diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy index 500b8d14ed..aafe4022b5 100644 --- a/preprocessor/DynareBison.yy +++ b/preprocessor/DynareBison.yy @@ -175,6 +175,7 @@ class ParsingDriver; %type <string_val> filename symbol vec_of_vec_value vec_value_list date_expr %type <string_val> vec_value_1 vec_value signed_inf signed_number_w_inf %type <string_val> range vec_value_w_inf vec_value_1_w_inf named_var +%type <string_val> integer_range signed_integer_range %type <symbol_type_val> change_type_arg %type <vector_string_val> change_type_var_list subsamples_eq_opt prior_eq_opt options_eq_opt calibration_range %type <vector_int_val> vec_int_elem vec_int_1 vec_int vec_int_number @@ -2367,6 +2368,8 @@ moment_calibration_item : symbol COMMA symbol COMMA calibration_range ';' { driver.add_moment_calibration_item($1, $3, new string("0"), $5); } | symbol COMMA symbol '(' signed_integer ')' COMMA calibration_range ';' { driver.add_moment_calibration_item($1, $3, $5, $8); } + | symbol COMMA symbol '(' signed_integer_range ')' COMMA calibration_range ';' + { driver.add_moment_calibration_item($1, $3, $5, $8); } ; irf_calibration : IRF_CALIBRATION ';' irf_calibration_list END ';' @@ -2381,6 +2384,8 @@ irf_calibration_item : symbol COMMA symbol COMMA calibration_range ';' { driver.add_irf_calibration_item($1, new string("1"), $3, $5); } | symbol '(' INT_NUMBER ')' COMMA symbol COMMA calibration_range ';' { driver.add_irf_calibration_item($1, $3, $6, $8); } + | symbol '(' integer_range ')' COMMA symbol COMMA calibration_range ';' + { driver.add_irf_calibration_item($1, $3, $6, $8); } ; o_dr_algo : DR_ALGO EQUAL INT_NUMBER { @@ -2842,6 +2847,31 @@ range : symbol ':' symbol $$ = $1; }; +integer_range : INT_NUMBER ':' INT_NUMBER + { + $1->append(":"); + $1->append(*$3); + delete $3; + $$ = $1; + }; + +signed_integer_range : signed_integer ':' signed_integer + { + $1->append(":"); + $1->append(*$3); + delete $3; + $$ = $1; + } + | MINUS '(' signed_integer ':' signed_integer ')' + { + $3->insert(0, "-("); + $3->append(":"); + $3->append(*$5); + delete $5; + $3->append(")"); + $$ = $3; + }; + vec_int_number : INT_NUMBER { $$ = new vector<int>(); $$->push_back(atoi((*$1).c_str())); delete $1; }; vec_int_elem : vec_int_number diff --git a/preprocessor/ParsingDriver.cc b/preprocessor/ParsingDriver.cc index 3a46428f1f..72e3c22c0a 100644 --- a/preprocessor/ParsingDriver.cc +++ b/preprocessor/ParsingDriver.cc @@ -2606,7 +2606,7 @@ ParsingDriver::add_parallel_local_file(string *filename) } void -ParsingDriver::add_moment_calibration_item(string *endo1, string *endo2, string *lag, vector<string *> *range) +ParsingDriver::add_moment_calibration_item(string *endo1, string *endo2, string *lags, vector<string *> *range) { MomentCalibration::Constraint c; @@ -2622,8 +2622,8 @@ ParsingDriver::add_moment_calibration_item(string *endo1, string *endo2, string error("Variable " + *endo2 + " is not an endogenous."); delete endo2; - c.lag = abs(atoi(lag->c_str())); - delete lag; + c.lags = *lags; + delete lags; assert(range->size() == 2); c.lower_bound = *((*range)[0]); @@ -2643,7 +2643,7 @@ void ParsingDriver::end_moment_calibration() } void -ParsingDriver::add_irf_calibration_item(string *endo, string *period, string *exo, vector<string *> *range) +ParsingDriver::add_irf_calibration_item(string *endo, string *periods, string *exo, vector<string *> *range) { IrfCalibration::Constraint c; @@ -2653,8 +2653,8 @@ ParsingDriver::add_irf_calibration_item(string *endo, string *period, string *ex error("Variable " + *endo + " is not an endogenous."); delete endo; - c.period = atoi(period->c_str()); - delete period; + c.periods = *periods; + delete periods; check_symbol_existence(*exo); c.exo = mod_file->symbol_table.getID(*exo); diff --git a/preprocessor/ParsingDriver.hh b/preprocessor/ParsingDriver.hh index 24b47ffce7..dec73dfe67 100644 --- a/preprocessor/ParsingDriver.hh +++ b/preprocessor/ParsingDriver.hh @@ -658,11 +658,11 @@ public: //! Processing the parallel_local_files option void add_parallel_local_file(string *filename); //! Add an item of a moment_calibration statement - void add_moment_calibration_item(string *endo1, string *endo2, string *lag, vector<string *> *range); + void add_moment_calibration_item(string *endo1, string *endo2, string *lags, vector<string *> *range); //! End a moment_calibration statement void end_moment_calibration(); //! Add an item of an irf_calibration statement - void add_irf_calibration_item(string *endo, string *period, string *exo, vector<string *> *range); + void add_irf_calibration_item(string *endo, string *periods, string *exo, vector<string *> *range); //! End a moment_calibration statement void end_irf_calibration(); }; diff --git a/preprocessor/Shocks.cc b/preprocessor/Shocks.cc index d9af2d0526..c6b51ee479 100644 --- a/preprocessor/Shocks.cc +++ b/preprocessor/Shocks.cc @@ -402,7 +402,7 @@ MomentCalibration::writeOutput(ostream &output, const string &basename) const const Constraint &c = constraints[i]; output << "'" << symbol_table.getName(c.endo1) << "', " << "'" << symbol_table.getName(c.endo2) << "', " - << c.lag << ", " + << c.lags << ", " << "[ " << c.lower_bound << ", " << c.upper_bound << " ];" << endl; } @@ -424,7 +424,7 @@ IrfCalibration::writeOutput(ostream &output, const string &basename) const const Constraint &c = constraints[i]; output << "'" << symbol_table.getName(c.endo) << "', " << "'" << symbol_table.getName(c.exo) << "', " - << c.period << ", " + << c.periods << ", " << "[ " << c.lower_bound << ", " << c.upper_bound << " ];" << endl; } diff --git a/preprocessor/Shocks.hh b/preprocessor/Shocks.hh index 17f6bb8570..ef68b78e47 100644 --- a/preprocessor/Shocks.hh +++ b/preprocessor/Shocks.hh @@ -103,7 +103,7 @@ public: struct Constraint { int endo1, endo2; - int lag; + string lags; string lower_bound, upper_bound; }; typedef vector<Constraint> constraints_t; @@ -121,9 +121,9 @@ class IrfCalibration : public Statement public: struct Constraint { - int endo, period; + int endo; int exo; - string lower_bound, upper_bound; + string periods, lower_bound, upper_bound; }; typedef vector<Constraint> constraints_t; private: -- GitLab