pac_expectation: fix substitution

issue#70
Houtan Bastani 2018-02-09 16:57:12 +01:00
parent fa96ccbd7a
commit cb411e5642
5 changed files with 42 additions and 38 deletions

View File

@ -3384,11 +3384,12 @@ DynamicModel::addEquationsForVar(map<string, pair<SymbolList, int> > &var_model_
void
DynamicModel::fillPacExpectationVarInfo(string &var_model_name,
vector<int> &lhs,
map<int, set<int > > &rhs,
vector<bool> &nonstationary)
{
for (size_t i = 0; i < equations.size(); i++)
equations[i]->fillPacExpectationVarInfo(var_model_name, rhs, nonstationary);
equations[i]->fillPacExpectationVarInfo(var_model_name, lhs, rhs, nonstationary);
}
void

View File

@ -299,6 +299,7 @@ public:
void addEquationsForVar(map<string, pair<SymbolList, int> > &var_model_info);
//! Add var_model info to pac_expectation nodes
void fillPacExpectationVarInfo(string &var_model_name,
vector<int> &lhs,
map<int, set<int > > &rhs,
vector<bool> &nonstationary);
//! Substitutes pac_expectation operator

View File

@ -579,7 +579,7 @@ NumConstNode::setVarExpectationIndex(map<string, pair<SymbolList, int> > &var_mo
}
void
NumConstNode::fillPacExpectationVarInfo(string &var_model_name, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg)
NumConstNode::fillPacExpectationVarInfo(string &var_model_name_arg, vector<int> &lhs_arg, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg)
{
}
@ -1635,7 +1635,7 @@ VariableNode::setVarExpectationIndex(map<string, pair<SymbolList, int> > &var_mo
}
void
VariableNode::fillPacExpectationVarInfo(string &var_model_name, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg)
VariableNode::fillPacExpectationVarInfo(string &var_model_name_arg, vector<int> &lhs_arg, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg)
{
}
@ -3032,9 +3032,9 @@ UnaryOpNode::setVarExpectationIndex(map<string, pair<SymbolList, int> > &var_mod
}
void
UnaryOpNode::fillPacExpectationVarInfo(string &var_model_name, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg)
UnaryOpNode::fillPacExpectationVarInfo(string &var_model_name_arg, vector<int> &lhs_arg, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg)
{
arg->fillPacExpectationVarInfo(var_model_name, rhs_arg, nonstationary_arg);
arg->fillPacExpectationVarInfo(var_model_name_arg, lhs_arg, rhs_arg, nonstationary_arg);
}
bool
@ -4568,10 +4568,10 @@ BinaryOpNode::setVarExpectationIndex(map<string, pair<SymbolList, int> > &var_mo
}
void
BinaryOpNode::fillPacExpectationVarInfo(string &var_model_name, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg)
BinaryOpNode::fillPacExpectationVarInfo(string &var_model_name_arg, vector<int> &lhs_arg, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg)
{
arg1->fillPacExpectationVarInfo(var_model_name, rhs_arg, nonstationary_arg);
arg2->fillPacExpectationVarInfo(var_model_name, rhs_arg, nonstationary_arg);
arg1->fillPacExpectationVarInfo(var_model_name_arg, lhs_arg, rhs_arg, nonstationary_arg);
arg2->fillPacExpectationVarInfo(var_model_name_arg, lhs_arg, rhs_arg, nonstationary_arg);
}
bool
@ -5344,11 +5344,11 @@ TrinaryOpNode::setVarExpectationIndex(map<string, pair<SymbolList, int> > &var_m
}
void
TrinaryOpNode::fillPacExpectationVarInfo(string &var_model_name, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg)
TrinaryOpNode::fillPacExpectationVarInfo(string &var_model_name_arg, vector<int> &lhs_arg, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg)
{
arg1->fillPacExpectationVarInfo(var_model_name, rhs_arg, nonstationary_arg);
arg2->fillPacExpectationVarInfo(var_model_name, rhs_arg, nonstationary_arg);
arg3->fillPacExpectationVarInfo(var_model_name, rhs_arg, nonstationary_arg);
arg1->fillPacExpectationVarInfo(var_model_name_arg, lhs_arg, rhs_arg, nonstationary_arg);
arg2->fillPacExpectationVarInfo(var_model_name_arg, lhs_arg, rhs_arg, nonstationary_arg);
arg3->fillPacExpectationVarInfo(var_model_name_arg, lhs_arg, rhs_arg, nonstationary_arg);
}
bool
@ -5718,10 +5718,10 @@ AbstractExternalFunctionNode::setVarExpectationIndex(map<string, pair<SymbolList
}
void
AbstractExternalFunctionNode::fillPacExpectationVarInfo(string &var_model_name, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg)
AbstractExternalFunctionNode::fillPacExpectationVarInfo(string &var_model_name_arg, vector<int> &lhs_arg, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg)
{
for (vector<expr_t>::const_iterator it = arguments.begin(); it != arguments.end(); it++)
(*it)->fillPacExpectationVarInfo(var_model_name, rhs_arg, nonstationary_arg);
(*it)->fillPacExpectationVarInfo(var_model_name_arg, lhs_arg, rhs_arg, nonstationary_arg);
}
bool
@ -7189,7 +7189,7 @@ VarExpectationNode::setVarExpectationIndex(map<string, pair<SymbolList, int> > &
}
void
VarExpectationNode::fillPacExpectationVarInfo(string &var_model_name, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg)
VarExpectationNode::fillPacExpectationVarInfo(string &var_model_name_arg, vector<int> &lhs_arg, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg)
{
}
@ -7554,11 +7554,13 @@ PacExpectationNode::writeJsonOutput(ostream &output,
}
void
PacExpectationNode::fillPacExpectationVarInfo(string &var_model_name_arg, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg)
PacExpectationNode::fillPacExpectationVarInfo(string &var_model_name_arg, vector<int> &lhs_arg,
map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg)
{
if (var_model_name != var_model_name_arg)
return;
lhs = lhs_arg;
z_vec = rhs_arg;
for (vector<bool>::const_iterator it = nonstationary_arg.begin();
@ -7581,37 +7583,36 @@ PacExpectationNode::substitutePacExpectation(map<const PacExpectationNode *, con
if (myit != subst_table.end())
return const_cast<BinaryOpNode *>(myit->second);
int maxlag = z_vec.size();
expr_t subExpr = datatree.AddNonNegativeConstant("0");
if (stationary_vars_present)
for (map<int, set<int> >::const_iterator it = z_vec.begin();
it != z_vec.end(); it++)
for (set<int>::const_iterator it1 = it->second.begin(); it1 != it->second.end(); it1++)
for (int i = 1; i < maxlag + 1; i++)
for (vector<int>::const_iterator it = lhs.begin(); it != lhs.end(); it++)
{
stringstream param_name_h0;
param_name_h0 << "h0_" << model_name
<< "_var_" << datatree.symbol_table.getName(*it1)
<< "_lag_" << it->first;
<< "_var_" << datatree.symbol_table.getName(*it)
<< "_lag_" << i;
int new_param_symb_id = datatree.symbol_table.addSymbol(param_name_h0.str(), eParameter);
h0_indices.push_back(new_param_symb_id);
subExpr = datatree.AddPlus(subExpr,
datatree.AddTimes(datatree.AddVariable(new_param_symb_id),
datatree.AddVariable(*it1, it->first)));
datatree.AddVariable(*it, i)));
}
if (nonstationary_vars_present)
for (map<int, set<int > >::const_iterator it = z_vec.begin();
it != z_vec.end(); it++)
for (set<int>::const_iterator it1 = it->second.begin(); it1 != it->second.end(); it1++)
for (int i = 1; i < maxlag + 1; i++)
for (vector<int>::const_iterator it = lhs.begin(); it != lhs.end(); it++)
{
stringstream param_name_h1;
param_name_h1 << "h1_" << model_name
<< "_var_" << datatree.symbol_table.getName(*it1)
<< "_lag_" << it->first;
<< "_var_" << datatree.symbol_table.getName(*it)
<< "_lag_" << i;
int new_param_symb_id = datatree.symbol_table.addSymbol(param_name_h1.str(), eParameter);
h1_indices.push_back(new_param_symb_id);
subExpr = datatree.AddPlus(subExpr,
datatree.AddTimes(datatree.AddVariable(new_param_symb_id),
datatree.AddVariable(*it1, it->first)));
datatree.AddVariable(*it, i)));
}
growth_param_index = datatree.symbol_table.addSymbol(model_name +

View File

@ -490,7 +490,7 @@ class ExprNode
virtual bool isVarModelReferenced(const string &model_info_name) const = 0;
//! Fills var_model info for pac_expectation node
virtual void fillPacExpectationVarInfo(string &var_model_name, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg) = 0;
virtual void fillPacExpectationVarInfo(string &var_model_name, vector<int> &lhs, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg) = 0;
//! Fills map
virtual void getEndosAndMaxLags(map<string, int> &model_endos_and_lags) const = 0;
@ -560,7 +560,7 @@ public:
virtual expr_t removeTrendLeadLag(map<int, expr_t> trend_symbols_map) const;
virtual bool isInStaticForm() const;
virtual void setVarExpectationIndex(map<string, pair<SymbolList, int> > &var_model_info);
virtual void fillPacExpectationVarInfo(string &var_model_name, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg);
virtual void fillPacExpectationVarInfo(string &var_model_name_arg, vector<int> &lhs_arg, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg);
virtual bool isVarModelReferenced(const string &model_info_name) const;
virtual void getEndosAndMaxLags(map<string, int> &model_endos_and_lags) const;
virtual expr_t substituteStaticAuxiliaryVariable() const;
@ -639,7 +639,7 @@ public:
virtual expr_t removeTrendLeadLag(map<int, expr_t> trend_symbols_map) const;
virtual bool isInStaticForm() const;
virtual void setVarExpectationIndex(map<string, pair<SymbolList, int> > &var_model_info);
virtual void fillPacExpectationVarInfo(string &var_model_name, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg);
virtual void fillPacExpectationVarInfo(string &var_model_name_arg, vector<int> &lhs_arg, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg);
virtual bool isVarModelReferenced(const string &model_info_name) const;
virtual void getEndosAndMaxLags(map<string, int> &model_endos_and_lags) const;
//! Substitute auxiliary variables by their expression in static model
@ -741,7 +741,7 @@ public:
virtual expr_t removeTrendLeadLag(map<int, expr_t> trend_symbols_map) const;
virtual bool isInStaticForm() const;
virtual void setVarExpectationIndex(map<string, pair<SymbolList, int> > &var_model_info);
virtual void fillPacExpectationVarInfo(string &var_model_name, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg);
virtual void fillPacExpectationVarInfo(string &var_model_name_arg, vector<int> &lhs_arg, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg);
virtual bool isVarModelReferenced(const string &model_info_name) const;
virtual void getEndosAndMaxLags(map<string, int> &model_endos_and_lags) const;
//! Substitute auxiliary variables by their expression in static model
@ -861,7 +861,7 @@ public:
expr_t getNonZeroPartofEquation() const;
virtual bool isInStaticForm() const;
virtual void setVarExpectationIndex(map<string, pair<SymbolList, int> > &var_model_info);
virtual void fillPacExpectationVarInfo(string &var_model_name, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg);
virtual void fillPacExpectationVarInfo(string &var_model_name_arg, vector<int> &lhs_arg, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg);
virtual bool isVarModelReferenced(const string &model_info_name) const;
virtual void getEndosAndMaxLags(map<string, int> &model_endos_and_lags) const;
//! Substitute auxiliary variables by their expression in static model
@ -949,7 +949,7 @@ public:
virtual expr_t removeTrendLeadLag(map<int, expr_t> trend_symbols_map) const;
virtual bool isInStaticForm() const;
virtual void setVarExpectationIndex(map<string, pair<SymbolList, int> > &var_model_info);
virtual void fillPacExpectationVarInfo(string &var_model_name, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg);
virtual void fillPacExpectationVarInfo(string &var_model_name_arg, vector<int> &lhs_arg, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg);
virtual bool isVarModelReferenced(const string &model_info_name) const;
virtual void getEndosAndMaxLags(map<string, int> &model_endos_and_lags) const;
//! Substitute auxiliary variables by their expression in static model
@ -1045,7 +1045,7 @@ public:
virtual expr_t removeTrendLeadLag(map<int, expr_t> trend_symbols_map) const;
virtual bool isInStaticForm() const;
virtual void setVarExpectationIndex(map<string, pair<SymbolList, int> > &var_model_info);
virtual void fillPacExpectationVarInfo(string &var_model_name, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg);
virtual void fillPacExpectationVarInfo(string &var_model_name_arg, vector<int> &lhs_arg, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg);
virtual bool isVarModelReferenced(const string &model_info_name) const;
virtual void getEndosAndMaxLags(map<string, int> &model_endos_and_lags) const;
//! Substitute auxiliary variables by their expression in static model
@ -1234,7 +1234,7 @@ public:
virtual expr_t removeTrendLeadLag(map<int, expr_t> trend_symbols_map) const;
virtual bool isInStaticForm() const;
virtual void setVarExpectationIndex(map<string, pair<SymbolList, int> > &var_model_info);
virtual void fillPacExpectationVarInfo(string &var_model_name, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg);
virtual void fillPacExpectationVarInfo(string &var_model_name_arg, vector<int> &lhs_arg, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg);
virtual bool isVarModelReferenced(const string &model_info_name) const;
virtual void getEndosAndMaxLags(map<string, int> &model_endos_and_lags) const;
virtual expr_t substituteStaticAuxiliaryVariable() const;
@ -1247,6 +1247,7 @@ private:
const string model_name, var_model_name;
const int discount_symb_id, growth_symb_id;
bool stationary_vars_present, nonstationary_vars_present;
vector<int> lhs;
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;
@ -1304,7 +1305,7 @@ public:
virtual expr_t removeTrendLeadLag(map<int, expr_t> trend_symbols_map) const;
virtual bool isInStaticForm() const;
virtual void setVarExpectationIndex(map<string, pair<SymbolList, int> > &var_model_info);
virtual void fillPacExpectationVarInfo(string &var_model_name, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg);
virtual void fillPacExpectationVarInfo(string &var_model_name_arg, vector<int> &lhs_arg, map<int, set<int > > &rhs_arg, vector<bool> &nonstationary_arg);
virtual bool isVarModelReferenced(const string &model_info_name) const;
virtual void getEndosAndMaxLags(map<string, int> &model_endos_and_lags) const;
virtual expr_t substituteStaticAuxiliaryVariable() const;

View File

@ -389,7 +389,7 @@ ModFile::transformPass(bool nostrict, bool stochastic, bool compute_xrefs, const
map<int, set<int > > rhs_pac;
vms->getVarModelName(var_model_name);
vms->getVarModelRHS(rhs_pac);
dynamic_model.fillPacExpectationVarInfo(var_model_name, rhs_pac, nonstationary);
dynamic_model.fillPacExpectationVarInfo(var_model_name, lhs, rhs_pac, nonstationary);
dynamic_model.substitutePacExpectation();
}
}