From fe0c781c06823da40609012ac1e7d6003272ffb5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Wed, 18 Dec 2019 17:32:41 +0100
Subject: [PATCH] =?UTF-8?q?New=20=E2=80=9Cplanner=5Fdiscount=5Flatex=5Fnam?=
 =?UTF-8?q?e=E2=80=9D=20option=20of=20=E2=80=9Cramsey=5Fpolicy=E2=80=9D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Accepts a TeX name, as for variable declarations (i.e. enclosed within dollar
signs).

Ref. dynare#1686
---
 src/DynareBison.yy   |  7 +++++--
 src/DynareFlex.ll    |  1 +
 src/ParsingDriver.cc | 40 ++++++++++++++++++++++++++++++++++------
 src/ParsingDriver.hh | 10 ++++++++--
 4 files changed, 48 insertions(+), 10 deletions(-)

diff --git a/src/DynareBison.yy b/src/DynareBison.yy
index 22f5b679..ad8fd04e 100644
--- a/src/DynareBison.yy
+++ b/src/DynareBison.yy
@@ -114,7 +114,8 @@ class ParsingDriver;
 %token SHOCKS SHOCK_DECOMPOSITION SHOCK_GROUPS USE_SHOCK_GROUPS SIGMA_E SIMUL SIMUL_ALGO SIMUL_SEED ENDOGENOUS_TERMINAL_PERIOD
 %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 DISCRETIONARY_POLICY DISCRETIONARY_TOL EVALUATE_PLANNER_OBJECTIVE
+%token TEX RAMSEY_MODEL RAMSEY_POLICY RAMSEY_CONSTRAINTS PLANNER_DISCOUNT PLANNER_DISCOUNT_LATEX_NAME
+%token DISCRETIONARY_POLICY DISCRETIONARY_TOL EVALUATE_PLANNER_OBJECTIVE
 %token <string> TEX_NAME
 %token UNIFORM_PDF UNIT_ROOT_VARS USE_DLL USEAUTOCORR GSA_SAMPLE_FILE USE_UNIVARIATE_FILTERS_IF_SINGULARITY_IS_DETECTED
 %token VALUES VAR VAREXO VAREXO_DET VARIABLE VAROBS VAREXOBS PREDETERMINED_VARIABLES VAR_EXPECTATION VAR_EXPECTATION_MODEL PLOT_SHOCK_DECOMPOSITION MODEL_LOCAL_VARIABLE
@@ -2380,6 +2381,7 @@ ramsey_model_options_list : ramsey_model_options_list COMMA ramsey_model_options
                            ;
 
 ramsey_model_options :  o_planner_discount
+                      | o_planner_discount_latex_name
                       | o_instruments
                       ;
 
@@ -3398,7 +3400,8 @@ o_plot_priors: PLOT_PRIORS EQUAL INT_NUMBER {driver.option_num("plot_priors", $3
 o_aim_solver: AIM_SOLVER {driver.option_num("aim_solver", "true"); };
 o_partial_information : PARTIAL_INFORMATION {driver.option_num("partial_information", "true"); };
 o_sub_draws: SUB_DRAWS EQUAL INT_NUMBER {driver.option_num("sub_draws",$3);};
-o_planner_discount : PLANNER_DISCOUNT EQUAL expression { driver.declare_optimal_policy_discount_factor_parameter($3); };
+o_planner_discount : PLANNER_DISCOUNT EQUAL expression { driver.set_planner_discount($3); };
+o_planner_discount_latex_name : PLANNER_DISCOUNT_LATEX_NAME EQUAL TEX_NAME { driver.set_planner_discount_latex_name($3); };
 o_sylvester : SYLVESTER EQUAL FIXED_POINT {driver.option_num("sylvester_fp", "true"); }
                | SYLVESTER EQUAL DEFAULT {driver.option_num("sylvester_fp", "false"); };
 o_sylvester_fixed_point_tol : SYLVESTER_FIXED_POINT_TOL EQUAL non_negative_number {driver.option_num("sylvester_fixed_point_tol",$3);};
diff --git a/src/DynareFlex.ll b/src/DynareFlex.ll
index 83a14f25..005610f9 100644
--- a/src/DynareFlex.ll
+++ b/src/DynareFlex.ll
@@ -774,6 +774,7 @@ DATE -?[0-9]+([ya]|m([1-9]|1[0-2])|q[1-4]|w([1-9]{1}|[1-4][0-9]|5[0-2]))
 <DYNARE_STATEMENT>series {return token::SERIES;}
 <DYNARE_STATEMENT>mh_recover {return token::MH_RECOVER;}
 <DYNARE_STATEMENT>planner_discount {return token::PLANNER_DISCOUNT;}
+<DYNARE_STATEMENT>planner_discount_latex_name {return token::PLANNER_DISCOUNT_LATEX_NAME;}
 <DYNARE_STATEMENT>calibration {return token::CALIBRATION;}
 <DYNARE_STATEMENT>irf_plot_threshold {return token::IRF_PLOT_THRESHOLD;}
 <DYNARE_STATEMENT>no_homotopy {return token::NO_HOMOTOPY;}
diff --git a/src/ParsingDriver.cc b/src/ParsingDriver.cc
index a600c3ca..bf55ac51 100644
--- a/src/ParsingDriver.cc
+++ b/src/ParsingDriver.cc
@@ -246,10 +246,15 @@ ParsingDriver::declare_statement_local_variable(const string &name)
 }
 
 void
-ParsingDriver::declare_optimal_policy_discount_factor_parameter(expr_t exprnode)
+ParsingDriver::set_planner_discount(expr_t value)
 {
-  declare_parameter("optimal_policy_discount_factor");
-  init_param("optimal_policy_discount_factor", exprnode);
+  planner_discount = value;
+}
+
+void
+ParsingDriver::set_planner_discount_latex_name(string tex_name)
+{
+  planner_discount_latex_name = move(tex_name);
 }
 
 void
@@ -2173,20 +2178,40 @@ void
 ParsingDriver::ramsey_model()
 {
   if (!mod_file->symbol_table.exists("optimal_policy_discount_factor"))
-    declare_optimal_policy_discount_factor_parameter(data_tree->One);
+    {
+      if (!planner_discount)
+        planner_discount = data_tree->One;
+      declare_parameter("optimal_policy_discount_factor", planner_discount_latex_name);
+      init_param("optimal_policy_discount_factor", planner_discount);
+    }
+  else if (planner_discount)
+    error("ramsey_model: the 'planner_discount' option cannot be used when the 'optimal_policy_discount_factor' parameter is explicitly declared.");
+
   mod_file->addStatement(make_unique<RamseyModelStatement>(options_list));
   options_list.clear();
+  planner_discount = nullptr;
+  planner_discount_latex_name.clear();
 }
 
 void
 ParsingDriver::ramsey_policy()
 {
   warning("The 'ramsey_policy' statement is deprecated. Please use 'ramsey_model', 'stoch_simul', and 'evaluate_planner_objective' instead.");
+
   if (!mod_file->symbol_table.exists("optimal_policy_discount_factor"))
-    declare_optimal_policy_discount_factor_parameter(data_tree->One);
+    {
+      if (!planner_discount)
+        planner_discount = data_tree->One;
+      declare_parameter("optimal_policy_discount_factor");
+      init_param("optimal_policy_discount_factor", planner_discount);
+    }
+  else if (planner_discount)
+    error("ramsey_policy: the 'planner_discount' option cannot be used when the 'optimal_policy_discount_factor' parameter is explicitly declared.");
+
   mod_file->addStatement(make_unique<RamseyPolicyStatement>(mod_file->symbol_table, ramsey_policy_list, options_list));
   options_list.clear();
   ramsey_policy_list.clear();
+  planner_discount = nullptr;
 }
 
 void
@@ -2205,7 +2230,10 @@ void
 ParsingDriver::discretionary_policy()
 {
   if (!mod_file->symbol_table.exists("optimal_policy_discount_factor"))
-    declare_optimal_policy_discount_factor_parameter(data_tree->One);
+    {
+      declare_parameter("optimal_policy_discount_factor");
+      init_param("optimal_policy_discount_factor", data_tree->One);
+    }
   mod_file->addStatement(make_unique<DiscretionaryPolicyStatement>(symbol_list, options_list));
   symbol_list.clear();
   options_list.clear();
diff --git a/src/ParsingDriver.hh b/src/ParsingDriver.hh
index 8b772dce..f711e9dd 100644
--- a/src/ParsingDriver.hh
+++ b/src/ParsingDriver.hh
@@ -239,6 +239,10 @@ private:
   vector<pair<int, int>> init2shocks;
   //! Temporary storage for ramsey policy. Workaround for issue #1355
   vector<string> ramsey_policy_list;
+  /* Temporary storage for planner_discount and planner_discount_latex_name
+     options of ramsey_model and ramsey_policy */
+  expr_t planner_discount{nullptr};
+  string planner_discount_latex_name;
   //! reset the values for temporary storage
   void reset_current_external_function_options();
   //! Adds a model lagged variable to ModelTree and VariableTable
@@ -395,8 +399,10 @@ public:
   void check_subsample_declaration_exists(const string &name1, const string &name2, const string &subsample_name);
   //! Copies the set of subsamples from_name to_name
   void copy_subsamples(string to_name1, string to_name2, string from_name1, string from_name2);
-  //! Declares declare_optimal_policy_discount_factor as a parameter and initializes it to exprnode
-  void declare_optimal_policy_discount_factor_parameter(expr_t exprnode);
+  //! Sets the value of the planner_discount option of ramsey_{model,policy}
+  void set_planner_discount(expr_t value);
+  //! Sets the value of the planner_discount_latex_name option of ramsey_model
+  void set_planner_discount_latex_name(string tex_name);
   //! Adds a predetermined_variable
   void add_predetermined_variable(const string &name);
   //! Declares and initializes a local parameter
-- 
GitLab