From c1e415500151559c4d85ae0341779811e9e83b7a Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 14 Dec 2011 13:58:21 +0100 Subject: [PATCH] preprocessor: add subsamples statement --- preprocessor/CodeInterpreter.hh | 17 +++++----- preprocessor/ComputingTasks.cc | 23 +++++++++++-- preprocessor/DynareBison.yy | 22 +++++++++++- preprocessor/DynareFlex.ll | 1 + preprocessor/ExprNode.cc | 8 +++++ preprocessor/ParsingDriver.cc | 60 +++++++++++++++++++++++++++++++++ preprocessor/ParsingDriver.hh | 19 +++++++++-- 7 files changed, 136 insertions(+), 14 deletions(-) diff --git a/preprocessor/CodeInterpreter.hh b/preprocessor/CodeInterpreter.hh index f730cbbc9..964d05c89 100644 --- a/preprocessor/CodeInterpreter.hh +++ b/preprocessor/CodeInterpreter.hh @@ -140,14 +140,15 @@ enum BlockSimulationType /*! Warning: do not to change existing values for 0 to 4: the values matter for homotopy_setup command */ enum SymbolType { - eEndogenous = 0, //!< Endogenous - eExogenous = 1, //!< Exogenous - eExogenousDet = 2, //!< Exogenous deterministic - eParameter = 4, //!< Parameter - eModelLocalVariable = 10, //!< Local variable whose scope is model (pound expression) - eModFileLocalVariable = 11, //!< Local variable whose scope is mod file (model excluded) - eExternalFunction = 12, //!< External (user-defined) function - eTrend = 13 //!< Trend variable + eEndogenous = 0, //!< Endogenous + eExogenous = 1, //!< Exogenous + eExogenousDet = 2, //!< Exogenous deterministic + eParameter = 4, //!< Parameter + eModelLocalVariable = 10, //!< Local variable whose scope is model (pound expression) + eModFileLocalVariable = 11, //!< Local variable whose scope is mod file (model excluded) + eExternalFunction = 12, //!< External (user-defined) function + eTrend = 13, //!< Trend variable + eStatementDeclaredVariable = 14 //!< Local variable assigned within a Statement (see subsample statement for example) }; enum ExpressionType diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc index aa3029dd7..aae564fe8 100644 --- a/preprocessor/ComputingTasks.cc +++ b/preprocessor/ComputingTasks.cc @@ -1522,6 +1522,15 @@ BasicPriorStatement::checkPass(ModFileStructure &mod_file_struct) cerr << "ERROR: You must pass the shape option to the prior statement." << endl; exit(EXIT_FAILURE); } + if (options_list.num_options.find("date1") != options_list.num_options.end() || + options_list.num_options.find("date2") != options_list.num_options.end()) + if (options_list.num_options.find("date1") == options_list.num_options.end() || + options_list.num_options.find("date2") == options_list.num_options.end()) + { + cerr << "ERROR: PriorStatement::checkPass(1). Should not arrive here. " + << "Please inform Dynare Team." << endl; + exit(EXIT_FAILURE); + } } void @@ -1556,10 +1565,16 @@ BasicPriorStatement::writeVarianceOption(ostream &output, const string &lhs_fiel void BasicPriorStatement::writeOutputHelper(ostream &output, const string &field, const string &lhs_field) const { - OptionsList::num_options_t::const_iterator it = options_list.num_options.find(field); - if (it != options_list.num_options.end()) + + OptionsList::num_options_t::const_iterator itn = options_list.num_options.find(field); + if (itn != options_list.num_options.end()) output << "estimation_info" << lhs_field << "(prior_indx)." << field - << " = " << it->second << ";" << endl; + << " = " << itn->second << ";" << endl; + + OptionsList::date_options_t::const_iterator itd = options_list.date_options.find(field); + if (itd != options_list.date_options.end()) + output << "estimation_info" << lhs_field << "(prior_indx)." << field + << " = '" << itd->second << "';" << endl; } PriorStatement::PriorStatement(const string &name_arg, @@ -1592,6 +1607,8 @@ PriorStatement::writeOutput(ostream &output, const string &basename) const writeOutputHelper(output, "stdev", lhs_field); writeOutputHelper(output, "shape", lhs_field); writeOutputHelper(output, "shift", lhs_field); + writeOutputHelper(output, "date1", lhs_field); + writeOutputHelper(output, "date2", lhs_field); writeOutputHelper(output, "domain", lhs_field); writeOutputHelper(output, "interval", lhs_field); BasicPriorStatement::writeVarianceOption(output, lhs_field); diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy index 299bae37d..0dcbffb64 100644 --- a/preprocessor/DynareBison.yy +++ b/preprocessor/DynareBison.yy @@ -105,7 +105,7 @@ class ParsingDriver; %token INT_NUMBER %token DATE_NUMBER %token INV_GAMMA_PDF INV_GAMMA1_PDF INV_GAMMA2_PDF IRF IRF_SHOCKS -%token KALMAN_ALGO KALMAN_TOL +%token KALMAN_ALGO KALMAN_TOL SUBSAMPLES %token LABELS LAPLACE LIK_ALGO LIK_INIT LINEAR LOAD_IDENT_FILES LOAD_MH_FILE LOAD_PARAMS_AND_STEADY_STATE LOGLINEAR %token MARKOWITZ MARGINAL_DENSITY MAX MAXIT %token MFS MH_DROP MH_INIT_SCALE MH_JSCALE MH_MODE MH_NBLOCKS MH_REPLIC MH_RECOVER MIN MINIMAL_SOLVING_PERIODS @@ -217,6 +217,7 @@ statement : parameters | set_time | data | prior + | subsamples | varobs | observation_trends | unit_root_vars @@ -1197,8 +1198,21 @@ data_options : o_file | o_xls_range ; +subsamples : symbol '.' SUBSAMPLES '(' subsamples_name_list ')' ';' + { driver.set_subsamples($1); } + ; + +subsamples_name_list : subsamples_name_list COMMA o_subsample_name + | o_subsample_name + ; + prior : symbol '.' PRIOR '(' prior_options_list ')' ';' { driver.set_prior($1); } + | symbol '.' symbol '.' PRIOR '(' prior_options_list ')' ';' + { + driver.add_subsample_range(new string (*$1), $3); + driver.set_prior($1); + } | STD '(' symbol ')' '.' PRIOR '(' prior_options_list ')' ';' { driver.set_std_prior($3); } | CORR '(' symbol COMMA symbol')' '.' PRIOR '(' prior_options_list ')' ';' @@ -1955,6 +1969,12 @@ o_nograph : NOGRAPH | GRAPH { driver.option_num("nograph", "0"); } ; +o_subsample_name : symbol EQUAL date_number ':' date_number + { + driver.declare_statement_local_variable(new string (*$1)); + driver.set_subsample_name_equal_to_date_range($1, $3, $5); + } + ; o_conf_sig : CONF_SIG EQUAL non_negative_number { driver.option_num("conf_sig", $3); }; o_mh_replic : MH_REPLIC EQUAL INT_NUMBER { driver.option_num("mh_replic", $3); }; o_mh_drop : MH_DROP EQUAL non_negative_number { driver.option_num("mh_drop", $3); }; diff --git a/preprocessor/DynareFlex.ll b/preprocessor/DynareFlex.ll index 56afce82e..d0ff91fd7 100644 --- a/preprocessor/DynareFlex.ll +++ b/preprocessor/DynareFlex.ll @@ -189,6 +189,7 @@ string eofbuff; /* End of a Dynare block */ end {BEGIN INITIAL; return token::END;} +subsamples {return token::SUBSAMPLES;} prior {return token::PRIOR;} std {BEGIN DYNARE_STATEMENT; return token::STD;} corr {BEGIN DYNARE_STATEMENT; return token::CORR;} diff --git a/preprocessor/ExprNode.cc b/preprocessor/ExprNode.cc index 0f1f63763..0a8a06a94 100644 --- a/preprocessor/ExprNode.cc +++ b/preprocessor/ExprNode.cc @@ -496,6 +496,7 @@ VariableNode::prepareForDerivation() non_null_derivatives = datatree.local_variables_table[symb_id]->non_null_derivatives; break; case eModFileLocalVariable: + case eStatementDeclaredVariable: // Such a variable is never derived break; case eExternalFunction: @@ -523,6 +524,9 @@ VariableNode::computeDerivative(int deriv_id) case eModFileLocalVariable: cerr << "ModFileLocalVariable is not derivable" << endl; exit(EXIT_FAILURE); + case eStatementDeclaredVariable: + cerr << "eStatementDeclaredVariable is not derivable" << endl; + exit(EXIT_FAILURE); case eExternalFunction: cerr << "Impossible case!" << endl; exit(EXIT_FAILURE); @@ -735,6 +739,7 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type, case eExternalFunction: case eTrend: + case eStatementDeclaredVariable: cerr << "Impossible case" << endl; exit(EXIT_FAILURE); } @@ -917,6 +922,9 @@ VariableNode::getChainRuleDerivative(int deriv_id, const map &recur case eModFileLocalVariable: cerr << "ModFileLocalVariable is not derivable" << endl; exit(EXIT_FAILURE); + case eStatementDeclaredVariable: + cerr << "eStatementDeclaredVariable is not derivable" << endl; + exit(EXIT_FAILURE); case eExternalFunction: cerr << "Impossible case!" << endl; exit(EXIT_FAILURE); diff --git a/preprocessor/ParsingDriver.cc b/preprocessor/ParsingDriver.cc index 9cf200c89..c16e6fedd 100644 --- a/preprocessor/ParsingDriver.cc +++ b/preprocessor/ParsingDriver.cc @@ -175,6 +175,16 @@ ParsingDriver::declare_parameter(string *name, string *tex_name) delete tex_name; } +void +ParsingDriver::declare_statement_local_variable(string *name) +{ + if (mod_file->symbol_table.exists(*name)) + error("Symbol " + *name + " cannot be assigned within a statement " + + "while being assigned elsewhere in the modfile"); + declare_symbol(name, eStatementDeclaredVariable, NULL); + delete name; +} + void ParsingDriver::declare_optimal_policy_discount_factor_parameter(expr_t exprnode) { @@ -1224,6 +1234,56 @@ ParsingDriver::estimation_data() options_list.clear(); } +void +ParsingDriver::set_subsamples(string *name) +{ + check_symbol_is_parameter(name); + if (subsample_declarations.find(*name) != subsample_declarations.end()) + error("Parameter " + *name + " has more than one subsample statement." + + "You may only have one subsample statement per parameter."); + subsample_declarations[*name] = subsample_declaration_map; + subsample_declaration_map.clear(); + delete name; +} + +void +ParsingDriver::check_symbol_is_statement_variable(string *name) +{ + check_symbol_existence(*name); + int symb_id = mod_file->symbol_table.getID(*name); + if (mod_file->symbol_table.getType(symb_id) != eStatementDeclaredVariable) + error(*name + " is not a variable assigned in a statement"); +} + +void +ParsingDriver::set_subsample_name_equal_to_date_range(string *name, string *date1, string *date2) +{ + check_symbol_is_statement_variable(name); + if (subsample_declaration_map.find(*name) != subsample_declaration_map.end()) + error("Symbol " + *name + " may only be assigned once in a SUBSAMPLE statement"); + subsample_declaration_map[*name] = make_pair(*date1, *date2); + delete name; + delete date1; + delete date2; +} + +void +ParsingDriver::add_subsample_range(string *parameter, string *subsample_name) +{ + check_symbol_is_parameter(parameter); + check_symbol_is_statement_variable(subsample_name); + subsample_declarations_t::const_iterator it = subsample_declarations.find(*parameter); + if (it == subsample_declarations.end()) + error("A subsample statement has not been issued for " + *parameter); + subsample_declaration_map_t tmp_map = it->second; + if (tmp_map.find(*subsample_name) == tmp_map.end()) + error("The subsample name " + *subsample_name + " was not previously declared in a subsample statement."); + option_date("date1", tmp_map[*subsample_name].first); + option_date("date2", tmp_map[*subsample_name].second); + delete parameter; + delete subsample_name; +} + void ParsingDriver::set_prior(string *name) { diff --git a/preprocessor/ParsingDriver.hh b/preprocessor/ParsingDriver.hh index 6c35297e8..0deebfd14 100644 --- a/preprocessor/ParsingDriver.hh +++ b/preprocessor/ParsingDriver.hh @@ -84,6 +84,9 @@ private: //! Checks that a given symbol exists and is a parameter, and stops with an error message if it isn't void check_symbol_is_parameter(string *name); + //! Checks that a given symbol was assigned within a Statement + void check_symbol_is_statement_variable(string *name); + //! Checks that a given symbol exists and is a endogenous or exogenous, and stops with an error message if it isn't void check_symbol_is_endogenous_or_exogenous(string *name); @@ -186,6 +189,12 @@ private: vector declared_nonstationary_vars; //! Temporary storage for a variance declared in the prior statement expr_t prior_variance; + //! Temporary storage for declaring subsamples: map + typedef map > subsample_declaration_map_t; + subsample_declaration_map_t subsample_declaration_map; + //! Temporary storage for subsample statement: map + typedef map subsample_declarations_t; + subsample_declarations_t subsample_declarations; //! reset the values for temporary storage void reset_current_external_function_options(); //! Adds a model lagged variable to ModelTree and VariableTable @@ -239,6 +248,14 @@ public: void declare_exogenous_det(string *name, string *tex_name = NULL); //! Declares a parameter void declare_parameter(string *name, string *tex_name = NULL); + //! Declares a statement local variable + void declare_statement_local_variable(string *name); + //! Completes a subsample statement + void set_subsamples(string *name); + //! Declares a subsample, assigning the value to name + void set_subsample_name_equal_to_date_range(string *name, string *date1, string *date2); + //! Adds a subsample range to the list of options for the prior statement + void add_subsample_range(string *parameter, string *subsample_name); //! Declares declare_optimal_policy_discount_factor as a parameter and initializes it to exprnode void declare_optimal_policy_discount_factor_parameter(expr_t exprnode); //! Adds a predetermined_variable @@ -375,8 +392,6 @@ public: void set_std_prior(string *arg); //! Sets the prior for estimated correlation void set_corr_prior(string *arg1, string *arg2); - //! Sets the subsamples for a parameter - void set_subsamples(string *arg); //! Runs estimation process void run_estimation(); //! Runs dynare_sensitivy()