diff --git a/src/ComputingTasks.cc b/src/ComputingTasks.cc index 75ae03e88001d0d68d17a6faf1b0a973e378e18e..afeb51a2c0bfe1632ae6d69a4095453878a1c3fb 100644 --- a/src/ComputingTasks.cc +++ b/src/ComputingTasks.cc @@ -1769,6 +1769,51 @@ ObservationTrendsStatement::writeJsonOutput(ostream &output) const << "}"; } +FilterInitialStateStatement::FilterInitialStateStatement(const filter_initial_state_elements_t &filter_initial_state_elements_arg, + const SymbolTable &symbol_table_arg) : + filter_initial_state_elements(filter_initial_state_elements_arg), + symbol_table(symbol_table_arg) +{ +} + +void +FilterInitialStateStatement::writeOutput(ostream &output, const string &basename, bool minimal_workspace) const +{ + output << "M_.filter_initial_state = cell(M_.endo_nbr, 2);" << endl; + for (filter_initial_state_elements_t::const_iterator it = filter_initial_state_elements.begin(); + it != filter_initial_state_elements.end(); it++) + { + int symb_id = it->first.first; + int lag = it->first.second; + SymbolType type = symbol_table.getType(symb_id); + + if ((type == eEndogenous && lag < 0) || type == eExogenous) + { + try + { + // This function call must remain the 1st statement in this block + symb_id = symbol_table.searchAuxiliaryVars(symb_id, lag); + } + catch (SymbolTable::SearchFailedException &e) + { + if (type == eEndogenous) + { + cerr << "filter_initial_state: internal error, please contact the developers"; + exit(EXIT_FAILURE); + } + // We don't fail for exogenous, because they are not replaced by + // auxiliary variables in deterministic mode. + } + } + + output << "M_.filter_initial_state(" + << symbol_table.getTypeSpecificID(symb_id) + 1 + << ",:) = {'" << symbol_table.getName(symb_id) << "', '"; + it->second->writeOutput(output); + output << ";'};" << endl; + } +} + OsrParamsStatement::OsrParamsStatement(SymbolList symbol_list_arg, const SymbolTable &symbol_table_arg) : symbol_list{move(symbol_list_arg)}, symbol_table{symbol_table_arg} diff --git a/src/ComputingTasks.hh b/src/ComputingTasks.hh index 6bc0d05072b73f390c43d011a189f1d1dd5b75f2..b18d0b5e1250a49366f578e98b9d86ef1e9aa9fc 100644 --- a/src/ComputingTasks.hh +++ b/src/ComputingTasks.hh @@ -315,6 +315,19 @@ public: void writeJsonOutput(ostream &output) const override; }; +class FilterInitialStateStatement : public Statement +{ +public: + typedef map<pair<int, int>, expr_t> filter_initial_state_elements_t; +private: + const filter_initial_state_elements_t filter_initial_state_elements; + const SymbolTable &symbol_table; +public: + FilterInitialStateStatement(const filter_initial_state_elements_t &filter_initial_state_elements_arg, + const SymbolTable &symbol_table_arg); + virtual void writeOutput(ostream &output, const string &basename, bool minimal_workspace) const; +}; + class OsrParamsStatement : public Statement { private: diff --git a/src/DynareBison.yy b/src/DynareBison.yy index e19e73255ba3a3a8f9ca7c5df664fd0653a881bf..ea17141f53a05ad8f4a3585f8e128781183ca4cb 100644 --- a/src/DynareBison.yy +++ b/src/DynareBison.yy @@ -100,7 +100,7 @@ class ParsingDriver; %token MODE_CHECK MODE_CHECK_NEIGHBOURHOOD_SIZE MODE_CHECK_SYMMETRIC_PLOTS MODE_CHECK_NUMBER_OF_POINTS MODE_COMPUTE MODE_FILE MODEL MODEL_COMPARISON MODEL_INFO MSHOCKS ABS SIGN %token MODEL_DIAGNOSTICS MODIFIEDHARMONICMEAN MOMENTS_VARENDO CONTEMPORANEOUS_CORRELATION DIFFUSE_FILTER SUB_DRAWS TAPER_STEPS GEWEKE_INTERVAL RAFTERY_LEWIS_QRS RAFTERY_LEWIS_DIAGNOSTICS MCMC_JUMPING_COVARIANCE MOMENT_CALIBRATION %token NUMBER_OF_PARTICLES RESAMPLING SYSTEMATIC GENERIC RESAMPLING_THRESHOLD RESAMPLING_METHOD KITAGAWA STRATIFIED SMOOTH -%token CPF_WEIGHTS AMISANOTRISTANI MURRAYJONESPARSLOW WRITE_EQUATION_TAGS +%token CPF_WEIGHTS AMISANOTRISTANI MURRAYJONESPARSLOW WRITE_EQUATION_TAGS FILTER_INITIAL_STATE %token NONLINEAR_FILTER_INITIALIZATION FILTER_ALGORITHM PROPOSAL_APPROXIMATION CUBATURE UNSCENTED MONTECARLO DISTRIBUTION_APPROXIMATION %token <string> NAME %token USE_PENALIZED_OBJECTIVE_FOR_HESSIAN INIT_STATE FAST_REALTIME RESCALE_PREDICTION_ERROR_COVARIANCE GENERATE_IRFS @@ -244,6 +244,7 @@ statement : parameters | options_eq | varobs | observation_trends + | filter_initial_state | varexobs | unit_root_vars | dsample @@ -2084,6 +2085,14 @@ trend_list : trend_list trend_element trend_element : symbol '(' expression ')' ';' { driver.set_trend_element($1, $3); }; +filter_initial_state : FILTER_INITIAL_STATE ';' filter_initial_state_list END ';' { driver.set_filter_initial_state(); }; + +filter_initial_state_list : filter_initial_state_list filter_initial_state_element + | filter_initial_state_element + ; + +filter_initial_state_element : symbol '(' signed_integer ')' EQUAL expression ';' { driver.set_filter_initial_state_element($1, $3, $6); }; + unit_root_vars : UNIT_ROOT_VARS symbol_list ';' { driver.set_unit_root_vars(); }; optim_weights : OPTIM_WEIGHTS ';' optim_weights_list END ';' { driver.optim_weights(); }; diff --git a/src/DynareFlex.ll b/src/DynareFlex.ll index 124f8c9625d677ce1f8926ea16ff61652a76189c..6c556d522d1d35a6645f93ba8aaec3031af89784 100644 --- a/src/DynareFlex.ll +++ b/src/DynareFlex.ll @@ -201,6 +201,7 @@ DATE -?[0-9]+([ya]|m([1-9]|1[0-2])|q[1-4]) <INITIAL>initval {BEGIN DYNARE_BLOCK; return token::INITVAL;} <INITIAL>endval {BEGIN DYNARE_BLOCK; return token::ENDVAL;} <INITIAL>histval {BEGIN DYNARE_BLOCK; return token::HISTVAL;} +<INITIAL>filter_initial_state {BEGIN DYNARE_BLOCK; return token::FILTER_INITIAL_STATE;} <INITIAL>shocks {BEGIN DYNARE_BLOCK; return token::SHOCKS;} <INITIAL>shock_groups {BEGIN DYNARE_BLOCK; return token::SHOCK_GROUPS;} <INITIAL>init2shocks {BEGIN DYNARE_BLOCK; return token::INIT2SHOCKS;} diff --git a/src/ParsingDriver.cc b/src/ParsingDriver.cc index b2a4085f3bc1cae9308c14d77cc47d751f2f74c7..952e05c0f49844106c1bf426eebed900c27d3794 100644 --- a/src/ParsingDriver.cc +++ b/src/ParsingDriver.cc @@ -1937,6 +1937,13 @@ ParsingDriver::set_trends() trend_elements.clear(); } +void +ParsingDriver::set_filter_initial_state() +{ + mod_file->addStatement(new FilterInitialStateStatement(filter_initial_state_elements, mod_file->symbol_table)); + filter_initial_state_elements.clear(); +} + void ParsingDriver::set_trend_element(string arg1, expr_t arg2) { @@ -1946,6 +1953,39 @@ ParsingDriver::set_trend_element(string arg1, expr_t arg2) trend_elements[move(arg1)] = arg2; } +void +ParsingDriver::set_filter_initial_state_element(string *name, string *lag, expr_t rhs) +{ + check_symbol_existence(*name); + int symb_id = mod_file->symbol_table.getID(*name); + SymbolType type = mod_file->symbol_table.getType(symb_id); + int ilag = atoi(lag->c_str()); + + if (type != eEndogenous + && type != eExogenous + && type != eExogenousDet) + error("filter_initial_state: " + *name + " should be an endogenous or exogenous variable"); + + if ((type == eExogenous || type == eExogenousDet) && lag == 0) + error("filter_initial_state: exogenous variable " + *name + " must be provided with a lag"); + + pair<int, int> key(symb_id, ilag); + if (filter_initial_state_elements.find(key) != filter_initial_state_elements.end()) + error("filter_initial_state: (" + *name + ", " + *lag + ") declared twice"); + + if (mod_file->dynamic_model.minLagForSymbol(symb_id) > ilag - 1) + { + ostringstream s; + s << ilag - 1; + error("filter_initial_state: variable " + *name + " does not appear in the model with the lag " + s.str() + " (see the reference manual for the timing convention in 'filter_initial_state')"); + } + + filter_initial_state_elements[key] = rhs; + + delete name; + delete lag; +} + void ParsingDriver::set_optim_weights(string name, expr_t value) { diff --git a/src/ParsingDriver.hh b/src/ParsingDriver.hh index 6f735acad1ee1c8954ecc76b0e84860b03eecdb8..39c6d1f1702089807c2d1c5efaa7e0d6ebb7514c 100644 --- a/src/ParsingDriver.hh +++ b/src/ParsingDriver.hh @@ -141,6 +141,8 @@ private: OptionsList options_list; //! Temporary storage for trend elements ObservationTrendsStatement::trend_elements_t trend_elements; + /! Temporary storage for filter_initial_state elements + FilterInitialStateStatement::filter_initial_state_elements_t filter_initial_state_elements; //! Temporary storage for filename list of ModelComparison (contains weights) ModelComparisonStatement::filename_list_t filename_list; //! Temporary storage for list of EstimationParams (from estimated_params* statements) @@ -612,6 +614,10 @@ public: void forecast(); void set_trends(); void set_trend_element(string arg1, expr_t arg2); + //! filter_initial_state block + void set_filter_initial_state(); + //! element for filter_initial_state block + void set_filter_initial_state_element(string *name, string *lag, expr_t rhs); void set_unit_root_vars(); void optim_weights(); void set_optim_weights(string name, expr_t value);