Commit 539fddc9 authored by Houtan Bastani's avatar Houtan Bastani

introduce var model table

parent 6015b0e5
......@@ -380,154 +380,6 @@ PacModelStatement::getPacModelInfoForPacExpectation() const
return { name, aux_model_name, growth_symb_id };
}
VarModelStatement::VarModelStatement(SymbolList symbol_list_arg,
OptionsList options_list_arg,
string name_arg,
const SymbolTable &symbol_table_arg) :
symbol_list(move(symbol_list_arg)),
options_list(move(options_list_arg)),
name{move(name_arg)},
symbol_table(symbol_table_arg)
{
}
void
VarModelStatement::getVarModelInfo(string &var_model_name,
map<string, pair<SymbolList, int>> &var_model_info,
map<string, vector<string>> &var_model_eqtags) const
{
var_model_name = name;
if (symbol_list.empty())
{
auto it = options_list.vector_str_options.find("var.eqtags");
var_model_eqtags[name] = it->second;
}
else
{
auto it = options_list.num_options.find("var.order");
var_model_info[name] = { symbol_list, stoi(it->second) };
}
}
void
VarModelStatement::fillVarModelInfoFromEquations(vector<int> &eqnumber_arg, vector<int> &lhs_arg,
vector<set<pair<int, int>>> &rhs_arg,
vector<bool> &nonstationary_arg,
vector<bool> &diff_arg,
vector<int> &orig_diff_var_arg,
int max_lag_arg)
{
eqnumber = eqnumber_arg;
lhs = lhs_arg;
rhs_by_eq = rhs_arg;
nonstationary = nonstationary_arg;
diff = diff_arg;
orig_diff_var = orig_diff_var_arg;
max_lag = max_lag_arg;
}
void
VarModelStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings)
{
}
void
VarModelStatement::writeOutput(ostream &output, const string &basename, bool minimal_workspace) const
{
options_list.writeOutput(output);
if (!symbol_list.empty())
symbol_list.writeOutput("options_.var.var_list_", output);
output << "M_.var." << name << " = options_.var;" << endl
<< "options_ = rmfield(options_, 'var');" << endl;
output << "M_.var." << name << ".eqn = [";
for (auto it = eqnumber.begin();
it != eqnumber.end(); it++)
{
if (it != eqnumber.begin())
output << " ";
output << *it + 1;
}
output << "];" << endl
<< "M_.var." << name << ".lhs = [";
for (auto it = lhs.begin();
it != lhs.end(); it++)
{
if (it != lhs.begin())
output << " ";
output << symbol_table.getTypeSpecificID(*it) + 1;
}
output << "];" << endl
<< "M_.var." << name << ".max_lag = " << max_lag << ";" << endl
<< "M_.var." << name << ".nonstationary = [";
for (auto it = nonstationary.begin();
it != nonstationary.end(); it++)
{
if (it != nonstationary.begin())
output << " ";
if (*it)
output << "true";
else
output << "false";
}
output << "];" << endl
<< "M_.var." << name << ".diff = [";
for (auto it = diff.begin();
it != diff.end(); it++)
{
if (it != diff.begin())
output << " ";
if (*it)
output << "true";
else
output << "false";
}
output << "];" << endl
<< "M_.var." << name << ".orig_diff_var = [";
for (auto it = orig_diff_var.begin();
it != orig_diff_var.end(); it++)
{
if (it != orig_diff_var.begin())
output << " ";
if (*it == -1)
output << -1;
else
output << symbol_table.getTypeSpecificID(*it) + 1;
}
output << "];" << endl;
int i = 1;
for (auto it = rhs_by_eq.begin();
it != rhs_by_eq.end(); it++, i++)
{
output << "M_.var." << name << ".rhs.vars_at_eq{" << i << "}.var = [";
for (auto it1 = it->begin();
it1 != it->end(); it1++)
{
if (it1 != it->begin())
output << " ";
output << symbol_table.getTypeSpecificID(it1->first) + 1;
}
output << "];" << endl
<< "M_.var." << name << ".rhs.vars_at_eq{" << i << "}.lag = [";
for (auto it1 = it->begin();
it1 != it->end(); it1++)
{
if (it1 != it->begin())
output << " ";
output << it1->second;
}
output << "];" << endl;
}
}
void
VarModelStatement::writeJsonOutput(ostream &output) const
{
output << "{\"statementName\": \"var_model\","
<< "\"model_name\": \"" << name << "\"}";
}
VarEstimationStatement::VarEstimationStatement(OptionsList options_list_arg) :
options_list(move(options_list_arg))
{
......@@ -2210,9 +2062,10 @@ ModelComparisonStatement::writeJsonOutput(ostream &output) const
PlannerObjectiveStatement::PlannerObjectiveStatement(SymbolTable &symbol_table,
NumericalConstants &num_constants,
ExternalFunctionsTable &external_functions_table,
TrendComponentModelTable &trend_component_model_table_arg) :
model_tree{symbol_table, num_constants,
external_functions_table, trend_component_model_table_arg}
TrendComponentModelTable &trend_component_model_table_arg,
VarModelTable &var_model_table_arg) :
model_tree{symbol_table, num_constants, external_functions_table,
trend_component_model_table_arg, var_model_table_arg}
{
}
......
......@@ -141,36 +141,6 @@ public:
tuple<string, string, int> getPacModelInfoForPacExpectation() const;
};
class VarModelStatement : public Statement
{
public:
const SymbolList symbol_list;
const OptionsList options_list;
const string name;
private:
const SymbolTable &symbol_table;
vector<int> eqnumber, lhs, orig_diff_var;
vector<set<pair<int, int>>> rhs_by_eq; // rhs by equation
vector<bool> nonstationary, diff;
int max_lag;
public:
VarModelStatement(SymbolList symbol_list_arg,
OptionsList options_list_arg,
string name_arg,
const SymbolTable &symbol_table_arg);
void getVarModelInfo(string &var_model_name,
map<string, pair<SymbolList, int>> &var_model_info,
map<string, vector<string>> &var_model_eqtags) const;
void fillVarModelInfoFromEquations(vector<int> &eqnumber_arg, vector<int> &lhs_arg,
vector<set<pair<int, int>>> &rhs_arg,
vector<bool> &nonstationary_arg,
vector<bool> &diff_arg, vector<int> &orig_diff_var_arg,
int max_lag_arg);
void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) override;
void writeOutput(ostream &output, const string &basename, bool minimal_workspace) const override;
void writeJsonOutput(ostream &output) const override;
};
class VarRestrictionsStatement : public Statement
{
private:
......@@ -541,7 +511,8 @@ public:
PlannerObjectiveStatement(SymbolTable &symbol_table,
NumericalConstants &num_constants,
ExternalFunctionsTable &external_functions_table,
TrendComponentModelTable &trend_component_model_table_arg);
TrendComponentModelTable &trend_component_model_table_arg,
VarModelTable &var_model_table_arg);
/*! \todo check there are only endogenous variables at the current period in the objective
(no exogenous, no lead/lag) */
void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) override;
......
......@@ -29,11 +29,13 @@
DataTree::DataTree(SymbolTable &symbol_table_arg,
NumericalConstants &num_constants_arg,
ExternalFunctionsTable &external_functions_table_arg,
TrendComponentModelTable &trend_component_model_table_arg) :
TrendComponentModelTable &trend_component_model_table_arg,
VarModelTable &var_model_table_arg) :
symbol_table(symbol_table_arg),
num_constants(num_constants_arg),
external_functions_table(external_functions_table_arg),
trend_component_model_table(trend_component_model_table_arg),
var_model_table(var_model_table_arg),
node_counter(0)
{
Zero = AddNonNegativeConstant("0");
......
......@@ -59,6 +59,8 @@ protected:
ExternalFunctionsTable &external_functions_table;
//! A reference to the trend component model table
TrendComponentModelTable &trend_component_model_table;
//! A reference to the VAR model table
VarModelTable &var_model_table;
//! num_constant_id -> NumConstNode
using num_const_node_map_t = map<int, NumConstNode *>;
......@@ -136,7 +138,8 @@ public:
DataTree(SymbolTable &symbol_table_arg,
NumericalConstants &num_constants_arg,
ExternalFunctionsTable &external_functions_table_arg,
TrendComponentModelTable &trend_component_model_table_arg);
TrendComponentModelTable &trend_component_model_table_arg,
VarModelTable &var_model_table_arg);
virtual
~DataTree();
......
This diff is collapsed.
......@@ -250,7 +250,11 @@ private:
void findPacExpectationEquationNumbers(vector<int> &eqnumber) const;
public:
DynamicModel(SymbolTable &symbol_table_arg, NumericalConstants &num_constants_arg, ExternalFunctionsTable &external_functions_table_arg, TrendComponentModelTable &trend_component_model_table_arg);
DynamicModel(SymbolTable &symbol_table_arg,
NumericalConstants &num_constants_arg,
ExternalFunctionsTable &external_functions_table_arg,
TrendComponentModelTable &trend_component_model_table_arg,
VarModelTable &var_model_table_arg);
//! Adds a variable node
/*! This implementation allows for non-zero lag */
VariableNode *AddVariable(int symb_id, int lag = 0) override;
......@@ -304,8 +308,14 @@ public:
void fillTrendComponentModelTable() const;
void fillTrendComponentModelTableFromOrigModel(StaticModel &static_model) const;
//! Add aux equations (and aux variables) for variables declared in var_model at max order if they don't already exist
// void addEquationsForVar(map<string, pair<SymbolList, int>> &var_model_info);
//! Fill the Var Model Table
void fillVarModelTable() const;
void fillVarModelTableFromOrigModel(StaticModel &static_model) const;
//! Add aux equations (and aux variables) for variables declared in var_model
//! at max order if they don't already exist
void addEquationsForVar();
//! Get Pac equation parameter info
void walkPacParameters();
//! Add var_model info to pac_expectation nodes
......
This diff is collapsed.
/*
* Copyright (C) 2006-2017 Dynare Team
* Copyright (C) 2006-2018 Dynare Team
*
* This file is part of Dynare.
*
......@@ -52,6 +52,8 @@ public:
ExternalFunctionsTable external_functions_table;
//! Numerical constants table
NumericalConstants num_constants;
//! Var Model Table used for storing info about trend component models
VarModelTable var_model_table;
//! Trend Component Model Table used for storing info about trend component models
TrendComponentModelTable trend_component_model_table;
//! Expressions outside model block
......
......@@ -1004,9 +1004,10 @@ ModelTree::BlockLinear(const blocks_derivatives_t &blocks_derivatives, const vec
ModelTree::ModelTree(SymbolTable &symbol_table_arg,
NumericalConstants &num_constants_arg,
ExternalFunctionsTable &external_functions_table_arg,
TrendComponentModelTable &trend_component_model_table_arg) :
DataTree(symbol_table_arg, num_constants_arg,
external_functions_table_arg, trend_component_model_table_arg),
TrendComponentModelTable &trend_component_model_table_arg,
VarModelTable &var_model_table_arg) :
DataTree(symbol_table_arg, num_constants_arg, external_functions_table_arg,
trend_component_model_table_arg, var_model_table_arg),
cutoff(1e-15),
mfs(0)
......
......@@ -324,7 +324,8 @@ public:
ModelTree(SymbolTable &symbol_table_arg,
NumericalConstants &num_constants_arg,
ExternalFunctionsTable &external_functions_table_arg,
TrendComponentModelTable &trend_component_model_table_arg);
TrendComponentModelTable &trend_component_model_table_arg,
VarModelTable &var_model_table_arg);
//! Absolute value under which a number is considered to be zero
double cutoff;
//! Compute the minimum feedback set
......
......@@ -349,7 +349,8 @@ ParsingDriver::declare_or_change_type(SymbolType new_type, const string &name)
auto dm = make_unique<DynamicModel>(mod_file->symbol_table,
mod_file->num_constants,
mod_file->external_functions_table,
mod_file->trend_component_model_table);
mod_file->trend_component_model_table,
mod_file->var_model_table);
mod_file->dynamic_model.updateAfterVariableChange(*dm);
// remove error messages
......@@ -1444,25 +1445,34 @@ ParsingDriver::trend_component_model()
void
ParsingDriver::var_model()
{
OptionsList::string_options_t::const_iterator it = options_list.string_options.find("var.model_name");
if (it == options_list.string_options.end())
const auto its = options_list.string_options.find("var.model_name");
if (its == options_list.string_options.end())
error("You must pass the model_name option to the var_model statement.");
auto name = it->second;
if (options_list.vector_str_options.find("var.eqtags") != options_list.vector_str_options.end())
if (!symbol_list.empty())
error("You cannot pass a symbol list when passing equation tags to the var_model statement");
else if (options_list.num_options.find("var.order") != options_list.num_options.end())
error("You cannot pass the order option when passing equation tags to the var_model statement");
auto name = its->second;
int order = 0;
const auto itn = options_list.num_options.find("var.order");
if (itn != options_list.num_options.end())
order = stoi(itn->second);
else
if (!symbol_list.empty())
if (options_list.num_options.find("var.order") == options_list.num_options.end())
error("You must pass the order option when passing a symbol list to the var_model statement");
error("You must pass the order option when passing a symbol list to the var_model statement");
mod_file->addStatement(new VarModelStatement(symbol_list, options_list, name, mod_file->symbol_table));
var_map[it->second] = symbol_list.getSymbols();
vector<string> eqtags;
const auto itvs = options_list.vector_str_options.find("var.eqtags");
if (itvs != options_list.vector_str_options.end())
{
eqtags = itvs->second;
if (!symbol_list.empty())
error("You cannot pass a symbol list when passing equation tags to the var_model statement");
else if (itn != options_list.num_options.end())
error("You cannot pass the order option when passing equation tags to the var_model statement");
}
mod_file->var_model_table.addVarModel(name, eqtags, make_pair(symbol_list, order));
symbol_list.clear();
options_list.clear();
var_map[its->second] = symbol_list.getSymbols();
}
void
......@@ -2071,8 +2081,8 @@ ParsingDriver::begin_planner_objective()
planner_objective_statement = new PlannerObjectiveStatement(mod_file->symbol_table,
mod_file->num_constants,
mod_file->external_functions_table,
mod_file->trend_component_model_table);
mod_file->trend_component_model_table,
mod_file->var_model_table);
set_current_data_tree(&planner_objective_statement->getPlannerObjective());
}
......
......@@ -32,9 +32,10 @@
StaticModel::StaticModel(SymbolTable &symbol_table_arg,
NumericalConstants &num_constants_arg,
ExternalFunctionsTable &external_functions_table_arg,
TrendComponentModelTable &trend_component_model_table_arg) :
ModelTree(symbol_table_arg, num_constants_arg,
external_functions_table_arg, trend_component_model_table_arg),
TrendComponentModelTable &trend_component_model_table_arg,
VarModelTable &var_model_table_arg) :
ModelTree(symbol_table_arg, num_constants_arg, external_functions_table_arg,
trend_component_model_table_arg, var_model_table_arg),
global_temporary_terms(true)
{
}
......
......@@ -163,7 +163,8 @@ public:
StaticModel(SymbolTable &symbol_table_arg,
NumericalConstants &num_constants,
ExternalFunctionsTable &external_functions_table_arg,
TrendComponentModelTable &trend_component_model_table_arg);
TrendComponentModelTable &trend_component_model_table_arg,
VarModelTable &var_model_table_arg);
//! Writes information on block decomposition when relevant
void writeOutput(ostream &output, bool block) const;
......
/*
* Copyright (C) 2010-2017 Dynare Team
* Copyright (C) 2010-2018 Dynare Team
*
* This file is part of Dynare.
*
......@@ -26,9 +26,10 @@ SteadyStateModel::SteadyStateModel(SymbolTable &symbol_table_arg,
NumericalConstants &num_constants_arg,
ExternalFunctionsTable &external_functions_table_arg,
TrendComponentModelTable &trend_component_model_table_arg,
VarModelTable &var_model_table_arg,
const StaticModel &static_model_arg) :
DataTree(symbol_table_arg, num_constants_arg,
external_functions_table_arg, trend_component_model_table_arg),
DataTree(symbol_table_arg, num_constants_arg, external_functions_table_arg,
trend_component_model_table_arg, var_model_table_arg),
static_model(static_model_arg)
{
}
......
/*
* Copyright (C) 2010-2017 Dynare Team
* Copyright (C) 2010-2018 Dynare Team
*
* This file is part of Dynare.
*
......@@ -39,6 +39,7 @@ public:
NumericalConstants &num_constants_arg,
ExternalFunctionsTable &external_functions_table_arg,
TrendComponentModelTable &trend_component_model_table_arg,
VarModelTable &var_model_table_arg,
const StaticModel &static_model_arg);
//! Add an expression of the form "var = expr;"
void addDefinition(int symb_id, expr_t expr);
......
......@@ -301,3 +301,241 @@ TrendComponentModelTable::writeJsonOutput(ostream &output) const
output << "]}";
}
}
VarModelTable::VarModelTable(SymbolTable &symbol_table_arg) :
symbol_table(symbol_table_arg)
{
}
void
VarModelTable::addVarModel(string name_arg, vector<string> eqtags_arg,
pair<SymbolList, int> symbol_list_and_order_arg)
{
if (isExistingVarModelName(name_arg))
{
cerr << "Error: a VAR model already exists with the name " << name_arg << endl;
exit(EXIT_FAILURE);
}
eqtags[name_arg] = move(eqtags_arg);
symbol_list_and_order[name_arg] = move(symbol_list_and_order_arg);
names.insert(move(name_arg));
}
map<string, pair<SymbolList, int>>
VarModelTable::getSymbolListAndOrder() const
{
return symbol_list_and_order;
}
void
VarModelTable::writeOutput(ostream &output) const
{
for (const auto &name : names)
{
output << "M_.var." << name << ".model_name = '" << name << "';" << endl;
if (!symbol_list_and_order.empty())
{
symbol_list_and_order.at(name).first.writeOutput("M_.var." + name + ".var_list_", output);
output << "M_.var." << name << ".order = "
<< symbol_list_and_order.at(name).second << ";" << endl;
}
output << "M_.var." << name << ".eqtags = {";
for (const auto &it : eqtags.at(name))
output << "'" << it << "'; ";
output << "};" << endl
<< "M_.var." << name << ".eqn = [";
for (auto it : eqnums.at(name))
output << it + 1 << " ";
output << "];" << endl
<< "M_.var." << name << ".lhs = [";
for (auto it : lhs.at(name))
output << symbol_table.getTypeSpecificID(it) + 1 << " ";
output << "];" << endl
<< "M_.var." << name << ".max_lag = [";
for (auto it : max_lags.at(name))
output << it << " ";
output << "];" << endl
<< "M_.var." << name << ".diff = [";
for (const auto &it : diff.at(name))
output << (it ? "true" : "false") << " ";
output << "];" << endl
<< "M_.var." << name << ".orig_diff_var = [";
for (auto it : orig_diff_var.at(name))
output << (it >= 0 ? symbol_table.getTypeSpecificID(it) + 1 : -1) << " ";
output << "];" << endl;
int i = 1;
for (const auto &it : rhs.at(name))
{
output << "M_.var." << name << ".rhs.vars_at_eq{" << i << "}.var = [";
for (const auto &it1 : it)
output << symbol_table.getTypeSpecificID(it1.first) + 1 << " ";
output << "];" << endl
<< "M_.var." << name << ".rhs.vars_at_eq{" << i << "}.lag = [";
for (const auto &it1 : it)
output << it1.second << " ";
output << "];" << endl;
i++;
}
}
}
void
VarModelTable::writeJsonOutput(ostream &output) const
{
for (const auto &name : names)
{
if (name != *(names.begin()))
output << ", ";
output << "{\"statementName\": \"var_model\","
<< "\"model_name\": \"" << name << "\",";
if (symbol_list_and_order.empty())
{
output << "\"eqtags\": [";
for (const auto &it : eqtags.at(name))
{
output << "\"" << it << "\"";
if (&it != &eqtags.at(name).back())
output << ", ";
}
output << "]";
}
else
{
output << "\"order\": \"" << symbol_list_and_order.at(name).second << "\",";
}
output << "}";
}
}
map<string, vector<string>>
VarModelTable::getEqTags() const
{
return eqtags;
}
vector<string>
VarModelTable::getEqTags(const string &name_arg) const
{
checkModelName(name_arg);
return eqtags.find(name_arg)->second;
}
void
VarModelTable::checkModelName(const string &name_arg) const
{
if (!isExistingVarModelName(name_arg))
{
cerr << name_arg
<< " is not a recognized equation tag of a VAR model equation" << endl;
exit(EXIT_FAILURE);
}
}
void
VarModelTable::setEqNums(map<string, vector<int>> eqnums_arg)
{
eqnums = move(eqnums_arg);
}
void
VarModelTable::setLhs(map<string, vector<int>> lhs_arg)
{
lhs = move(lhs_arg);
}
void
VarModelTable::setRhs(map<string, vector<set<pair<int, int>>>> rhs_arg)
{
rhs = move(rhs_arg);
}
void
VarModelTable::setLhsExprT(map<string, vector<expr_t>> lhs_expr_t_arg)
{
lhs_expr_t = move(lhs_expr_t_arg);
}
void
VarModelTable::setNonstationary(map<string, vector<bool>> nonstationary_arg)
{
nonstationary = move(nonstationary_arg);
}
map<string, vector<int>>
VarModelTable::getEqNums() const
{
return eqnums;
}
vector<int>
VarModelTable::getEqNums(const string &name_arg) const
{
checkModelName(name_arg);
return eqnums.find(name_arg)->second;
}
void