From 62ba9a434a8a72cb8957e7818ef147d1602f304e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Wed, 8 Sep 2021 16:38:15 +0200 Subject: [PATCH] estimated_params_{bounds,init} now check that a parameter (or correlation) is never declared twice This check was previously only implemented in estimated_params. --- src/ComputingTasks.cc | 90 ++++++++++++++++++++++++++----------------- src/ComputingTasks.hh | 26 ++++++++----- 2 files changed, 71 insertions(+), 45 deletions(-) diff --git a/src/ComputingTasks.cc b/src/ComputingTasks.cc index 859ccfc6..46214752 100644 --- a/src/ComputingTasks.cc +++ b/src/ComputingTasks.cc @@ -1362,16 +1362,58 @@ DsampleStatement::writeJsonOutput(ostream &output) const << R"("value2": )" << val2 << "}"; } -EstimatedParamsStatement::EstimatedParamsStatement(vector estim_params_list_arg, - const SymbolTable &symbol_table_arg) : +AbstractEstimatedParamsStatement::AbstractEstimatedParamsStatement(vector estim_params_list_arg, + const SymbolTable &symbol_table_arg) : estim_params_list{move(estim_params_list_arg)}, symbol_table{symbol_table_arg} { } +void +AbstractEstimatedParamsStatement::commonCheckPass() const +{ + // Check that no parameter/endogenous is declared twice in the block + set already_declared; + set> already_declared_corr; + for (const auto &it : estim_params_list) + { + if (it.type == 3) // Correlation + { + // Use lexical ordering for the pair of symbols + 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 `" << blockName() << "' block, the correlation between " << it.name << " and " << it.name2 << " is declared twice." << endl; + exit(EXIT_FAILURE); + } + } + else + { + if (already_declared.find(it.name) == already_declared.end()) + already_declared.insert(it.name); + else + { + cerr << "ERROR: in `" << blockName() << "' block, the symbol " << it.name << " is declared twice." << endl; + exit(EXIT_FAILURE); + } + } + } +} + +EstimatedParamsStatement::EstimatedParamsStatement(vector 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") @@ -1394,36 +1436,6 @@ EstimatedParamsStatement::checkPass(ModFileStructure &mod_file_struct, WarningCo } } - // Check that no parameter/endogenous is declared twice in the block - set already_declared; - set> already_declared_corr; - for (const auto &it : estim_params_list) - { - 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); - - 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; - exit(EXIT_FAILURE); - } - } - else - { - if (already_declared.find(it.name) == already_declared.end()) - already_declared.insert(it.name); - else - { - cerr << "ERROR: in `estimated_params' block, the symbol " << it.name << " is declared twice." << endl; - exit(EXIT_FAILURE); - } - } - } - // Fill in mod_file_struct.estimated_parameters (related to #469) for (const auto &it : estim_params_list) if (it.type == 2 && it.name != "dsge_prior_weight") @@ -1537,8 +1549,7 @@ EstimatedParamsStatement::writeJsonOutput(ostream &output) const EstimatedParamsInitStatement::EstimatedParamsInitStatement(vector 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 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 EstimatedParamsBoundsStatement::writeOutput(ostream &output, const string &basename, bool minimal_workspace) const { diff --git a/src/ComputingTasks.hh b/src/ComputingTasks.hh index 91dd83d0..989c54c1 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 estim_params_list; const SymbolTable &symbol_table; + AbstractEstimatedParamsStatement(vector 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 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 estim_params_list; - const SymbolTable &symbol_table; const bool use_calibration; public: EstimatedParamsInitStatement(vector 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 estim_params_list; - const SymbolTable &symbol_table; public: EstimatedParamsBoundsStatement(vector 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; };