preprocessor: pac_expectation fixes

issue#70
Houtan Bastani 2018-02-08 17:51:31 +01:00
parent b60f0d92a0
commit 2f28aded6e
9 changed files with 45 additions and 16 deletions

View File

@ -358,7 +358,16 @@ VarModelStatement::writeOutput(ostream &output, const string &basename, bool min
if (!symbol_list.empty())
symbol_list.writeOutput("options_.var.var_list_", output);
output << "options_.var.lhs = [";
output << "options_.var.eqn = [";
for (vector<int>::const_iterator it = eqnumber.begin();
it != eqnumber.end(); it++)
{
if (it != eqnumber.begin())
output << " ";
output << *it + 1;
}
output << "];" << endl
<< "options_.var.lhs = [";
for (vector<int>::const_iterator it = lhs.begin();
it != lhs.end(); it++)
{

View File

@ -521,14 +521,14 @@ DataTree::AddVarExpectation(const int symb_id, const int forecast_horizon, const
}
expr_t
DataTree::AddPacExpectation(const string &model_name, const int discount_id, const int growth_id)
DataTree::AddPacExpectation(const string &model_name, const string &var_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_id, growth_id)));
pac_expectation_node_map.find(make_pair(model_name, make_pair(var_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_id, growth_id);
return new PacExpectationNode(*this, model_name, var_model_name, discount_id, growth_id);
}
expr_t

View File

@ -82,7 +82,7 @@ protected:
var_expectation_node_map_t var_expectation_node_map;
// (model_name, (discount, growth)) -> PacExpectationNode
typedef map<pair<string, pair<int, int> >, PacExpectationNode *> pac_expectation_node_map_t;
typedef map<pair<string, 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, const int discount_id, const int growth_id);
expr_t AddPacExpectation(const string &model_name, const string &var_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

View File

@ -115,7 +115,7 @@ class ParsingDriver;
%token NONLINEAR_FILTER_INITIALIZATION FILTER_ALGORITHM PROPOSAL_APPROXIMATION CUBATURE UNSCENTED MONTECARLO DISTRIBUTION_APPROXIMATION
%token <string_val> NAME
%token USE_PENALIZED_OBJECTIVE_FOR_HESSIAN INIT_STATE RESCALE_PREDICTION_ERROR_COVARIANCE GENERATE_IRFS
%token NAN_CONSTANT NO_STATIC NOBS NOCONSTANT NODISPLAY NOCORR NODIAGNOSTIC NOFUNCTIONS NO_HOMOTOPY
%token NAN_CONSTANT NO_STATIC NOBS NOCONSTANT NODISPLAY NOCORR NODIAGNOSTIC NOFUNCTIONS NO_HOMOTOPY VAR_MODEL_NAME
%token NOGRAPH POSTERIOR_NOGRAPH POSTERIOR_GRAPH NOMOMENTS NOPRINT NORMAL_PDF SAVE_DRAWS MODEL_NAME STDERR_MULTIPLES DIAGONAL_ONLY
%token OBSERVATION_TRENDS OPTIM OPTIM_WEIGHTS ORDER OSR OSR_PARAMS MAX_DIM_COVA_GROUP ADVANCED OUTFILE OUTVARS OVERWRITE DISCOUNT
%token PARALLEL_LOCAL_FILES PARAMETERS PARAMETER_SET PARTIAL_INFORMATION PERIODS PERIOD PLANNER_OBJECTIVE PLOT_CONDITIONAL_FORECAST PLOT_PRIORS PREFILTER PRESAMPLE
@ -973,7 +973,9 @@ pac_expectation_options_list : pac_expectation_options_list COMMA pac_expectatio
| pac_expectation_options
;
pac_expectation_options : MODEL_NAME EQUAL QUOTED_STRING
pac_expectation_options : VAR_MODEL_NAME EQUAL NAME
{ driver.add_pac_expectation_var_model_name($3); }
| MODEL_NAME EQUAL NAME
{ driver.add_pac_expectation_model_name($3); }
| DISCOUNT EQUAL symbol
{ driver.add_pac_expectation_discount($3); }

View File

@ -342,6 +342,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
<DYNARE_STATEMENT>optim {return token::OPTIM;}
<DYNARE_STATEMENT>periods {return token::PERIODS;}
<DYNARE_STATEMENT,DYNARE_BLOCK>model_name {return token::MODEL_NAME;}
<DYNARE_BLOCK>var_model_name {return token::VAR_MODEL_NAME;}
<DYNARE_STATEMENT>endogenous_terminal_period {return token::ENDOGENOUS_TERMINAL_PERIOD;}
<DYNARE_STATEMENT>sub_draws {return token::SUB_DRAWS;}
<DYNARE_STATEMENT>minimal_solving_periods {return token::MINIMAL_SOLVING_PERIODS;}

View File

@ -7209,16 +7209,18 @@ VarExpectationNode::writeJsonOutput(ostream &output,
PacExpectationNode::PacExpectationNode(DataTree &datatree_arg,
const string &model_name_arg,
const string &var_model_name_arg,
const int discount_symb_id_arg,
const int growth_symb_id_arg) :
ExprNode(datatree_arg),
model_name(model_name_arg),
var_model_name(var_model_name_arg),
discount_symb_id(discount_symb_id_arg),
growth_symb_id(growth_symb_id_arg),
stationary_vars_present(false),
nonstationary_vars_present(false)
{
datatree.pac_expectation_node_map[make_pair(model_name, make_pair(discount_symb_id, growth_symb_id))] = this;
datatree.pac_expectation_node_map[make_pair(model_name, make_pair(var_model_name, make_pair(discount_symb_id, growth_symb_id)))] = this;
}
void
@ -7246,13 +7248,13 @@ PacExpectationNode::computeTemporaryTerms(map<expr_t, int> &reference_count,
expr_t
PacExpectationNode::toStatic(DataTree &static_datatree) const
{
return static_datatree.AddPacExpectation(string(model_name), discount_symb_id, growth_symb_id);
return static_datatree.AddPacExpectation(string(model_name), string(var_model_name), discount_symb_id, growth_symb_id);
}
expr_t
PacExpectationNode::cloneDynamic(DataTree &dynamic_datatree) const
{
return dynamic_datatree.AddPacExpectation(string(model_name), discount_symb_id, growth_symb_id);
return dynamic_datatree.AddPacExpectation(string(model_name), string(var_model_name), discount_symb_id, growth_symb_id);
}
void
@ -7265,12 +7267,13 @@ PacExpectationNode::writeOutput(ostream &output, ExprNodeOutputType output_type,
if (IS_LATEX(output_type))
{
output << "PAC_EXPECTATION" << LEFT_PAR(output_type) << model_name << ", "
<< discount_symb_id << ", " << growth_symb_id;
<< var_model_name << ", " << discount_symb_id << ", " << growth_symb_id;
output << RIGHT_PAR(output_type);
return;
}
output << "M_.pac_expectation." << model_name << ".discount_param_index = "
output <<"M_.pac_expectation." << model_name << ".var_model_name = '" << var_model_name << "';" << endl
<< "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

View File

@ -1244,14 +1244,14 @@ public:
class PacExpectationNode : public ExprNode
{
private:
const string model_name;
const string model_name, var_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,
PacExpectationNode(DataTree &datatree_arg, const string &model_name, const string &var_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,

View File

@ -2678,6 +2678,9 @@ ParsingDriver::add_pac_expectation()
if (pac_expectation_model_name.empty())
error("pac_expectation: you must pass the model_name option");
if (pac_expectation_var_model_name.empty())
error("pac_expectation: you must pass the var_model_name option");
if (pac_expectation_discount.empty())
error("pac_expectation: you must pass the discount option");
@ -2691,6 +2694,7 @@ ParsingDriver::add_pac_expectation()
mod_file->symbol_table.getID(pac_expectation_growth);
expr_t pac_exp_node = data_tree->AddPacExpectation(pac_expectation_model_name,
pac_expectation_var_model_name,
pac_expectation_discount_id,
pac_expectation_growth_id);
@ -2708,6 +2712,15 @@ ParsingDriver::add_pac_expectation_model_name(string *arg)
delete arg;
}
void
ParsingDriver::add_pac_expectation_var_model_name(string *arg)
{
if (!pac_expectation_var_model_name.empty())
error("pac_expectation: you can only pass the var_model_name option once");
pac_expectation_var_model_name = *arg;
delete arg;
}
void
ParsingDriver::add_pac_expectation_discount(string *arg)
{

View File

@ -253,7 +253,7 @@ private:
void clear_VAR_storage();
//! Used by pac_expectation
string pac_expectation_model_name, pac_expectation_discount, pac_expectation_growth;
string pac_expectation_model_name, pac_expectation_var_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)
@ -694,6 +694,7 @@ public:
expr_t add_pac_expectation();
//! Adds arguments for pac_expectation
void add_pac_expectation_model_name(string *arg);
void add_pac_expectation_var_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