Commit a69c53b0 authored by Houtan Bastani's avatar Houtan Bastani
Browse files

clean up var_model code

parent 03f88931
......@@ -308,31 +308,17 @@ VarModelStatement::fillVarModelInfoFromEquations(vector<int> &eqnumber_arg, vect
it != rhs_by_eq.end(); it++)
for (set<pair<int, int> >::const_iterator it1 = it->begin();
it1 != it->end(); it1++)
if (find(lhs.begin(), lhs.end(), it1->first) == lhs.end()
&& find(orig_diff_var.begin(), orig_diff_var.end(), it1->first) == orig_diff_var.end())
{/*
cerr << "ERROR " << name << ": " << symbol_table.getName(it1->first)
<< " cannot appear in the VAR because it does not appear on the LHS" << endl;
exit(EXIT_FAILURE);
*/
}
else
{
map<int, set<int> >::iterator mit = rhs.find(abs(it1->second));
if (mit == rhs.end())
{
if (it1->second > 0)
{
cerr << "ERROR " << name << ": you cannot have a variable with a lead in a VAR" << endl;
exit(EXIT_FAILURE);
}
set<int> si;
si.insert(it1->first);
rhs[abs(it1->second)] = si;
}
else
mit->second.insert(it1->first);
}
{
map<int, set<int> >::iterator mit = rhs.find(abs(it1->second));
if (mit == rhs.end())
{
set<int> si;
si.insert(it1->first);
rhs[abs(it1->second)] = si;
}
else
mit->second.insert(it1->first);
}
}
void
......@@ -341,7 +327,6 @@ VarModelStatement::getVarModelName(string &var_model_name) const
var_model_name = name;
}
void
VarModelStatement::getVarModelRHS(map<int, set<int > > &rhs_arg) const
{
......@@ -386,45 +371,31 @@ VarModelStatement::writeOutput(ostream &output, const string &basename, bool min
output << " ";
output << it->first;
}
output << "];" << endl;
int i = 1;
for (map<int, set<int> >::const_iterator it = rhs.begin();
it != rhs.end(); it++, i++)
{
output << "options_.var.rhs.vars_at_lag{" << i << "} = [";
for (set<int>::const_iterator it1 = it->second.begin();
it1 != it->second.end(); it1++)
{
if (it1 != it->second.begin())
output << " ";
output << symbol_table.getTypeSpecificID(*it1) + 1;
}
output << "];" << endl;
}
output << "options_.var.nonstationary = logical([";
output << "];" << endl
<< "options_.var.nonstationary = [";
for (vector<bool>::const_iterator it = nonstationary.begin();
it != nonstationary.end(); it++)
{
if (it != nonstationary.begin())
output << " ";
if (*it)
output << "1";
output << "true";
else
output << "0";
output << "false";
}
output << "]);" << endl
<< "options_.var.diff = logical([";
output << "];" << endl
<< "options_.var.diff = [";
for (vector<bool>::const_iterator it = diff.begin();
it != diff.end(); it++)
{
if (it != diff.begin())
output << " ";
if (*it)
output << "1";
output << "true";
else
output << "0";
output << "false";
}
output << "]);" << endl
output << "];" << endl
<< "options_.var.orig_diff_var = [";
for (vector<int>::const_iterator it = orig_diff_var.begin();
it != orig_diff_var.end(); it++)
......@@ -437,7 +408,7 @@ VarModelStatement::writeOutput(ostream &output, const string &basename, bool min
output << symbol_table.getTypeSpecificID(*it) + 1;
}
output << "];" << endl;
i = 1;
int i = 1;
for (vector<set<pair<int, int > > >::const_iterator it = rhs_by_eq.begin();
it != rhs_by_eq.end(); it++, i++)
{
......
......@@ -3244,19 +3244,19 @@ DynamicModel::getVarModelVariablesFromEqTags(vector<string> &var_model_eqtags,
for (vector<string>::const_iterator itvareqs = var_model_eqtags.begin();
itvareqs != var_model_eqtags.end(); itvareqs++)
{
int eqnumber_int = -1;
int eqn = -1;
set<pair<int, int> > lhs_set, lhs_tmp_set, rhs_set;
string eqtag (*itvareqs);
for (vector<pair<int, pair<string, string> > >::const_iterator iteqtag =
equation_tags.begin(); iteqtag != equation_tags.end(); iteqtag++)
if (iteqtag->second.first.compare("name") == 0
&& iteqtag->second.second.compare(eqtag) == 0)
if (iteqtag->second.first == "name"
&& iteqtag->second.second == eqtag)
{
eqnumber_int = iteqtag->first;
eqn = iteqtag->first;
break;
}
if (eqnumber_int == -1)
if (eqn == -1)
{
cerr << "ERROR: equation tag '" << eqtag << "' not found" << endl;
exit(EXIT_FAILURE);
......@@ -3265,42 +3265,52 @@ DynamicModel::getVarModelVariablesFromEqTags(vector<string> &var_model_eqtags,
bool nonstationary_bool = false;
for (vector<pair<int, pair<string, string> > >::const_iterator iteqtag =
equation_tags.begin(); iteqtag != equation_tags.end(); iteqtag++)
if (iteqtag->first == eqnumber_int)
if (iteqtag->second.first.compare("data_type") == 0
&& iteqtag->second.second.compare("nonstationary") == 0)
if (iteqtag->first == eqn)
if (iteqtag->second.first == "data_type"
&& iteqtag->second.second == "nonstationary")
{
nonstationary_bool = true;
break;
}
nonstationary.push_back(nonstationary_bool);
equations[eqnumber_int]->get_arg1()->collectDynamicVariables(eEndogenous, lhs_set);
equations[eqnumber_int]->get_arg1()->collectDynamicVariables(eExogenous, lhs_tmp_set);
equations[eqnumber_int]->get_arg1()->collectDynamicVariables(eParameter, lhs_tmp_set);
equations[eqn]->get_arg1()->collectDynamicVariables(eEndogenous, lhs_set);
equations[eqn]->get_arg1()->collectDynamicVariables(eExogenous, lhs_tmp_set);
equations[eqn]->get_arg1()->collectDynamicVariables(eParameter, lhs_tmp_set);
if (lhs_set.size() != 1 || !lhs_tmp_set.empty())
{
cerr << "ERROR: A VAR may only have one endogenous variable on the LHS" << endl;
cerr << "ERROR: in Equation " << eqtag
<< ". A VAR may only have one endogenous variable on the LHS. " << endl;
exit(EXIT_FAILURE);
}
set<pair<int, int> >::const_iterator it = lhs_set.begin();
if (it->second != 0)
{
cerr << "ERROR: The variable on the LHS of a VAR may not appear with a lead or a lag" << endl;
cerr << "ERROR: in Equation " << eqtag
<< ". The variable on the LHS of a VAR may not appear with a lead or a lag. "
<< endl;
exit(EXIT_FAILURE);
}
eqnumber.push_back(eqnumber_int);
eqnumber.push_back(eqn);
lhs.push_back(it->first);
nonstationary.push_back(nonstationary_bool);
equations[eqnumber_int]->get_arg2()->collectDynamicVariables(eEndogenous, rhs_set);
equations[eqn]->get_arg2()->collectDynamicVariables(eEndogenous, rhs_set);
for (it = rhs_set.begin(); it != rhs_set.end(); it++)
if (it->second > 0)
{
cerr << "ERROR: in Equation " << eqtag
<< ". A VAR may not have leaded or contemporaneous variables on the RHS. " << endl;
exit(EXIT_FAILURE);
}
rhs.push_back(rhs_set);
}
}
void
DynamicModel::getDiffInfo(vector<int> &eqnumber, vector<bool> &diff, vector<int> &orig_diff_var) const
DynamicModel::getVarLhsDiffInfo(vector<int> &eqnumber, vector<bool> &diff, vector<int> &orig_diff_var) const
{
for (vector<int>::const_iterator it = eqnumber.begin();
it != eqnumber.end(); it++)
......@@ -3310,13 +3320,12 @@ DynamicModel::getDiffInfo(vector<int> &eqnumber, vector<bool> &diff, vector<int>
{
set<pair<int, int> > diff_set;
equations[*it]->get_arg1()->collectDynamicVariables(eEndogenous, diff_set);
if (diff_set.empty() || diff_set.size() != 1)
if (diff_set.size() != 1)
{
cerr << "ERROR: problem getting variable for diff operator in equation " << *it << endl;
cerr << "ERROR: problem getting variable for LHS diff operator in equation " << *it << endl;
exit(EXIT_FAILURE);
}
set<pair<int, int> >::const_iterator it1 = diff_set.begin();
orig_diff_var.push_back(it1->first);
orig_diff_var.push_back(diff_set.begin()->first);
}
else
orig_diff_var.push_back(-1);
......
......@@ -294,7 +294,7 @@ public:
vector<bool> &nonstationary) const;
// Get equtaino information on diff operator
void getDiffInfo(vector<int> &eqnumber, vector<bool> &diff, vector<int> &orig_diff_var) const;
void getVarLhsDiffInfo(vector<int> &eqnumber, vector<bool> &diff, vector<int> &orig_diff_var) const;
//! Set indices for var expectation in dynamic model file
void setVarExpectationIndices(map<string, pair<SymbolList, int> > &var_model_info);
......
......@@ -4725,21 +4725,22 @@ BinaryOpNode::walkPacParametersHelper(const expr_t arg1, const expr_t arg2,
set<pair<int, pair<int, int> > > &params_and_vals) const
{
set<int> params;
set<pair<int, int> > endogs;
arg1->collectVariables(eParameter, params);
if (params.size() != 1)
return;
set<pair<int, int> > endogs;
arg2->collectDynamicVariables(eEndogenous, endogs);
if (params.size() == 1)
if (endogs.size() == 1)
params_and_vals.insert(make_pair(*(params.begin()), *(endogs.begin())));
else
if (endogs.size() == 2)
if (endogs.size() == 1)
params_and_vals.insert(make_pair(*(params.begin()), *(endogs.begin())));
else if (endogs.size() == 2)
{
BinaryOpNode *testarg2 = dynamic_cast<BinaryOpNode *>(arg2);
if (testarg2 != NULL && testarg2->get_op_code() == oMinus)
{
BinaryOpNode *testarg2 = dynamic_cast<BinaryOpNode *>(arg2);
VariableNode *test_arg1 = dynamic_cast<VariableNode *>(testarg2->get_arg1());
VariableNode *test_arg2 = dynamic_cast<VariableNode *>(testarg2->get_arg2());
if (testarg2 != NULL && testarg2->get_op_code() == oMinus
&& test_arg1 != NULL &&test_arg2 != NULL
&& lhs.first != -1)
if (test_arg1 != NULL && test_arg2 != NULL && lhs.first != -1)
{
int find_symb_id = -1;
try
......@@ -4766,6 +4767,7 @@ BinaryOpNode::walkPacParametersHelper(const expr_t arg1, const expr_t arg2,
}
}
}
}
}
void
......
......@@ -384,8 +384,9 @@ ModFile::transformPass(bool nostrict, bool stochastic, bool compute_xrefs, const
vector<bool> nonstationary, diff;
dynamic_model.getVarModelVariablesFromEqTags(var_model_eqtags,
eqnumber, lhs, rhs, nonstationary);
original_model.getDiffInfo(eqnumber, diff, orig_diff_var);
vms->fillVarModelInfoFromEquations(eqnumber, lhs, rhs, nonstationary, diff, orig_diff_var);
original_model.getVarLhsDiffInfo(eqnumber, diff, orig_diff_var);
vms->fillVarModelInfoFromEquations(eqnumber, lhs, rhs, nonstationary,
diff, orig_diff_var);
string var_model_name;
map<int, set<int > > rhs_pac;
vms->getVarModelName(var_model_name);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment