From cecc9aad6952362631c454baafee544af18d2bc3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Tue, 28 Mar 2023 18:45:16 +0200
Subject: [PATCH] New fields: M_.ramsey_orig_{eq,endo}_nbr
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

– M_.ramsey_orig_endo_nbr is the number of endogenous variables in the model
  present just before adding the Lagrange multipliers and computing the Ramsey
  FOC; it is by construction equal to the number of equations that will be added
  by the process of computing the FOCs
– M_.ramsey_orig_eq_nbr is the number of equations in the model present just
  before adding the Lagrange multipliers and computing the Ramsey FOC; it is by
  construction equal to the number of Lagrange multipliers that will be added by
  the process of computing the FOCs

Note that both may be greater than the number of endogenous/equations written
by the user, because some auxiliary variables may have already been added.

Also note that the number of policy instruments in M_.ramsey_orig_endo_nbr −
M_.ramsey_orig_eq_nbr

As a consequence, drop M_.ramsey_eq_nbr (which was actually equal to
M_.ramsey_orig_endo_nbr) and M_.orig_eq_nbr (which was actually equal to
M_.ramsey_orig_eq_nbr, but would also be set in the non-Ramsey case). The new
names are clearer.
---
 src/ModFile.cc   | 11 +++++------
 src/Statement.hh | 15 ++++++++++-----
 2 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/src/ModFile.cc b/src/ModFile.cc
index a5fbc490..8e4a9736 100644
--- a/src/ModFile.cc
+++ b/src/ModFile.cc
@@ -473,9 +473,9 @@ ModFile::transformPass(bool nostrict, bool stochastic, bool compute_xrefs, bool
 
   epilogue.toStatic();
 
-  mod_file_struct.orig_eq_nbr = dynamic_model.equation_number();
   if (mod_file_struct.ramsey_model_present)
     {
+      mod_file_struct.ramsey_orig_eq_nbr = dynamic_model.equation_number();
       PlannerObjectiveStatement *pos = nullptr;
       for (auto &statement : statements)
         if (auto pos2 = dynamic_cast<PlannerObjectiveStatement *>(statement.get()); pos2)
@@ -499,7 +499,6 @@ ModFile::transformPass(bool nostrict, bool stochastic, bool compute_xrefs, bool
       ramsey_FOC_equations_dynamic_model = dynamic_model;
       mod_file_struct.ramsey_orig_endo_nbr = ramsey_FOC_equations_dynamic_model.computeRamseyPolicyFOCs(planner_objective);
       ramsey_FOC_equations_dynamic_model.replaceMyEquations(dynamic_model);
-      mod_file_struct.ramsey_eq_nbr = dynamic_model.equation_number() - mod_file_struct.orig_eq_nbr;
     }
 
   dynamic_model.createVariableMapping();
@@ -620,7 +619,7 @@ ModFile::transformPass(bool nostrict, bool stochastic, bool compute_xrefs, bool
     cout << "Found " << dynamic_model.equation_number() << " equation(s)." << endl;
   else
     {
-      cout << "Found " << mod_file_struct.orig_eq_nbr  << " equation(s)." << endl;
+      cout << "Found " << mod_file_struct.ramsey_orig_eq_nbr  << " equation(s)." << endl;
       cout << "Found " << dynamic_model.equation_number() << " FOC equation(s) for Ramsey Problem." << endl;
     }
 
@@ -932,9 +931,9 @@ ModFile::writeMOutput(const string &basename, bool clear_all, bool clear_global,
                 << "  error('DYNARE: Can''t find bytecode DLL. Please compile it or remove the ''bytecode'' option.')" << endl
                 << "end" << endl;
 
-  mOutputFile << "M_.orig_eq_nbr = " << mod_file_struct.orig_eq_nbr << ";" << endl
-              << "M_.eq_nbr = " << dynamic_model.equation_number() << ";" << endl
-              << "M_.ramsey_eq_nbr = " << mod_file_struct.ramsey_eq_nbr << ";" << endl
+  mOutputFile << "M_.eq_nbr = " << dynamic_model.equation_number() << ";" << endl
+              << "M_.ramsey_orig_eq_nbr = " << mod_file_struct.ramsey_orig_eq_nbr << ";" << endl
+              << "M_.ramsey_orig_endo_nbr = " << mod_file_struct.ramsey_orig_endo_nbr << ";" << endl
               << "M_.set_auxiliary_variables = exist(['./+' M_.fname '/set_auxiliary_variables.m'], 'file') == 2;" << endl;
 
   epilogue.writeOutput(mOutputFile);
diff --git a/src/Statement.hh b/src/Statement.hh
index 083a1b9c..ce8e1eed 100644
--- a/src/Statement.hh
+++ b/src/Statement.hh
@@ -129,14 +129,19 @@ struct ModFileStructure
   bool corr_options_statement_present{false};
   //! Whether a Markov Switching DSGE is present
   bool ms_dsge_present{false};
-  //! Stores the original number of equations in the model_block
-  int orig_eq_nbr{0};
-  //! Stores the number of equations added to the Ramsey model
-  int ramsey_eq_nbr{0};
+  /* The number of equations in the model present just before adding the
+     Lagrange multipliers and computing the Ramsey FOC; it is by construction
+     equal to the number of Lagrange multipliers that will be added by the
+     process of computing the FOCs. May be greater than the number of equations
+     written by the user, because some auxiliary variables may have already
+     been added. */
+  int ramsey_orig_eq_nbr {0};
   /* The number of endogenous variables in the model present just before adding
      the Lagrange multipliers and computing the Ramsey FOC; it is by
      construction equal to the number of equations that will be added by the
-     process of computing the FOCs */
+     process of computing the FOCs. May be greater than the number of
+     endogenous declared by the user, because some auxiliary variables may have
+     already been added. */
   int ramsey_orig_endo_nbr {0};
   //! Whether there was a steady_state_model block
   bool steady_state_model_present{false};
-- 
GitLab