diff --git a/DataTree.cc b/DataTree.cc index f2179a6ddae2809ec95e0ee8adfe4aaf9af89f76..a779fd86d2561460b4e1661a8781ffaadebfec4f 100644 --- a/DataTree.cc +++ b/DataTree.cc @@ -521,13 +521,14 @@ DataTree::AddVarExpectation(const int symb_id, const int forecast_horizon, const } expr_t -DataTree::AddPacExpectation(const string &model_name, const expr_t discount, const expr_t growth) +DataTree::AddPacExpectation(const string &model_name, const int discount_id, const int growth_id) { - pac_expectation_node_map_t::iterator it = pac_expectation_node_map.find(make_pair(model_name, make_pair(discount, growth))); + pac_expectation_node_map_t::iterator it = + pac_expectation_node_map.find(make_pair(model_name, make_pair(discount_id, growth_id))); if (it != pac_expectation_node_map.end()) return it->second; - - return new PacExpectationNode(*this, model_name, discount, growth); + cout << "addpacexp " << model_name << endl; + return new PacExpectationNode(*this, model_name, discount_id, growth_id); } expr_t diff --git a/DataTree.hh b/DataTree.hh index 61be6c0b8e594be6cf8bb626a3fc1b80b112c0d2..9dd42fb4eb128592fc1f4837b936ad3f0efd6aee 100644 --- a/DataTree.hh +++ b/DataTree.hh @@ -82,7 +82,7 @@ protected: var_expectation_node_map_t var_expectation_node_map; // (model_name, (discount, growth)) -> PacExpectationNode - typedef map<pair<string, pair<expr_t, expr_t> >, PacExpectationNode *> pac_expectation_node_map_t; + typedef map<pair<string, pair<int, int> >, PacExpectationNode *> pac_expectation_node_map_t; pac_expectation_node_map_t pac_expectation_node_map; // ((arguments, deriv_idx), symb_id) -> FirstDerivExternalFunctionNode @@ -232,7 +232,7 @@ public: //! Adds "var_expectation(arg1, arg2, model_name=arg3)" to model tree expr_t AddVarExpectation(const int symb_id, const int forecast_horizon, const string &model_name); //! Adds pac_expectation command to model tree - expr_t AddPacExpectation(const string &model_name, expr_t discount, expr_t growth); + expr_t AddPacExpectation(const string &model_name, const int discount_id, const int growth_id); //! Adds a model local variable with its value void AddLocalVariable(int symb_id, expr_t value) throw (LocalVariableException); //! Adds an external function node diff --git a/DynareBison.yy b/DynareBison.yy index 2b59c8613787decafef852846996b6ad46eeb2c2..75113c9254cce3589a7497a058e9446f7c6fcb08 100644 --- a/DynareBison.yy +++ b/DynareBison.yy @@ -908,8 +908,8 @@ hand_side : '(' hand_side ')' { $$ = driver.add_var_expectation($3, new string("1"), $7); } | VAR_EXPECTATION '(' symbol COMMA INT_NUMBER COMMA MODEL_NAME EQUAL NAME ')' { $$ = driver.add_var_expectation($3, $5, $9); } - | PAC_EXPECTATION '(' MODEL_NAME EQUAL NAME COMMA DISCOUNT EQUAL hand_side COMMA GROWTH EQUAL hand_side')' - { $$ = driver.add_pac_expectation($5, $9, $13); } + | PAC_EXPECTATION '(' pac_expectation_options_list ')' + { $$ = driver.add_pac_expectation(); } | MINUS hand_side %prec UMINUS { $$ = driver.add_uminus($2); } | PLUS hand_side @@ -968,6 +968,19 @@ hand_side : '(' hand_side ')' { $$ = driver.add_steady_state($3); } ; + +pac_expectation_options_list : pac_expectation_options_list COMMA pac_expectation_options + | pac_expectation_options + ; + +pac_expectation_options : MODEL_NAME EQUAL QUOTED_STRING + { driver.add_pac_expectation_model_name($3); } + | DISCOUNT EQUAL symbol + { driver.add_pac_expectation_discount($3); } + | GROWTH EQUAL symbol + { driver.add_pac_expectation_growth($3); } + ; + comma_hand_side : hand_side { driver.add_external_function_arg($1); } | comma_hand_side COMMA hand_side diff --git a/ExprNode.cc b/ExprNode.cc index 79988cb67711f733185adac885dc70b836bceb6a..dcdfd38b19dc044c10e1df0bd2ad7648009dc3ef 100644 --- a/ExprNode.cc +++ b/ExprNode.cc @@ -7156,16 +7156,16 @@ VarExpectationNode::writeJsonOutput(ostream &output, PacExpectationNode::PacExpectationNode(DataTree &datatree_arg, const string &model_name_arg, - const expr_t discount_arg, - const expr_t growth_arg) : + const int discount_symb_id_arg, + const int growth_symb_id_arg) : ExprNode(datatree_arg), model_name(model_name_arg), - discount(discount_arg), - growth(growth_arg), + discount_symb_id(discount_symb_id_arg), + growth_symb_id(growth_symb_id_arg), stationary_vars_present(true), nonstationary_vars_present(true) { - datatree.pac_expectation_node_map[make_pair(model_name, make_pair(discount, growth))] = this; + datatree.pac_expectation_node_map[make_pair(model_name, make_pair(discount_symb_id, growth_symb_id))] = this; } void @@ -7193,13 +7193,15 @@ PacExpectationNode::computeTemporaryTerms(map<expr_t, int> &reference_count, expr_t PacExpectationNode::toStatic(DataTree &static_datatree) const { - return static_datatree.AddPacExpectation(model_name, discount, growth); + cout << "toStatic " << model_name << endl; + return static_datatree.AddPacExpectation(string(model_name), discount_symb_id, growth_symb_id); } expr_t PacExpectationNode::cloneDynamic(DataTree &dynamic_datatree) const { - return dynamic_datatree.AddPacExpectation(model_name, discount, growth); + cout << "cloneDynamic " << model_name << endl; + return dynamic_datatree.AddPacExpectation(string(model_name), discount_symb_id, growth_symb_id); } void @@ -7211,20 +7213,16 @@ PacExpectationNode::writeOutput(ostream &output, ExprNodeOutputType output_type, if (IS_LATEX(output_type)) { - output << "PAC_EXPECTATION" << LEFT_PAR(output_type) << model_name << ", "; - discount->writeOutput(output, output_type, temporary_terms, tef_terms); - output << ", "; - growth->writeOutput(output, output_type, temporary_terms, tef_terms); + output << "PAC_EXPECTATION" << LEFT_PAR(output_type) << model_name << ", " + << discount_symb_id << ", " << growth_symb_id; output << RIGHT_PAR(output_type); return; } - output << "M_.pac_expectation." << model_name << ".discount = "; - discount->writeOutput(output, oMatlabOutsideModel, temporary_terms, tef_terms); - output << ";" << endl - << "M_.pac_expectation." << model_name << ".growth = "; - growth->writeOutput(output, oMatlabOutsideModel, temporary_terms, tef_terms); - output << ";" << endl + output << "M_.pac_expectation." << model_name << ".discount_param_index = " + << datatree.symbol_table.getTypeSpecificID(discount_symb_id) + 1 << ";" << endl + << "M_.pac_expectation." << model_name << ".growth_name = '" + << datatree.symbol_table.getName(growth_symb_id) << "';" << endl << "M_.pac_expectation." << model_name << ".growth_neutrality_param_index = " << datatree.symbol_table.getTypeSpecificID(growth_param_index) + 1 << ";" << endl << "M_.pac_expectation." << model_name << ".h0_param_indices = ["; @@ -7484,11 +7482,9 @@ PacExpectationNode::writeJsonOutput(ostream &output, { output << "pac_expectation(" << ", model_name = " << model_name - << ", "; - discount->writeJsonOutput(output, temporary_terms, tef_terms, isdynamic); - output << ", "; - growth->writeJsonOutput(output, temporary_terms, tef_terms, isdynamic); - output << ")"; + << ", " << discount_symb_id + << ", " << growth_symb_id + << ")"; } void @@ -7603,11 +7599,12 @@ PacExpectationNode::substitutePacExpectation(map<const PacExpectationNode *, con datatree.AddVariable(*it1, it->first))); } - growth_param_index = datatree.symbol_table.addSymbol(model_name + "_pac_growth_neutrality_correction", + growth_param_index = datatree.symbol_table.addSymbol(model_name + + "_pac_growth_neutrality_correction", eParameter); subExpr = datatree.AddPlus(subExpr, datatree.AddTimes(datatree.AddVariable(growth_param_index), - growth)); + datatree.AddVariable(growth_symb_id))); subst_table[const_cast<PacExpectationNode *>(this)] = dynamic_cast<BinaryOpNode *>(subExpr); diff --git a/ExprNode.hh b/ExprNode.hh index 6600403b264fd1fde5cd6c4d63b712d80cb52f23..80052b1bbc1f2814e97a8a5e105c5cdf205629a1 100644 --- a/ExprNode.hh +++ b/ExprNode.hh @@ -1234,14 +1234,15 @@ public: class PacExpectationNode : public ExprNode { private: - const string &model_name; - const expr_t discount, growth; + const string model_name; + const int discount_symb_id, growth_symb_id; bool stationary_vars_present, nonstationary_vars_present; map<int, set<int > > z_vec; // lag -> set< symb_id > (all vars that appear at a given lag) vector<int> h0_indices, h1_indices; int growth_param_index; public: - PacExpectationNode(DataTree &datatree_arg, const string &model_name, const expr_t discount_arg, const expr_t growth_arg); + PacExpectationNode(DataTree &datatree_arg, const string &model_name, + const int discount_arg, const int growth_arg); virtual void computeTemporaryTerms(map<expr_t, pair<int, NodeTreeReference> > &reference_count, map<NodeTreeReference, temporary_terms_t> &temp_terms_map, bool is_matlab, NodeTreeReference tr) const; diff --git a/ParsingDriver.cc b/ParsingDriver.cc index 0a8924a2aa5300dba938df1d26b4903b74ef3ca9..bd771b853c24d92cef19a7ee17abd6b86fd15172 100644 --- a/ParsingDriver.cc +++ b/ParsingDriver.cc @@ -2673,9 +2673,62 @@ ParsingDriver::add_var_expectation(string *arg1, string *arg2, string *arg3) } expr_t -ParsingDriver::add_pac_expectation(string *model_name, expr_t discount, expr_t growth) +ParsingDriver::add_pac_expectation() { - return data_tree->AddPacExpectation(*model_name, discount, growth); + if (pac_expectation_model_name.empty()) + error("pac_expectation: you must pass the model_name option"); + + if (pac_expectation_discount.empty()) + error("pac_expectation: you must pass the discount option"); + + if (pac_expectation_growth.empty()) + error("pac_expectation: you must pass the growth option"); + + int pac_expectation_discount_id = + mod_file->symbol_table.getID(pac_expectation_discount); + + int pac_expectation_growth_id = + mod_file->symbol_table.getID(pac_expectation_growth); + + expr_t pac_exp_node = data_tree->AddPacExpectation(pac_expectation_model_name, + pac_expectation_discount_id, + pac_expectation_growth_id); + + pac_expectation_model_name = pac_expectation_discount = pac_expectation_growth = ""; + + return pac_exp_node; +} + +void +ParsingDriver::add_pac_expectation_model_name(string *arg) +{ + if (!pac_expectation_model_name.empty()) + error("pac_expectation: you can only pass the model_name option once"); + pac_expectation_model_name = *arg; + delete arg; +} + +void +ParsingDriver::add_pac_expectation_discount(string *arg) +{ + if (!pac_expectation_discount.empty()) + error("pac_expectation: you can only pass the discount option once"); + check_symbol_is_parameter(arg); + pac_expectation_discount = *arg; + delete arg; +} + +void +ParsingDriver::add_pac_expectation_growth(string *arg) +{ + if (!pac_expectation_growth.empty()) + error("pac_expectation: you can only pass the growth option once"); + check_symbol_existence(*arg); + SymbolType type = mod_file->symbol_table.getType(mod_file->symbol_table.getID(*arg)); + if (type != eParameter && type != eEndogenous) + error("pac_expectation growth argument must either be a parameter or an endogenous variable."); + pac_expectation_growth = *arg; + delete arg; } expr_t diff --git a/ParsingDriver.hh b/ParsingDriver.hh index bf5c12b16c5ab61d1aa1e83498b5db053f1e10aa..c83793a6ebfeed58d22272100a8faa665e5092f8 100644 --- a/ParsingDriver.hh +++ b/ParsingDriver.hh @@ -251,6 +251,10 @@ private: //! Used by VAR restrictions void clear_VAR_storage(); + + //! Used by pac_expectation + string pac_expectation_model_name, pac_expectation_discount, pac_expectation_growth; + public: ParsingDriver(WarningConsolidation &warnings_arg, bool nostrict_arg) : warnings(warnings_arg), nostrict(nostrict_arg), model_error_encountered(false) { @@ -687,7 +691,11 @@ public: //! Writes token "VAR_EXPECTATION(arg1, arg2, arg3)" to model tree expr_t add_var_expectation(string *arg1, string *arg2, string *arg3); //! Writes token "PAC_EXPECTATION(model_name, discount, growth)" to model tree - expr_t add_pac_expectation(string *model_name, expr_t discount, expr_t growth); + expr_t add_pac_expectation(); + //! Adds arguments for pac_expectation + void add_pac_expectation_model_name(string *arg); + void add_pac_expectation_discount(string *arg); + void add_pac_expectation_growth(string *arg); //! Writes token "diff(arg1)" to model tree expr_t add_diff(expr_t arg1); //! Writes token "adl(arg1, lag)" to model tree