From 61b37942abcd1cba688b95633ac7553aa335faad Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Tue, 13 Jul 2021 13:38:25 +0000 Subject: [PATCH] First part of Occbin interface --- src/ComputingTasks.cc | 104 ++++++++++++++++++++++++++++++++++++++ src/ComputingTasks.hh | 42 ++++++++++++++++ src/DynareBison.yy | 113 +++++++++++++++++++++++++++++++++++++++++- src/DynareFlex.ll | 18 +++++++ src/ModFile.cc | 3 +- src/ParsingDriver.cc | 29 +++++++++++ src/ParsingDriver.hh | 8 +++ 7 files changed, 314 insertions(+), 3 deletions(-) diff --git a/src/ComputingTasks.cc b/src/ComputingTasks.cc index b51ec5b9..d57356c9 100644 --- a/src/ComputingTasks.cc +++ b/src/ComputingTasks.cc @@ -945,6 +945,110 @@ DiscretionaryPolicyStatement::writeJsonOutput(ostream &output) const output << "}"; } +OccbinSetupStatement::OccbinSetupStatement(OptionsList options_list_arg) : + options_list{move(options_list_arg)} +{ +} + +void +OccbinSetupStatement::writeOutput(ostream &output, const string &basename, bool minimal_workspace) const +{ + options_list.writeOutput(output, "options_occbin_"); + output << "[M_, options_] = occbin.setup(M_, options_, options_occbin_);" << endl; +} + +void +OccbinSetupStatement::writeJsonOutput(ostream &output) const +{ + output << R"({"statementName": "occbin_setup")"; + if (options_list.getNumberOfOptions()) + { + output << ", "; + options_list.writeJsonOutput(output); + } + output << "}"; +} + +OccbinSolverStatement::OccbinSolverStatement(OptionsList options_list_arg) : + options_list{move(options_list_arg)} +{ +} + +void +OccbinSolverStatement::writeOutput(ostream &output, const string &basename, bool minimal_workspace) const +{ + options_list.writeOutput(output, "options_.occbin"); + output << "oo_ = occbin.solver(M_, oo_, options_);" << endl; +} + +void +OccbinSolverStatement::writeJsonOutput(ostream &output) const +{ + output << R"({"statementName": "occbin_solver")"; + if (options_list.getNumberOfOptions()) + { + output << ", "; + options_list.writeJsonOutput(output); + } + output << "}"; +} + +OccbinWriteRegimesStatement::OccbinWriteRegimesStatement(OptionsList options_list_arg) : + options_list{move(options_list_arg)} +{ +} + +void +OccbinWriteRegimesStatement::writeOutput(ostream &output, const string &basename, bool minimal_workspace) const +{ + options_list.writeOutput(output, "options_.occbin"); + output << "occbin.write_regimes_to_xls(oo_.occbin.regime_history, M_, options_);" << endl; +} + +void +OccbinWriteRegimesStatement::writeJsonOutput(ostream &output) const +{ + output << R"({"statementName": "occbin_write_regimes_xls")"; + if (options_list.getNumberOfOptions()) + { + output << ", "; + options_list.writeJsonOutput(output); + } + output << "}"; +} + +OccbinGraphStatement::OccbinGraphStatement(SymbolList symbol_list_arg, + OptionsList options_list_arg) : + symbol_list{move(symbol_list_arg)}, + options_list{move(options_list_arg)} +{ +} + +void +OccbinGraphStatement::writeOutput(ostream &output, const string &basename, bool minimal_workspace) const +{ + symbol_list.writeOutput("var_list_", output); + options_list.writeOutput(output, "options_occbin_"); + output << "occbin.graph(M_, options_, options_occbin_, oo_, var_list_);" << endl; +} + +void +OccbinGraphStatement::writeJsonOutput(ostream &output) const +{ + output << R"({"statementName": "occbin_graph")"; + if (options_list.getNumberOfOptions()) + { + output << ", "; + options_list.writeJsonOutput(output); + } + if (!symbol_list.empty()) + { + output << ", "; + symbol_list.writeJsonOutput(output); + } + output << "}"; +} + EstimationStatement::EstimationStatement(const SymbolTable &symbol_table_arg, SymbolList symbol_list_arg, OptionsList options_list_arg) : diff --git a/src/ComputingTasks.hh b/src/ComputingTasks.hh index 26ecc408..92d95709 100644 --- a/src/ComputingTasks.hh +++ b/src/ComputingTasks.hh @@ -230,6 +230,48 @@ public: void writeJsonOutput(ostream &output) const override; }; +class OccbinSetupStatement : public Statement +{ +private: + const OptionsList options_list; +public: + OccbinSetupStatement(OptionsList options_list_arg); + void writeOutput(ostream &output, const string &basename, bool minimal_workspace) const override; + void writeJsonOutput(ostream &output) const override; +}; + +class OccbinSolverStatement : public Statement +{ +private: + const OptionsList options_list; +public: + OccbinSolverStatement(OptionsList options_list_arg); + void writeOutput(ostream &output, const string &basename, bool minimal_workspace) const override; + void writeJsonOutput(ostream &output) const override; +}; + +class OccbinWriteRegimesStatement : public Statement +{ +private: + const OptionsList options_list; +public: + OccbinWriteRegimesStatement(OptionsList options_list_arg); + void writeOutput(ostream &output, const string &basename, bool minimal_workspace) const override; + void writeJsonOutput(ostream &output) const override; +}; + +class OccbinGraphStatement : public Statement +{ +private: + const OptionsList options_list; + const SymbolList symbol_list; +public: + OccbinGraphStatement(SymbolList symbol_list_arg, + OptionsList options_list_arg); + void writeOutput(ostream &output, const string &basename, bool minimal_workspace) const override; + void writeJsonOutput(ostream &output) const override; +}; + class DiscretionaryPolicyStatement : public Statement { private: diff --git a/src/DynareBison.yy b/src/DynareBison.yy index e85eeade..98cee52e 100644 --- a/src/DynareBison.yy +++ b/src/DynareBison.yy @@ -119,7 +119,11 @@ class ParsingDriver; %token SMOOTHER SMOOTHER2HISTVAL SQUARE_ROOT_SOLVER STACK_SOLVE_ALGO STEADY_STATE_MODEL SOLVE_ALGO SOLVER_PERIODS ROBUST_LIN_SOLVE %token STDERR STEADY STOCH_SIMUL SYLVESTER SYLVESTER_FIXED_POINT_TOL REGIMES REGIME REALTIME_SHOCK_DECOMPOSITION CONDITIONAL UNCONDITIONAL %token TEX RAMSEY_MODEL RAMSEY_POLICY RAMSEY_CONSTRAINTS PLANNER_DISCOUNT PLANNER_DISCOUNT_LATEX_NAME -%token DISCRETIONARY_POLICY DISCRETIONARY_TOL EVALUATE_PLANNER_OBJECTIVE +%token DISCRETIONARY_POLICY DISCRETIONARY_TOL EVALUATE_PLANNER_OBJECTIVE +%token OCCBIN_SETUP OCCBIN_SOLVER OCCBIN_WRITE_REGIMES OCCBIN_GRAPH SIMUL_MAXIT SMOOTHER_MAXIT SIMUL_PERIODS SMOOTHER_PERIODS SIMUL_CURB_RETRENCH SMOOTHER_CURB_RETRENCH SIMUL_CHECK_AHEAD_PERIODS SMOOTHER_CHECK_AHEAD_PERIODS +%token SIMUL_DEBUG SMOOTHER_DEBUG +%token LIKELIHOOD_INVERSION_FILTER SMOOTHER_INVERSION_FILTER FILTER_USE_RELEXATION +%token LIKELIHOOD_PIECEWISE_KALMAN_FILTER SMOOTHER_PIECEWISE_KALMAN_FILTER %token TEX_NAME TRUE %token UNIFORM_PDF UNIT_ROOT_VARS USE_DLL USEAUTOCORR GSA_SAMPLE_FILE USE_UNIVARIATE_FILTERS_IF_SINGULARITY_IS_DETECTED %token VALUES SCALES VAR VAREXO VAREXO_DET VARIABLE VAROBS VAREXOBS PREDETERMINED_VARIABLES VAR_EXPECTATION VAR_EXPECTATION_MODEL PLOT_SHOCK_DECOMPOSITION MODEL_LOCAL_VARIABLE @@ -270,6 +274,10 @@ statement : parameters | ramsey_policy | ramsey_constraints | evaluate_planner_objective + | occbin_setup + | occbin_solver + | occbin_write_regimes + | occbin_graph | discretionary_policy | bvar_density | bvar_forecast @@ -2385,6 +2393,79 @@ evaluate_planner_objective : EVALUATE_PLANNER_OBJECTIVE ';' { driver.evaluate_planner_objective(); } ; +occbin_setup : OCCBIN_SETUP ';' + { driver.occbin_setup(); } + | OCCBIN_SETUP '(' occbin_setup_options_list ')' ';' + { driver.occbin_setup(); } + +occbin_setup_options_list : occbin_setup_option COMMA occbin_setup_options_list + | occbin_setup_option + ; + +occbin_setup_option : o_occbin_simul_periods + | o_occbin_simul_maxit + | o_occbin_simul_curb_retrench + | o_occbin_simul_check_ahead_periods + | o_occbin_simul_debug + | o_occbin_likelihood_inversion_filter + | o_occbin_likelihood_piecewise_kalman_filter + | o_occbin_smoother_inversion_filter + | o_occbin_smoother_piecewise_kalman_filter + | o_occbin_smoother_periods + | o_occbin_smoother_maxit + | o_occbin_smoother_curb_retrench + | o_occbin_smoother_check_ahead_periods + | o_occbin_smoother_debug + | o_occbin_filter_use_relaxation + ; + +occbin_solver : OCCBIN_SOLVER ';' + { driver.occbin_solver(); } + | OCCBIN_SOLVER '(' occbin_solver_options_list ')' ';' + { driver.occbin_solver(); } + | OCCBIN_SOLVER '(' occbin_solver_options_list ')' symbol_list ';' + { driver.occbin_solver(); } + +occbin_solver_options_list : occbin_solver_option COMMA occbin_solver_options_list + | occbin_solver_option + ; + +occbin_solver_option : o_occbin_simul_periods + | o_occbin_simul_maxit + | o_occbin_simul_curb_retrench + | o_occbin_simul_check_ahead_periods + | o_occbin_simul_debug + ; + +occbin_write_regimes : OCCBIN_WRITE_REGIMES ';' + { driver.occbin_write_regimes(); } + | OCCBIN_WRITE_REGIMES '(' occbin_write_regimes_options_list ')' ';' + { driver.occbin_write_regimes(); } + +occbin_write_regimes_options_list : occbin_write_regimes_option COMMA occbin_write_regimes_options_list + | occbin_write_regimes_option + ; + +occbin_write_regimes_option : o_occbin_write_regimes_periods + | o_occbin_write_regimes_filename + ; + +occbin_graph : OCCBIN_GRAPH ';' + { driver.occbin_graph(); } + | OCCBIN_GRAPH '(' occbin_graph_options_list ')' ';' + { driver.occbin_graph(); } + | OCCBIN_GRAPH symbol_list ';' + { driver.occbin_graph(); } + | OCCBIN_GRAPH '(' occbin_graph_options_list ')' symbol_list ';' + { driver.occbin_graph(); } + ; + +occbin_graph_options_list : occbin_graph_option COMMA occbin_graph_options_list + | occbin_graph_option + ; + +occbin_graph_option : o_occbin_graph_noconstant ; + discretionary_policy : DISCRETIONARY_POLICY ';' { driver.discretionary_policy(); } | DISCRETIONARY_POLICY '(' discretionary_policy_options_list ')' ';' @@ -3880,6 +3961,36 @@ o_checks_via_subsets : CHECKS_VIA_SUBSETS EQUAL INT_NUMBER { driver.option_num(" o_max_dim_subsets_groups : MAX_DIM_SUBSETS_GROUPS EQUAL INT_NUMBER { driver.option_num("max_dim_subsets_groups", $3); }; o_zero_moments_tolerance : ZERO_MOMENTS_TOLERANCE EQUAL non_negative_number { driver.option_num("zero_moments_tolerance", $3); }; + +// Some options to "occbin_solver" +o_occbin_simul_maxit : SIMUL_MAXIT EQUAL INT_NUMBER { driver.option_num("simul.maxit", $3); }; +o_occbin_simul_periods : SIMUL_PERIODS EQUAL INT_NUMBER { driver.option_num("simul.periods", $3); }; +o_occbin_simul_curb_retrench : SIMUL_CURB_RETRENCH { driver.option_num("simul.curb_retrench", "true"); }; +o_occbin_simul_check_ahead_periods : SIMUL_CHECK_AHEAD_PERIODS EQUAL INT_NUMBER { driver.option_num("simul.check_ahead_periods", $3); }; +o_occbin_simul_debug : SIMUL_DEBUG { driver.option_num("simul.debug", "true"); }; + +// Some options to "occbin_setup" +o_occbin_likelihood_inversion_filter : LIKELIHOOD_INVERSION_FILTER { driver.option_num("likelihood.inversion_filter", "true"); }; +o_occbin_likelihood_piecewise_kalman_filter : LIKELIHOOD_PIECEWISE_KALMAN_FILTER { driver.option_num("likelihood.inversion_filter", "false"); }; +o_occbin_smoother_inversion_filter : SMOOTHER_INVERSION_FILTER { driver.option_num("smoother.inversion_filter", "true"); }; +o_occbin_smoother_piecewise_kalman_filter : SMOOTHER_PIECEWISE_KALMAN_FILTER { driver.option_num("smoother.inversion_filter", "false"); }; +o_occbin_filter_use_relaxation : FILTER_USE_RELEXATION { driver.option_num("filter.use_relaxation", "true"); }; +o_occbin_smoother_maxit : SMOOTHER_MAXIT EQUAL INT_NUMBER { driver.option_num("smoother.maxit", $3); }; +o_occbin_smoother_periods : SMOOTHER_PERIODS EQUAL INT_NUMBER { driver.option_num("smoother.periods", $3); }; +o_occbin_smoother_curb_retrench : SMOOTHER_CURB_RETRENCH { driver.option_num("smoother.curb_retrench", "true"); }; +o_occbin_smoother_check_ahead_periods : SMOOTHER_CHECK_AHEAD_PERIODS EQUAL INT_NUMBER { driver.option_num("smoother.check_ahead_periods", $3); }; +o_occbin_smoother_debug : SMOOTHER_DEBUG { driver.option_num("smoother.debug", "true"); }; + +// Some options to "occbin_write_regimes" +o_occbin_write_regimes_periods : PERIODS EQUAL vec_int + { driver.option_vec_int("write_regimes.periods", $3); }; + | PERIODS EQUAL vec_int_number + { driver.option_vec_int("write_regimes.periods", $3); } +o_occbin_write_regimes_filename : FILENAME EQUAL filename { driver.option_str("write_regimes.filename", $3); }; + +// Some options to "occbin_graph" +o_occbin_graph_noconstant : NOCONSTANT { driver.option_num("graph.steady_state", "false"); }; + range : symbol ':' symbol { $$ = $1 + ':' + $3; } diff --git a/src/DynareFlex.ll b/src/DynareFlex.ll index 1ebb669f..6816154d 100644 --- a/src/DynareFlex.ll +++ b/src/DynareFlex.ll @@ -148,6 +148,10 @@ DATE -?[0-9]+([ya]|m([1-9]|1[0-2])|q[1-4]) ramsey_model {BEGIN DYNARE_STATEMENT; return token::RAMSEY_MODEL;} ramsey_policy {BEGIN DYNARE_STATEMENT; return token::RAMSEY_POLICY;} evaluate_planner_objective {BEGIN DYNARE_STATEMENT; return token::EVALUATE_PLANNER_OBJECTIVE;} +occbin_setup {BEGIN DYNARE_STATEMENT; return token::OCCBIN_SETUP;} +occbin_solver {BEGIN DYNARE_STATEMENT; return token::OCCBIN_SOLVER;} +occbin_write_regimes {BEGIN DYNARE_STATEMENT; return token::OCCBIN_WRITE_REGIMES;} +occbin_graph {BEGIN DYNARE_STATEMENT; return token::OCCBIN_GRAPH;} discretionary_policy {BEGIN DYNARE_STATEMENT; return token::DISCRETIONARY_POLICY;} identification {BEGIN DYNARE_STATEMENT; return token::IDENTIFICATION;} @@ -559,6 +563,20 @@ DATE -?[0-9]+([ya]|m([1-9]|1[0-2])|q[1-4]) max_block_iterations {return token::MAX_BLOCK_ITERATIONS;} max_repeated_optimization_runs {return token::MAX_REPEATED_OPTIMIZATION_RUNS;} maxit {return token::MAXIT;} +simul_maxit {return token::SIMUL_MAXIT;} +smoother_maxit {return token::SMOOTHER_MAXIT;} +simul_periods {return token::SIMUL_PERIODS;} +smoother_periods {return token::SMOOTHER_PERIODS;} +simul_curb_retrench {return token::SIMUL_CURB_RETRENCH;} +smoother_curb_retrench {return token::SMOOTHER_CURB_RETRENCH;} +simul_check_ahead_periods {return token::SIMUL_CHECK_AHEAD_PERIODS;} +smoother_check_ahead_periods {return token::SMOOTHER_CHECK_AHEAD_PERIODS;} +smoother_debug {return token::SMOOTHER_DEBUG;} +likelihood_inversion_filter {return token::LIKELIHOOD_INVERSION_FILTER;} +likelihood_piecewise_kalman_filter {return token::LIKELIHOOD_PIECEWISE_KALMAN_FILTER;} +smoother_inversion_filter {return token::SMOOTHER_INVERSION_FILTER;} +smoother_piecewise_kalman_filter {return token::SMOOTHER_PIECEWISE_KALMAN_FILTER;} +filter_use_relaxation {return token::FILTER_USE_RELEXATION;} function_convergence_criterion {return token::FUNCTION_CONVERGENCE_CRITERION;} parameter_convergence_criterion {return token::PARAMETER_CONVERGENCE_CRITERION;} number_of_large_perturbations {return token::NUMBER_OF_LARGE_PERTURBATIONS;} diff --git a/src/ModFile.cc b/src/ModFile.cc index af3e4fcb..8fc0b422 100644 --- a/src/ModFile.cc +++ b/src/ModFile.cc @@ -1043,8 +1043,7 @@ ModFile::writeMOutput(const string &basename, bool clear_all, bool clear_global, } if (occbin) - mOutputFile << "options_ = occbin.set_default_options(options_, M_);" << endl - << "M_ = occbin.get_info(M_);" << endl; + mOutputFile << "[M_, oo_, options_] = occbin.initialize(M_, oo_, options_);" << endl; if (onlymodel || gui) for (const auto &statement : statements) diff --git a/src/ParsingDriver.cc b/src/ParsingDriver.cc index 49656a4c..0a42ffd5 100644 --- a/src/ParsingDriver.cc +++ b/src/ParsingDriver.cc @@ -2058,6 +2058,35 @@ ParsingDriver::evaluate_planner_objective() mod_file->addStatement(make_unique()); } +void +ParsingDriver::occbin_setup() +{ + mod_file->addStatement(make_unique(options_list)); + options_list.clear(); +} + +void +ParsingDriver::occbin_solver() +{ + mod_file->addStatement(make_unique(options_list)); + options_list.clear(); +} + +void +ParsingDriver::occbin_write_regimes() +{ + mod_file->addStatement(make_unique(options_list)); + options_list.clear(); +} + +void +ParsingDriver::occbin_graph() +{ + mod_file->addStatement(make_unique(symbol_list, options_list)); + options_list.clear(); + symbol_list.clear(); +} + void ParsingDriver::discretionary_policy() { diff --git a/src/ParsingDriver.hh b/src/ParsingDriver.hh index cc6fb433..972bd40b 100644 --- a/src/ParsingDriver.hh +++ b/src/ParsingDriver.hh @@ -640,6 +640,14 @@ public: void ramsey_policy(); //! Evaluate Planner Objective void evaluate_planner_objective(); + //! Set up Occbin + void occbin_setup(); + //! Run Occbin solver + void occbin_solver(); + //! Run Occbin write XLS + void occbin_write_regimes(); + //! Run plotting of Occbin results + void occbin_graph(); //! Discretionary policy statement void discretionary_policy(); //! Adds a write_latex_dynamic_model statement