From c99aa4aed0c684c74aee0f3679c3c0f1f843313f Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Thu, 3 Nov 2016 12:56:54 +0100 Subject: [PATCH] var_forecast: move var declaration from options_ to M_, simplify structure in which it is stored --- matlab/var_forecast.m | 30 +++++++----------------------- preprocessor/ComputingTasks.cc | 27 ++++++++++++++------------- preprocessor/ComputingTasks.hh | 12 +++++++----- preprocessor/DynareBison.yy | 6 +++--- preprocessor/ModFile.cc | 3 --- preprocessor/ParsingDriver.cc | 6 +++++- preprocessor/Statement.hh | 2 -- 7 files changed, 36 insertions(+), 50 deletions(-) diff --git a/matlab/var_forecast.m b/matlab/var_forecast.m index 51eee8394..622c7dba6 100644 --- a/matlab/var_forecast.m +++ b/matlab/var_forecast.m @@ -1,8 +1,7 @@ -function y = var_forecast(M_, options_, name, h, y, fcv) +function y = var_forecast(M_, name, h, y, fcv) % name : filename % M_ -% options_ % name string name of var model, provided in var statement % h int number of steps-ahead forecast % y matrix rows: realizations of endogenous variables in declaration order; cols: realizations in t, t-1, t-2 ... order of VAR @@ -25,30 +24,15 @@ function y = var_forecast(M_, options_, name, h, y, fcv) % From Matlab backend: % >> yt = [0.0600; 33.0000; 0.0300; 22.0000]; % >> ytm1 = [0.0550; 11.0000; 0.0300; 88.0000]; -% >> var_forecast(M_, options_, 'm1', 1, [yt ytm1]) -% >> var_forecast(M_, options_, 'm1', 2, [yt ytm1], ['a']) - -%% Find var in options_ -order = ''; -var_list = ''; -for i=1:length(options_.var) - if strcmp(options_.var(i).name, name) - order = options_.var(i).order; - var_list = options_.var(i).var_list_; - break; - end -end - -if isempty(order) - error([name ' not found in var specification declared in .mod file']); -end +% >> var_forecast(M_, 'm1', 1, [yt ytm1]) +% >> var_forecast(M_, 'm1', 2, [yt ytm1], ['a']) %% construct y assert(length(y) == length(M_.endo_names)); endo_names = cellstr(M_.endo_names); yidx = zeros(size(endo_names)); -for i=1:length(var_list) - yidx = yidx | strcmp(strtrim(var_list(i,:)), endo_names); +for i=1:length(M_.var.(name).var_list_) + yidx = yidx | strcmp(strtrim(M_.var.(name).var_list_(i,:)), endo_names); end y = y(yidx,:); @@ -65,8 +49,8 @@ assert(h >= 1); lm = length(mu); lc = length(coefficients); -assert(lc == order); -if size(y,1) ~= lm || size(y,2) ~= order +assert(lc == M_.var.(name).order); +if size(y,1) ~= lm || size(y,2) ~= M_.var.(name).order error('The dimensions of y are not correct. It should be an nvars x order matrix'); end diff --git a/preprocessor/ComputingTasks.cc b/preprocessor/ComputingTasks.cc index 81ce4cf9d..c7a297880 100644 --- a/preprocessor/ComputingTasks.cc +++ b/preprocessor/ComputingTasks.cc @@ -171,19 +171,20 @@ PriorPosteriorFunctionStatement::writeOutput(ostream &output, const string &base << "'" << type << "');" << endl; } -VARStatement::VARStatement(const SymbolList &symbol_list_arg, - const OptionsList &options_list_arg, - const SymbolTable &symbol_table_arg) : +VarModelStatement::VarModelStatement(const SymbolList &symbol_list_arg, + const OptionsList &options_list_arg, + const string &name_arg, + const SymbolTable &symbol_table_arg) : symbol_list(symbol_list_arg), options_list(options_list_arg), + name(name_arg), symbol_table(symbol_table_arg) { } void -VARStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) +VarModelStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings) { - mod_file_struct.var_model_present = true; vector symbols = symbol_list.get_symbols(); for (vector::const_iterator it = symbols.begin(); it != symbols.end(); it++) if (symbol_table.getType(*it) != eEndogenous) @@ -192,27 +193,27 @@ VARStatement::checkPass(ModFileStructure &mod_file_struct, WarningConsolidation exit(EXIT_FAILURE); } - OptionsList::num_options_t::const_iterator it = options_list.num_options.find("var(varidx).order"); + OptionsList::num_options_t::const_iterator it = options_list.num_options.find("var.order"); if (it == options_list.num_options.end()) { - cerr << "ERROR: You must provide the order option to the var command." << endl; + cerr << "ERROR: You must provide the order option to the var_model statement." << endl; exit(EXIT_FAILURE); } - OptionsList::string_options_t::const_iterator it1 = options_list.string_options.find("var(varidx).name"); - if (it1 == options_list.string_options.end()) + if (name.empty()) { - cerr << "ERROR: You must provide the model_name option to the var command." << endl; + cerr << "ERROR: You must provide the model_name option to the var_model statement." << endl; exit(EXIT_FAILURE); } } void -VARStatement::writeOutput(ostream &output, const string &basename, bool minimal_workspace) const +VarModelStatement::writeOutput(ostream &output, const string &basename, bool minimal_workspace) const { options_list.writeOutput(output); - symbol_list.writeOutput("options_.var(varidx).var_list_", output); - output << "varidx = varidx + 1;" << endl; + symbol_list.writeOutput("options_.var.var_list_", output); + output << "M_.var." << name << " = options_.var;" << endl + << "clear options_.var;" << endl; } StochSimulStatement::StochSimulStatement(const SymbolList &symbol_list_arg, diff --git a/preprocessor/ComputingTasks.hh b/preprocessor/ComputingTasks.hh index d9a0c536d..bba0efbfb 100644 --- a/preprocessor/ComputingTasks.hh +++ b/preprocessor/ComputingTasks.hh @@ -110,16 +110,18 @@ public: virtual void writeOutput(ostream &output, const string &basename, bool minimal_workspace) const; }; -class VARStatement : public Statement +class VarModelStatement : public Statement { private: const SymbolList symbol_list; const OptionsList options_list; - const SymbolTable &symbol_table; + const string &name; + const SymbolTable &symbol_table; public: - VARStatement(const SymbolList &symbol_list_arg, - const OptionsList &options_list_arg, - const SymbolTable &symbol_table_arg); + VarModelStatement(const SymbolList &symbol_list_arg, + const OptionsList &options_list_arg, + const string &name_arg, + const SymbolTable &symbol_table_arg); virtual void checkPass(ModFileStructure &mod_file_struct, WarningConsolidation &warnings); virtual void writeOutput(ostream &output, const string &basename, bool minimal_workspace) const; }; diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy index 3e23212b6..224a19658 100644 --- a/preprocessor/DynareBison.yy +++ b/preprocessor/DynareBison.yy @@ -347,7 +347,7 @@ var : VAR var_list ';' { driver.end_nonstationary_var(true, $6); } ; -var_model : VAR '(' var_model_options_list ')' symbol_list ';' { driver.var_model(); } ; +var_model : VAR_MODEL '(' var_model_options_list ')' symbol_list ';' { driver.var_model(); } ; var_model_options_list : var_model_options_list COMMA var_model_options | var_model_options @@ -2827,8 +2827,8 @@ o_simul_seed : SIMUL_SEED EQUAL INT_NUMBER { driver.error("'simul_seed' option i o_qz_criterium : QZ_CRITERIUM EQUAL non_negative_number { driver.option_num("qz_criterium", $3); }; o_qz_zero_threshold : QZ_ZERO_THRESHOLD EQUAL non_negative_number { driver.option_num("qz_zero_threshold", $3); }; o_file : FILE EQUAL filename { driver.option_str("file", $3); }; -o_var_name : MODEL_NAME EQUAL symbol { driver.option_str("var(varidx).name", $3); }; -o_var_order : ORDER EQUAL INT_NUMBER { driver.option_num("var(varidx).order", $3); }; +o_var_name : MODEL_NAME EQUAL symbol { driver.option_str("var.model_name", $3); }; +o_var_order : ORDER EQUAL INT_NUMBER { driver.option_num("var.order", $3); }; o_series : SERIES EQUAL symbol { driver.option_str("series", $3); }; o_datafile : DATAFILE EQUAL filename { driver.option_str("datafile", $3); }; o_dirname : DIRNAME EQUAL filename { driver.option_str("dirname", $3); }; diff --git a/preprocessor/ModFile.cc b/preprocessor/ModFile.cc index 1b114d1e0..5719d0197 100644 --- a/preprocessor/ModFile.cc +++ b/preprocessor/ModFile.cc @@ -808,9 +808,6 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool clear_glo static_model.writeOutput(mOutputFile, block); } - if (mod_file_struct.var_model_present) - mOutputFile << "varidx = 1;" << endl; - // Print statements for (vector::const_iterator it = statements.begin(); it != statements.end(); it++) diff --git a/preprocessor/ParsingDriver.cc b/preprocessor/ParsingDriver.cc index 4fdf1396a..d90f09332 100644 --- a/preprocessor/ParsingDriver.cc +++ b/preprocessor/ParsingDriver.cc @@ -1282,7 +1282,11 @@ ParsingDriver::stoch_simul() void ParsingDriver::var_model() { - mod_file->addStatement(new VARStatement(symbol_list, options_list, mod_file->symbol_table)); + OptionsList::string_options_t::const_iterator it = options_list.string_options.find("var.model_name"); + if (it == options_list.string_options.end()) + error("You must pass the model_name option to the var_model statement."); + const string *name = new string(it->second); + mod_file->addStatement(new VarModelStatement(symbol_list, options_list, *name, mod_file->symbol_table)); symbol_list.clear(); options_list.clear(); } diff --git a/preprocessor/Statement.hh b/preprocessor/Statement.hh index 1676ff313..ee7846ff4 100644 --- a/preprocessor/Statement.hh +++ b/preprocessor/Statement.hh @@ -122,8 +122,6 @@ public: int orig_eq_nbr; //! Stores the number of equations added to the Ramsey model int ramsey_eq_nbr; - //! Whether a VAR statement is present - bool var_model_present; }; class Statement