From e1033c76b956cf3acde1e3b25cd1e1a5670015f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Charybdis=29?= Date: Thu, 13 Oct 2016 18:19:38 +0200 Subject: [PATCH] Added declaration of observed exogenous variables. With integration test. --- preprocessor/DynareBison.yy | 13 +++- preprocessor/DynareFlex.ll | 2 + preprocessor/ParsingDriver.cc | 18 ++++++ preprocessor/ParsingDriver.hh | 4 ++ preprocessor/SymbolTable.cc | 63 +++++++++++++++++++ preprocessor/SymbolTable.hh | 11 ++++ tests/Makefile.am | 1 + .../preprocessor.mod | 32 ++++++++++ 8 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 tests/exogenous-observed-variables/preprocessor.mod diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy index 246c8ddac..ebe98d143 100644 --- a/preprocessor/DynareBison.yy +++ b/preprocessor/DynareBison.yy @@ -129,7 +129,7 @@ class ParsingDriver; %token TEX RAMSEY_MODEL RAMSEY_POLICY RAMSEY_CONSTRAINTS PLANNER_DISCOUNT DISCRETIONARY_POLICY DISCRETIONARY_TOL %token TEX_NAME %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 VALUES VAR VAREXO VAREXO_DET VAROBS VAREXOBS PREDETERMINED_VARIABLES %token WRITE_LATEX_DYNAMIC_MODEL WRITE_LATEX_STATIC_MODEL WRITE_LATEX_ORIGINAL_MODEL %token XLS_SHEET XLS_RANGE LMMCP OCCBIN BANDPASS_FILTER COLORMAP %left COMMA @@ -232,6 +232,7 @@ statement : parameters | options_eq | varobs | observation_trends + | varexobs | unit_root_vars | dsample | rplot @@ -1893,6 +1894,16 @@ varobs_list : varobs_list symbol { driver.add_varobs($1); } ; +varexobs : VAREXOBS { driver.check_varexobs(); } varexobs_list ';'; + +varexobs_list : varexobs_list symbol + { driver.add_varexobs($2); } + | varexobs_list COMMA symbol + { driver.add_varexobs($3); } + | symbol + { driver.add_varexobs($1); } + ; + observation_trends : OBSERVATION_TRENDS ';' trend_list END ';' { driver.set_trends(); }; trend_list : trend_list trend_element diff --git a/preprocessor/DynareFlex.ll b/preprocessor/DynareFlex.ll index c8d1accdd..38055136c 100644 --- a/preprocessor/DynareFlex.ll +++ b/preprocessor/DynareFlex.ll @@ -117,6 +117,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 set_time {BEGIN DYNARE_STATEMENT; return token::SET_TIME;} data {BEGIN DYNARE_STATEMENT; return token::DATA;} varobs {BEGIN DYNARE_STATEMENT; return token::VAROBS;} +varexobs {BEGIN DYNARE_STATEMENT; return token::VAREXOBS;} unit_root_vars {BEGIN DYNARE_STATEMENT; return token::UNIT_ROOT_VARS;} rplot {BEGIN DYNARE_STATEMENT; return token::RPLOT;} osr_params {BEGIN DYNARE_STATEMENT; return token::OSR_PARAMS;} @@ -761,6 +762,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 steady_state {return token::STEADY_STATE;} expectation {return token::EXPECTATION;} varobs {return token::VAROBS;} +varexobs {return token::VAREXOBS;} full {return token::FULL;} nan {return token::NAN_CONSTANT;} inf {return token::INF_CONSTANT;} diff --git a/preprocessor/ParsingDriver.cc b/preprocessor/ParsingDriver.cc index dc7953ce9..7d7c99b48 100644 --- a/preprocessor/ParsingDriver.cc +++ b/preprocessor/ParsingDriver.cc @@ -1776,6 +1776,24 @@ ParsingDriver::add_varobs(string *name) delete name; } +void +ParsingDriver::check_varexobs() +{ + if (mod_file->symbol_table.observedExogenousVariablesNbr() > 0) + error("varexobs: you cannot have several 'varexobs' statements in the same MOD file"); +} + +void +ParsingDriver::add_varexobs(string *name) +{ + check_symbol_existence(*name); + int symb_id = mod_file->symbol_table.getID(*name); + if (mod_file->symbol_table.getType(symb_id) != eExogenous) + error("varexobs: " + *name + " is not an exogenous variable"); + mod_file->symbol_table.addObservedExogenousVariable(symb_id); + delete name; +} + void ParsingDriver::set_trends() { diff --git a/preprocessor/ParsingDriver.hh b/preprocessor/ParsingDriver.hh index 28385563c..a9f731c3f 100644 --- a/preprocessor/ParsingDriver.hh +++ b/preprocessor/ParsingDriver.hh @@ -486,6 +486,10 @@ public: void check_varobs(); //! Add a new observed variable void add_varobs(string *name); + //! Check that no observed exogenous variable has yet be defined + void check_varexobs(); + //! Add a new observed exogenous variable + void add_varexobs(string *name); //! Svar_Identification Statement void begin_svar_identification(); void end_svar_identification(); diff --git a/preprocessor/SymbolTable.cc b/preprocessor/SymbolTable.cc index 17b63e953..6d5f6ac2d 100644 --- a/preprocessor/SymbolTable.cc +++ b/preprocessor/SymbolTable.cc @@ -394,6 +394,21 @@ SymbolTable::writeOutput(ostream &output) const throw (NotYetFrozenException) output << getTypeSpecificID(*it)+1 << " "; output << " ];" << endl; } + + if (observedExogenousVariablesNbr() > 0) + { + int ic = 1; + output << "options_.varexobs = cell(1);" << endl; + for (vector::const_iterator it = varexobs.begin(); + it != varexobs.end(); it++, ic++) + output << "options_.varexobs(" << ic << ") = {'" << getName(*it) << "'};" << endl; + + output << "options_.varexobs_id = [ "; + for (vector::const_iterator it = varexobs.begin(); + it != varexobs.end(); it++) + output << getTypeSpecificID(*it)+1 << " "; + output << " ];" << endl; + } } void @@ -491,6 +506,20 @@ SymbolTable::writeCOutput(ostream &output) const throw (NotYetFrozenException) } output << "};" << endl; } + + output << "int observedExogenousVariablesNbr = " << observedExogenousVariablesNbr() << ";" << endl; + if (observedExogenousVariablesNbr() > 0) + { + output << "int varexobs[" << observedExogenousVariablesNbr() << "] = {"; + for (vector::const_iterator it = varexobs.begin(); + it != varexobs.end(); it++) + { + if ( it != varexobs.begin() ) + output << ","; + output << getTypeSpecificID(*it); + } + output << "};" << endl; + } } void @@ -549,6 +578,10 @@ SymbolTable::writeCCOutput(ostream &output) const throw (NotYetFrozenException) for (vector::const_iterator it = varobs.begin(); it != varobs.end(); it++) output << "varobs.push_back(" << getTypeSpecificID(*it) << ");" << endl; + + for (vector::const_iterator it = varexobs.begin(); + it != varexobs.end(); it++) + output << "varexobs.push_back(" << getTypeSpecificID(*it) << ");" << endl; } int @@ -776,6 +809,36 @@ SymbolTable::getObservedVariableIndex(int symb_id) const return (int) (it - varobs.begin()); } +void +SymbolTable::addObservedExogenousVariable(int symb_id) throw (UnknownSymbolIDException) +{ + if (symb_id < 0 || symb_id >= size) + throw UnknownSymbolIDException(symb_id); + + assert(getType(symb_id) != eEndogenous); + varexobs.push_back(symb_id); +} + +int +SymbolTable::observedExogenousVariablesNbr() const +{ + return (int) varexobs.size(); +} + +bool +SymbolTable::isObservedExogenousVariable(int symb_id) const +{ + return (find(varexobs.begin(), varexobs.end(), symb_id) != varexobs.end()); +} + +int +SymbolTable::getObservedExogenousVariableIndex(int symb_id) const +{ + vector::const_iterator it = find(varexobs.begin(), varexobs.end(), symb_id); + assert(it != varexobs.end()); + return (int) (it - varexobs.begin()); +} + vector SymbolTable::getTrendVarIds() const { diff --git a/preprocessor/SymbolTable.hh b/preprocessor/SymbolTable.hh index c99dd462f..1015003d9 100644 --- a/preprocessor/SymbolTable.hh +++ b/preprocessor/SymbolTable.hh @@ -121,6 +121,9 @@ private: //! Stores the list of observed variables vector varobs; + //! Stores the list of observed exogenous variables + vector varexobs; + public: SymbolTable(); //! Thrown when trying to access an unknown symbol (by name) @@ -314,6 +317,14 @@ public: bool isObservedVariable(int symb_id) const; //! Return the index of a given observed variable in the vector of all observed variables int getObservedVariableIndex(int symb_id) const; + //! Add an observed exogenous variable + void addObservedExogenousVariable(int symb_id) throw (UnknownSymbolIDException); + //! Return the number of observed exogenous variables + int observedExogenousVariablesNbr() const; + //! Is a given symbol in the set of observed exogenous variables + bool isObservedExogenousVariable(int symb_id) const; + //! Return the index of a given observed exogenous variable in the vector of all observed variables + int getObservedExogenousVariableIndex(int symb_id) const; vector getTrendVarIds() const; //! Get list of exogenous variables set getExogenous() const; diff --git a/tests/Makefile.am b/tests/Makefile.am index e3487d8d2..336932c6e 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -27,6 +27,7 @@ MODFILES = \ ms-sbvar/test_ms_variances_repeated_runs.mod \ fs2000/fs2000.mod \ ep/rbc.mod \ + exogenous-observed-variables/preprocessor.mod \ estimation/fs2000_with_weibull_prior.mod \ estimation/fs2000_initialize_from_calib.mod \ estimation/fs2000_calibrated_covariance.mod \ diff --git a/tests/exogenous-observed-variables/preprocessor.mod b/tests/exogenous-observed-variables/preprocessor.mod new file mode 100644 index 000000000..20a71f2b3 --- /dev/null +++ b/tests/exogenous-observed-variables/preprocessor.mod @@ -0,0 +1,32 @@ +var y z; + +varexo e x u; + +parameters rho; + +rho = .9; + + +model(linear); +y = z + e ; +z = rho*z(-1) + u; +end; + +shocks; +var e; stderr .01; +var u; stderr .01; +end; + +varobs y; + +varexobs x; + +assert(length(options_.varobs)==1); +assert(length(options_.varexobs)==1); +assert(length(options_.varobs_id)==1); +assert(length(options_.varexobs_id)==1); +assert(options_.varobs_id==1); +assert(options_.varexobs_id==2); +assert(options_.varobs{1}=='y'); +assert(options_.varexobs{1}=='x'); +