Commit 4f9e8a6e authored by sebastien's avatar sebastien
Browse files

v4 parser:

* clearly separated the parsing process from the output writing, using an abstract intermediary representation of the mod file
* created the Statement class, and its derivatives, for that purpose
* fixed bug with "corr" statements in "shocks" blocks (variables indexes given to matlab were incorrect)
* fixed bug with "optim" option of "estimation" (this option was not passed to matlab)
* fixed bug with "dynatype" and "dynasave" (variable list was not passed correctly to matlab)
* fixed bug with "calib" and "calib_var" (they were unusable because of flex misspecification)
* fixed segmentation fault with "estimated_params_bound" and "estimation_params_init"


git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@1117 ac1d8469-bf42-47a9-8791-bf33cf982152
parent 9337cdef
......@@ -7,569 +7,626 @@
#include <sstream>
using namespace std;
//------------------------------------------------------------------------------
#include "ComputingTasks.hh"
#include "Interface.hh"
//------------------------------------------------------------------------------
//ostringstream ComputingTasks::output;
//------------------------------------------------------------------------------
ComputingTasks::ComputingTasks(const SymbolTable &symbol_table_arg) : symbol_table(symbol_table_arg)
#include "Statement.hh"
SteadyStatement::SteadyStatement(const OptionsList &options_list_arg) :
options_list(options_list_arg)
{
}
void
SteadyStatement::writeOutput(ostream &output) const
{
options_list.writeOutput(output);
output << "steady;\n";
}
CheckStatement::CheckStatement(const OptionsList &options_list_arg) :
options_list(options_list_arg)
{
}
void
CheckStatement::writeOutput(ostream &output) const
{
// Empty
options_list.writeOutput(output);
output << "check;\n";
}
//------------------------------------------------------------------------------
ComputingTasks::~ComputingTasks()
SimulStatement::SimulStatement(const OptionsList &options_list_arg) :
options_list(options_list_arg)
{
// Empty
}
//------------------------------------------------------------------------------
void ComputingTasks::setOutput(ostringstream* iOutput)
void
SimulStatement::writeOutput(ostream &output) const
{
output = iOutput;
options_list.writeOutput(output);
output << "simul(oo_.dr);\n";
}
//------------------------------------------------------------------------------
void ComputingTasks::set(void)
StochSimulStatement::StochSimulStatement(const TmpSymbolTable &tmp_symbol_table_arg,
const OptionsList &options_list_arg) :
tmp_symbol_table(tmp_symbol_table_arg),
options_list(options_list_arg)
{
// Empty
}
//------------------------------------------------------------------------------
void ComputingTasks::setSteady(void)
void
StochSimulStatement::writeOutput(ostream &output) const
{
*output << "steady;\n";
options_list.writeOutput(output);
tmp_symbol_table.writeOutput("var_list_", output);
output << "stoch_simul(var_list_);\n";
}
//------------------------------------------------------------------------------
void ComputingTasks::setCheck(void)
EstimationStatement::EstimationStatement(const TmpSymbolTable &tmp_symbol_table_arg,
const OptionsList &options_list_arg) :
tmp_symbol_table(tmp_symbol_table_arg),
options_list(options_list_arg)
{
*output << "check;\n";
}
//------------------------------------------------------------------------------
void ComputingTasks::setSimul(void)
void
EstimationStatement::writeOutput(ostream &output) const
{
*output << "simul(oo_.dr);\n";
options_list.writeOutput(output);
tmp_symbol_table.writeOutput("var_list_", output);
output << "dynare_estimation(var_list_);\n";
}
//------------------------------------------------------------------------------
void ComputingTasks::setStochSimul(string tmp1)
PriorAnalysisStatement::PriorAnalysisStatement(const TmpSymbolTable &tmp_symbol_table_arg,
const OptionsList &options_list_arg) :
tmp_symbol_table(tmp_symbol_table_arg),
options_list(options_list_arg)
{
}
void
PriorAnalysisStatement::writeOutput(ostream &output) const
{
options_list.writeOutput(output);
tmp_symbol_table.writeOutput("var_list_", output);
output << "prior_analysis(var_list_);\n";
}
PosteriorAnalysisStatement::PosteriorAnalysisStatement(const TmpSymbolTable &tmp_symbol_table_arg,
const OptionsList &options_list_arg) :
tmp_symbol_table(tmp_symbol_table_arg),
options_list(options_list_arg)
{
}
void
PosteriorAnalysisStatement::writeOutput(ostream &output) const
{
options_list.writeOutput(output);
tmp_symbol_table.writeOutput("var_list_", output);
output << "posterior_analysis(var_list_);\n";
}
RplotStatement::RplotStatement(const TmpSymbolTable &tmp_symbol_table_arg,
const OptionsList &options_list_arg) :
tmp_symbol_table(tmp_symbol_table_arg),
options_list(options_list_arg)
{
*output << tmp1;
*output << "stoch_simul(var_list_);\n";
}
//------------------------------------------------------------------------------
void ComputingTasks::setOption(string name, string value)
void
RplotStatement::writeOutput(ostream &output) const
{
*output << "options_." << name << " = " << value << ";\n";
options_list.writeOutput(output);
tmp_symbol_table.writeOutput("var_list_", output);
output << "rplot(var_list_);\n";
}
//------------------------------------------------------------------------------
void ComputingTasks::setOption(string name, string value1, string value2)
UnitRootVarsStatement::UnitRootVarsStatement(const TmpSymbolTable &tmp_symbol_table_arg) :
tmp_symbol_table(tmp_symbol_table_arg)
{
*output << "options_." << name << " = [" << value1 << "; " << value2 << "];\n";
}
//------------------------------------------------------------------------------
void ComputingTasks::runEstimation(string tmp1)
void
UnitRootVarsStatement::writeOutput(ostream &output) const
{
*output << tmp1;
*output << "dynare_estimation(var_list_);\n";
tmp_symbol_table.writeOutput("options_.unit_root_vars", output);
}
//------------------------------------------------------------------------------
void ComputingTasks::runPriorAnalysis(string tmp1)
PeriodsStatement::PeriodsStatement(int periods_arg) : periods(periods_arg)
{
*output << tmp1;
*output << "prior_analysis(var_list_);\n";
}
//------------------------------------------------------------------------------
void ComputingTasks::runPosteriorAnalysis(string tmp1)
void
PeriodsStatement::writeOutput(ostream &output) const
{
*output << tmp1;
*output << "posterior_analysis(var_list_);\n";
output << "options_.periods = " << periods << ";" << endl;
output << "options_.simul = 1;" << endl;
}
//------------------------------------------------------------------------------
void ComputingTasks::runRplot(string tmp1)
DsampleStatement::DsampleStatement(int val1_arg) : val1(val1_arg), val2(-1)
{
*output << tmp1;
*output << "rplot(var_list_);\n";
}
//------------------------------------------------------------------------------
void ComputingTasks::setEstimationInit(void)
DsampleStatement::DsampleStatement(int val1_arg, int val2_arg) : val1(val1_arg), val2(val2_arg)
{
*output << "global estim_params_\n";
*output << "var_list_ = [];\n";
*output << "estim_params_.var_exo = [];\n";
*output << "estim_params_.var_endo = [];\n";
*output << "estim_params_.corrx = [];\n";
*output << "estim_params_.corrn = [];\n";
*output << "estim_params_.param_names = [];\n";
*output << "estim_params_.user_param_names = [];\n";
*output << "estim_params_.param_vals = [];\n";
*output << "M_.H = 0;\n";
}
//------------------------------------------------------------------------------
void ComputingTasks::setOptimOptions(string str1, string str2, int task)
void
DsampleStatement::writeOutput(ostream &output) const
{
string optim_string;
int start;
switch(task)
if (val2 < 0)
output << "options_.dsample = " << val1 << ";" << endl;
else
output << "options_.dsample = [" << val1 << "; " << val2 << "];" << endl;
}
VarobsStatement::VarobsStatement(const TmpSymbolTable &tmp_symbol_table_arg) :
tmp_symbol_table(tmp_symbol_table_arg)
{
}
void
VarobsStatement::writeOutput(ostream &output) const
{
tmp_symbol_table.writeOutput("options_.varobs", output);
}
EstimatedParamsStatement::EstimatedParamsStatement(const vector<EstimationParams> &estim_params_list_arg,
const SymbolTable &symbol_table_arg) :
estim_params_list(estim_params_list_arg),
symbol_table(symbol_table_arg)
{
}
void
EstimatedParamsStatement::writeOutput(ostream &output) const
{
output << "global estim_params_\n";
output << "var_list_ = [];\n";
output << "estim_params_.var_exo = [];\n";
output << "estim_params_.var_endo = [];\n";
output << "estim_params_.corrx = [];\n";
output << "estim_params_.corrn = [];\n";
output << "estim_params_.param_names = [];\n";
output << "estim_params_.user_param_names = [];\n";
output << "estim_params_.param_vals = [];\n";
output << "M_.H = 0;\n";
vector<EstimationParams>::const_iterator it;
for(it = estim_params_list.begin(); it != estim_params_list.end(); it++)
{
case 1:
optim_string = "options_.optim_opt = '";
start = 0;
return;
case 2:
if (start > 0)
{
optim_string += ",";
}
else
if (symbol_table.isReferenced(it->name) == eNotReferenced
&& it->name != "dsge_prior_weight")
return;
switch(it->type)
{
start = 1;
case 1:
if (symbol_table.getType(it->name) == eExogenous)
output << "estim_params_.var_exo = [estim_params_.var_exo; ";
else if (symbol_table.getType(it->name) == eEndogenous)
output << "estim_params_.var_endo = [estim_params_.var_endo; ";
output << symbol_table.getID(it->name)+1;
break;
case 2:
output << "estim_params_.param_vals = [estim_params_.param_vals; ";
output << symbol_table.getID(it->name)+1;
break;
case 3:
if (symbol_table.getType(it->name) == eExogenous)
output << "estim_params_.corrx = [estim_params_.corrx; ";
else if (symbol_table.getType(it->name) == eEndogenous)
output << "estim_params_.corrn = [estim_params_.corrn; ";
output << symbol_table.getID(it->name)+1;
output << " " << symbol_table.getID(it->name2)+1;
break;
}
optim_string += "''";
optim_string += str1;
optim_string += "'',";
if (str2[0] >= 'A' && str2[0] <= 'z')
{
optim_string += "''";
optim_string += str2;
optim_string += "''";
}
else
{
optim_string += str2;
}
return;
case 3:
optim_string += "';\n";
*output << optim_string;
output << " " << it->init_val << " " << it->low_bound
<< " " << it->up_bound << " " << it->prior
<< " " << it->mean << " " << it->std
<< " " << it->p3 << " " << it->p4 << " " << it->jscale << "];\n";
}
}
//------------------------------------------------------------------------------
void ComputingTasks::setEstimatedElements(void)
EstimatedParamsInitStatement::EstimatedParamsInitStatement(const vector<EstimationParams> &estim_params_list_arg,
const SymbolTable &symbol_table_arg) :
estim_params_list(estim_params_list_arg),
symbol_table(symbol_table_arg)
{
if (!symbol_table.Exist(EstimParams->name))
{
string msg = "Unknown symbol: "+EstimParams->name;
error(msg.c_str());
}
if (symbol_table.isReferenced(EstimParams->name) == eNotReferenced & EstimParams->name != "dsge_prior_weight")
{
return;
}
if ((EstimParams->init_val).size() == 0)
{
EstimParams->init_val = EstimParams->mean;
}
switch(EstimParams->type)
}
void
EstimatedParamsInitStatement::writeOutput(ostream &output) const
{
vector<EstimationParams>::const_iterator it;
for(it = estim_params_list.begin(); it != estim_params_list.end(); it++)
{
case 1:
if (symbol_table.getType(EstimParams->name) == eExogenous)
{
*output << "estim_params_.var_exo = [estim_params_.var_exo; ";
}
else if (symbol_table.getType(EstimParams->name) == eEndogenous)
{
*output << "estim_params_.var_endo = [estim_params_.var_endo; ";
}
*output << symbol_table.getID(EstimParams->name)+1;
break;
case 2:
*output << "estim_params_.param_vals = [estim_params_.param_vals; ";
*output << symbol_table.getID(EstimParams->name)+1;
break;
case 3:
if (symbol_table.getType(EstimParams->name) == eExogenous)
if (symbol_table.isReferenced(it->name) == eNotReferenced)
return;
if (it->type < 3)
{
*output << "estim_params_.corrx = [estim_params_.corrx; ";
if (symbol_table.getType(it->name) == eExogenous)
{
output << "tmp1 = find(estim_params_.var_exo(:,1)==" << symbol_table.getID(it->name)+1 << ");\n";
output << "estim_params_.var_exo(tmp1,2) = " << it->init_val << ";\n";
}
else if (symbol_table.getType(it->name) == eEndogenous)
{
output << "tmp1 = find(estim_params_.var_endo(:,1)==" << symbol_table.getID(it->name)+1 << ");\n";
output << "estim_params_.var_endo(tmp1,2) = " << it->init_val << ";\n";
}
else if (symbol_table.getType(it->name) == eParameter)
{
output << "tmp1 = find(estim_params_.param_vals(:,1)==" << symbol_table.getID(it->name)+1 << ");\n";
output << "estim_params_.param_vals(tmp1,2) = " << it->init_val << ";\n";
}
}
else if (symbol_table.getType(EstimParams->name) == eEndogenous)
else
{
*output << "estim_params_.corrn = [estim_params_.corrn; ";
if (symbol_table.getType(it->name) == eExogenous)
{
output << "tmp1 = find((estim_params_.corrx(:,1)==" << symbol_table.getID(it->name)+1 << ")) & (estim_params_.corrx(:,2)==" << symbol_table.getID(it->name2)+1 << ");\n";
output << "estim_params_.corrx(tmp1,3) = " << it->init_val << ";\n";
}
else if (symbol_table.getType(it->name) == eEndogenous)
{
output << "tmp1 = find((estim_params_.corrn(:,1)==" << symbol_table.getID(it->name)+1 << ")) & (estim_params_.corrn(:,2)==" << symbol_table.getID(it->name2)+1 << ";\n";
output << "estim_params_.corrx(tmp1,3) = " << it->init_val << ";\n";
}
}
*output << symbol_table.getID(EstimParams->name)+1;
*output << " " << symbol_table.getID(EstimParams->name2)+1;
break;
}
*output << " " << EstimParams->init_val << " " << EstimParams->low_bound << " " <<
EstimParams->up_bound << " " << EstimParams->prior << " ";
*output << EstimParams->mean << " " << EstimParams->std << " " <<
EstimParams->p3 << " " << EstimParams->p4 << " " << EstimParams->jscale << "];\n";
EstimParams->clear();
}
//------------------------------------------------------------------------------
void ComputingTasks::setEstimatedInitElements(void)
EstimatedParamsBoundsStatement::EstimatedParamsBoundsStatement(const vector<EstimationParams> &estim_params_list_arg,
const SymbolTable &symbol_table_arg) :
estim_params_list(estim_params_list_arg),
symbol_table(symbol_table_arg)
{
if (!symbol_table.Exist(EstimParams->name))
{
string msg = "Unknown symbol: "+EstimParams->name;
error(msg.c_str());
}
if (symbol_table.isReferenced(EstimParams->name) == eNotReferenced)
{
return;
}
if ((EstimParams->init_val).size() == 0)
{
EstimParams->init_val = EstimParams->mean;
}
if (EstimParams->type < 3)
}
void
EstimatedParamsBoundsStatement::writeOutput(ostream &output) const
{
vector<EstimationParams>::const_iterator it;
for(it = estim_params_list.begin(); it != estim_params_list.end(); it++)
{
if (symbol_table.getType(EstimParams->name) == eExogenous)
if (symbol_table.isReferenced(it->name) == eNotReferenced)
return;
if (it->type < 3)
{
*output << "tmp1 = find(estim_params_.var_exo(:,1)==" << symbol_table.getID(EstimParams->name)+1 << ");\n";
*output << "estim_params_.var_exo(tmp1,2) = " << EstimParams->init_val << ";\n";
if (symbol_table.getType(it->name) == eExogenous)
{
output << "tmp1 = find(estim_params_.var_exo(:,1)==" << symbol_table.getID(it->name)+1 << ");\n";
output << "estim_params_.var_exo(tmp1,3) = " << it->low_bound << ";\n";
output << "estim_params_.var_exo(tmp1,4) = " << it->up_bound << ";\n";
}
else if (symbol_table.getType(it->name) == eEndogenous)
{
output << "tmp1 = find(estim_params_.var_endo(:,1)==" << symbol_table.getID(it->name)+1 << ");\n";
output << "estim_params_.var_endo(tmp1,3) = " << it->low_bound << ";\n";
output << "estim_params_.var_endo(tmp1,4) = " << it->up_bound << ";\n";
}
else if (symbol_table.getType(it->name) == eParameter)
{
output << "tmp1 = find(estim_params_.param_vals(:,1)==" << symbol_table.getID(it->name)+1 << ");\n";
output << "estim_params_.param_vals(tmp1,3) = " << it->low_bound << ";\n";
output << "estim_params_.param_vals(tmp1,4) = " << it->up_bound << ";\n";
}
}
else if (symbol_table.getType(EstimParams->name) == eEndogenous)
{
*output << "tmp1 = find(estim_params_.var_endo(:,1)==" << symbol_table.getID(EstimParams->name)+1 << ");\n";
*output << "estim_params_.var_endo(tmp1,2) = " << EstimParams->init_val << ";\n";
}
else if (symbol_table.getType(EstimParams->name) == eParameter)
else
{
*output << "tmp1 = find(estim_params_.param_vals(:,1)==" << symbol_table.getID(EstimParams->name)+1 << ");\n";
*output << "estim_params_.param_vals(tmp1,2) = " << EstimParams->init_val << ";\n";
if (symbol_table.getType(it->name) == eExogenous)
{
output << "tmp1 = find((estim_params_.corrx(:,1)==" << symbol_table.getID(it->name)+1 << ")) & (estim_params_.corrx(:,2)==" << symbol_table.getID(it->name2)+1 << ");\n";
output << "estim_params_.corrx(tmp1,4) = " << it->low_bound << ";\n";
output << "estim_params_.corrx(tmp1,5) = " << it->up_bound << ";\n";
}
else if (symbol_table.getType(it->name) == eEndogenous)
{
output << "tmp1 = find((estim_params_.corrn(:,1)==" << symbol_table.getID(it->name)+1 << ")) & (estim_params_.corrn(:,2)==" << symbol_table.getID(it->name2)+1 << ";\n";
output << "estim_params_.corrx(tmp1,4) = " << it->low_bound << ";\n";
output << "estim_params_.corrx(tmp1,5) = " << it->up_bound << ";\n";
}
}
}
else
}
ObservationTrendsStatement::ObservationTrendsStatement(const trend_elements_type &trend_elements_arg,
const SymbolTable &symbol_table_arg) :
trend_elements(trend_elements_arg),
symbol_table(symbol_table_arg)
{
}
void
ObservationTrendsStatement::writeOutput(ostream &output) const
{
output << "options_.trend_coeff_ = {};" << endl;
trend_elements_type::const_iterator it;
for(it = trend_elements.begin(); it != trend_elements.end(); it++)
{
if (symbol_table.getType(EstimParams->name) == eExogenous)
Type type = symbol_table.getType(it->first);
if (type == eEndogenous)
{
*output << "tmp1 = find((estim_params_.corrx(:,1)==" << symbol_table.getID(EstimParams->name)+1 << ")) & (estim_params_.corrx(:,2)==" << symbol_table.getID(EstimParams->name2)+1 << ");\n";
*output << "estim_params_.corrx(tmp1,3) = " << EstimParams->init_val << ";\n";
}
else if (symbol_table.getType(EstimParams->name) == eEndogenous)
{
*output << "tmp1 = find((estim_params_.corrn(:,1)==" << symbol_table.getID(EstimParams->name)+1 << ")) & (estim_params_.corrn(:,2)==" << symbol_table.getID(EstimParams->name2)+1 << ";\n";
*output << "estim_params_.corrx(tmp1,3) = " << EstimParams->init_val << ";\n";
output << "tmp1 = strmatch('" << it->first << "',options_.varobs,'exact');\n";
output << "options_.trend_coeffs{tmp1} = '" << it->second << "';\n";
}
else
cout << "Error : Non-variable symbol used in TREND_COEFF: " << it->first << endl;
}
EstimParams->clear();
}
//------------------------------------------------------------------------------
void ComputingTasks::setEstimatedBoundsElements(void)
CalibVarStatement::CalibVarStatement(const calib_var_type &calib_var_arg,
const calib_covar_type &calib_covar_arg,
const calib_ac_type &calib_ac_arg,
const SymbolTable &symbol_table_arg) :
calib_var(calib_var_arg),
calib_covar(calib_covar_arg),
calib_ac(calib_ac_arg),
symbol_table(symbol_table_arg)
{
if (!symbol_table.Exist(EstimParams->name))
{
string msg = "Unknown symbol: "+EstimParams->name;
error(msg.c_str());
}
if (symbol_table.isReferenced(EstimParams->name) == eNotReferenced)
{
return;
}
if ((EstimParams->init_val).size() == 0)
}
void
CalibVarStatement::writeOutput(ostream &output) const
{
output << interfaces::comment() << "\n" << interfaces::comment() << "CALIB_VAR \n"
<< interfaces::comment() << "\n";
for(int i = 1; i < 4 ; i++)
{
EstimParams->init_val = EstimParams->mean;
output << "calib_var_index{" << i << "} = [];\n";
output << "calib_targets{" << i << "} = [];\n";
output << "calib_weights{" << i << "}=[];\n";
}
if (EstimParams->type < 3)
// Print calibration variances
for(calib_var_type::const_iterator it = calib_var.begin();
it != calib_var.end(); it++)
{
if (symbol_table.getType(EstimParams->name) == eExogenous)
{
*output << "tmp1 = find(estim_params_.var_exo(:,1)==" << symbol_table.getID(EstimParams->name)+1 << ");\n";
*output << "estim_params_.var_exo(tmp1,3) = " << EstimParams->low_bound << ";\n";
*output << "estim_params_.var_exo(tmp1,4) = " << EstimParams->up_bound << ";\n";
}