finishing ramsey_model implementation

issue#70
Michel Juillard 2014-03-09 09:52:44 +01:00
parent 6364e74a3e
commit 8d042b8121
7 changed files with 41 additions and 30 deletions

View File

@ -202,6 +202,10 @@ RamseyModelStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsol
void void
RamseyModelStatement::writeOutput(ostream &output, const string &basename) const RamseyModelStatement::writeOutput(ostream &output, const string &basename) const
{ {
// options_.ramsey_policy indicates that a Ramsey model is present in the *.mod file
// this affects the computation of the steady state that uses a special algorithm
// It should probably rather be a M_ field, but we leave it in options_ for historical reason
output << "options_.ramsey_policy = 1;\n";
} }
RamseyPolicyStatement::RamseyPolicyStatement(const SymbolList &symbol_list_arg, RamseyPolicyStatement::RamseyPolicyStatement(const SymbolList &symbol_list_arg,
@ -214,6 +218,10 @@ RamseyPolicyStatement::RamseyPolicyStatement(const SymbolList &symbol_list_arg,
void void
RamseyPolicyStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) RamseyPolicyStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings)
{ {
// ramsey_model_present indicates that the model is augmented with the FOC of the planner problem
mod_file_struct.ramsey_model_present = true;
// ramsey_policy_present indicates that ramsey_policy instruction for computation of first order approximation
// of a stochastic Ramsey problem if present in the *.mod file
mod_file_struct.ramsey_policy_present = true; mod_file_struct.ramsey_policy_present = true;
/* Fill in option_order of mod_file_struct /* Fill in option_order of mod_file_struct

View File

@ -39,7 +39,7 @@ ModFile::ModFile(WarningConsolidation &warnings_arg)
steady_state_model(symbol_table, num_constants, external_functions_table, static_model), steady_state_model(symbol_table, num_constants, external_functions_table, static_model),
linear(false), block(false), byte_code(false), use_dll(false), no_static(false), linear(false), block(false), byte_code(false), use_dll(false), no_static(false),
differentiate_forward_vars(false), differentiate_forward_vars(false),
nonstationary_variables(false), ramsey_policy_orig_eqn_nbr(0), nonstationary_variables(false), ramsey_model_orig_eqn_nbr(0),
warnings(warnings_arg) warnings(warnings_arg)
{ {
} }
@ -113,7 +113,7 @@ ModFile::checkPass()
(*it)->checkPass(mod_file_struct, warnings); (*it)->checkPass(mod_file_struct, warnings);
// Check the steady state block // Check the steady state block
steady_state_model.checkPass(mod_file_struct.ramsey_policy_present, warnings); steady_state_model.checkPass(mod_file_struct.ramsey_model_present, warnings);
// If order option has not been set, default to 2 // If order option has not been set, default to 2
if (!mod_file_struct.order_option) if (!mod_file_struct.order_option)
@ -136,12 +136,12 @@ ModFile::checkPass()
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (((mod_file_struct.ramsey_policy_present || mod_file_struct.discretionary_policy_present) if (((mod_file_struct.ramsey_model_present || mod_file_struct.discretionary_policy_present)
&& !mod_file_struct.planner_objective_present) && !mod_file_struct.planner_objective_present)
|| (!(mod_file_struct.ramsey_policy_present || mod_file_struct.discretionary_policy_present) || (!(mod_file_struct.ramsey_model_present || mod_file_struct.discretionary_policy_present)
&& mod_file_struct.planner_objective_present)) && mod_file_struct.planner_objective_present))
{ {
cerr << "ERROR: A planner_objective statement must be used with a ramsey_policy or a discretionary_policy statement and vice versa." << endl; cerr << "ERROR: A planner_objective statement must be used with a ramsey_model, a ramsey_policy or a discretionary_policy statement and vice versa." << endl;
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -246,9 +246,9 @@ ModFile::checkPass()
} }
if (dynamic_model.staticOnlyEquationsNbr() > 0 && if (dynamic_model.staticOnlyEquationsNbr() > 0 &&
(mod_file_struct.ramsey_policy_present || mod_file_struct.discretionary_policy_present)) (mod_file_struct.ramsey_model_present || mod_file_struct.discretionary_policy_present))
{ {
cerr << "ERROR: marking equations as [static] or [dynamic] is not possible with ramsey_policy or discretionary_policy" << endl; cerr << "ERROR: marking equations as [static] or [dynamic] is not possible with ramsey_model, ramsey_policy or discretionary_policy" << endl;
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -330,7 +330,7 @@ ModFile::transformPass(bool nostrict)
dynamic_model.removeTrendVariableFromEquations(); dynamic_model.removeTrendVariableFromEquations();
} }
if (mod_file_struct.ramsey_policy_present) if (mod_file_struct.ramsey_model_present)
{ {
StaticModel *planner_objective = NULL; StaticModel *planner_objective = NULL;
for (vector<Statement *>::iterator it = statements.begin(); it != statements.end(); it++) for (vector<Statement *>::iterator it = statements.begin(); it != statements.end(); it++)
@ -340,7 +340,7 @@ ModFile::transformPass(bool nostrict)
planner_objective = pos->getPlannerObjective(); planner_objective = pos->getPlannerObjective();
} }
assert(planner_objective != NULL); assert(planner_objective != NULL);
ramsey_policy_orig_eqn_nbr = dynamic_model.equation_number(); ramsey_model_orig_eqn_nbr = dynamic_model.equation_number();
/* /*
clone the model then clone the new equations back to the original because clone the model then clone the new equations back to the original because
@ -395,11 +395,11 @@ ModFile::transformPass(bool nostrict)
/* /*
Enforce the same number of equations and endogenous, except in three cases: Enforce the same number of equations and endogenous, except in three cases:
- ramsey_policy is used - ramsey_model, ramsey_policy or discretionary_policy is used
- a BVAR command is used and there is no equation (standalone BVAR estimation) - a BVAR command is used and there is no equation (standalone BVAR estimation)
- nostrict option is passed and there are more endogs than equations (dealt with before freeze) - nostrict option is passed and there are more endogs than equations (dealt with before freeze)
*/ */
if (!(mod_file_struct.ramsey_policy_present || mod_file_struct.discretionary_policy_present) if (!(mod_file_struct.ramsey_model_present || mod_file_struct.discretionary_policy_present)
&& !(mod_file_struct.bvar_present && dynamic_model.equation_number() == 0) && !(mod_file_struct.bvar_present && dynamic_model.equation_number() == 0)
&& (dynamic_model.equation_number() != symbol_table.endo_nbr())) && (dynamic_model.equation_number() != symbol_table.endo_nbr()))
{ {
@ -419,11 +419,11 @@ ModFile::transformPass(bool nostrict)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (!mod_file_struct.ramsey_policy_present) if (!mod_file_struct.ramsey_model_present)
cout << "Found " << dynamic_model.equation_number() << " equation(s)." << endl; cout << "Found " << dynamic_model.equation_number() << " equation(s)." << endl;
else else
{ {
cout << "Found " << ramsey_policy_orig_eqn_nbr << " equation(s)." << endl; cout << "Found " << ramsey_model_orig_eqn_nbr << " equation(s)." << endl;
cout << "Found " << dynamic_model.equation_number() << " FOC equation(s) for Ramsey Problem." << endl; cout << "Found " << dynamic_model.equation_number() << " FOC equation(s) for Ramsey Problem." << endl;
} }
@ -461,7 +461,7 @@ ModFile::computingPass(bool no_tmp_terms, FileOutputType output)
{ {
if (mod_file_struct.stoch_simul_present if (mod_file_struct.stoch_simul_present
|| mod_file_struct.estimation_present || mod_file_struct.osr_present || mod_file_struct.estimation_present || mod_file_struct.osr_present
|| mod_file_struct.ramsey_policy_present || mod_file_struct.identification_present || mod_file_struct.ramsey_model_present || mod_file_struct.identification_present
|| mod_file_struct.calib_smoother_present) || mod_file_struct.calib_smoother_present)
static_model.set_cutoff_to_zero(); static_model.set_cutoff_to_zero();
@ -476,7 +476,7 @@ ModFile::computingPass(bool no_tmp_terms, FileOutputType output)
if (mod_file_struct.simul_present || mod_file_struct.check_present if (mod_file_struct.simul_present || mod_file_struct.check_present
|| mod_file_struct.stoch_simul_present || mod_file_struct.stoch_simul_present
|| mod_file_struct.estimation_present || mod_file_struct.osr_present || mod_file_struct.estimation_present || mod_file_struct.osr_present
|| mod_file_struct.ramsey_policy_present || mod_file_struct.identification_present || mod_file_struct.ramsey_model_present || mod_file_struct.identification_present
|| mod_file_struct.calib_smoother_present) || mod_file_struct.calib_smoother_present)
{ {
if (mod_file_struct.simul_present) if (mod_file_struct.simul_present)
@ -485,7 +485,7 @@ ModFile::computingPass(bool no_tmp_terms, FileOutputType output)
{ {
if (mod_file_struct.stoch_simul_present if (mod_file_struct.stoch_simul_present
|| mod_file_struct.estimation_present || mod_file_struct.osr_present || mod_file_struct.estimation_present || mod_file_struct.osr_present
|| mod_file_struct.ramsey_policy_present || mod_file_struct.identification_present || mod_file_struct.ramsey_model_present || mod_file_struct.identification_present
|| mod_file_struct.calib_smoother_present) || mod_file_struct.calib_smoother_present)
dynamic_model.set_cutoff_to_zero(); dynamic_model.set_cutoff_to_zero();
if (mod_file_struct.order_option < 1 || mod_file_struct.order_option > 3) if (mod_file_struct.order_option < 1 || mod_file_struct.order_option > 3)
@ -714,8 +714,8 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, b
if (block && !byte_code) if (block && !byte_code)
mOutputFile << "addpath " << basename << ";" << endl; mOutputFile << "addpath " << basename << ";" << endl;
if (mod_file_struct.ramsey_policy_present) if (mod_file_struct.ramsey_model_present)
mOutputFile << "M_.orig_eq_nbr = " << ramsey_policy_orig_eqn_nbr << ";" << endl; mOutputFile << "M_.orig_eq_nbr = " << ramsey_model_orig_eqn_nbr << ";" << endl;
if (dynamic_model.equation_number() > 0) if (dynamic_model.equation_number() > 0)
{ {
@ -799,7 +799,7 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, b
} }
// Create steady state file // Create steady state file
steady_state_model.writeSteadyStateFile(basename, mod_file_struct.ramsey_policy_present); steady_state_model.writeSteadyStateFile(basename, mod_file_struct.ramsey_model_present);
cout << "done" << endl; cout << "done" << endl;
} }
@ -884,7 +884,7 @@ void
ModFile::writeExternalFiles(const string &basename, FileOutputType output, bool cuda) const ModFile::writeExternalFiles(const string &basename, FileOutputType output, bool cuda) const
{ {
writeModelCC(basename, cuda); writeModelCC(basename, cuda);
steady_state_model.writeSteadyStateFileCC(basename, mod_file_struct.ramsey_policy_present, cuda); steady_state_model.writeSteadyStateFileCC(basename, mod_file_struct.ramsey_model_present, cuda);
dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option); dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option);

View File

@ -93,7 +93,7 @@ public:
eval_context_t global_eval_context; eval_context_t global_eval_context;
//! Stores the original number of equations in the model_block //! Stores the original number of equations in the model_block
int ramsey_policy_orig_eqn_nbr; int ramsey_model_orig_eqn_nbr;
//! Stores the list of extra files to be transefered during a parallel run //! Stores the list of extra files to be transefered during a parallel run
/*! (i.e. option parallel_local_files of model block) */ /*! (i.e. option parallel_local_files of model block) */

View File

@ -29,6 +29,7 @@ ModFileStructure::ModFileStructure() :
osr_present(false), osr_present(false),
osr_params_present(false), osr_params_present(false),
optim_weights_present(false), optim_weights_present(false),
ramsey_model_present(false),
ramsey_policy_present(false), ramsey_policy_present(false),
discretionary_policy_present(false), discretionary_policy_present(false),
planner_objective_present(false), planner_objective_present(false),

View File

@ -48,6 +48,8 @@ public:
bool osr_params_present; bool osr_params_present;
//! Whether an optim weight statement is present //! Whether an optim weight statement is present
bool optim_weights_present; bool optim_weights_present;
//! Whether a ramsey_model statement is present
bool ramsey_model_present;
//! Whether a ramsey_policy statement is present //! Whether a ramsey_policy statement is present
bool ramsey_policy_present; bool ramsey_policy_present;
//! Whether a discretionary_objective statement is present //! Whether a discretionary_objective statement is present

View File

@ -56,7 +56,7 @@ SteadyStateModel::addMultipleDefinitions(const vector<int> &symb_ids, expr_t exp
} }
void void
SteadyStateModel::checkPass(bool ramsey_policy, WarningConsolidation &warnings) const SteadyStateModel::checkPass(bool ramsey_model, WarningConsolidation &warnings) const
{ {
if (def_table.size() == 0) if (def_table.size() == 0)
return; return;
@ -74,7 +74,7 @@ SteadyStateModel::checkPass(bool ramsey_policy, WarningConsolidation &warnings)
warnings << "WARNING: in the 'steady_state_model' block, variable '" << symbol_table.getName(symb_ids[j]) << "' is declared twice" << endl; warnings << "WARNING: in the 'steady_state_model' block, variable '" << symbol_table.getName(symb_ids[j]) << "' is declared twice" << endl;
// Check that expression has no undefined symbol // Check that expression has no undefined symbol
if (!ramsey_policy) if (!ramsey_model)
{ {
set<int> used_symbols; set<int> used_symbols;
const expr_t &expr = def_table[i].second; const expr_t &expr = def_table[i].second;
@ -105,7 +105,7 @@ SteadyStateModel::checkPass(bool ramsey_policy, WarningConsolidation &warnings)
} }
void void
SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_policy) const SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model) const
{ {
if (def_table.size() == 0) if (def_table.size() == 0)
return; return;
@ -153,7 +153,7 @@ SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_polic
} }
void void
SteadyStateModel::writeSteadyStateFileCC(const string &basename, bool ramsey_policy, bool cuda) const SteadyStateModel::writeSteadyStateFileCC(const string &basename, bool ramsey_model, bool cuda) const
{ {
string filename = basename + "_steadystate.cc"; string filename = basename + "_steadystate.cc";

View File

@ -41,15 +41,15 @@ public:
void addMultipleDefinitions(const vector<int> &symb_ids, expr_t expr); void addMultipleDefinitions(const vector<int> &symb_ids, expr_t expr);
//! Checks that definitions are in a recursive order, and that no variable is declared twice //! Checks that definitions are in a recursive order, and that no variable is declared twice
/*! /*!
\param[in] ramsey_policy Is there a ramsey_policy statement in the MOD file? If yes, then disable the check on the recursivity of the declarations \param[in] ramsey_model Is there a Ramsey model in the MOD file? If yes, then disable the check on the recursivity of the declarations
*/ */
void checkPass(bool ramsey_policy, WarningConsolidation &warnings) const; void checkPass(bool ramsey_model, WarningConsolidation &warnings) const;
//! Write the steady state file //! Write the steady state file
/*! /*!
\param[in] ramsey_policy Is there a ramsey_policy statement in the MOD file? If yes, then use the "ys" in argument of the steady state file as initial values \param[in] ramsey_model Is there a Ramsey model in the MOD file? If yes, then use the "ys" in argument of the steady state file as initial values
*/ */
void writeSteadyStateFile(const string &basename, bool ramsey_policy) const; void writeSteadyStateFile(const string &basename, bool ramsey_model) const;
void writeSteadyStateFileCC(const string &basename, bool ramsey_policy, bool cuda) const; void writeSteadyStateFileCC(const string &basename, bool ramsey_model, bool cuda) const;
}; };
#endif #endif