diff --git a/src/ComputingTasks.cc b/src/ComputingTasks.cc index 859ccfc6c99dbb2c205c5823f40b0b98786c09d1..462147523745de82b3aabc80faa70156c94166a0 100644 --- a/src/ComputingTasks.cc +++ b/src/ComputingTasks.cc @@ -1362,38 +1362,16 @@ DsampleStatement::writeJsonOutput(ostream &output) const << R"("value2": )" << val2 << "}"; } -EstimatedParamsStatement::EstimatedParamsStatement(vector<EstimationParams> estim_params_list_arg, - const SymbolTable &symbol_table_arg) : +AbstractEstimatedParamsStatement::AbstractEstimatedParamsStatement(vector<EstimationParams> estim_params_list_arg, + const SymbolTable &symbol_table_arg) : estim_params_list{move(estim_params_list_arg)}, symbol_table{symbol_table_arg} { } void -EstimatedParamsStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) +AbstractEstimatedParamsStatement::commonCheckPass() const { - for (const auto &it : estim_params_list) - { - if (it.name == "dsge_prior_weight") - mod_file_struct.dsge_prior_weight_in_estimated_params = true; - - // Handle case of degenerate beta prior - if (it.prior == PriorDistributions::beta) - try - { - if (it.mean->eval(eval_context_t()) == 0.5 - && it.std->eval(eval_context_t()) == 0.5) - { - cerr << "ERROR: The prior density is not defined for the beta distribution when the mean = standard deviation = 0.5." << endl; - exit(EXIT_FAILURE); - } - } - catch (ExprNode::EvalException &e) - { - // We don't have enough information to compute the numerical value, skip the test - } - } - // Check that no parameter/endogenous is declared twice in the block set<string> already_declared; set<pair<string, string>> already_declared_corr; @@ -1402,13 +1380,13 @@ EstimatedParamsStatement::checkPass(ModFileStructure &mod_file_struct, WarningCo if (it.type == 3) // Correlation { // Use lexical ordering for the pair of symbols - auto x = it.name < it.name2 ? pair(it.name, it.name2) : pair(it.name2, it.name); + auto x = it.name < it.name2 ? make_pair(it.name, it.name2) : make_pair(it.name2, it.name); if (already_declared_corr.find(x) == already_declared_corr.end()) already_declared_corr.insert(x); else { - cerr << "ERROR: in `estimated_params' block, the correlation between " << it.name << " and " << it.name2 << " is declared twice." << endl; + cerr << "ERROR: in `" << blockName() << "' block, the correlation between " << it.name << " and " << it.name2 << " is declared twice." << endl; exit(EXIT_FAILURE); } } @@ -1418,11 +1396,45 @@ EstimatedParamsStatement::checkPass(ModFileStructure &mod_file_struct, WarningCo already_declared.insert(it.name); else { - cerr << "ERROR: in `estimated_params' block, the symbol " << it.name << " is declared twice." << endl; + cerr << "ERROR: in `" << blockName() << "' block, the symbol " << it.name << " is declared twice." << endl; exit(EXIT_FAILURE); } } } +} + +EstimatedParamsStatement::EstimatedParamsStatement(vector<EstimationParams> estim_params_list_arg, + const SymbolTable &symbol_table_arg) : + AbstractEstimatedParamsStatement(move(estim_params_list_arg), symbol_table_arg) +{ +} + +void +EstimatedParamsStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) +{ + commonCheckPass(); + + for (const auto &it : estim_params_list) + { + if (it.name == "dsge_prior_weight") + mod_file_struct.dsge_prior_weight_in_estimated_params = true; + + // Handle case of degenerate beta prior + if (it.prior == PriorDistributions::beta) + try + { + if (it.mean->eval(eval_context_t()) == 0.5 + && it.std->eval(eval_context_t()) == 0.5) + { + cerr << "ERROR: The prior density is not defined for the beta distribution when the mean = standard deviation = 0.5." << endl; + exit(EXIT_FAILURE); + } + } + catch (ExprNode::EvalException &e) + { + // We don't have enough information to compute the numerical value, skip the test + } + } // Fill in mod_file_struct.estimated_parameters (related to #469) for (const auto &it : estim_params_list) @@ -1537,8 +1549,7 @@ EstimatedParamsStatement::writeJsonOutput(ostream &output) const EstimatedParamsInitStatement::EstimatedParamsInitStatement(vector<EstimationParams> estim_params_list_arg, const SymbolTable &symbol_table_arg, const bool use_calibration_arg) : - estim_params_list{move(estim_params_list_arg)}, - symbol_table{symbol_table_arg}, + AbstractEstimatedParamsStatement(move(estim_params_list_arg), symbol_table_arg), use_calibration{use_calibration_arg} { } @@ -1546,6 +1557,8 @@ EstimatedParamsInitStatement::EstimatedParamsInitStatement(vector<EstimationPara void EstimatedParamsInitStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) { + commonCheckPass(); + if (use_calibration) mod_file_struct.estim_params_use_calib = true; } @@ -1675,9 +1688,14 @@ EstimatedParamsInitStatement::writeJsonOutput(ostream &output) const EstimatedParamsBoundsStatement::EstimatedParamsBoundsStatement(vector<EstimationParams> estim_params_list_arg, const SymbolTable &symbol_table_arg) : - estim_params_list{move(estim_params_list_arg)}, - symbol_table{symbol_table_arg} + AbstractEstimatedParamsStatement(move(estim_params_list_arg), symbol_table_arg) +{ +} + +void +EstimatedParamsBoundsStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) { + commonCheckPass(); } void diff --git a/src/ComputingTasks.hh b/src/ComputingTasks.hh index 91dd83d0df1d4724c0172a6fadfdc786a2ded4fe..989c54c15ff8c21c9cf866fe28b24adbabcb5602 100644 --- a/src/ComputingTasks.hh +++ b/src/ComputingTasks.hh @@ -511,42 +511,50 @@ public: } }; -class EstimatedParamsStatement : public Statement +class AbstractEstimatedParamsStatement : public Statement { -private: +protected: const vector<EstimationParams> estim_params_list; const SymbolTable &symbol_table; + AbstractEstimatedParamsStatement(vector<EstimationParams> estim_params_list_arg, + const SymbolTable &symbol_table_arg); + virtual string blockName() const = 0; + // Part of the check pass that is common to the three estimated_params* blocks + void commonCheckPass() const; +}; + +class EstimatedParamsStatement : public AbstractEstimatedParamsStatement +{ public: EstimatedParamsStatement(vector<EstimationParams> estim_params_list_arg, const SymbolTable &symbol_table_arg); + string blockName() const override { return "estimated_params"; }; 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 EstimatedParamsInitStatement : public Statement +class EstimatedParamsInitStatement : public AbstractEstimatedParamsStatement { private: - const vector<EstimationParams> estim_params_list; - const SymbolTable &symbol_table; const bool use_calibration; public: EstimatedParamsInitStatement(vector<EstimationParams> estim_params_list_arg, const SymbolTable &symbol_table_arg, const bool use_calibration_arg); + string blockName() const override { return "estimated_params_init"; }; 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 EstimatedParamsBoundsStatement : public Statement +class EstimatedParamsBoundsStatement : public AbstractEstimatedParamsStatement { -private: - const vector<EstimationParams> estim_params_list; - const SymbolTable &symbol_table; public: EstimatedParamsBoundsStatement(vector<EstimationParams> estim_params_list_arg, const SymbolTable &symbol_table_arg); + string blockName() const override { return "estimated_params_bounds"; }; + 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; };