estimated_params_{bounds,init} now check that a parameter (or correlation) is never declared twice

This check was previously only implemented in estimated_params.
pac-components
Sébastien Villemot 2021-09-08 16:38:15 +02:00
parent 784dd4122a
commit 62ba9a434a
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
2 changed files with 71 additions and 45 deletions

View File

@ -1362,16 +1362,58 @@ 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
AbstractEstimatedParamsStatement::commonCheckPass() const
{
// Check that no parameter/endogenous is declared twice in the block
set<string> already_declared;
set<pair<string, string>> 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<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")
@ -1394,36 +1436,6 @@ EstimatedParamsStatement::checkPass(ModFileStructure &mod_file_struct, WarningCo
}
}
// Check that no parameter/endogenous is declared twice in the block
set<string> already_declared;
set<pair<string, string>> 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<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,11 +1688,16 @@ 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
EstimatedParamsBoundsStatement::writeOutput(ostream &output, const string &basename, bool minimal_workspace) const
{

View File

@ -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;
};