simplify code to find autoregressive matrix for VARs
parent
0544141545
commit
a3c08f932e
|
@ -4043,9 +4043,7 @@ DynamicModel::fillVarModelTable() const
|
||||||
var_model_table.setLhsExprT(lhs_expr_tr);
|
var_model_table.setLhsExprT(lhs_expr_tr);
|
||||||
|
|
||||||
// Fill AR Matrix
|
// Fill AR Matrix
|
||||||
map<string, map<tuple<int, int, int>, expr_t>> ARr;
|
var_model_table.setAR(fillAutoregressiveMatrixForVAR());
|
||||||
fillAutoregressiveMatrix(ARr, false);
|
|
||||||
var_model_table.setAR(ARr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -4115,17 +4113,32 @@ DynamicModel::fillVarModelTableFromOrigModel(StaticModel &static_model) const
|
||||||
var_model_table.setOrigDiffVar(orig_diff_var);
|
var_model_table.setOrigDiffVar(orig_diff_var);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
map<string, map<tuple<int, int, int>, expr_t>>
|
||||||
DynamicModel::fillAutoregressiveMatrix(map<string, map<tuple<int, int, int>, expr_t>> &ARr, bool is_trend_component_model) const
|
DynamicModel::fillAutoregressiveMatrixForVAR() const
|
||||||
{
|
{
|
||||||
auto eqnums = is_trend_component_model ?
|
map<string, map<tuple<int, int, int>, expr_t>> ARr;
|
||||||
trend_component_model_table.getNonTargetEqNums() : var_model_table.getEqNums();
|
for (const auto & it : var_model_table.getEqNums())
|
||||||
for (const auto & it : eqnums)
|
|
||||||
{
|
{
|
||||||
int i = 0;
|
int i = 0;
|
||||||
map<tuple<int, int, int>, expr_t> AR;
|
map<tuple<int, int, int>, expr_t> AR;
|
||||||
vector<int> lhs = is_trend_component_model ?
|
for (auto eqn : it.second)
|
||||||
trend_component_model_table.getNonTargetLhs(it.first) : var_model_table.getLhs(it.first);
|
{
|
||||||
|
auto *bopn = dynamic_cast<BinaryOpNode *>(equations[eqn]->arg2);
|
||||||
|
bopn->fillAutoregressiveRowForVAR(i++, var_model_table.getLhsOrigIds(it.first), AR);
|
||||||
|
}
|
||||||
|
ARr[it.first] = AR;
|
||||||
|
}
|
||||||
|
return ARr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
DynamicModel::fillAutoregressiveMatrix(map<string, map<tuple<int, int, int>, expr_t>> &ARr) const
|
||||||
|
{
|
||||||
|
for (const auto & it : trend_component_model_table.getNonTargetEqNums())
|
||||||
|
{
|
||||||
|
int i = 0;
|
||||||
|
map<tuple<int, int, int>, expr_t> AR;
|
||||||
|
vector<int> lhs = trend_component_model_table.getNonTargetLhs(it.first);
|
||||||
for (auto eqn : it.second)
|
for (auto eqn : it.second)
|
||||||
equations[eqn]->arg2->fillAutoregressiveRow(i++, lhs, AR);
|
equations[eqn]->arg2->fillAutoregressiveRow(i++, lhs, AR);
|
||||||
ARr[it.first] = AR;
|
ARr[it.first] = AR;
|
||||||
|
@ -4335,7 +4348,7 @@ void
|
||||||
DynamicModel::fillTrendComponentmodelTableAREC(ExprNode::subst_table_t &diff_subst_table) const
|
DynamicModel::fillTrendComponentmodelTableAREC(ExprNode::subst_table_t &diff_subst_table) const
|
||||||
{
|
{
|
||||||
map<string, map<tuple<int, int, int>, expr_t>> ARr, A0r, A0starr;
|
map<string, map<tuple<int, int, int>, expr_t>> ARr, A0r, A0starr;
|
||||||
fillAutoregressiveMatrix(ARr, true);
|
fillAutoregressiveMatrix(ARr);
|
||||||
trend_component_model_table.setAR(ARr);
|
trend_component_model_table.setAR(ARr);
|
||||||
fillErrorComponentMatrix(A0r, A0starr, diff_subst_table);
|
fillErrorComponentMatrix(A0r, A0starr, diff_subst_table);
|
||||||
trend_component_model_table.setA0(A0r, A0starr);
|
trend_component_model_table.setA0(A0r, A0starr);
|
||||||
|
|
|
@ -316,8 +316,11 @@ public:
|
||||||
//! Set the equations that have non-zero second derivatives
|
//! Set the equations that have non-zero second derivatives
|
||||||
void setNonZeroHessianEquations(map<int, string> &eqs);
|
void setNonZeroHessianEquations(map<int, string> &eqs);
|
||||||
|
|
||||||
//! Fill Autoregressive Matrix for var_model/trend_component_model
|
//! Fill Autoregressive Matrix for trend_component_model
|
||||||
void fillAutoregressiveMatrix(map<string, map<tuple<int, int, int>, expr_t>> &ARr, bool is_trend_component_model) const;
|
void fillAutoregressiveMatrix(map<string, map<tuple<int, int, int>, expr_t>> &ARr) const;
|
||||||
|
|
||||||
|
//! Fill Autoregressive Matrix for var_model
|
||||||
|
map<string, map<tuple<int, int, int>, expr_t>> fillAutoregressiveMatrixForVAR() const;
|
||||||
|
|
||||||
//! Fill Error Component Matrix for trend_component_model
|
//! Fill Error Component Matrix for trend_component_model
|
||||||
void fillErrorComponentMatrix(map<string, map<tuple<int, int, int>, expr_t>> &A0r, map<string, map<tuple<int, int, int>, expr_t>> &A0starr, ExprNode::subst_table_t &diff_subst_table) const;
|
void fillErrorComponentMatrix(map<string, map<tuple<int, int, int>, expr_t>> &A0r, map<string, map<tuple<int, int, int>, expr_t>> &A0starr, ExprNode::subst_table_t &diff_subst_table) const;
|
||||||
|
|
|
@ -5918,6 +5918,74 @@ BinaryOpNode::fillAutoregressiveRow(int eqn, const vector<int> &lhs, map<tuple<i
|
||||||
arg2->fillAutoregressiveRow(eqn, lhs, AR);
|
arg2->fillAutoregressiveRow(eqn, lhs, AR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
BinaryOpNode::fillAutoregressiveRowForVAR(int eqn, const vector<int> &lhs, map<tuple<int, int, int>, expr_t> &AR) const
|
||||||
|
{
|
||||||
|
vector<pair<expr_t, int>> terms;
|
||||||
|
decomposeAdditiveTerms(terms, 1);
|
||||||
|
for (const auto & it : terms)
|
||||||
|
{
|
||||||
|
auto bopn = dynamic_cast<BinaryOpNode *>(it.first);
|
||||||
|
if (bopn != nullptr)
|
||||||
|
{
|
||||||
|
auto vn1 = dynamic_cast<VariableNode *>(bopn->arg1);
|
||||||
|
auto vn2 = dynamic_cast<VariableNode *>(bopn->arg2);
|
||||||
|
if (vn1 && vn2)
|
||||||
|
{
|
||||||
|
int vid, lag;
|
||||||
|
VariableNode *param;
|
||||||
|
vid = -1;
|
||||||
|
if (datatree.symbol_table.getType(vn1->symb_id) == SymbolType::parameter
|
||||||
|
&& (datatree.symbol_table.getType(vn2->symb_id) == SymbolType::endogenous
|
||||||
|
|| datatree.symbol_table.getType(vn2->symb_id) == SymbolType::exogenous))
|
||||||
|
{
|
||||||
|
param = vn1;
|
||||||
|
vid = vn2->symb_id;
|
||||||
|
lag = vn2->lag;
|
||||||
|
}
|
||||||
|
else if (datatree.symbol_table.getType(vn2->symb_id) == SymbolType::parameter
|
||||||
|
&& (datatree.symbol_table.getType(vn1->symb_id) == SymbolType::endogenous
|
||||||
|
|| datatree.symbol_table.getType(vn1->symb_id) == SymbolType::exogenous))
|
||||||
|
{
|
||||||
|
param = vn2;
|
||||||
|
vid = vn1->symb_id;
|
||||||
|
lag = vn1->lag;
|
||||||
|
}
|
||||||
|
if (vid >= 0)
|
||||||
|
{
|
||||||
|
int vidineq = vid;
|
||||||
|
while (datatree.symbol_table.isAuxiliaryVariable(vid))
|
||||||
|
try
|
||||||
|
{
|
||||||
|
vid = datatree.symbol_table.getOrigSymbIdForAuxVar(vid);
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (vidineq != vid)
|
||||||
|
{
|
||||||
|
vid = datatree.symbol_table.getOrigSymbIdForDiffAuxVar(vidineq);
|
||||||
|
lag = -datatree.symbol_table.getOrigLeadLagForDiffAuxVar(vidineq);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (find(lhs.begin(), lhs.end(), vid) == lhs.end())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (AR.find({eqn, lag, vid}) != AR.end())
|
||||||
|
{
|
||||||
|
cerr << "BinaryOpNode::fillAutoregressiveRow: Error filling AR matrix: "
|
||||||
|
<< "lag/symb_id encountered more than once in equtaion" << endl;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
AR[{eqn, -lag, vid}] = param;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
BinaryOpNode::fillErrorCorrectionRowHelper(expr_t arg1, expr_t arg2,
|
BinaryOpNode::fillErrorCorrectionRowHelper(expr_t arg1, expr_t arg2,
|
||||||
int eqn,
|
int eqn,
|
||||||
|
|
|
@ -1041,6 +1041,7 @@ public:
|
||||||
void fillAutoregressiveRowHelper(expr_t arg1, expr_t arg2,
|
void fillAutoregressiveRowHelper(expr_t arg1, expr_t arg2,
|
||||||
int eqn, const vector<int> &lhs, map<tuple<int, int, int>, expr_t> &AR) const;
|
int eqn, const vector<int> &lhs, map<tuple<int, int, int>, expr_t> &AR) const;
|
||||||
void fillAutoregressiveRow(int eqn, const vector<int> &lhs, map<tuple<int, int, int>, expr_t> &AR) const override;
|
void fillAutoregressiveRow(int eqn, const vector<int> &lhs, map<tuple<int, int, int>, expr_t> &AR) const override;
|
||||||
|
void fillAutoregressiveRowForVAR(int eqn, const vector<int> &lhs, map<tuple<int, int, int>, expr_t> &AR) const;
|
||||||
void fillErrorCorrectionRowHelper(expr_t arg1, expr_t arg2,
|
void fillErrorCorrectionRowHelper(expr_t arg1, expr_t arg2,
|
||||||
int eqn, const vector<int> &nontrend_lhs, const vector<int> &trend_lhs,
|
int eqn, const vector<int> &nontrend_lhs, const vector<int> &trend_lhs,
|
||||||
map<tuple<int, int, int>, expr_t> &A0, map<tuple<int, int, int>, expr_t> &A0star) const;
|
map<tuple<int, int, int>, expr_t> &A0, map<tuple<int, int, int>, expr_t> &A0star) const;
|
||||||
|
|
|
@ -520,7 +520,7 @@ VarModelTable::writeOutput(const string &basename, ostream &output) const
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<int> lhs = getLhs(name);
|
vector<int> lhs = getLhsOrigIds(name);
|
||||||
ar_output << "if strcmp(model_name, '" << name << "')" << endl
|
ar_output << "if strcmp(model_name, '" << name << "')" << endl
|
||||||
<< " ar = zeros(" << lhs.size() << ", " << lhs.size() << ", " << getMaxLag(name) << ");" << endl;
|
<< " ar = zeros(" << lhs.size() << ", " << lhs.size() << ", " << getMaxLag(name) << ");" << endl;
|
||||||
for (const auto & it : AR.at(name))
|
for (const auto & it : AR.at(name))
|
||||||
|
@ -600,6 +600,26 @@ void
|
||||||
VarModelTable::setLhs(map<string, vector<int>> lhs_arg)
|
VarModelTable::setLhs(map<string, vector<int>> lhs_arg)
|
||||||
{
|
{
|
||||||
lhs = move(lhs_arg);
|
lhs = move(lhs_arg);
|
||||||
|
for (auto it : lhs)
|
||||||
|
{
|
||||||
|
vector<int> lhsvec;
|
||||||
|
for (auto ids : it.second)
|
||||||
|
{
|
||||||
|
int lhs_last_orig_symb_id = ids;
|
||||||
|
int lhs_orig_symb_id = ids;
|
||||||
|
if (symbol_table.isAuxiliaryVariable(lhs_orig_symb_id))
|
||||||
|
try
|
||||||
|
{
|
||||||
|
lhs_last_orig_symb_id = lhs_orig_symb_id;
|
||||||
|
lhs_orig_symb_id = symbol_table.getOrigSymbIdForAuxVar(lhs_orig_symb_id);
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
lhsvec.emplace_back(lhs_last_orig_symb_id);
|
||||||
|
}
|
||||||
|
lhs_orig_symb_ids[it.first] = lhsvec;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -681,6 +701,13 @@ VarModelTable::getLhs(const string &name_arg) const
|
||||||
return lhs.find(name_arg)->second;
|
return lhs.find(name_arg)->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vector<int>
|
||||||
|
VarModelTable::getLhsOrigIds(const string &name_arg) const
|
||||||
|
{
|
||||||
|
checkModelName(name_arg);
|
||||||
|
return lhs_orig_symb_ids.find(name_arg)->second;
|
||||||
|
}
|
||||||
|
|
||||||
vector<set<pair<int, int>>>
|
vector<set<pair<int, int>>>
|
||||||
VarModelTable::getRhs(const string &name_arg) const
|
VarModelTable::getRhs(const string &name_arg) const
|
||||||
{
|
{
|
||||||
|
|
|
@ -118,7 +118,7 @@ private:
|
||||||
set<string> names;
|
set<string> names;
|
||||||
map<string, pair<SymbolList, int>> symbol_list_and_order;
|
map<string, pair<SymbolList, int>> symbol_list_and_order;
|
||||||
map<string, vector<string>> eqtags;
|
map<string, vector<string>> eqtags;
|
||||||
map<string, vector<int>> eqnums, max_lags, lhs, orig_diff_var;
|
map<string, vector<int>> eqnums, max_lags, lhs, lhs_orig_symb_ids, orig_diff_var;
|
||||||
map<string, vector<set<pair<int, int>>>> rhs;
|
map<string, vector<set<pair<int, int>>>> rhs;
|
||||||
map<string, vector<bool>> diff;
|
map<string, vector<bool>> diff;
|
||||||
map<string, vector<expr_t>> lhs_expr_t;
|
map<string, vector<expr_t>> lhs_expr_t;
|
||||||
|
@ -141,6 +141,7 @@ public:
|
||||||
vector<int> getMaxLags(const string &name_arg) const;
|
vector<int> getMaxLags(const string &name_arg) const;
|
||||||
int getMaxLag(const string &name_arg) const;
|
int getMaxLag(const string &name_arg) const;
|
||||||
vector<int> getLhs(const string &name_arg) const;
|
vector<int> getLhs(const string &name_arg) const;
|
||||||
|
vector<int> getLhsOrigIds(const string &name_arg) const;
|
||||||
map<string, pair<SymbolList, int>> getSymbolListAndOrder() const;
|
map<string, pair<SymbolList, int>> getSymbolListAndOrder() const;
|
||||||
vector<set<pair<int, int>>> getRhs(const string &name_arg) const;
|
vector<set<pair<int, int>>> getRhs(const string &name_arg) const;
|
||||||
vector<expr_t> getLhsExprT(const string &name_arg) const;
|
vector<expr_t> getLhsExprT(const string &name_arg) const;
|
||||||
|
|
Loading…
Reference in New Issue