remove extra exo’s for shocks statement, nonstationary, and trend vars; fix bugs

issue#70
Houtan Bastani 2015-03-10 14:10:10 +01:00
parent 5cc4ad033f
commit 81a2a2ee0d
5 changed files with 164 additions and 2 deletions

View File

@ -3425,6 +3425,60 @@ DynamicModel::writeDynamicFile(const string &basename, bool block, bool bytecode
writeDynamicMFile(t_basename);
}
void
DynamicModel::reindexTrendSymbolsMap(DynamicModel &dynamic_model, SymbolTable &orig_symbol_table)
{
map<int, expr_t> orig_trend_symbols_map = trend_symbols_map;
trend_symbols_map.clear();
for (map<int, expr_t>::const_iterator it = orig_trend_symbols_map.begin();
it != orig_trend_symbols_map.end(); it++)
try
{
vector<int> symb_id (1, symbol_table.getID(orig_symbol_table.getName(it->first)));
addTrendVariables(symb_id,
it->second->cloneDynamicReindex(dynamic_model, orig_symbol_table));
}
catch(...)
{
cerr << "Error: unused exo in trend symbols." << endl;
exit(EXIT_FAILURE);
}
}
void
DynamicModel::reindexNonstationarySymbolsMap(DynamicModel &dynamic_model, SymbolTable &orig_symbol_table)
{
nonstationary_symbols_map_t orig_nonstationary_symbols_map = nonstationary_symbols_map;
nonstationary_symbols_map.clear();
for (nonstationary_symbols_map_t::const_iterator it = orig_nonstationary_symbols_map.begin();
it != orig_nonstationary_symbols_map.end(); it++)
try
{
vector<int> symb_id (1, symbol_table.getID(orig_symbol_table.getName(it->first)));
addNonstationaryVariables(symb_id,
it->second.first,
it->second.second->cloneDynamicReindex(dynamic_model, orig_symbol_table));
}
catch(...)
{
cerr << "Error: unused exo in nonstationary symbols." << endl;
exit(EXIT_FAILURE);
}
}
void
DynamicModel::resetDataTree()
{
num_const_node_map.clear();
variable_node_map.clear();
unary_op_node_map.clear();
binary_op_node_map.clear();
trinary_op_node_map.clear();
external_function_node_map.clear();
first_deriv_external_function_node_map.clear();
second_deriv_external_function_node_map.clear();
}
void
DynamicModel::reindexEquations(DynamicModel &dynamic_model, SymbolTable &orig_symbol_table)
{

View File

@ -238,6 +238,15 @@ public:
//! reindex equations after change in symbol_table
void reindexEquations(DynamicModel &dynamic_model, SymbolTable &orig_symbol_table);
//! reindex trend_symbol_map after change in symbol_table
void reindexTrendSymbolsMap(DynamicModel &dynamic_model, SymbolTable &orig_symbol_table);
//! reindex nonstationary_symbol_map after change in symbol_table
void reindexNonstationarySymbolsMap(DynamicModel &dynamic_model, SymbolTable &orig_symbol_table);
//! reset DataTree vars
void resetDataTree();
//! Replaces model equations with derivatives of Lagrangian w.r.t. endogenous
void computeRamseyPolicyFOCs(const StaticModel &static_model);
//! Replaces the model equations in dynamic_model with those in this model
@ -248,7 +257,7 @@ public:
//! Returns number of static only equations
size_t staticOnlyEquationsNbr() const;
//! Returns number of dynamic only equations
size_t dynamicOnlyEquationsNbr() const;

View File

@ -313,6 +313,7 @@ ModFile::transformPass(bool nostrict)
{
SymbolTable orig_symbol_table = symbol_table;
symbol_table.rmExo(unusedExo);
dynamic_model.resetDataTree();
dynamic_model.reindexEquations(dynamic_model, orig_symbol_table);
vector<Statement *> orig_statements = statements;
statements.clear();

View File

@ -67,6 +67,31 @@ AbstractShocksStatement::writeDetShocks(ostream &output) const
output << "M_.exo_det_length = " << exo_det_length << ";\n";
}
AbstractShocksStatement::det_shocks_t
AbstractShocksStatement::reindexDetShocksSymbIds(DataTree &dynamic_datatree, SymbolTable &orig_symbol_table)
{
det_shocks_t new_det_shocks;
SymbolTable *new_symbol_table = dynamic_datatree.getSymbolTable();
for (det_shocks_t::const_iterator it=det_shocks.begin(); it!=det_shocks.end(); it++)
try
{
vector<DetShockElement> det_shock_vec;
for (int i=0; i<it->second.size(); i++)
{
DetShockElement dse;
dse.period1 = it->second[1].period1;
dse.period2 = it->second[1].period2;
dse.value = it->second[i].value->cloneDynamicReindex(dynamic_datatree, orig_symbol_table);
det_shock_vec.push_back(dse);
}
new_det_shocks[new_symbol_table->getID(orig_symbol_table.getName(it->first))] = det_shock_vec;
}
catch (...)
{
}
return new_det_shocks;
}
ShocksStatement::ShocksStatement(bool overwrite_arg,
const det_shocks_t &det_shocks_arg,
const var_and_std_shocks_t &var_shocks_arg,
@ -332,6 +357,69 @@ ShocksStatement::has_calibrated_measurement_errors() const
return false;
}
Statement *
ShocksStatement::cloneAndReindexSymbIds(DataTree &dynamic_datatree, SymbolTable &orig_symbol_table)
{
var_and_std_shocks_t new_var_shocks, new_std_shocks;
covar_and_corr_shocks_t new_covar_shocks, new_corr_shocks;
SymbolTable *new_symbol_table = dynamic_datatree.getSymbolTable();
for (var_and_std_shocks_t::const_iterator it = var_shocks.begin();
it != var_shocks.end(); it++)
try
{
new_var_shocks[new_symbol_table->getID(orig_symbol_table.getName(it->first))] =
it->second->cloneDynamicReindex(dynamic_datatree, orig_symbol_table);
}
catch (...)
{
}
for (var_and_std_shocks_t::const_iterator it = std_shocks.begin();
it != std_shocks.end(); it++)
try
{
new_std_shocks[new_symbol_table->getID(orig_symbol_table.getName(it->first))] =
it->second->cloneDynamicReindex(dynamic_datatree, orig_symbol_table);
}
catch (...)
{
}
for (covar_and_corr_shocks_t::const_iterator it = covar_shocks.begin();
it != covar_shocks.end(); it++)
try
{
new_covar_shocks[make_pair(new_symbol_table->getID(orig_symbol_table.getName(it->first.first)),
new_symbol_table->getID(orig_symbol_table.getName(it->first.second)))] =
it->second->cloneDynamicReindex(dynamic_datatree, orig_symbol_table);
}
catch (...)
{
}
for (covar_and_corr_shocks_t::const_iterator it = corr_shocks.begin();
it != corr_shocks.end(); it++)
try
{
new_corr_shocks[make_pair(new_symbol_table->getID(orig_symbol_table.getName(it->first.first)),
new_symbol_table->getID(orig_symbol_table.getName(it->first.second)))] =
it->second->cloneDynamicReindex(dynamic_datatree, orig_symbol_table);
}
catch (...)
{
}
return new ShocksStatement(overwrite,
reindexDetShocksSymbIds(dynamic_datatree, orig_symbol_table),
new_var_shocks,
new_std_shocks,
new_covar_shocks,
new_corr_shocks,
*(dynamic_datatree.getSymbolTable()));
}
MShocksStatement::MShocksStatement(bool overwrite_arg,
const det_shocks_t &det_shocks_arg,
const SymbolTable &symbol_table_arg) :
@ -352,6 +440,14 @@ MShocksStatement::writeOutput(ostream &output, const string &basename) const
writeDetShocks(output);
}
Statement *
MShocksStatement::cloneAndReindexSymbIds(DataTree &dynamic_datatree, SymbolTable &orig_symbol_table)
{
return new MShocksStatement(overwrite,
reindexDetShocksSymbIds(dynamic_datatree, orig_symbol_table),
*(dynamic_datatree.getSymbolTable()));
}
ConditionalForecastPathsStatement::ConditionalForecastPathsStatement(const AbstractShocksStatement::det_shocks_t &paths_arg) :
paths(paths_arg),
path_length(-1)

View File

@ -50,7 +50,7 @@ protected:
const det_shocks_t det_shocks;
const SymbolTable &symbol_table;
void writeDetShocks(ostream &output) const;
det_shocks_t reindexDetShocksSymbIds(DataTree &dynamic_datatree, SymbolTable &orig_symbol_table);
AbstractShocksStatement(bool mshocks_arg, bool overwrite_arg,
const det_shocks_t &det_shocks_arg,
const SymbolTable &symbol_table_arg);
@ -79,6 +79,7 @@ public:
const SymbolTable &symbol_table_arg);
virtual void writeOutput(ostream &output, const string &basename) const;
virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings);
virtual Statement *cloneAndReindexSymbIds(DataTree &dynamic_datatree, SymbolTable &orig_symbol_table);
};
class MShocksStatement : public AbstractShocksStatement
@ -88,6 +89,7 @@ public:
const det_shocks_t &det_shocks_arg,
const SymbolTable &symbol_table_arg);
virtual void writeOutput(ostream &output, const string &basename) const;
virtual Statement *cloneAndReindexSymbIds(DataTree &dynamic_datatree, SymbolTable &orig_symbol_table);
};
class ConditionalForecastPathsStatement : public Statement