From 75e125771dda70276b50ec0af791e83a0b2a4989 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Wed, 21 Dec 2011 11:17:58 +0100 Subject: [PATCH 01/14] Ref. manual: fix typo --- doc/dynare.texi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/dynare.texi b/doc/dynare.texi index 2aa309a62..d6089777c 100644 --- a/doc/dynare.texi +++ b/doc/dynare.texi @@ -3803,7 +3803,7 @@ algorithm. For the time being, @code{mh_replic} should be larger than @item sub_draws = @var{INTEGER} @anchor{sub_draws} number of draws from the Metropolis iterations that -are used to compute posterior distirbution of various objects (smoothed +are used to compute posterior distribution of various objects (smoothed variable, smoothed shocks, forecast, moments, IRF). @code{sub_draws} should be smaller than the total number of Metropolis draws available. Default: @code{min(1200,0.25*Total number of draws)} From 29bead75c922b87232956f00d702253bf89101b8 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 29 Nov 2011 17:14:26 +0100 Subject: [PATCH 02/14] preprocessor: add set_time command --- matlab/global_initialization.m | 1 + preprocessor/ComputingTasks.cc | 11 +++++++++++ preprocessor/ComputingTasks.hh | 10 ++++++++++ preprocessor/DynareBison.yy | 14 ++++++++++++-- preprocessor/DynareFlex.ll | 6 ++++++ preprocessor/ParsingDriver.cc | 28 ++++++++++++++++++++++++++++ preprocessor/ParsingDriver.hh | 6 ++++++ preprocessor/Statement.cc | 9 +++++++++ preprocessor/Statement.hh | 2 ++ 9 files changed, 85 insertions(+), 2 deletions(-) diff --git a/matlab/global_initialization.m b/matlab/global_initialization.m index 03a7b1a1c..d12c84604 100644 --- a/matlab/global_initialization.m +++ b/matlab/global_initialization.m @@ -182,6 +182,7 @@ options_.ramsey_policy = 0; options_.timeless = 0; % estimation +options_.initial_period = dynDate(1); options_.Harvey_scale_factor = 10; options_.MaxNumberOfBytes = 1e6; options_.MaximumNumberOfMegaBytes = 111; diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc index 78831ce14..39993b134 100644 --- a/preprocessor/ComputingTasks.cc +++ b/preprocessor/ComputingTasks.cc @@ -1431,3 +1431,14 @@ SvarStatement::writeOutput(ostream &output, const string &basename) const else output << "'ALL';" << endl; } + +SetTimeStatement::SetTimeStatement(const OptionsList &options_list_arg) : + options_list(options_list_arg) +{ +} + +void +SetTimeStatement::writeOutput(ostream &output, const string &basename) const +{ + options_list.writeOutput(output); +} diff --git a/preprocessor/ComputingTasks.hh b/preprocessor/ComputingTasks.hh index 2c7a0e869..75851a611 100644 --- a/preprocessor/ComputingTasks.hh +++ b/preprocessor/ComputingTasks.hh @@ -556,4 +556,14 @@ public: virtual void writeOutput(ostream &output, const string &basename) const; }; +class SetTimeStatement : public Statement +{ +private: + const OptionsList options_list; +public: + SetTimeStatement(const OptionsList &options_list_arg); + virtual void writeOutput(ostream &output, const string &basename) const; +}; + + #endif diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy index 0503ea970..a533cdec0 100644 --- a/preprocessor/DynareBison.yy +++ b/preprocessor/DynareBison.yy @@ -96,13 +96,14 @@ class ParsingDriver; %token CHANGE_TYPE CHECK CONDITIONAL_FORECAST CONDITIONAL_FORECAST_PATHS CONF_SIG CONSTANT CONTROLLED_VAREXO CORR COVAR CUTOFF %token DATAFILE DR_ALGO DROP DSAMPLE DYNASAVE DYNATYPE CALIBRATION %token END ENDVAL EQUAL ESTIMATION ESTIMATED_PARAMS ESTIMATED_PARAMS_BOUNDS ESTIMATED_PARAMS_INIT -%token FILENAME FILTER_STEP_AHEAD FILTERED_VARS FIRST_OBS +%token FILENAME FILTER_STEP_AHEAD FILTERED_VARS FIRST_OBS SET_TIME %token FLOAT_NUMBER %token FORECAST K_ORDER_SOLVER INSTRUMENTS %token GAMMA_PDF GRAPH CONDITIONAL_VARIANCE_DECOMPOSITION NOCHECK %token HISTVAL HOMOTOPY_SETUP HOMOTOPY_MODE HOMOTOPY_STEPS HP_FILTER HP_NGRID %token IDENTIFICATION INF_CONSTANT INITVAL INITVAL_FILE %token INT_NUMBER +%token DATE_NUMBER %token INV_GAMMA_PDF INV_GAMMA1_PDF INV_GAMMA2_PDF IRF IRF_SHOCKS %token KALMAN_ALGO KALMAN_TOL %token LABELS LAPLACE LIK_ALGO LIK_INIT LINEAR LOAD_IDENT_FILES LOAD_MH_FILE LOAD_PARAMS_AND_STEADY_STATE LOGLINEAR @@ -173,7 +174,7 @@ class ParsingDriver; %type expression expression_or_empty %type equation hand_side -%type non_negative_number signed_number signed_integer +%type non_negative_number signed_number signed_integer date_number %type filename symbol %type vec_value_1 vec_value %type range prior @@ -213,6 +214,7 @@ statement : parameters | estimated_params | estimated_params_bounds | estimated_params_init + | set_time | varobs | observation_trends | unit_root_vars @@ -963,6 +965,10 @@ non_negative_number : INT_NUMBER | FLOAT_NUMBER ; +date_number : DATE_NUMBER + | INT_NUMBER + ; + signed_number : PLUS non_negative_number { $$ = $2; } | MINUS non_negative_number @@ -1153,6 +1159,10 @@ prior : BETA_PDF { $$ = new string("6"); } ; +set_time : SET_TIME '(' date_number ')' ';' + { driver.set_time($3); } + ; + estimation : ESTIMATION ';' { driver.run_estimation(); } | ESTIMATION '(' estimation_options_list ')' ';' diff --git a/preprocessor/DynareFlex.ll b/preprocessor/DynareFlex.ll index e9230dd4e..7f46f7caa 100644 --- a/preprocessor/DynareFlex.ll +++ b/preprocessor/DynareFlex.ll @@ -108,6 +108,7 @@ string eofbuff; periods {BEGIN DYNARE_STATEMENT; return token::PERIODS;} model_info {BEGIN DYNARE_STATEMENT; return token::MODEL_INFO;} estimation {BEGIN DYNARE_STATEMENT; return token::ESTIMATION;} +set_time {BEGIN DYNARE_STATEMENT; return token::SET_TIME;} varobs {BEGIN DYNARE_STATEMENT; return token::VAROBS;} unit_root_vars {BEGIN DYNARE_STATEMENT; return token::UNIT_ROOT_VARS;} rplot {BEGIN DYNARE_STATEMENT; return token::RPLOT;} @@ -591,6 +592,11 @@ string eofbuff; return token::INT_NUMBER; } +([1-2][0-9]{3}[Mm](([1-9])|(1[0-2])))|([1-2][0-9]{3}[Qq][1-4])|([1-2][0-9]{3}[Ww](([1-9]{1})|([1-5][0-9]))) { + yylval->string_val = new string(yytext); + return token::DATE_NUMBER; +} + \'[^\']+\' { yylval->string_val = new string(yytext + 1); yylval->string_val->resize(yylval->string_val->length() - 1); diff --git a/preprocessor/ParsingDriver.cc b/preprocessor/ParsingDriver.cc index 73fc785fc..bc734f8ff 100644 --- a/preprocessor/ParsingDriver.cc +++ b/preprocessor/ParsingDriver.cc @@ -1056,6 +1056,23 @@ ParsingDriver::option_str(const string &name_option, const string &opt) options_list.string_options[name_option] = opt; } +void +ParsingDriver::option_date(const string &name_option, string *opt) +{ + option_date(name_option, *opt); + delete opt; +} + +void +ParsingDriver::option_date(const string &name_option, const string &opt) +{ + if (options_list.date_options.find(name_option) + != options_list.date_options.end()) + error("option " + name_option + " declared twice"); + + options_list.date_options[name_option] = opt; +} + void ParsingDriver::option_symbol_list(const string &name_option) { @@ -1184,6 +1201,17 @@ ParsingDriver::set_unit_root_vars() symbol_list.clear(); } +void +ParsingDriver::set_time(string *arg) +{ + string arg1 = *arg; + for (size_t i=0; iaddStatement(new SetTimeStatement(options_list)); + options_list.clear(); +} + void ParsingDriver::run_estimation() { diff --git a/preprocessor/ParsingDriver.hh b/preprocessor/ParsingDriver.hh index dedf5e6db..d3d7ac01c 100644 --- a/preprocessor/ParsingDriver.hh +++ b/preprocessor/ParsingDriver.hh @@ -319,6 +319,10 @@ public: void option_str(const string &name_option, string *opt); //! Sets an option to a string value void option_str(const string &name_option, const string &opt); + //! Sets an option to a date value + void option_date(const string &name_option, string *opt); + //! Sets an option to a date value + void option_date(const string &name_option, const string &opt); //! Sets an option to a list of symbols (used in conjunction with add_in_symbol_list()) void option_symbol_list(const string &name_option); //! Sets an option to a vector of integers @@ -351,6 +355,8 @@ public: void external_function_option(const string &name_option, const string &opt); //! Add a line in an estimated params block void add_estimated_params_element(); + //! Sets the frequency of the data + void set_time(string *arg); //! Runs estimation process void run_estimation(); //! Runs dynare_sensitivy() diff --git a/preprocessor/Statement.cc b/preprocessor/Statement.cc index ea34a2366..5d61270f2 100644 --- a/preprocessor/Statement.cc +++ b/preprocessor/Statement.cc @@ -88,6 +88,10 @@ OptionsList::writeOutput(ostream &output) const it != string_options.end(); it++) output << "options_." << it->first << " = '" << it->second << "';" << endl; + for (date_options_t::const_iterator it = date_options.begin(); + it != date_options.end(); it++) + output << "options_." << it->first << " = dynDate('" << it->second << "');" << endl; + for (symbol_list_options_t::const_iterator it = symbol_list_options.begin(); it != symbol_list_options.end(); it++) it->second.writeOutput("options_." + it->first, output); @@ -127,6 +131,10 @@ OptionsList::writeOutput(ostream &output, const string &option_group) const it != string_options.end(); it++) output << option_group << "." << it->first << " = '" << it->second << "';" << endl; + for (date_options_t::const_iterator it = date_options.begin(); + it != date_options.end(); it++) + output << option_group << "." << it->first << " = dynDate('" << it->second << "');" << endl; + for (symbol_list_options_t::const_iterator it = symbol_list_options.begin(); it != symbol_list_options.end(); it++) it->second.writeOutput(option_group + "." + it->first, output); @@ -154,6 +162,7 @@ OptionsList::clear() num_options.clear(); paired_num_options.clear(); string_options.clear(); + date_options.clear(); symbol_list_options.clear(); vector_int_options.clear(); } diff --git a/preprocessor/Statement.hh b/preprocessor/Statement.hh index 1d27815eb..3b9aebd84 100644 --- a/preprocessor/Statement.hh +++ b/preprocessor/Statement.hh @@ -118,11 +118,13 @@ public: typedef map num_options_t; typedef map > paired_num_options_t; typedef map string_options_t; + typedef map date_options_t; typedef map symbol_list_options_t; typedef map > vec_int_options_t; num_options_t num_options; paired_num_options_t paired_num_options; string_options_t string_options; + date_options_t date_options; symbol_list_options_t symbol_list_options; vec_int_options_t vector_int_options; void writeOutput(ostream &output) const; From 1e78d7065924fb38cd777d0f3e2cc9c09cbf8dee Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 6 Dec 2011 15:42:57 +0100 Subject: [PATCH 03/14] preprocessor: add data command --- matlab/global_initialization.m | 5 +++ preprocessor/ComputingTasks.cc | 57 ++++++++++++++++++++++++++++++++++ preprocessor/ComputingTasks.hh | 10 ++++++ preprocessor/DynareBison.yy | 30 ++++++++++++++++-- preprocessor/DynareFlex.ll | 3 ++ preprocessor/ParsingDriver.cc | 7 +++++ preprocessor/ParsingDriver.hh | 2 ++ preprocessor/Statement.hh | 2 ++ 8 files changed, 114 insertions(+), 2 deletions(-) diff --git a/matlab/global_initialization.m b/matlab/global_initialization.m index d12c84604..00e937555 100644 --- a/matlab/global_initialization.m +++ b/matlab/global_initialization.m @@ -183,6 +183,11 @@ options_.timeless = 0; % estimation options_.initial_period = dynDate(1); +options_.dataset.firstobs = options_.initial_period; +options_.dataset.lastobs = NaN; +options_.dataset.nobs = NaN; +options_.dataset.xls_sheet = NaN; +options_.dataset.xls_range = NaN; options_.Harvey_scale_factor = 10; options_.MaxNumberOfBytes = 1e6; options_.MaximumNumberOfMegaBytes = 111; diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc index 39993b134..0a765469e 100644 --- a/preprocessor/ComputingTasks.cc +++ b/preprocessor/ComputingTasks.cc @@ -325,6 +325,30 @@ EstimationStatement::checkPass(ModFileStructure &mod_file_struct) cerr << "ERROR: An estimation statement cannot take more than one dsge_var option." << endl; exit(EXIT_FAILURE); } + + if (options_list.string_options.find("datafile") == options_list.string_options.end() && + !mod_file_struct.estimation_data_statement_present) + { + cerr << "ERROR: The estimation statement requires a data file to be supplied " + << "either from the data statement or from the deprecated option datafile." << endl; + exit(EXIT_FAILURE); + } + + if (options_list.string_options.find("datafile") != options_list.string_options.end()) + cerr << "WARNING: The datafile option of estimation has been deprecated. " + << "Use the data command instead." << endl; + + if (options_list.string_options.find("xls_sheet") != options_list.string_options.end()) + cerr << "WARNING: The xls_sheet option of estimation has been deprecated. " + << "Use the data command instead." << endl; + + if (options_list.string_options.find("xls_range") != options_list.string_options.end()) + cerr << "WARNING: The xls_range option of estimation has been deprecated. " + << "Use the data command instead." << endl; + + if (options_list.num_options.find("first_obs") != options_list.num_options.end()) + cerr << "WARNING: The first_obs option of estimation has been deprecated. " + << "Use the data command instead." << endl; } void @@ -1442,3 +1466,36 @@ SetTimeStatement::writeOutput(ostream &output, const string &basename) const { options_list.writeOutput(output); } + +EstimationDataStatement::EstimationDataStatement(const OptionsList &options_list_arg) : + options_list(options_list_arg) +{ +} + +void +EstimationDataStatement::checkPass(ModFileStructure &mod_file_struct) +{ + mod_file_struct.estimation_data_statement_present = true; + + OptionsList::num_options_t::const_iterator it = options_list.num_options.find("nobs"); + if (it != options_list.num_options.end()) + if (atoi(it->second.c_str()) <= 0) + { + cerr << "ERROR: The nobs option of the data statement only accepts positive integers." << endl; + exit(EXIT_FAILURE); + } + + if (options_list.string_options.find("file") == options_list.string_options.end()) + { + cerr << "ERROR: The file option must be passed to the data statement." << endl; + exit(EXIT_FAILURE); + } +} + +void +EstimationDataStatement::writeOutput(ostream &output, const string &basename) const +{ + options_list.writeOutput(output, "options_.dataset"); + if (options_list.date_options.find("first_obs") == options_list.date_options.end()) + output << "options_.dataset.firstobs = options_.initial_period;" << endl; +} diff --git a/preprocessor/ComputingTasks.hh b/preprocessor/ComputingTasks.hh index 75851a611..a04a7d18b 100644 --- a/preprocessor/ComputingTasks.hh +++ b/preprocessor/ComputingTasks.hh @@ -565,5 +565,15 @@ public: virtual void writeOutput(ostream &output, const string &basename) const; }; +class EstimationDataStatement : public Statement +{ +private: + const OptionsList options_list; +public: + EstimationDataStatement(const OptionsList &options_list_arg); + virtual void checkPass(ModFileStructure &mod_file_struct); + virtual void writeOutput(ostream &output, const string &basename) const; +}; + #endif diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy index a533cdec0..772369462 100644 --- a/preprocessor/DynareBison.yy +++ b/preprocessor/DynareBison.yy @@ -94,9 +94,9 @@ class ParsingDriver; %token BVAR_PRIOR_MU BVAR_PRIOR_OMEGA BVAR_PRIOR_TAU BVAR_PRIOR_TRAIN %token BVAR_REPLIC BYTECODE %token CHANGE_TYPE CHECK CONDITIONAL_FORECAST CONDITIONAL_FORECAST_PATHS CONF_SIG CONSTANT CONTROLLED_VAREXO CORR COVAR CUTOFF -%token DATAFILE DR_ALGO DROP DSAMPLE DYNASAVE DYNATYPE CALIBRATION +%token DATAFILE FILE DR_ALGO DROP DSAMPLE DYNASAVE DYNATYPE CALIBRATION %token END ENDVAL EQUAL ESTIMATION ESTIMATED_PARAMS ESTIMATED_PARAMS_BOUNDS ESTIMATED_PARAMS_INIT -%token FILENAME FILTER_STEP_AHEAD FILTERED_VARS FIRST_OBS SET_TIME +%token FILENAME FILTER_STEP_AHEAD FILTERED_VARS FIRST_OBS LAST_OBS SET_TIME %token FLOAT_NUMBER %token FORECAST K_ORDER_SOLVER INSTRUMENTS %token GAMMA_PDF GRAPH CONDITIONAL_VARIANCE_DECOMPOSITION NOCHECK @@ -215,6 +215,7 @@ statement : parameters | estimated_params_bounds | estimated_params_init | set_time + | data | varobs | observation_trends | unit_root_vars @@ -1163,6 +1164,22 @@ set_time : SET_TIME '(' date_number ')' ';' { driver.set_time($3); } ; +data : DATA '(' data_options_list ')'';' + { driver.estimation_data(); } + ; + +data_options_list : data_options_list COMMA data_options + | data_options + ; + +data_options : o_file + | o_new_estimation_data_first_obs + | o_last_obs + | o_new_estimation_data_nobs + | o_xls_sheet + | o_xls_range + ; + estimation : ESTIMATION ';' { driver.run_estimation(); } | ESTIMATION '(' estimation_options_list ')' ';' @@ -1862,6 +1879,7 @@ o_mfs : MFS EQUAL INT_NUMBER { driver.mfs($3); }; o_simul : SIMUL; // Do nothing, only here for backward compatibility o_simul_seed : SIMUL_SEED EQUAL INT_NUMBER { driver.error("'simul_seed' option is no longer supported; use 'set_dynare_seed' command instead"); } ; o_qz_criterium : QZ_CRITERIUM EQUAL non_negative_number { driver.option_num("qz_criterium", $3); }; +o_file : FILE EQUAL filename { driver.option_str("file", $3); }; o_datafile : DATAFILE EQUAL filename { driver.option_str("datafile", $3); }; o_nobs : NOBS EQUAL vec_int { driver.option_vec_int("nobs", $3); } @@ -1874,6 +1892,14 @@ o_conditional_variance_decomposition : CONDITIONAL_VARIANCE_DECOMPOSITION EQUAL { driver.option_vec_int("conditional_variance_decomposition", $3); } ; o_first_obs : FIRST_OBS EQUAL INT_NUMBER { driver.option_num("first_obs", $3); }; +o_new_estimation_data_first_obs : FIRST_OBS EQUAL date_number + { driver.option_date("first_obs", $3); } + ; +o_last_obs : LAST_OBS EQUAL date_number + { driver.option_date("last_obs", $3); } + ; +o_new_estimation_data_nobs : NOBS EQUAL INT_NUMBER { driver.option_num("nobs", $3); }; + o_prefilter : PREFILTER EQUAL INT_NUMBER { driver.option_num("prefilter", $3); }; o_presample : PRESAMPLE EQUAL INT_NUMBER { driver.option_num("presample", $3); }; o_lik_algo : LIK_ALGO EQUAL INT_NUMBER { driver.option_num("lik_algo", $3); }; diff --git a/preprocessor/DynareFlex.ll b/preprocessor/DynareFlex.ll index 7f46f7caa..ea17c7c63 100644 --- a/preprocessor/DynareFlex.ll +++ b/preprocessor/DynareFlex.ll @@ -109,6 +109,7 @@ string eofbuff; model_info {BEGIN DYNARE_STATEMENT; return token::MODEL_INFO;} estimation {BEGIN DYNARE_STATEMENT; return token::ESTIMATION;} set_time {BEGIN DYNARE_STATEMENT; return token::SET_TIME;} +data {BEGIN DYNARE_STATEMENT; return token::DATA;} varobs {BEGIN DYNARE_STATEMENT; return token::VAROBS;} unit_root_vars {BEGIN DYNARE_STATEMENT; return token::UNIT_ROOT_VARS;} rplot {BEGIN DYNARE_STATEMENT; return token::RPLOT;} @@ -189,8 +190,10 @@ string eofbuff; end {BEGIN INITIAL; return token::END;} /* Inside of a Dynare statement */ +file {return token::FILE;} datafile {return token::DATAFILE;} nobs {return token::NOBS;} +last_obs {return token::LAST_OBS;} first_obs {return token::FIRST_OBS;} prefilter {return token::PREFILTER;} presample {return token::PRESAMPLE;} diff --git a/preprocessor/ParsingDriver.cc b/preprocessor/ParsingDriver.cc index bc734f8ff..541f08580 100644 --- a/preprocessor/ParsingDriver.cc +++ b/preprocessor/ParsingDriver.cc @@ -1212,6 +1212,13 @@ ParsingDriver::set_time(string *arg) options_list.clear(); } +void +ParsingDriver::estimation_data() +{ + mod_file->addStatement(new EstimationDataStatement(options_list)); + options_list.clear(); +} + void ParsingDriver::run_estimation() { diff --git a/preprocessor/ParsingDriver.hh b/preprocessor/ParsingDriver.hh index d3d7ac01c..2afbd09c0 100644 --- a/preprocessor/ParsingDriver.hh +++ b/preprocessor/ParsingDriver.hh @@ -357,6 +357,8 @@ public: void add_estimated_params_element(); //! Sets the frequency of the data void set_time(string *arg); + //! Estimation Data + void estimation_data(); //! Runs estimation process void run_estimation(); //! Runs dynare_sensitivy() diff --git a/preprocessor/Statement.hh b/preprocessor/Statement.hh index 3b9aebd84..0f0c2beee 100644 --- a/preprocessor/Statement.hh +++ b/preprocessor/Statement.hh @@ -86,6 +86,8 @@ public: bool dsge_var_estimated; //! Whether there is a bayesian_irf option passed to the estimation statement bool bayesian_irf_present; + //! Whether there is a data statement present + bool estimation_data_statement_present; }; class Statement From f5bfdbb23f127cfa4fdb66834d8025c96b65cf87 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Sun, 11 Dec 2011 15:35:26 +0100 Subject: [PATCH 04/14] preprocessor: add prior statement --- matlab/global_initialization.m | 21 +++- preprocessor/ComputingTasks.cc | 184 +++++++++++++++++++++++++++++++++ preprocessor/ComputingTasks.hh | 56 ++++++++++ preprocessor/DynareBison.yy | 100 +++++++++++++----- preprocessor/DynareFlex.ll | 37 ++++++- preprocessor/ModFile.cc | 2 +- preprocessor/ParsingDriver.cc | 69 ++++++++++++- preprocessor/ParsingDriver.hh | 18 ++++ preprocessor/Statement.hh | 6 ++ 9 files changed, 461 insertions(+), 32 deletions(-) diff --git a/matlab/global_initialization.m b/matlab/global_initialization.m index 00e937555..e3325df19 100644 --- a/matlab/global_initialization.m +++ b/matlab/global_initialization.m @@ -28,7 +28,7 @@ function global_initialization() % You should have received a copy of the GNU General Public License % along with Dynare. If not, see . -global oo_ M_ options_ estim_params_ bayestopt_ +global oo_ M_ options_ estim_params_ bayestopt_ estimation_info estim_params_ = []; bayestopt_ = []; @@ -182,6 +182,25 @@ options_.ramsey_policy = 0; options_.timeless = 0; % estimation +estimation_info.prior = struct('name', {}, 'shape', {}, 'mean', {}, ... + 'mode', {}, 'stdev', {}, 'date1', {}, ... + 'date2', {}, 'shift', {}, 'variance', {}); +estimation_info.structural_innovation.prior = struct('name', {}, 'shape', {}, 'mean', {}, ... + 'mode', {}, 'stdev', {}, 'date1', {}, ... + 'date2', {}, 'shift', {}, 'variance', {}); +estimation_info.structural_innovation_corr.prior = struct('name', {}, 'shape', {}, 'mean', {}, ... + 'mode', {}, 'stdev', {}, 'date1', {}, ... + 'date2', {}, 'shift', {}, 'variance', {}); +estimation_info.measurement_error.prior = struct('name', {}, 'shape', {}, 'mean', {}, ... + 'mode', {}, 'stdev', {}, 'date1', {}, ... + 'date2', {}, 'shift', {}, 'variance', {}); +estimation_info.measurement_error_corr.prior = struct('name', {}, 'shape', {}, 'mean', {}, ... + 'mode', {}, 'stdev', {}, 'date1', {}, ... + 'date2', {}, 'shift', {}, 'variance', {}); +estimation_info.measurement_error.prior_index = {}; +estimation_info.structural_innovation.prior_index = {}; +estimation_info.measurement_error_corr.prior_index = {}; +estimation_info.structural_innovation_corr.prior_index = {}; options_.initial_period = dynDate(1); options_.dataset.firstobs = options_.initial_period; options_.dataset.lastobs = NaN; diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc index 0a765469e..aa3029dd7 100644 --- a/preprocessor/ComputingTasks.cc +++ b/preprocessor/ComputingTasks.cc @@ -1499,3 +1499,187 @@ EstimationDataStatement::writeOutput(ostream &output, const string &basename) co if (options_list.date_options.find("first_obs") == options_list.date_options.end()) output << "options_.dataset.firstobs = options_.initial_period;" << endl; } + +BasicPriorStatement::~BasicPriorStatement() +{ +} + +BasicPriorStatement::BasicPriorStatement(const string &name_arg, + const expr_t &variance_arg, + const OptionsList &options_list_arg) : + name(name_arg), + variance(variance_arg), + options_list(options_list_arg), + first_statement_encountered(false) +{ +} + +void +BasicPriorStatement::checkPass(ModFileStructure &mod_file_struct) +{ + if (options_list.num_options.find("shape") == options_list.num_options.end()) + { + cerr << "ERROR: You must pass the shape option to the prior statement." << endl; + exit(EXIT_FAILURE); + } +} + +void +BasicPriorStatement::get_base_name(const SymbolType symb_type, string &lhs_field) const +{ + if (symb_type == eExogenous || symb_type == eExogenousDet) + lhs_field = "structural_innovation"; + else + lhs_field = "measurement_error"; +} + +void +BasicPriorStatement::writePriorIndex(ostream &output, const string &lhs_field) const +{ + if (first_statement_encountered) + output << "prior_indx = 1;" << endl; + else + output << "prior_indx = size(estimation_info" << lhs_field << "_index, 2) + 1;" << endl; +} + +void +BasicPriorStatement::writeVarianceOption(ostream &output, const string &lhs_field) const +{ + if (variance) + { + output << "estimation_info" << lhs_field << "(prior_indx).variance = "; + variance->writeOutput(output); + output << ";" << endl; + } +} + +void +BasicPriorStatement::writeOutputHelper(ostream &output, const string &field, const string &lhs_field) const +{ + OptionsList::num_options_t::const_iterator it = options_list.num_options.find(field); + if (it != options_list.num_options.end()) + output << "estimation_info" << lhs_field << "(prior_indx)." << field + << " = " << it->second << ";" << endl; +} + +PriorStatement::PriorStatement(const string &name_arg, + const expr_t &variance_arg, + const OptionsList &options_list_arg) : + BasicPriorStatement(name_arg, variance_arg, options_list_arg) +{ +} + +void +PriorStatement::checkPass(ModFileStructure &mod_file_struct) +{ + BasicPriorStatement::checkPass(mod_file_struct); + if (!mod_file_struct.prior_statement_present) + first_statement_encountered = true; + mod_file_struct.prior_statement_present = true; +} + +void +PriorStatement::writeOutput(ostream &output, const string &basename) const +{ + string lhs_field = ".prior"; + + BasicPriorStatement::writePriorIndex(output, lhs_field); + output << "estimation_info" << lhs_field << "_index(prior_indx) = {'" << name << "'};" << endl + << "estimation_info" << lhs_field <<"(prior_indx).name = '" << name << "';" << endl; + + writeOutputHelper(output, "mean", lhs_field); + writeOutputHelper(output, "mode", lhs_field); + writeOutputHelper(output, "stdev", lhs_field); + writeOutputHelper(output, "shape", lhs_field); + writeOutputHelper(output, "shift", lhs_field); + writeOutputHelper(output, "domain", lhs_field); + writeOutputHelper(output, "interval", lhs_field); + BasicPriorStatement::writeVarianceOption(output, lhs_field); +} + +StdPriorStatement::StdPriorStatement(const string &name_arg, + const expr_t &variance_arg, + const OptionsList &options_list_arg, + const SymbolTable &symbol_table_arg ) : + BasicPriorStatement(name_arg, variance_arg, options_list_arg), + symbol_table(symbol_table_arg) +{ +} + +void +StdPriorStatement::checkPass(ModFileStructure &mod_file_struct) +{ + BasicPriorStatement::checkPass(mod_file_struct); + if (!mod_file_struct.std_prior_statement_present) + first_statement_encountered = true; + mod_file_struct.std_prior_statement_present = true; +} + +void +StdPriorStatement::writeOutput(ostream &output, const string &basename) const +{ + string lhs_field; + get_base_name(symbol_table.getType(name), lhs_field); + lhs_field = "." + lhs_field + ".prior"; + + BasicPriorStatement::writePriorIndex(output, lhs_field); + output << "estimation_info" << lhs_field << "_index(prior_indx) = {'" << name << "'};" << endl; + output << "estimation_info" << lhs_field << "(prior_indx).name = '" << name << "';" << endl; + + writeOutputHelper(output, "mean", lhs_field); + writeOutputHelper(output, "mode", lhs_field); + writeOutputHelper(output, "stdev", lhs_field); + writeOutputHelper(output, "shape", lhs_field); + writeOutputHelper(output, "shift", lhs_field); + writeOutputHelper(output, "domain", lhs_field); + writeOutputHelper(output, "interval", lhs_field); + BasicPriorStatement::writeVarianceOption(output, lhs_field); +} + +CorrPriorStatement::CorrPriorStatement(const string &name_arg1, const string &name_arg2, + const expr_t &variance_arg, + const OptionsList &options_list_arg, + const SymbolTable &symbol_table_arg ) : + BasicPriorStatement(name_arg1, variance_arg, options_list_arg), + name1(name_arg2), + symbol_table(symbol_table_arg) +{ +} + +void +CorrPriorStatement::checkPass(ModFileStructure &mod_file_struct) +{ + BasicPriorStatement::checkPass(mod_file_struct); + if (symbol_table.getType(name) != symbol_table.getType(name1)) + { + cerr << "ERROR: In the corr(A,B).prior statement, A and B must be of the same type. " + << "In your case, " << name << " and " << name1 << " are of different " + << "types." << endl; + exit(EXIT_FAILURE); + } + if (!mod_file_struct.corr_prior_statement_present) + first_statement_encountered = true; + mod_file_struct.corr_prior_statement_present = true; +} + +void +CorrPriorStatement::writeOutput(ostream &output, const string &basename) const +{ + string lhs_field; + get_base_name(symbol_table.getType(name), lhs_field); + lhs_field = "." + lhs_field + "_corr.prior"; + + BasicPriorStatement::writePriorIndex(output, lhs_field); + output << "estimation_info" << lhs_field << "_index(prior_indx) = {'" << name << "_" << name1 << "'};" << endl; + output << "estimation_info" << lhs_field << "(prior_indx).name1 = '" << name << "';" << endl; + output << "estimation_info" << lhs_field << "(prior_indx).name2 = '" << name1 << "';" << endl; + + writeOutputHelper(output, "mean", lhs_field); + writeOutputHelper(output, "mode", lhs_field); + writeOutputHelper(output, "stdev", lhs_field); + writeOutputHelper(output, "shape", lhs_field); + writeOutputHelper(output, "shift", lhs_field); + writeOutputHelper(output, "domain", lhs_field); + writeOutputHelper(output, "interval", lhs_field); + BasicPriorStatement::writeVarianceOption(output, lhs_field); +} diff --git a/preprocessor/ComputingTasks.hh b/preprocessor/ComputingTasks.hh index a04a7d18b..c69043fb6 100644 --- a/preprocessor/ComputingTasks.hh +++ b/preprocessor/ComputingTasks.hh @@ -575,5 +575,61 @@ public: virtual void writeOutput(ostream &output, const string &basename) const; }; +class BasicPriorStatement : public Statement +{ +public: + virtual ~BasicPriorStatement(); +protected: + const string name; + const expr_t variance; + const OptionsList options_list; + bool first_statement_encountered; + BasicPriorStatement(const string &name_arg, + const expr_t &variance_arg, + const OptionsList &options_list_arg); + virtual void checkPass(ModFileStructure &mod_file_struct); + void get_base_name(const SymbolType symb_type, string &lhs_field) const; + void writePriorIndex(ostream &output, const string &lhs_field) const; + void writeVarianceOption(ostream &output, const string &lhs_field) const; + void writeOutputHelper(ostream &output, const string &field, const string &lhs_field) const; +}; + +class PriorStatement : public BasicPriorStatement +{ +public: + PriorStatement(const string &name_arg, + const expr_t &variance_arg, + const OptionsList &options_list_arg); + virtual void checkPass(ModFileStructure &mod_file_struct); + virtual void writeOutput(ostream &output, const string &basename) const; +}; + +class StdPriorStatement : public BasicPriorStatement +{ +private: + const SymbolTable symbol_table; +public: + StdPriorStatement(const string &name_arg, + const expr_t &variance_arg, + const OptionsList &options_list_arg, + const SymbolTable &symbol_table_arg); + virtual void checkPass(ModFileStructure &mod_file_struct); + virtual void writeOutput(ostream &output, const string &basename) const; +}; + +class CorrPriorStatement : public BasicPriorStatement +{ +private: + const string name1; + const SymbolTable symbol_table; +public: + CorrPriorStatement(const string &name_arg1, + const string &name_arg2, + const expr_t &variance_arg, + const OptionsList &options_list_arg, + const SymbolTable &symbol_table_arg); + virtual void checkPass(ModFileStructure &mod_file_struct); + virtual void writeOutput(ostream &output, const string &basename) const; +}; #endif diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy index 772369462..299bae37d 100644 --- a/preprocessor/DynareBison.yy +++ b/preprocessor/DynareBison.yy @@ -98,8 +98,8 @@ class ParsingDriver; %token END ENDVAL EQUAL ESTIMATION ESTIMATED_PARAMS ESTIMATED_PARAMS_BOUNDS ESTIMATED_PARAMS_INIT %token FILENAME FILTER_STEP_AHEAD FILTERED_VARS FIRST_OBS LAST_OBS SET_TIME %token FLOAT_NUMBER -%token FORECAST K_ORDER_SOLVER INSTRUMENTS -%token GAMMA_PDF GRAPH CONDITIONAL_VARIANCE_DECOMPOSITION NOCHECK +%token FORECAST K_ORDER_SOLVER INSTRUMENTS PRIOR SHIFT MEAN STDEV VARIANCE MODE INTERVAL SHAPE DOMAINN +%token GAMMA_PDF GRAPH CONDITIONAL_VARIANCE_DECOMPOSITION NOCHECK STD %token HISTVAL HOMOTOPY_SETUP HOMOTOPY_MODE HOMOTOPY_STEPS HP_FILTER HP_NGRID %token IDENTIFICATION INF_CONSTANT INITVAL INITVAL_FILE %token INT_NUMBER @@ -147,7 +147,7 @@ class ParsingDriver; %token VLISTLOG VLISTPER %token RESTRICTION RESTRICTIONS RESTRICTION_FNAME CROSS_RESTRICTIONS NLAGS CONTEMP_REDUCED_FORM REAL_PSEUDO_FORECAST %token NONE DUMMY_OBS NSTATES INDXSCALESSTATES NO_BAYESIAN_PRIOR SPECIFICATION SIMS_ZHA -%token ALPHA BETA ABAND NINV CMS NCMS CNUM +%token ALPHA BETA ABAND NINV CMS NCMS CNUM GAMMA INV_GAMMA INV_GAMMA1 INV_GAMMA2 NORMAL UNIFORM %token GSIG2_LMDM Q_DIAG FLAT_PRIOR NCSK NSTD %token INDXPARR INDXOVR INDXAP APBAND INDXIMF IMFBAND INDXFORE FOREBAND INDXGFOREHAT INDXGIMFHAT %token INDXESTIMA INDXGDLS EQ_MS FILTER_COVARIANCE FILTER_DECOMPOSITION @@ -175,9 +175,9 @@ class ParsingDriver; %type expression expression_or_empty %type equation hand_side %type non_negative_number signed_number signed_integer date_number -%type filename symbol +%type filename symbol prior_distribution %type vec_value_1 vec_value -%type range prior +%type range prior_pdf_string %type change_type_arg %type change_type_var_list %type vec_int_elem vec_int_1 vec_int vec_int_number @@ -216,6 +216,7 @@ statement : parameters | estimated_params_init | set_time | data + | prior | varobs | observation_trends | unit_root_vars @@ -1014,18 +1015,18 @@ estimated_elem1 : STDERR symbol } ; -estimated_elem2 : prior COMMA estimated_elem3 +estimated_elem2 : prior_pdf_string COMMA estimated_elem3 { driver.estim_params.prior = *$1; delete $1; } - | expression_or_empty COMMA prior COMMA estimated_elem3 + | expression_or_empty COMMA prior_pdf_string COMMA estimated_elem3 { driver.estim_params.init_val = $1; driver.estim_params.prior = *$3; delete $3; } - | expression_or_empty COMMA expression_or_empty COMMA expression_or_empty COMMA prior COMMA estimated_elem3 + | expression_or_empty COMMA expression_or_empty COMMA expression_or_empty COMMA prior_pdf_string COMMA estimated_elem3 { driver.estim_params.init_val = $1; driver.estim_params.low_bound = $3; @@ -1144,21 +1145,37 @@ estimated_bounds_elem : STDERR symbol COMMA expression COMMA expression ';' } ; -prior : BETA_PDF - { $$ = new string("1"); } - | GAMMA_PDF - { $$ = new string("2"); } - | NORMAL_PDF - { $$ = new string("3"); } - | INV_GAMMA_PDF - { $$ = new string("4"); } - | INV_GAMMA1_PDF - { $$ = new string("4"); } - | UNIFORM_PDF - { $$ = new string("5"); } - | INV_GAMMA2_PDF - { $$ = new string("6"); } - ; +prior_distribution : BETA + { $$ = new string("1"); } + | GAMMA + { $$ = new string("2"); } + | NORMAL + { $$ = new string("3"); } + | INV_GAMMA + { $$ = new string("4"); } + | INV_GAMMA1 + { $$ = new string("4"); } + | UNIFORM + { $$ = new string("5"); } + | INV_GAMMA2 + { $$ = new string("6"); } + ; + +prior_pdf_string : BETA_PDF + { $$ = new string("1"); } + | GAMMA_PDF + { $$ = new string("2"); } + | NORMAL_PDF + { $$ = new string("3"); } + | INV_GAMMA_PDF + { $$ = new string("4"); } + | INV_GAMMA1_PDF + { $$ = new string("4"); } + | UNIFORM_PDF + { $$ = new string("5"); } + | INV_GAMMA2_PDF + { $$ = new string("6"); } + ; set_time : SET_TIME '(' date_number ')' ';' { driver.set_time($3); } @@ -1180,6 +1197,28 @@ data_options : o_file | o_xls_range ; +prior : symbol '.' PRIOR '(' prior_options_list ')' ';' + { driver.set_prior($1); } + | STD '(' symbol ')' '.' PRIOR '(' prior_options_list ')' ';' + { driver.set_std_prior($3); } + | CORR '(' symbol COMMA symbol')' '.' PRIOR '(' prior_options_list ')' ';' + { driver.set_corr_prior($3, $5); } + ; + +prior_options_list : prior_options_list COMMA prior_options + | prior_options + ; + +prior_options : o_shift + | o_mean + | o_stdev + | o_variance + | o_mode + | o_interval + | o_shape + | o_domain + ; + estimation : ESTIMATION ';' { driver.run_estimation(); } | ESTIMATION '(' estimation_options_list ')' ';' @@ -1898,8 +1937,15 @@ o_new_estimation_data_first_obs : FIRST_OBS EQUAL date_number o_last_obs : LAST_OBS EQUAL date_number { driver.option_date("last_obs", $3); } ; +o_shift : SHIFT EQUAL signed_number { driver.option_num("shift", $3); }; +o_shape : SHAPE EQUAL prior_distribution { driver.option_num("shape", $3); }; +o_mode : MODE EQUAL signed_number { driver.option_num("mode", $3); }; +o_mean : MEAN EQUAL signed_number { driver.option_num("mean", $3); }; +o_stdev : STDEV EQUAL non_negative_number { driver.option_num("stdev", $3); }; +o_domain : DOMAINN EQUAL vec_value { driver.option_num("domain", $3); }; +o_interval : INTERVAL EQUAL vec_value { driver.option_num("interval", $3); }; +o_variance : VARIANCE EQUAL expression { driver.add_expression_to_prior_statement($3); } o_new_estimation_data_nobs : NOBS EQUAL INT_NUMBER { driver.option_num("nobs", $3); }; - o_prefilter : PREFILTER EQUAL INT_NUMBER { driver.option_num("prefilter", $3); }; o_presample : PRESAMPLE EQUAL INT_NUMBER { driver.option_num("presample", $3); }; o_lik_algo : LIK_ALGO EQUAL INT_NUMBER { driver.option_num("lik_algo", $3); }; @@ -2276,6 +2322,12 @@ symbol : NAME | CMS | NCMS | CNUM + | GAMMA + | INV_GAMMA + | INV_GAMMA1 + | INV_GAMMA2 + | NORMAL + | UNIFORM ; %% diff --git a/preprocessor/DynareFlex.ll b/preprocessor/DynareFlex.ll index ea17c7c63..56afce82e 100644 --- a/preprocessor/DynareFlex.ll +++ b/preprocessor/DynareFlex.ll @@ -189,12 +189,24 @@ string eofbuff; /* End of a Dynare block */ end {BEGIN INITIAL; return token::END;} +prior {return token::PRIOR;} +std {BEGIN DYNARE_STATEMENT; return token::STD;} +corr {BEGIN DYNARE_STATEMENT; return token::CORR;} + /* Inside of a Dynare statement */ file {return token::FILE;} datafile {return token::DATAFILE;} nobs {return token::NOBS;} last_obs {return token::LAST_OBS;} first_obs {return token::FIRST_OBS;} +mean {return token::MEAN;} +stdev {return token::STDEV;} +domain {return token::DOMAINN;} +variance {return token::VARIANCE;} +mode {return token::MODE;} +interval {return token::INTERVAL;} +shape {return token::SHAPE;} +shift {return token::SHIFT;} prefilter {return token::PREFILTER;} presample {return token::PRESAMPLE;} lik_algo {return token::LIK_ALGO;} @@ -253,7 +265,6 @@ string eofbuff; nargs {return token::EXT_FUNC_NARGS;} first_deriv_provided {return token::FIRST_DERIV_PROVIDED;} second_deriv_provided {return token::SECOND_DERIV_PROVIDED;} - freq {return token::FREQ;} monthly {return token::MONTHLY; } quarterly {return token::QUARTERLY; } @@ -281,6 +292,30 @@ string eofbuff; yylval->string_val = new string(yytext); return token::BETA; } +gamma { + yylval->string_val = new string(yytext); + return token::GAMMA; +} +inv_gamma { + yylval->string_val = new string(yytext); + return token::INV_GAMMA; +} +inv_gamma1 { + yylval->string_val = new string(yytext); + return token::INV_GAMMA1; +} +inv_gamma2 { + yylval->string_val = new string(yytext); + return token::INV_GAMMA2; +} +normal { + yylval->string_val = new string(yytext); + return token::NORMAL; +} +uniform { + yylval->string_val = new string(yytext); + return token::UNIFORM; +} gsig2_lmdm {return token::GSIG2_LMDM;} specification {return token::SPECIFICATION;} sims_zha {return token::SIMS_ZHA;} diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc index 20356dfbd..6b79d10f3 100644 --- a/preprocessor/ModFile.cc +++ b/preprocessor/ModFile.cc @@ -449,7 +449,7 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool console, } mOutputFile << "tic;" << endl - << "global M_ oo_ options_ ys0_ ex0_" << endl + << "global M_ oo_ options_ ys0_ ex0_ estimation_info" << endl << "options_ = [];" << endl << "M_.fname = '" << basename << "';" << endl << "%" << endl diff --git a/preprocessor/ParsingDriver.cc b/preprocessor/ParsingDriver.cc index 541f08580..9cf200c89 100644 --- a/preprocessor/ParsingDriver.cc +++ b/preprocessor/ParsingDriver.cc @@ -46,6 +46,15 @@ ParsingDriver::check_symbol_existence(const string &name) error("Unknown symbol: " + name); } +void +ParsingDriver::check_symbol_is_parameter(string *name) +{ + check_symbol_existence(*name); + int symb_id = mod_file->symbol_table.getID(*name); + if (mod_file->symbol_table.getType(symb_id) != eParameter) + error(*name + " is not a parameter"); +} + void ParsingDriver::set_current_data_tree(DataTree *data_tree_arg) { @@ -372,13 +381,9 @@ ParsingDriver::dsample(string *arg1, string *arg2) void ParsingDriver::init_param(string *name, expr_t rhs) { - check_symbol_existence(*name); + check_symbol_is_parameter(name); int symb_id = mod_file->symbol_table.getID(*name); - if (mod_file->symbol_table.getType(symb_id) != eParameter) - error(*name + " is not a parameter"); - mod_file->addStatement(new InitParamStatement(symb_id, rhs, mod_file->symbol_table)); - delete name; } @@ -1219,6 +1224,60 @@ ParsingDriver::estimation_data() options_list.clear(); } +void +ParsingDriver::set_prior(string *name) +{ + check_symbol_is_parameter(name); + mod_file->addStatement(new PriorStatement(*name, prior_variance, options_list)); + options_list.clear(); + prior_variance = NULL; + delete name; +} + +void +ParsingDriver::add_expression_to_prior_statement(expr_t variance) +{ + prior_variance = variance; +} + +void +ParsingDriver::check_symbol_is_endogenous_or_exogenous(string *name) +{ + check_symbol_existence(*name); + int symb_id = mod_file->symbol_table.getID(*name); + switch(mod_file->symbol_table.getType(symb_id)) + { + case eEndogenous: + case eExogenous: + case eExogenousDet: + break; + default: + error(*name + " is neither endogenous or exogenous."); + } +} + +void +ParsingDriver::set_std_prior(string *name) +{ + check_symbol_is_endogenous_or_exogenous(name); + mod_file->addStatement(new StdPriorStatement(*name, prior_variance, options_list, mod_file->symbol_table)); + options_list.clear(); + prior_variance = NULL; + delete name; +} + +void +ParsingDriver::set_corr_prior(string *name1, string *name2) +{ + check_symbol_is_endogenous_or_exogenous(name1); + check_symbol_is_endogenous_or_exogenous(name2); + mod_file->addStatement(new CorrPriorStatement(*name1, *name2, prior_variance, options_list, mod_file->symbol_table)); + options_list.clear(); + prior_variance = NULL; + delete name1; + delete name2; +} + void ParsingDriver::run_estimation() { diff --git a/preprocessor/ParsingDriver.hh b/preprocessor/ParsingDriver.hh index 2afbd09c0..6c35297e8 100644 --- a/preprocessor/ParsingDriver.hh +++ b/preprocessor/ParsingDriver.hh @@ -81,6 +81,12 @@ private: //! Checks that a given symbol exists, and stops with an error message if it doesn't void check_symbol_existence(const string &name); + //! Checks that a given symbol exists and is a parameter, and stops with an error message if it isn't + void check_symbol_is_parameter(string *name); + + //! Checks that a given symbol exists and is a endogenous or exogenous, and stops with an error message if it isn't + void check_symbol_is_endogenous_or_exogenous(string *name); + //! Helper to add a symbol declaration void declare_symbol(const string *name, SymbolType type, const string *tex_name); @@ -178,6 +184,8 @@ private: vector declared_trend_vars; //! Temporary storage for declaring nonstationary variables vector declared_nonstationary_vars; + //! Temporary storage for a variance declared in the prior statement + expr_t prior_variance; //! reset the values for temporary storage void reset_current_external_function_options(); //! Adds a model lagged variable to ModelTree and VariableTable @@ -359,6 +367,16 @@ public: void set_time(string *arg); //! Estimation Data void estimation_data(); + //! Sets the prior for a parameter + void set_prior(string *arg); + //! Adds the variance option to its temporary holding place + void add_expression_to_prior_statement(expr_t variance); + //! Sets the prior for estimated std dev + void set_std_prior(string *arg); + //! Sets the prior for estimated correlation + void set_corr_prior(string *arg1, string *arg2); + //! Sets the subsamples for a parameter + void set_subsamples(string *arg); //! Runs estimation process void run_estimation(); //! Runs dynare_sensitivy() diff --git a/preprocessor/Statement.hh b/preprocessor/Statement.hh index 0f0c2beee..a6091437a 100644 --- a/preprocessor/Statement.hh +++ b/preprocessor/Statement.hh @@ -88,6 +88,12 @@ public: bool bayesian_irf_present; //! Whether there is a data statement present bool estimation_data_statement_present; + //! Whether there is a prior statement present + bool prior_statement_present; + //! Whether there is a std prior statement present + bool std_prior_statement_present; + //! Whether there is a corr prior statement present + bool corr_prior_statement_present; }; class Statement From c1e415500151559c4d85ae0341779811e9e83b7a Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 14 Dec 2011 13:58:21 +0100 Subject: [PATCH 05/14] preprocessor: add subsamples statement --- preprocessor/CodeInterpreter.hh | 17 +++++----- preprocessor/ComputingTasks.cc | 23 +++++++++++-- preprocessor/DynareBison.yy | 22 +++++++++++- preprocessor/DynareFlex.ll | 1 + preprocessor/ExprNode.cc | 8 +++++ preprocessor/ParsingDriver.cc | 60 +++++++++++++++++++++++++++++++++ preprocessor/ParsingDriver.hh | 19 +++++++++-- 7 files changed, 136 insertions(+), 14 deletions(-) diff --git a/preprocessor/CodeInterpreter.hh b/preprocessor/CodeInterpreter.hh index f730cbbc9..964d05c89 100644 --- a/preprocessor/CodeInterpreter.hh +++ b/preprocessor/CodeInterpreter.hh @@ -140,14 +140,15 @@ enum BlockSimulationType /*! Warning: do not to change existing values for 0 to 4: the values matter for homotopy_setup command */ enum SymbolType { - eEndogenous = 0, //!< Endogenous - eExogenous = 1, //!< Exogenous - eExogenousDet = 2, //!< Exogenous deterministic - eParameter = 4, //!< Parameter - eModelLocalVariable = 10, //!< Local variable whose scope is model (pound expression) - eModFileLocalVariable = 11, //!< Local variable whose scope is mod file (model excluded) - eExternalFunction = 12, //!< External (user-defined) function - eTrend = 13 //!< Trend variable + eEndogenous = 0, //!< Endogenous + eExogenous = 1, //!< Exogenous + eExogenousDet = 2, //!< Exogenous deterministic + eParameter = 4, //!< Parameter + eModelLocalVariable = 10, //!< Local variable whose scope is model (pound expression) + eModFileLocalVariable = 11, //!< Local variable whose scope is mod file (model excluded) + eExternalFunction = 12, //!< External (user-defined) function + eTrend = 13, //!< Trend variable + eStatementDeclaredVariable = 14 //!< Local variable assigned within a Statement (see subsample statement for example) }; enum ExpressionType diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc index aa3029dd7..aae564fe8 100644 --- a/preprocessor/ComputingTasks.cc +++ b/preprocessor/ComputingTasks.cc @@ -1522,6 +1522,15 @@ BasicPriorStatement::checkPass(ModFileStructure &mod_file_struct) cerr << "ERROR: You must pass the shape option to the prior statement." << endl; exit(EXIT_FAILURE); } + if (options_list.num_options.find("date1") != options_list.num_options.end() || + options_list.num_options.find("date2") != options_list.num_options.end()) + if (options_list.num_options.find("date1") == options_list.num_options.end() || + options_list.num_options.find("date2") == options_list.num_options.end()) + { + cerr << "ERROR: PriorStatement::checkPass(1). Should not arrive here. " + << "Please inform Dynare Team." << endl; + exit(EXIT_FAILURE); + } } void @@ -1556,10 +1565,16 @@ BasicPriorStatement::writeVarianceOption(ostream &output, const string &lhs_fiel void BasicPriorStatement::writeOutputHelper(ostream &output, const string &field, const string &lhs_field) const { - OptionsList::num_options_t::const_iterator it = options_list.num_options.find(field); - if (it != options_list.num_options.end()) + + OptionsList::num_options_t::const_iterator itn = options_list.num_options.find(field); + if (itn != options_list.num_options.end()) output << "estimation_info" << lhs_field << "(prior_indx)." << field - << " = " << it->second << ";" << endl; + << " = " << itn->second << ";" << endl; + + OptionsList::date_options_t::const_iterator itd = options_list.date_options.find(field); + if (itd != options_list.date_options.end()) + output << "estimation_info" << lhs_field << "(prior_indx)." << field + << " = '" << itd->second << "';" << endl; } PriorStatement::PriorStatement(const string &name_arg, @@ -1592,6 +1607,8 @@ PriorStatement::writeOutput(ostream &output, const string &basename) const writeOutputHelper(output, "stdev", lhs_field); writeOutputHelper(output, "shape", lhs_field); writeOutputHelper(output, "shift", lhs_field); + writeOutputHelper(output, "date1", lhs_field); + writeOutputHelper(output, "date2", lhs_field); writeOutputHelper(output, "domain", lhs_field); writeOutputHelper(output, "interval", lhs_field); BasicPriorStatement::writeVarianceOption(output, lhs_field); diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy index 299bae37d..0dcbffb64 100644 --- a/preprocessor/DynareBison.yy +++ b/preprocessor/DynareBison.yy @@ -105,7 +105,7 @@ class ParsingDriver; %token INT_NUMBER %token DATE_NUMBER %token INV_GAMMA_PDF INV_GAMMA1_PDF INV_GAMMA2_PDF IRF IRF_SHOCKS -%token KALMAN_ALGO KALMAN_TOL +%token KALMAN_ALGO KALMAN_TOL SUBSAMPLES %token LABELS LAPLACE LIK_ALGO LIK_INIT LINEAR LOAD_IDENT_FILES LOAD_MH_FILE LOAD_PARAMS_AND_STEADY_STATE LOGLINEAR %token MARKOWITZ MARGINAL_DENSITY MAX MAXIT %token MFS MH_DROP MH_INIT_SCALE MH_JSCALE MH_MODE MH_NBLOCKS MH_REPLIC MH_RECOVER MIN MINIMAL_SOLVING_PERIODS @@ -217,6 +217,7 @@ statement : parameters | set_time | data | prior + | subsamples | varobs | observation_trends | unit_root_vars @@ -1197,8 +1198,21 @@ data_options : o_file | o_xls_range ; +subsamples : symbol '.' SUBSAMPLES '(' subsamples_name_list ')' ';' + { driver.set_subsamples($1); } + ; + +subsamples_name_list : subsamples_name_list COMMA o_subsample_name + | o_subsample_name + ; + prior : symbol '.' PRIOR '(' prior_options_list ')' ';' { driver.set_prior($1); } + | symbol '.' symbol '.' PRIOR '(' prior_options_list ')' ';' + { + driver.add_subsample_range(new string (*$1), $3); + driver.set_prior($1); + } | STD '(' symbol ')' '.' PRIOR '(' prior_options_list ')' ';' { driver.set_std_prior($3); } | CORR '(' symbol COMMA symbol')' '.' PRIOR '(' prior_options_list ')' ';' @@ -1955,6 +1969,12 @@ o_nograph : NOGRAPH | GRAPH { driver.option_num("nograph", "0"); } ; +o_subsample_name : symbol EQUAL date_number ':' date_number + { + driver.declare_statement_local_variable(new string (*$1)); + driver.set_subsample_name_equal_to_date_range($1, $3, $5); + } + ; o_conf_sig : CONF_SIG EQUAL non_negative_number { driver.option_num("conf_sig", $3); }; o_mh_replic : MH_REPLIC EQUAL INT_NUMBER { driver.option_num("mh_replic", $3); }; o_mh_drop : MH_DROP EQUAL non_negative_number { driver.option_num("mh_drop", $3); }; diff --git a/preprocessor/DynareFlex.ll b/preprocessor/DynareFlex.ll index 56afce82e..d0ff91fd7 100644 --- a/preprocessor/DynareFlex.ll +++ b/preprocessor/DynareFlex.ll @@ -189,6 +189,7 @@ string eofbuff; /* End of a Dynare block */ end {BEGIN INITIAL; return token::END;} +subsamples {return token::SUBSAMPLES;} prior {return token::PRIOR;} std {BEGIN DYNARE_STATEMENT; return token::STD;} corr {BEGIN DYNARE_STATEMENT; return token::CORR;} diff --git a/preprocessor/ExprNode.cc b/preprocessor/ExprNode.cc index 0f1f63763..0a8a06a94 100644 --- a/preprocessor/ExprNode.cc +++ b/preprocessor/ExprNode.cc @@ -496,6 +496,7 @@ VariableNode::prepareForDerivation() non_null_derivatives = datatree.local_variables_table[symb_id]->non_null_derivatives; break; case eModFileLocalVariable: + case eStatementDeclaredVariable: // Such a variable is never derived break; case eExternalFunction: @@ -523,6 +524,9 @@ VariableNode::computeDerivative(int deriv_id) case eModFileLocalVariable: cerr << "ModFileLocalVariable is not derivable" << endl; exit(EXIT_FAILURE); + case eStatementDeclaredVariable: + cerr << "eStatementDeclaredVariable is not derivable" << endl; + exit(EXIT_FAILURE); case eExternalFunction: cerr << "Impossible case!" << endl; exit(EXIT_FAILURE); @@ -735,6 +739,7 @@ VariableNode::writeOutput(ostream &output, ExprNodeOutputType output_type, case eExternalFunction: case eTrend: + case eStatementDeclaredVariable: cerr << "Impossible case" << endl; exit(EXIT_FAILURE); } @@ -917,6 +922,9 @@ VariableNode::getChainRuleDerivative(int deriv_id, const map &recur case eModFileLocalVariable: cerr << "ModFileLocalVariable is not derivable" << endl; exit(EXIT_FAILURE); + case eStatementDeclaredVariable: + cerr << "eStatementDeclaredVariable is not derivable" << endl; + exit(EXIT_FAILURE); case eExternalFunction: cerr << "Impossible case!" << endl; exit(EXIT_FAILURE); diff --git a/preprocessor/ParsingDriver.cc b/preprocessor/ParsingDriver.cc index 9cf200c89..c16e6fedd 100644 --- a/preprocessor/ParsingDriver.cc +++ b/preprocessor/ParsingDriver.cc @@ -175,6 +175,16 @@ ParsingDriver::declare_parameter(string *name, string *tex_name) delete tex_name; } +void +ParsingDriver::declare_statement_local_variable(string *name) +{ + if (mod_file->symbol_table.exists(*name)) + error("Symbol " + *name + " cannot be assigned within a statement " + + "while being assigned elsewhere in the modfile"); + declare_symbol(name, eStatementDeclaredVariable, NULL); + delete name; +} + void ParsingDriver::declare_optimal_policy_discount_factor_parameter(expr_t exprnode) { @@ -1224,6 +1234,56 @@ ParsingDriver::estimation_data() options_list.clear(); } +void +ParsingDriver::set_subsamples(string *name) +{ + check_symbol_is_parameter(name); + if (subsample_declarations.find(*name) != subsample_declarations.end()) + error("Parameter " + *name + " has more than one subsample statement." + + "You may only have one subsample statement per parameter."); + subsample_declarations[*name] = subsample_declaration_map; + subsample_declaration_map.clear(); + delete name; +} + +void +ParsingDriver::check_symbol_is_statement_variable(string *name) +{ + check_symbol_existence(*name); + int symb_id = mod_file->symbol_table.getID(*name); + if (mod_file->symbol_table.getType(symb_id) != eStatementDeclaredVariable) + error(*name + " is not a variable assigned in a statement"); +} + +void +ParsingDriver::set_subsample_name_equal_to_date_range(string *name, string *date1, string *date2) +{ + check_symbol_is_statement_variable(name); + if (subsample_declaration_map.find(*name) != subsample_declaration_map.end()) + error("Symbol " + *name + " may only be assigned once in a SUBSAMPLE statement"); + subsample_declaration_map[*name] = make_pair(*date1, *date2); + delete name; + delete date1; + delete date2; +} + +void +ParsingDriver::add_subsample_range(string *parameter, string *subsample_name) +{ + check_symbol_is_parameter(parameter); + check_symbol_is_statement_variable(subsample_name); + subsample_declarations_t::const_iterator it = subsample_declarations.find(*parameter); + if (it == subsample_declarations.end()) + error("A subsample statement has not been issued for " + *parameter); + subsample_declaration_map_t tmp_map = it->second; + if (tmp_map.find(*subsample_name) == tmp_map.end()) + error("The subsample name " + *subsample_name + " was not previously declared in a subsample statement."); + option_date("date1", tmp_map[*subsample_name].first); + option_date("date2", tmp_map[*subsample_name].second); + delete parameter; + delete subsample_name; +} + void ParsingDriver::set_prior(string *name) { diff --git a/preprocessor/ParsingDriver.hh b/preprocessor/ParsingDriver.hh index 6c35297e8..0deebfd14 100644 --- a/preprocessor/ParsingDriver.hh +++ b/preprocessor/ParsingDriver.hh @@ -84,6 +84,9 @@ private: //! Checks that a given symbol exists and is a parameter, and stops with an error message if it isn't void check_symbol_is_parameter(string *name); + //! Checks that a given symbol was assigned within a Statement + void check_symbol_is_statement_variable(string *name); + //! Checks that a given symbol exists and is a endogenous or exogenous, and stops with an error message if it isn't void check_symbol_is_endogenous_or_exogenous(string *name); @@ -186,6 +189,12 @@ private: vector declared_nonstationary_vars; //! Temporary storage for a variance declared in the prior statement expr_t prior_variance; + //! Temporary storage for declaring subsamples: map + typedef map > subsample_declaration_map_t; + subsample_declaration_map_t subsample_declaration_map; + //! Temporary storage for subsample statement: map + typedef map subsample_declarations_t; + subsample_declarations_t subsample_declarations; //! reset the values for temporary storage void reset_current_external_function_options(); //! Adds a model lagged variable to ModelTree and VariableTable @@ -239,6 +248,14 @@ public: void declare_exogenous_det(string *name, string *tex_name = NULL); //! Declares a parameter void declare_parameter(string *name, string *tex_name = NULL); + //! Declares a statement local variable + void declare_statement_local_variable(string *name); + //! Completes a subsample statement + void set_subsamples(string *name); + //! Declares a subsample, assigning the value to name + void set_subsample_name_equal_to_date_range(string *name, string *date1, string *date2); + //! Adds a subsample range to the list of options for the prior statement + void add_subsample_range(string *parameter, string *subsample_name); //! Declares declare_optimal_policy_discount_factor as a parameter and initializes it to exprnode void declare_optimal_policy_discount_factor_parameter(expr_t exprnode); //! Adds a predetermined_variable @@ -375,8 +392,6 @@ public: void set_std_prior(string *arg); //! Sets the prior for estimated correlation void set_corr_prior(string *arg1, string *arg2); - //! Sets the subsamples for a parameter - void set_subsamples(string *arg); //! Runs estimation process void run_estimation(); //! Runs dynare_sensitivy() From eb20a995f28d11e061e0bbb1e1aeba54d6a5eba3 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 15 Dec 2011 11:49:04 +0100 Subject: [PATCH 06/14] preprocessor: allow symbol.subsample=symbol.subsample --- preprocessor/DynareBison.yy | 5 +++++ preprocessor/ParsingDriver.cc | 15 +++++++++++++++ preprocessor/ParsingDriver.hh | 2 ++ 3 files changed, 22 insertions(+) diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy index 0dcbffb64..d23d5137d 100644 --- a/preprocessor/DynareBison.yy +++ b/preprocessor/DynareBison.yy @@ -218,6 +218,7 @@ statement : parameters | data | prior | subsamples + | subsamples_eq | varobs | observation_trends | unit_root_vars @@ -1202,6 +1203,10 @@ subsamples : symbol '.' SUBSAMPLES '(' subsamples_name_list ')' ';' { driver.set_subsamples($1); } ; +subsamples_eq : symbol '.' SUBSAMPLES EQUAL symbol '.' SUBSAMPLES ';' + { driver.copy_subsamples($1, $5); } + ; + subsamples_name_list : subsamples_name_list COMMA o_subsample_name | o_subsample_name ; diff --git a/preprocessor/ParsingDriver.cc b/preprocessor/ParsingDriver.cc index c16e6fedd..30ab3369d 100644 --- a/preprocessor/ParsingDriver.cc +++ b/preprocessor/ParsingDriver.cc @@ -1234,6 +1234,21 @@ ParsingDriver::estimation_data() options_list.clear(); } +void +ParsingDriver::copy_subsamples(string *to_parameter, string *from_parameter) +{ + check_symbol_is_parameter(to_parameter); + check_symbol_is_parameter(from_parameter); + if (subsample_declarations.find(*to_parameter) != subsample_declarations.end()) + error("Parameter " + *to_parameter + " has more than one subsample statement." + + "You may only have one subsample statement per parameter."); + if (subsample_declarations.find(*from_parameter) == subsample_declarations.end()) + error("Parameter " + *from_parameter + " does not have an associated subsample statement."); + subsample_declarations[*to_parameter] = subsample_declarations[*from_parameter]; + delete to_parameter; + delete from_parameter; +} + void ParsingDriver::set_subsamples(string *name) { diff --git a/preprocessor/ParsingDriver.hh b/preprocessor/ParsingDriver.hh index 0deebfd14..74ba84409 100644 --- a/preprocessor/ParsingDriver.hh +++ b/preprocessor/ParsingDriver.hh @@ -256,6 +256,8 @@ public: void set_subsample_name_equal_to_date_range(string *name, string *date1, string *date2); //! Adds a subsample range to the list of options for the prior statement void add_subsample_range(string *parameter, string *subsample_name); + //! Copies the set of subsamples from_parameter to_parameter + void copy_subsamples(string *to_parameter, string *from_parameter); //! Declares declare_optimal_policy_discount_factor as a parameter and initializes it to exprnode void declare_optimal_policy_discount_factor_parameter(expr_t exprnode); //! Adds a predetermined_variable From 87ffab3200d520d0f88f2bb1f995fa4c9d311add Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 14 Dec 2011 17:19:14 +0100 Subject: [PATCH 07/14] preprocessor: add symbol.options statement --- matlab/global_initialization.m | 4 + preprocessor/ComputingTasks.cc | 168 +++++++++++++++++++++++++++++++++ preprocessor/ComputingTasks.hh | 47 +++++++++ preprocessor/DynareBison.yy | 59 +++++++++++- preprocessor/DynareFlex.ll | 4 + preprocessor/ParsingDriver.cc | 29 ++++++ preprocessor/ParsingDriver.hh | 6 ++ preprocessor/Statement.hh | 6 ++ 8 files changed, 319 insertions(+), 4 deletions(-) diff --git a/matlab/global_initialization.m b/matlab/global_initialization.m index e3325df19..aa57f47bc 100644 --- a/matlab/global_initialization.m +++ b/matlab/global_initialization.m @@ -201,6 +201,10 @@ estimation_info.measurement_error.prior_index = {}; estimation_info.structural_innovation.prior_index = {}; estimation_info.measurement_error_corr.prior_index = {}; estimation_info.structural_innovation_corr.prior_index = {}; +estimation_info.measurement_error.options_index = {}; +estimation_info.structural_innovation.options_index = {}; +estimation_info.measurement_error_corr.options_index = {}; +estimation_info.structural_innovation_corr.options_index = {}; options_.initial_period = dynDate(1); options_.dataset.firstobs = options_.initial_period; options_.dataset.lastobs = NaN; diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc index aae564fe8..169333e65 100644 --- a/preprocessor/ComputingTasks.cc +++ b/preprocessor/ComputingTasks.cc @@ -1700,3 +1700,171 @@ CorrPriorStatement::writeOutput(ostream &output, const string &basename) const writeOutputHelper(output, "interval", lhs_field); BasicPriorStatement::writeVarianceOption(output, lhs_field); } + +BasicOptionsStatement::~BasicOptionsStatement() +{ +} + +BasicOptionsStatement::BasicOptionsStatement(const string &name_arg, + const OptionsList &options_list_arg) : + name(name_arg), + options_list(options_list_arg), + first_statement_encountered(false) +{ +} + +void +BasicOptionsStatement::checkPass(ModFileStructure &mod_file_struct) +{ + if (options_list.num_options.find("date1") != options_list.num_options.end() || + options_list.num_options.find("date2") != options_list.num_options.end()) + if (options_list.num_options.find("date1") == options_list.num_options.end() || + options_list.num_options.find("date2") == options_list.num_options.end()) + { + cerr << "ERROR: OptionsStatement::checkPass(1). Should not arrive here. " + << "Please inform Dynare Team." << endl; + exit(EXIT_FAILURE); + } +} + +void +BasicOptionsStatement::writeOptionsIndex(ostream &output, const string &lhs_field) const +{ + if (first_statement_encountered) + output << "options_indx = 1;" << endl; + else + output << "options_indx = size(estimation_info" << lhs_field << "_index, 2) + 1;" << endl; +} + +void +BasicOptionsStatement::get_base_name(const SymbolType symb_type, string &lhs_field) const +{ + if (symb_type == eExogenous || symb_type == eExogenousDet) + lhs_field = "structural_innovation"; + else + lhs_field = "measurement_error"; +} + +void +BasicOptionsStatement::writeOutputHelper(ostream &output, const string &field, const string &lhs_field) const +{ + OptionsList::num_options_t::const_iterator itn = options_list.num_options.find(field); + if (itn != options_list.num_options.end()) + output << "estimation_info" << lhs_field << "(options_indx)." << field + << " = " << itn->second << ";" << endl; + + OptionsList::date_options_t::const_iterator itd = options_list.date_options.find(field); + if (itd != options_list.date_options.end()) + output << "estimation_info" << lhs_field << "(options_indx)." << field + << " = '" << itd->second << "';" << endl; +} + +OptionsStatement::OptionsStatement(const string &name_arg, + const OptionsList &options_list_arg) : + BasicOptionsStatement(name_arg, options_list_arg) +{ +} + +void +OptionsStatement::checkPass(ModFileStructure &mod_file_struct) +{ + BasicOptionsStatement::checkPass(mod_file_struct); + if (!mod_file_struct.options_statement_present) + first_statement_encountered = true; + mod_file_struct.options_statement_present = true; +} + +void +OptionsStatement::writeOutput(ostream &output, const string &basename) const +{ + string lhs_field = ".options"; + + BasicOptionsStatement::writeOptionsIndex(output, lhs_field); + output << "estimation_info" << lhs_field <<"_index(options_indx) = {'" << name << "'};" << endl + << "estimation_info" << lhs_field << "(options_indx).name = '" << name << "';" << endl; + + writeOutputHelper(output, "init", lhs_field); + writeOutputHelper(output, "bounds", lhs_field); + writeOutputHelper(output, "jscale", lhs_field); + writeOutputHelper(output, "date1", lhs_field); + writeOutputHelper(output, "date2", lhs_field); +} + +StdOptionsStatement::StdOptionsStatement(const string &name_arg, + const OptionsList &options_list_arg, + const SymbolTable &symbol_table_arg ) : + BasicOptionsStatement(name_arg, options_list_arg), + symbol_table(symbol_table_arg) +{ +} + +void +StdOptionsStatement::checkPass(ModFileStructure &mod_file_struct) +{ + BasicOptionsStatement::checkPass(mod_file_struct); + if (!mod_file_struct.std_options_statement_present) + first_statement_encountered = true; + mod_file_struct.std_options_statement_present = true; +} + +void +StdOptionsStatement::writeOutput(ostream &output, const string &basename) const +{ + string lhs_field; + get_base_name(symbol_table.getType(name), lhs_field); + lhs_field = "." + lhs_field + ".options"; + + BasicOptionsStatement::writeOptionsIndex(output, lhs_field); + output << "estimation_info" << lhs_field << "_index(options_indx) = {'" << name << "'};" << endl; + output << "estimation_info" << lhs_field << "(options_indx).name = '" << name << "';" << endl; + + writeOutputHelper(output, "init", lhs_field); + writeOutputHelper(output, "bounds", lhs_field); + writeOutputHelper(output, "jscale", lhs_field); + writeOutputHelper(output, "date1", lhs_field); + writeOutputHelper(output, "date2", lhs_field); +} + +CorrOptionsStatement::CorrOptionsStatement(const string &name_arg1, const string &name_arg2, + const OptionsList &options_list_arg, + const SymbolTable &symbol_table_arg ) : + BasicOptionsStatement(name_arg1, options_list_arg), + name1(name_arg2), + symbol_table(symbol_table_arg) +{ +} + +void +CorrOptionsStatement::checkPass(ModFileStructure &mod_file_struct) +{ + if (symbol_table.getType(name) != symbol_table.getType(name1)) + { + cerr << "ERROR: In the corr(A,B).options statement, A and B must be of the same type. " + << "In your case, " << name << " and " << name1 << " are of different " + << "types." << endl; + exit(EXIT_FAILURE); + } + if (!mod_file_struct.corr_options_statement_present) + first_statement_encountered = true; + mod_file_struct.corr_prior_statement_present = true; +} + +void +CorrOptionsStatement::writeOutput(ostream &output, const string &basename) const +{ + string lhs_field; + get_base_name(symbol_table.getType(name), lhs_field); + lhs_field = "." + lhs_field + "_corr.options"; + + BasicOptionsStatement::writeOptionsIndex(output, lhs_field); + output << "estimation_info" << lhs_field << "_index(options_indx) = {'" << name << "_" << name1 << "'};" << endl; + lhs_field += "."; + output << "estimation_info" << lhs_field << "(options_indx).name1 = '" << name << "';" << endl; + output << "estimation_info" << lhs_field << "(options_indx).name2 = '" << name1 << "';" << endl; + + writeOutputHelper(output, "init", lhs_field); + writeOutputHelper(output, "bounds", lhs_field); + writeOutputHelper(output, "jscale", lhs_field); + writeOutputHelper(output, "date1", lhs_field); + writeOutputHelper(output, "date2", lhs_field); +} diff --git a/preprocessor/ComputingTasks.hh b/preprocessor/ComputingTasks.hh index c69043fb6..397139aeb 100644 --- a/preprocessor/ComputingTasks.hh +++ b/preprocessor/ComputingTasks.hh @@ -632,4 +632,51 @@ public: virtual void writeOutput(ostream &output, const string &basename) const; }; +class BasicOptionsStatement : public Statement +{ +public: + virtual ~BasicOptionsStatement(); +protected: + const string name; + const OptionsList options_list; + bool first_statement_encountered; + BasicOptionsStatement(const string &name_arg, + const OptionsList &options_list_arg); + void get_base_name(const SymbolType symb_type, string &lhs_field) const; + virtual void checkPass(ModFileStructure &mod_file_struct); + void writeOptionsIndex(ostream &output, const string &lhs_field) const; + void writeOutputHelper(ostream &output, const string &field, const string &lhs_field) const; +}; + +class OptionsStatement : public BasicOptionsStatement +{ +public: + OptionsStatement(const string &name_arg, const OptionsList &options_list_arg); + virtual void checkPass(ModFileStructure &mod_file_struct); + virtual void writeOutput(ostream &output, const string &basename) const; +}; + +class StdOptionsStatement : public BasicOptionsStatement +{ +private: + const SymbolTable symbol_table; +public: + StdOptionsStatement(const string &name_arg, const OptionsList &options_list_arg, + const SymbolTable &symbol_table_arg); + virtual void checkPass(ModFileStructure &mod_file_struct); + virtual void writeOutput(ostream &output, const string &basename) const; +}; + +class CorrOptionsStatement : public BasicOptionsStatement +{ +private: + const string name1; + const SymbolTable symbol_table; +public: + CorrOptionsStatement(const string &name_arg1, const string &name_arg2, + const OptionsList &options_list_arg, const SymbolTable &symbol_table_arg); + virtual void checkPass(ModFileStructure &mod_file_struct); + virtual void writeOutput(ostream &output, const string &basename) const; +}; + #endif diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy index d23d5137d..53eba4661 100644 --- a/preprocessor/DynareBison.yy +++ b/preprocessor/DynareBison.yy @@ -101,11 +101,11 @@ class ParsingDriver; %token FORECAST K_ORDER_SOLVER INSTRUMENTS PRIOR SHIFT MEAN STDEV VARIANCE MODE INTERVAL SHAPE DOMAINN %token GAMMA_PDF GRAPH CONDITIONAL_VARIANCE_DECOMPOSITION NOCHECK STD %token HISTVAL HOMOTOPY_SETUP HOMOTOPY_MODE HOMOTOPY_STEPS HP_FILTER HP_NGRID -%token IDENTIFICATION INF_CONSTANT INITVAL INITVAL_FILE +%token IDENTIFICATION INF_CONSTANT INITVAL INITVAL_FILE BOUNDS JSCALE INIT %token INT_NUMBER %token DATE_NUMBER %token INV_GAMMA_PDF INV_GAMMA1_PDF INV_GAMMA2_PDF IRF IRF_SHOCKS -%token KALMAN_ALGO KALMAN_TOL SUBSAMPLES +%token KALMAN_ALGO KALMAN_TOL SUBSAMPLES OPTIONS %token LABELS LAPLACE LIK_ALGO LIK_INIT LINEAR LOAD_IDENT_FILES LOAD_MH_FILE LOAD_PARAMS_AND_STEADY_STATE LOGLINEAR %token MARKOWITZ MARGINAL_DENSITY MAX MAXIT %token MFS MH_DROP MH_INIT_SCALE MH_JSCALE MH_MODE MH_NBLOCKS MH_REPLIC MH_RECOVER MIN MINIMAL_SOLVING_PERIODS @@ -176,8 +176,8 @@ class ParsingDriver; %type equation hand_side %type non_negative_number signed_number signed_integer date_number %type filename symbol prior_distribution -%type vec_value_1 vec_value -%type range prior_pdf_string +%type vec_value_1 vec_value signed_inf signed_number_w_inf +%type range prior_pdf_string vec_value_w_inf vec_value_1_w_inf %type change_type_arg %type change_type_var_list %type vec_int_elem vec_int_1 vec_int vec_int_number @@ -219,6 +219,7 @@ statement : parameters | prior | subsamples | subsamples_eq + | options | varobs | observation_trends | unit_root_vars @@ -980,6 +981,18 @@ signed_number : PLUS non_negative_number | non_negative_number ; +signed_inf : PLUS INF_CONSTANT + { $$ = new string ("Inf"); } + | MINUS INF_CONSTANT + { $$ = new string ("-Inf"); } + | INF_CONSTANT + { $$ = new string ("Inf"); } + ; + +signed_number_w_inf : signed_inf + | signed_number + ; + estimated_params : ESTIMATED_PARAMS ';' estimated_list END ';' { driver.estimated_params(); }; estimated_list : estimated_list estimated_elem @@ -1238,6 +1251,28 @@ prior_options : o_shift | o_domain ; +options : symbol '.' OPTIONS '(' options_options_list ')' ';' + { driver.set_options($1); } + | symbol '.' symbol '.' OPTIONS '(' options_options_list ')' ';' + { + driver.add_subsample_range(new string (*$1), $3); + driver.set_options($1); + } + | STD '(' symbol ')' '.' OPTIONS '(' options_options_list ')' ';' + { driver.set_std_options($3); } + | CORR '(' symbol COMMA symbol')' '.' OPTIONS '(' options_options_list ')' ';' + { driver.set_corr_options($3, $5); } + ; + +options_options_list : options_options_list COMMA options_options + | options_options + ; + +options_options : o_jscale + | o_init + | o_bounds + ; + estimation : ESTIMATION ';' { driver.run_estimation(); } | ESTIMATION '(' estimation_options_list ')' ';' @@ -1961,6 +1996,9 @@ o_shape : SHAPE EQUAL prior_distribution { driver.option_num("shape", $3); }; o_mode : MODE EQUAL signed_number { driver.option_num("mode", $3); }; o_mean : MEAN EQUAL signed_number { driver.option_num("mean", $3); }; o_stdev : STDEV EQUAL non_negative_number { driver.option_num("stdev", $3); }; +o_jscale : JSCALE EQUAL non_negative_number { driver.option_num("jscale", $3); }; +o_init : INIT EQUAL signed_number { driver.option_num("init", $3); }; +o_bounds : BOUNDS EQUAL vec_value_w_inf { driver.option_num("bounds", $3); }; o_domain : DOMAINN EQUAL vec_value { driver.option_num("domain", $3); }; o_interval : INTERVAL EQUAL vec_value { driver.option_num("interval", $3); }; o_variance : VARIANCE EQUAL expression { driver.add_expression_to_prior_statement($3); } @@ -2339,6 +2377,19 @@ vec_value_1 : '[' signed_number vec_value : vec_value_1 ']' { $1->append("]"); $$ = $1; }; +vec_value_1_w_inf : '[' signed_number_w_inf + { $2->insert(0, "["); $$ = $2;} + | vec_value_1_w_inf signed_number_w_inf + { + $1->append(" "); + $1->append(*$2); + delete $2; + $$ = $1; + } + ; + +vec_value_w_inf : vec_value_1_w_inf ']' { $1->append("]"); $$ = $1; }; + symbol : NAME | ALPHA | BETA diff --git a/preprocessor/DynareFlex.ll b/preprocessor/DynareFlex.ll index d0ff91fd7..3684ea8d8 100644 --- a/preprocessor/DynareFlex.ll +++ b/preprocessor/DynareFlex.ll @@ -190,6 +190,7 @@ string eofbuff; end {BEGIN INITIAL; return token::END;} subsamples {return token::SUBSAMPLES;} +options {return token::OPTIONS;} prior {return token::PRIOR;} std {BEGIN DYNARE_STATEMENT; return token::STD;} corr {BEGIN DYNARE_STATEMENT; return token::CORR;} @@ -208,6 +209,9 @@ string eofbuff; interval {return token::INTERVAL;} shape {return token::SHAPE;} shift {return token::SHIFT;} +bounds {return token::BOUNDS;} +init {return token::INIT;} +jscale {return token::JSCALE;} prefilter {return token::PREFILTER;} presample {return token::PRESAMPLE;} lik_algo {return token::LIK_ALGO;} diff --git a/preprocessor/ParsingDriver.cc b/preprocessor/ParsingDriver.cc index 30ab3369d..4bc662010 100644 --- a/preprocessor/ParsingDriver.cc +++ b/preprocessor/ParsingDriver.cc @@ -1315,6 +1315,15 @@ ParsingDriver::add_expression_to_prior_statement(expr_t variance) prior_variance = variance; } +void +ParsingDriver::set_options(string *name) +{ + check_symbol_is_parameter(name); + mod_file->addStatement(new OptionsStatement(*name, options_list)); + options_list.clear(); + delete name; +} + void ParsingDriver::check_symbol_is_endogenous_or_exogenous(string *name) { @@ -1341,6 +1350,15 @@ ParsingDriver::set_std_prior(string *name) delete name; } +void +ParsingDriver::set_std_options(string *name) +{ + check_symbol_is_endogenous_or_exogenous(name); + // mod_file->addStatement(new StdOptionsStatement(*name, options_list, mod_file->symbol_table)); + options_list.clear(); + delete name; +} + void ParsingDriver::set_corr_prior(string *name1, string *name2) { @@ -1353,6 +1371,17 @@ ParsingDriver::set_corr_prior(string *name1, string *name2) delete name2; } +void +ParsingDriver::set_corr_options(string *name1, string *name2) +{ + check_symbol_is_endogenous_or_exogenous(name1); + check_symbol_is_endogenous_or_exogenous(name2); + // mod_file->addStatement(new CorrOptionsStatement(*name1, *name2, options_list, mod_file->symbol_table)); + options_list.clear(); + delete name1; + delete name2; +} + void ParsingDriver::run_estimation() { diff --git a/preprocessor/ParsingDriver.hh b/preprocessor/ParsingDriver.hh index 74ba84409..a19ad5ed2 100644 --- a/preprocessor/ParsingDriver.hh +++ b/preprocessor/ParsingDriver.hh @@ -390,10 +390,16 @@ public: void set_prior(string *arg); //! Adds the variance option to its temporary holding place void add_expression_to_prior_statement(expr_t variance); + //! Sets the options for a parameter + void set_options(string *arg); //! Sets the prior for estimated std dev void set_std_prior(string *arg); + //! Sets the options for estimated std dev + void set_std_options(string *arg); //! Sets the prior for estimated correlation void set_corr_prior(string *arg1, string *arg2); + //! Sets the options for estimated correlation + void set_corr_options(string *arg1, string *arg2); //! Runs estimation process void run_estimation(); //! Runs dynare_sensitivy() diff --git a/preprocessor/Statement.hh b/preprocessor/Statement.hh index a6091437a..9aeecf95e 100644 --- a/preprocessor/Statement.hh +++ b/preprocessor/Statement.hh @@ -94,6 +94,12 @@ public: bool std_prior_statement_present; //! Whether there is a corr prior statement present bool corr_prior_statement_present; + //! Whether there is a options statement present + bool options_statement_present; + //! Whether there is a std options statement present + bool std_options_statement_present; + //! Whether there is a corr options statement present + bool corr_options_statement_present; }; class Statement From 16bf5c76423041204460e0d5a719d4287680f181 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 19 Dec 2011 17:13:13 +0100 Subject: [PATCH 08/14] preprocessor: removed unused token --- preprocessor/DynareBison.yy | 2 +- preprocessor/DynareFlex.ll | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy index 53eba4661..00623ef13 100644 --- a/preprocessor/DynareBison.yy +++ b/preprocessor/DynareBison.yy @@ -145,7 +145,7 @@ class ParsingDriver; /* end of GSA analysis*/ %token FREQ INITIAL_YEAR INITIAL_SUBPERIOD FINAL_YEAR FINAL_SUBPERIOD DATA VLIST LOG_VAR PERCENT_VAR %token VLISTLOG VLISTPER -%token RESTRICTION RESTRICTIONS RESTRICTION_FNAME CROSS_RESTRICTIONS NLAGS CONTEMP_REDUCED_FORM REAL_PSEUDO_FORECAST +%token RESTRICTION RESTRICTION_FNAME CROSS_RESTRICTIONS NLAGS CONTEMP_REDUCED_FORM REAL_PSEUDO_FORECAST %token NONE DUMMY_OBS NSTATES INDXSCALESSTATES NO_BAYESIAN_PRIOR SPECIFICATION SIMS_ZHA %token ALPHA BETA ABAND NINV CMS NCMS CNUM GAMMA INV_GAMMA INV_GAMMA1 INV_GAMMA2 NORMAL UNIFORM %token GSIG2_LMDM Q_DIAG FLAT_PRIOR NCSK NSTD diff --git a/preprocessor/DynareFlex.ll b/preprocessor/DynareFlex.ll index 3684ea8d8..626aad86b 100644 --- a/preprocessor/DynareFlex.ll +++ b/preprocessor/DynareFlex.ll @@ -471,7 +471,6 @@ string eofbuff; # {return Dynare::parser::token_type (yytext[0]);} autocorr {return token::AUTOCORR;} -restrictions {return token::RESTRICTIONS;} restriction {return token::RESTRICTION;} /* Inside Dynare statement */ From 16e11d49df1a730513f5770038a5e51830f6379e Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 16 Dec 2011 11:18:24 +0100 Subject: [PATCH 09/14] ms-sbvar: replaced number_of_states with number_of_regimes --- preprocessor/ComputingTasks.cc | 10 +++++----- preprocessor/DynareBison.yy | 6 +++--- preprocessor/DynareFlex.ll | 2 +- preprocessor/ParsingDriver.cc | 8 ++++---- tests/ms-sbvar/test_ms_variances.mod | 2 +- tests/ms-sbvar/test_ms_variances_repeated_runs.mod | 2 +- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc index 169333e65..b77520e6b 100644 --- a/preprocessor/ComputingTasks.cc +++ b/preprocessor/ComputingTasks.cc @@ -1359,7 +1359,7 @@ MarkovSwitchingStatement::MarkovSwitchingStatement(const OptionsList &options_li void MarkovSwitchingStatement::writeOutput(ostream &output, const string &basename) const { - OptionsList::num_options_t::const_iterator itChain, itState, itNOS, itDuration; + OptionsList::num_options_t::const_iterator itChain, itState, itNOR, itDuration; itChain = options_list.num_options.find("ms.chain"); if (itChain == options_list.num_options.end()) @@ -1376,13 +1376,13 @@ MarkovSwitchingStatement::writeOutput(ostream &output, const string &basename) c } itState = options_list.num_options.find("ms.state"); - itNOS = options_list.num_options.find("ms.number_of_states"); + itNOR = options_list.num_options.find("ms.number_of_regimes"); if (itState != options_list.num_options.end() - && itNOS == options_list.num_options.end()) + && itNOR == options_list.num_options.end()) output << "options_.ms.ms_chain(" << itChain->second << ").state(" << itState->second << ").duration = " << itDuration->second << ";" << endl; else if (itState == options_list.num_options.end() - && itNOS != options_list.num_options.end()) - for (int i = 0; i < atoi(itNOS->second.c_str()); i++) + && itNOR != options_list.num_options.end()) + for (int i = 0; i < atoi(itNOR->second.c_str()); i++) output << "options_.ms.ms_chain(" << itChain->second << ").state(" << i+1 << ").duration = " << itDuration->second << ";" << endl; else { diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy index 00623ef13..ef3a2b988 100644 --- a/preprocessor/DynareBison.yy +++ b/preprocessor/DynareBison.yy @@ -156,7 +156,7 @@ class ParsingDriver; %token SBVAR TREND_VAR DEFLATOR GROWTH_FACTOR MS_IRF MS_VARIANCE_DECOMPOSITION %token MS_ESTIMATION MS_SIMULATION MS_COMPUTE_MDD MS_COMPUTE_PROBABILITIES MS_FORECAST %token SVAR_IDENTIFICATION EQUATION EXCLUSION LAG UPPER_CHOLESKY LOWER_CHOLESKY MONTHLY QUARTERLY -%token MARKOV_SWITCHING CHAIN STATE DURATION NUMBER_OF_STATES +%token MARKOV_SWITCHING CHAIN STATE DURATION NUMBER_OF_REGIMES %token SVAR COEFF COEFFICIENTS VARIANCES CONSTANTS EQUATIONS %token EXTERNAL_FUNCTION EXT_FUNC_NAME EXT_FUNC_NARGS FIRST_DERIV_PROVIDED SECOND_DERIV_PROVIDED %token SELECTED_VARIABLES_ONLY COVA_COMPUTE SIMULATION_FILE_TAG FILE_TAG @@ -742,7 +742,7 @@ ms_options_list : ms_options_list COMMA ms_options ms_options : o_chain | o_state | o_duration - | o_number_of_states + | o_number_of_regimes ; svar : SVAR '(' svar_options_list ')' ';' @@ -2226,7 +2226,7 @@ o_duration : DURATION EQUAL non_negative_number | DURATION EQUAL INF_CONSTANT { driver.option_num("ms.duration","Inf"); } ; -o_number_of_states : NUMBER_OF_STATES EQUAL INT_NUMBER { driver.option_num("ms.number_of_states",$3); }; +o_number_of_regimes : NUMBER_OF_REGIMES EQUAL INT_NUMBER { driver.option_num("ms.number_of_regimes",$3); }; o_coefficients : COEFFICIENTS { driver.option_str("ms.coefficients","svar_coefficients"); }; o_variances : VARIANCES { driver.option_str("ms.variances","svar_variances"); }; o_equations : EQUATIONS EQUAL vec_int diff --git a/preprocessor/DynareFlex.ll b/preprocessor/DynareFlex.ll index 626aad86b..e2b2a1708 100644 --- a/preprocessor/DynareFlex.ll +++ b/preprocessor/DynareFlex.ll @@ -505,7 +505,7 @@ string eofbuff; lower_cholesky {return token::LOWER_CHOLESKY;} chain {return token::CHAIN;} state {return token::STATE;} -number_of_states {return token::NUMBER_OF_STATES;} +number_of_regimes {return token::NUMBER_OF_REGIMES;} duration {return token::DURATION;} coefficients {return token::COEFFICIENTS;} variances {return token::VARIANCES;} diff --git a/preprocessor/ParsingDriver.cc b/preprocessor/ParsingDriver.cc index 4bc662010..6b0e65d22 100644 --- a/preprocessor/ParsingDriver.cc +++ b/preprocessor/ParsingDriver.cc @@ -1747,14 +1747,14 @@ ParsingDriver::markov_switching() error("The value passed to the chain option must be greater than zero."); it0 = options_list.num_options.find("ms.state"); - it1 = options_list.num_options.find("ms.number_of_states"); + it1 = options_list.num_options.find("ms.number_of_regimes"); if ((it0 == options_list.num_options.end()) && (it1 == options_list.num_options.end())) - error("Either a state option or a number_of_states option must be passed to the markov_switching statement."); + error("Either a state option or a number_of_regimes option must be passed to the markov_switching statement."); if ((it0 != options_list.num_options.end()) && (it1 != options_list.num_options.end())) - error("You cannot pass both a state option and a number_of_states option to the markov_switching statement."); + error("You cannot pass both a state option and a number_of_regimes option to the markov_switching statement."); if (it0 != options_list.num_options.end()) if (atoi(it0->second.c_str()) <= 0) @@ -1762,7 +1762,7 @@ ParsingDriver::markov_switching() if (it1 != options_list.num_options.end()) if (atoi(it1->second.c_str()) <= 0) - error("The value passed to the number_of_states option must be greater than zero."); + error("The value passed to the number_of_regimes option must be greater than zero."); string infStr("Inf"); it0 = options_list.num_options.find("ms.duration"); diff --git a/tests/ms-sbvar/test_ms_variances.mod b/tests/ms-sbvar/test_ms_variances.mod index acf3e827c..6f6d2477b 100644 --- a/tests/ms-sbvar/test_ms_variances.mod +++ b/tests/ms-sbvar/test_ms_variances.mod @@ -6,7 +6,7 @@ svar_identification; lower_cholesky; end; -markov_switching(chain=1,number_of_states=2,duration=2.5); +markov_switching(chain=1,number_of_regimes=2,duration=2.5); svar(variances, chain=1); diff --git a/tests/ms-sbvar/test_ms_variances_repeated_runs.mod b/tests/ms-sbvar/test_ms_variances_repeated_runs.mod index 35f64ab84..a4ffc6fb0 100644 --- a/tests/ms-sbvar/test_ms_variances_repeated_runs.mod +++ b/tests/ms-sbvar/test_ms_variances_repeated_runs.mod @@ -6,7 +6,7 @@ svar_identification; lower_cholesky; end; -markov_switching(chain=1,number_of_states=2,duration=2.5); +markov_switching(chain=1,number_of_regimes=2,duration=2.5); svar(variances, chain=1); From 3ff704d36d1b1edefc8b82b7f21fdfaa9fa6a303 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Fri, 16 Dec 2011 17:31:10 +0100 Subject: [PATCH 10/14] ms-sbvar: replace state with regime --- matlab/ms-sbvar/ms_write_markov_file.m | 4 ++-- matlab/ms-sbvar/plot_ms_probabilities.m | 2 +- preprocessor/ComputingTasks.cc | 12 ++++++------ preprocessor/DynareBison.yy | 6 +++--- preprocessor/DynareFlex.ll | 2 +- preprocessor/ParsingDriver.cc | 8 ++++---- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/matlab/ms-sbvar/ms_write_markov_file.m b/matlab/ms-sbvar/ms_write_markov_file.m index 96ccf0868..8d56a2f9f 100644 --- a/matlab/ms-sbvar/ms_write_markov_file.m +++ b/matlab/ms-sbvar/ms_write_markov_file.m @@ -52,7 +52,7 @@ function ms_write_markov_file(fname, options) %//=====================================================// fprintf(fh,'//== Number of states for state_variable[%d] ==//\n', ... i_chain); - n_states = length(options.ms.ms_chain(i_chain).state); + n_states = length(options.ms.ms_chain(i_chain).regime); fprintf(fh,'%d\n\n',n_states); %//== 03/15/06: DW TVBVAR code reads the data below and overwrite the prior data read somewhere else if any. @@ -64,7 +64,7 @@ function ms_write_markov_file(fname, options) i_chain); Alpha = ones(n_states,n_states); for i_state = 1:n_states - p = 1-1/options.ms.ms_chain(i_chain).state(i_state).duration; + p = 1-1/options.ms.ms_chain(i_chain).regime(i_state).duration; Alpha(i_state,i_state) = p*(n_states-1)/(1-p); fprintf(fh,'%22.16f',Alpha(i_state,:)); fprintf(fh,'\n'); diff --git a/matlab/ms-sbvar/plot_ms_probabilities.m b/matlab/ms-sbvar/plot_ms_probabilities.m index 79c7876b6..023ecfd44 100644 --- a/matlab/ms-sbvar/plot_ms_probabilities.m +++ b/matlab/ms-sbvar/plot_ms_probabilities.m @@ -31,7 +31,7 @@ function plot_ms_probabilities(computed_probabilities, options_) [T,num_grand_regimes] = size(computed_probabilities); num_chains = length(options_.ms.ms_chain); for i=1:num_chains - chains(i).num_regimes = length(options_.ms.ms_chain(i).state); + chains(i).num_regimes = length(options_.ms.ms_chain(i).regime); chains(i).probabilities = zeros([T,chains(i).num_regimes]); end diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc index b77520e6b..36049ab4e 100644 --- a/preprocessor/ComputingTasks.cc +++ b/preprocessor/ComputingTasks.cc @@ -1359,7 +1359,7 @@ MarkovSwitchingStatement::MarkovSwitchingStatement(const OptionsList &options_li void MarkovSwitchingStatement::writeOutput(ostream &output, const string &basename) const { - OptionsList::num_options_t::const_iterator itChain, itState, itNOR, itDuration; + OptionsList::num_options_t::const_iterator itChain, itRegime, itNOR, itDuration; itChain = options_list.num_options.find("ms.chain"); if (itChain == options_list.num_options.end()) @@ -1375,15 +1375,15 @@ MarkovSwitchingStatement::writeOutput(ostream &output, const string &basename) c exit(EXIT_FAILURE); } - itState = options_list.num_options.find("ms.state"); + itRegime = options_list.num_options.find("ms.regime"); itNOR = options_list.num_options.find("ms.number_of_regimes"); - if (itState != options_list.num_options.end() + if (itRegime != options_list.num_options.end() && itNOR == options_list.num_options.end()) - output << "options_.ms.ms_chain(" << itChain->second << ").state(" << itState->second << ").duration = " << itDuration->second << ";" << endl; - else if (itState == options_list.num_options.end() + output << "options_.ms.ms_chain(" << itChain->second << ").regime(" << itRegime->second << ").duration = " << itDuration->second << ";" << endl; + else if (itRegime == options_list.num_options.end() && itNOR != options_list.num_options.end()) for (int i = 0; i < atoi(itNOR->second.c_str()); i++) - output << "options_.ms.ms_chain(" << itChain->second << ").state(" << i+1 << ").duration = " << itDuration->second << ";" << endl; + output << "options_.ms.ms_chain(" << itChain->second << ").regime(" << i+1 << ").duration = " << itDuration->second << ";" << endl; else { cerr << "MarkovSwitchingStatement::writeOutput() Should not arrive here (3). Please report this to the Dynare Team." << endl; diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy index ef3a2b988..1956931f3 100644 --- a/preprocessor/DynareBison.yy +++ b/preprocessor/DynareBison.yy @@ -156,7 +156,7 @@ class ParsingDriver; %token SBVAR TREND_VAR DEFLATOR GROWTH_FACTOR MS_IRF MS_VARIANCE_DECOMPOSITION %token MS_ESTIMATION MS_SIMULATION MS_COMPUTE_MDD MS_COMPUTE_PROBABILITIES MS_FORECAST %token SVAR_IDENTIFICATION EQUATION EXCLUSION LAG UPPER_CHOLESKY LOWER_CHOLESKY MONTHLY QUARTERLY -%token MARKOV_SWITCHING CHAIN STATE DURATION NUMBER_OF_REGIMES +%token MARKOV_SWITCHING CHAIN REGIME DURATION NUMBER_OF_REGIMES %token SVAR COEFF COEFFICIENTS VARIANCES CONSTANTS EQUATIONS %token EXTERNAL_FUNCTION EXT_FUNC_NAME EXT_FUNC_NARGS FIRST_DERIV_PROVIDED SECOND_DERIV_PROVIDED %token SELECTED_VARIABLES_ONLY COVA_COMPUTE SIMULATION_FILE_TAG FILE_TAG @@ -740,7 +740,7 @@ ms_options_list : ms_options_list COMMA ms_options ; ms_options : o_chain - | o_state + | o_regime | o_duration | o_number_of_regimes ; @@ -2220,7 +2220,7 @@ o_cnum : CNUM EQUAL INT_NUMBER {driver.option_num("ms.cnum",$3); }; o_k_order_solver : K_ORDER_SOLVER {driver.option_num("k_order_solver","1"); }; o_pruning : PRUNING { driver.option_num("pruning", "1"); }; o_chain : CHAIN EQUAL INT_NUMBER { driver.option_num("ms.chain",$3); }; -o_state : STATE EQUAL INT_NUMBER { driver.option_num("ms.state",$3); }; +o_regime : REGIME EQUAL INT_NUMBER { driver.option_num("ms.regime",$3); }; o_duration : DURATION EQUAL non_negative_number { driver.option_num("ms.duration",$3); } | DURATION EQUAL INF_CONSTANT diff --git a/preprocessor/DynareFlex.ll b/preprocessor/DynareFlex.ll index e2b2a1708..293f9adc8 100644 --- a/preprocessor/DynareFlex.ll +++ b/preprocessor/DynareFlex.ll @@ -504,7 +504,7 @@ string eofbuff; upper_cholesky {return token::UPPER_CHOLESKY;} lower_cholesky {return token::LOWER_CHOLESKY;} chain {return token::CHAIN;} -state {return token::STATE;} +regime {return token::REGIME;} number_of_regimes {return token::NUMBER_OF_REGIMES;} duration {return token::DURATION;} coefficients {return token::COEFFICIENTS;} diff --git a/preprocessor/ParsingDriver.cc b/preprocessor/ParsingDriver.cc index 6b0e65d22..a2d73eb7b 100644 --- a/preprocessor/ParsingDriver.cc +++ b/preprocessor/ParsingDriver.cc @@ -1746,19 +1746,19 @@ ParsingDriver::markov_switching() else if (atoi(it0->second.c_str()) <= 0) error("The value passed to the chain option must be greater than zero."); - it0 = options_list.num_options.find("ms.state"); + it0 = options_list.num_options.find("ms.regime"); it1 = options_list.num_options.find("ms.number_of_regimes"); if ((it0 == options_list.num_options.end()) && (it1 == options_list.num_options.end())) - error("Either a state option or a number_of_regimes option must be passed to the markov_switching statement."); + error("Either a regime option or a number_of_regimes option must be passed to the markov_switching statement."); if ((it0 != options_list.num_options.end()) && (it1 != options_list.num_options.end())) - error("You cannot pass both a state option and a number_of_regimes option to the markov_switching statement."); + error("You cannot pass both a regime option and a number_of_regimes option to the markov_switching statement."); if (it0 != options_list.num_options.end()) if (atoi(it0->second.c_str()) <= 0) - error("The value passed to the state option must be greater than zero."); + error("The value passed to the regime option must be greater than zero."); if (it1 != options_list.num_options.end()) if (atoi(it1->second.c_str()) <= 0) From 0554e47dc0f647cda6e7809505d3ca041077a1cf Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Mon, 19 Dec 2011 15:57:28 +0100 Subject: [PATCH 11/14] ms-sbvar: remove regime argument and support duration=array --- preprocessor/ComputingTasks.cc | 22 ++++++++++++++-------- preprocessor/DynareBison.yy | 8 +++----- preprocessor/DynareFlex.ll | 1 - preprocessor/ParsingDriver.cc | 28 ++++++---------------------- 4 files changed, 23 insertions(+), 36 deletions(-) diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc index 36049ab4e..dcd000d46 100644 --- a/preprocessor/ComputingTasks.cc +++ b/preprocessor/ComputingTasks.cc @@ -1359,7 +1359,9 @@ MarkovSwitchingStatement::MarkovSwitchingStatement(const OptionsList &options_li void MarkovSwitchingStatement::writeOutput(ostream &output, const string &basename) const { - OptionsList::num_options_t::const_iterator itChain, itRegime, itNOR, itDuration; + bool isDurationAVec = true; + string infStr("Inf"); + OptionsList::num_options_t::const_iterator itChain, itNOR, itDuration; itChain = options_list.num_options.find("ms.chain"); if (itChain == options_list.num_options.end()) @@ -1374,16 +1376,20 @@ MarkovSwitchingStatement::writeOutput(ostream &output, const string &basename) c cerr << "MarkovSwitchingStatement::writeOutput() Should not arrive here (2). Please report this to the Dynare Team." << endl; exit(EXIT_FAILURE); } + else if (atof(itDuration->second.c_str()) || infStr.compare(itDuration->second) == 0) + isDurationAVec = false; + output << "options_.ms.duration = " << itDuration->second << ";" << endl; - itRegime = options_list.num_options.find("ms.regime"); itNOR = options_list.num_options.find("ms.number_of_regimes"); - if (itRegime != options_list.num_options.end() - && itNOR == options_list.num_options.end()) - output << "options_.ms.ms_chain(" << itChain->second << ").regime(" << itRegime->second << ").duration = " << itDuration->second << ";" << endl; - else if (itRegime == options_list.num_options.end() - && itNOR != options_list.num_options.end()) + if (itNOR != options_list.num_options.end()) for (int i = 0; i < atoi(itNOR->second.c_str()); i++) - output << "options_.ms.ms_chain(" << itChain->second << ").regime(" << i+1 << ").duration = " << itDuration->second << ";" << endl; + { + output << "options_.ms.ms_chain(" << itChain->second << ").regime(" + << i+1 << ").duration = options_.ms.duration"; + if (isDurationAVec) + output << "(" << i+1 << ")"; + output << ";" << endl; + } else { cerr << "MarkovSwitchingStatement::writeOutput() Should not arrive here (3). Please report this to the Dynare Team." << endl; diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy index 1956931f3..a6bb978c0 100644 --- a/preprocessor/DynareBison.yy +++ b/preprocessor/DynareBison.yy @@ -156,7 +156,7 @@ class ParsingDriver; %token SBVAR TREND_VAR DEFLATOR GROWTH_FACTOR MS_IRF MS_VARIANCE_DECOMPOSITION %token MS_ESTIMATION MS_SIMULATION MS_COMPUTE_MDD MS_COMPUTE_PROBABILITIES MS_FORECAST %token SVAR_IDENTIFICATION EQUATION EXCLUSION LAG UPPER_CHOLESKY LOWER_CHOLESKY MONTHLY QUARTERLY -%token MARKOV_SWITCHING CHAIN REGIME DURATION NUMBER_OF_REGIMES +%token MARKOV_SWITCHING CHAIN DURATION NUMBER_OF_REGIMES %token SVAR COEFF COEFFICIENTS VARIANCES CONSTANTS EQUATIONS %token EXTERNAL_FUNCTION EXT_FUNC_NAME EXT_FUNC_NARGS FIRST_DERIV_PROVIDED SECOND_DERIV_PROVIDED %token SELECTED_VARIABLES_ONLY COVA_COMPUTE SIMULATION_FILE_TAG FILE_TAG @@ -740,7 +740,6 @@ ms_options_list : ms_options_list COMMA ms_options ; ms_options : o_chain - | o_regime | o_duration | o_number_of_regimes ; @@ -2220,11 +2219,10 @@ o_cnum : CNUM EQUAL INT_NUMBER {driver.option_num("ms.cnum",$3); }; o_k_order_solver : K_ORDER_SOLVER {driver.option_num("k_order_solver","1"); }; o_pruning : PRUNING { driver.option_num("pruning", "1"); }; o_chain : CHAIN EQUAL INT_NUMBER { driver.option_num("ms.chain",$3); }; -o_regime : REGIME EQUAL INT_NUMBER { driver.option_num("ms.regime",$3); }; o_duration : DURATION EQUAL non_negative_number { driver.option_num("ms.duration",$3); } - | DURATION EQUAL INF_CONSTANT - { driver.option_num("ms.duration","Inf"); } + | DURATION EQUAL vec_value_w_inf + { driver.option_num("ms.duration",$3); } ; o_number_of_regimes : NUMBER_OF_REGIMES EQUAL INT_NUMBER { driver.option_num("ms.number_of_regimes",$3); }; o_coefficients : COEFFICIENTS { driver.option_str("ms.coefficients","svar_coefficients"); }; diff --git a/preprocessor/DynareFlex.ll b/preprocessor/DynareFlex.ll index 293f9adc8..824476d99 100644 --- a/preprocessor/DynareFlex.ll +++ b/preprocessor/DynareFlex.ll @@ -504,7 +504,6 @@ string eofbuff; upper_cholesky {return token::UPPER_CHOLESKY;} lower_cholesky {return token::LOWER_CHOLESKY;} chain {return token::CHAIN;} -regime {return token::REGIME;} number_of_regimes {return token::NUMBER_OF_REGIMES;} duration {return token::DURATION;} coefficients {return token::COEFFICIENTS;} diff --git a/preprocessor/ParsingDriver.cc b/preprocessor/ParsingDriver.cc index a2d73eb7b..178caa5fd 100644 --- a/preprocessor/ParsingDriver.cc +++ b/preprocessor/ParsingDriver.cc @@ -1738,7 +1738,7 @@ ParsingDriver::svar() void ParsingDriver::markov_switching() { - OptionsList::num_options_t::const_iterator it0, it1; + OptionsList::num_options_t::const_iterator it0; it0 = options_list.num_options.find("ms.chain"); if (it0 == options_list.num_options.end()) @@ -1746,31 +1746,15 @@ ParsingDriver::markov_switching() else if (atoi(it0->second.c_str()) <= 0) error("The value passed to the chain option must be greater than zero."); - it0 = options_list.num_options.find("ms.regime"); - it1 = options_list.num_options.find("ms.number_of_regimes"); - if ((it0 == options_list.num_options.end()) - && (it1 == options_list.num_options.end())) - error("Either a regime option or a number_of_regimes option must be passed to the markov_switching statement."); + it0 = options_list.num_options.find("ms.number_of_regimes"); + if (it0 == options_list.num_options.end()) + error("A number_of_regimes option must be passed to the markov_switching statement."); + else if (atoi(it0->second.c_str()) <= 0) + error("The value passed to the number_of_regimes option must be greater than zero."); - if ((it0 != options_list.num_options.end()) - && (it1 != options_list.num_options.end())) - error("You cannot pass both a regime option and a number_of_regimes option to the markov_switching statement."); - - if (it0 != options_list.num_options.end()) - if (atoi(it0->second.c_str()) <= 0) - error("The value passed to the regime option must be greater than zero."); - - if (it1 != options_list.num_options.end()) - if (atoi(it1->second.c_str()) <= 0) - error("The value passed to the number_of_regimes option must be greater than zero."); - - string infStr("Inf"); it0 = options_list.num_options.find("ms.duration"); if (it0 == options_list.num_options.end()) error("A duration option must be passed to the markov_switching statement."); - else if (infStr.compare(it0->second) != 0) - if (atof(it0->second.c_str()) <= 0.0) - error("The value passed to the duration option must be greater than zero."); mod_file->addStatement(new MarkovSwitchingStatement(options_list)); options_list.clear(); From ea8162c11fc517f7d4c8d59e226c6db8839bc11f Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 20 Dec 2011 17:01:18 +0100 Subject: [PATCH 12/14] preprocessor: allow vec_value to accept commas --- preprocessor/DynareBison.yy | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy index a6bb978c0..569de6462 100644 --- a/preprocessor/DynareBison.yy +++ b/preprocessor/DynareBison.yy @@ -2362,18 +2362,27 @@ vec_int : vec_int_1 ']' { $$ = $1; } ; -vec_value_1 : '[' signed_number - { $2->insert(0, "["); $$ = $2;} - | vec_value_1 signed_number - { - $1->append(" "); - $1->append(*$2); - delete $2; - $$ = $1; - } - ; +vec_value_1 : '[' signed_number { $2->insert(0,"["); $$ = $2; } + | '[' COMMA signed_number { $3->insert(0,"["); $$ = $3; } + | vec_value_1 signed_number + { + $1->append(" "); + $1->append(*$2); + delete $2; + $$ = $1; + } + | vec_value_1 COMMA signed_number + { + $1->append(" "); + $1->append(*$3); + delete $3; + $$ = $1; + } + ; -vec_value : vec_value_1 ']' { $1->append("]"); $$ = $1; }; +vec_value : vec_value_1 ']' { $1->append("]"); $$ = $1; } + | vec_value_1 COMMA ']' { $1->append("]"); $$ = $1; } + ; vec_value_1_w_inf : '[' signed_number_w_inf { $2->insert(0, "["); $$ = $2;} From a2061b6f4ef9b6232d0681aad9b14725a514d22c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Wed, 21 Dec 2011 18:37:45 +0100 Subject: [PATCH 13/14] Initialize empirical simulations with information provided in histval Closes: #195, #157 --- matlab/simult.m | 8 ++++---- matlab/stoch_simul.m | 10 +++++++++- matlab/stoch_simul_sparse.m | 7 ++++++- preprocessor/ComputingTasks.cc | 9 --------- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/matlab/simult.m b/matlab/simult.m index 0f8b66e28..303da134f 100644 --- a/matlab/simult.m +++ b/matlab/simult.m @@ -1,9 +1,9 @@ -function y_=simult(ys, dr) -% function y_=simult(ys, dr) +function y_=simult(y0, dr) +% function y_=simult(y0, dr) % Recursive Monte Carlo simulations % % INPUTS -% ys: vector of variables in steady state +% y0: vector of variables in initial period of the simulation % dr: structure of decisions rules for stochastic simulations % % OUTPUTS @@ -54,7 +54,7 @@ for i=1:replic if ~isempty(M_.Sigma_e) oo_.exo_simul(:,i_exo_var) = randn(options_.periods,nxs)*chol_S; end - y_ = simult_(ys,dr,oo_.exo_simul,order); + y_ = simult_(y0,dr,oo_.exo_simul,order); % elimninating initial value y_ = y_(:,2:end); if replic > 1 diff --git a/matlab/stoch_simul.m b/matlab/stoch_simul.m index 7d6e18b8e..ec6d09664 100644 --- a/matlab/stoch_simul.m +++ b/matlab/stoch_simul.m @@ -130,7 +130,15 @@ if options_.periods > 0 && ~PI_PCL_solver options_ =options_old; return end - oo_.endo_simul = simult(oo_.dr.ys,oo_.dr); + % Note that the first column of oo_.endo_simul is preserved by the following + % call to simult; this is important because stoch_simul can be followed by + % forecast (see ticket #157) + if size(oo_.endo_simul,2) == 0 + y0 = oo_.dr.ys; + else + y0 = oo_.endo_simul(:,1); + end + oo_.endo_simul = simult(y0,oo_.dr); dyn2vec; end diff --git a/matlab/stoch_simul_sparse.m b/matlab/stoch_simul_sparse.m index 21194408f..29488b9bb 100644 --- a/matlab/stoch_simul_sparse.m +++ b/matlab/stoch_simul_sparse.m @@ -82,7 +82,12 @@ elseif options_.periods ~= 0 options_ =options_old; return end - oo_.endo_simul = simult(repmat(oo_.dr.ys,1,M_.maximum_lag),oo_.dr); + if size(oo_.endo_simul,2) < maximum_lag + y0 = oo_.dr.ys; + else + y0 = oo_.endo_simul(:,1); + end + oo_.endo_simul = simult(y0,oo_.dr); dyn2vec; if options_.nomoments == 0 disp_moments(oo_.endo_simul,var_list); diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc index dcd000d46..4cc96f966 100644 --- a/preprocessor/ComputingTasks.cc +++ b/preprocessor/ComputingTasks.cc @@ -135,15 +135,6 @@ StochSimulStatement::checkPass(ModFileStructure &mod_file_struct) cerr << "ERROR: in 'stoch_simul', you cannot use option 'pruning' with 'k_order_solver' option or with 3rd order approximation" << endl; exit(EXIT_FAILURE); } - - // Workaround for ticket #157 - it = options_list.num_options.find("periods"); - if (it != options_list.num_options.end() && atoi(it->second.c_str()) > 0 - && mod_file_struct.histval_present) - { - cerr << "ERROR: the 'periods' option of 'stoch_simul' is not compatible with a 'histval' block" << endl; - exit(EXIT_FAILURE); - } } void From a8354326b155c4ba5b01ae0e7c1f047ac4800c74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Wed, 21 Dec 2011 18:38:09 +0100 Subject: [PATCH 14/14] Ref. manual: clarify default value for variables undeclared in histval --- doc/dynare.texi | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doc/dynare.texi b/doc/dynare.texi index d6089777c..5546a8358 100644 --- a/doc/dynare.texi +++ b/doc/dynare.texi @@ -1954,6 +1954,12 @@ of the simulation is period @code{0}, then period @code{-1}, and so on. If your lagged variables are linked by identities, be careful to satisfy these identities when you set historical initial values. +Variables not initialized in the @code{histval} block are assumed to +have a value of zero at period 0 and before. Note that this behavior +differs from the case where there is no @code{histval} block, where all +variables are initialized at their steady state value at period 0 and +before. + @examplehead @example