From ffe6eb4d4079e630fb5bf1dda48fedf7bddbbc9e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Tue, 28 Mar 2023 18:54:52 +0200
Subject: [PATCH] Preprocessor: overhaul of M_ fields counting
 Ramsey/discretionary orig endos/eqs
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

– new fields: M_.ramsey_orig_{eq,endo}_nbr
– drop M_.ramsey_eq_nbr and M_.orig_eq_nbr
– new field: M_.discretionary_orig_eq_nbr
---
 matlab/display_problematic_vars_Jacobian.m |  4 ++--
 matlab/dyn_ramsey_static.m                 | 15 +++++++-------
 matlab/evaluate_steady_state.m             | 24 ++++++++++------------
 matlab/model_diagnostics.m                 |  3 +--
 matlab/resid.m                             | 20 +++++++++++++-----
 preprocessor                               |  2 +-
 6 files changed, 37 insertions(+), 31 deletions(-)

diff --git a/matlab/display_problematic_vars_Jacobian.m b/matlab/display_problematic_vars_Jacobian.m
index 07fe5ecf13..f204b421a8 100644
--- a/matlab/display_problematic_vars_Jacobian.m
+++ b/matlab/display_problematic_vars_Jacobian.m
@@ -16,7 +16,7 @@ function []=display_problematic_vars_Jacobian(problemrow,problemcol,M_,x,type,ca
 %   none.
 %
 
-% Copyright © 2014-2021 Dynare Team
+% Copyright © 2014-2023 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -37,7 +37,7 @@ skipline();
 if nargin<6
     caller_string='';
 end
-initial_aux_eq_nbr=M_.ramsey_eq_nbr;
+initial_aux_eq_nbr=M_.ramsey_orig_endo_nbr;
 if strcmp(type,'dynamic')
     for ii=1:length(problemrow)
         if problemcol(ii)>max(M_.lead_lag_incidence)
diff --git a/matlab/dyn_ramsey_static.m b/matlab/dyn_ramsey_static.m
index f23db49d82..c9bb884796 100644
--- a/matlab/dyn_ramsey_static.m
+++ b/matlab/dyn_ramsey_static.m
@@ -100,10 +100,9 @@ params = M.params;
 endo_nbr = M.endo_nbr;
 endo_names = M.endo_names;
 orig_endo_nbr = M.orig_endo_nbr;
-aux_vars_type = [M.aux_vars.type];
-orig_endo_aux_nbr = orig_endo_nbr + min(find(aux_vars_type == 6)) - 1;
-orig_eq_nbr = M.orig_eq_nbr;
-inst_nbr = orig_endo_aux_nbr - orig_eq_nbr;
+ramsey_orig_endo_nbr = M.ramsey_orig_endo_nbr;
+ramsey_orig_eq_nbr = M.ramsey_orig_eq_nbr;
+inst_nbr = ramsey_orig_endo_nbr - ramsey_orig_eq_nbr;
 % indices of Lagrange multipliers
 fname = M.fname;
 
@@ -161,7 +160,7 @@ end
 % the auxiliary variables before the Lagrange multipliers are treated
 % as ordinary endogenous variables
 A = feval([fname '.ramsey_multipliers_static_g1'], xx, exo_ss, params, M.ramsey_multipliers_static_g1_sparse_rowval, M.ramsey_multipliers_static_g1_sparse_colval, M.ramsey_multipliers_static_g1_sparse_colptr);
-y = res(1:orig_endo_aux_nbr);
+y = res(1:ramsey_orig_endo_nbr);
 mult = -A\y;
 
 resids1 = y+A*mult;
@@ -175,13 +174,13 @@ end
 if options_.steadystate_flag
     resids = r1;
 else
-    resids = [res(orig_endo_aux_nbr+(1:orig_endo_nbr-inst_nbr)); r1];
+    resids = [res(ramsey_orig_endo_nbr+(1:orig_endo_nbr-inst_nbr)); r1];
 end
 rJ = [];
 if needs_set_auxiliary_variables
-    steady_state = s_a_v_func([xx(1:orig_endo_aux_nbr); mult]);
+    steady_state = s_a_v_func([xx(1:ramsey_orig_endo_nbr); mult]);
 else
-    steady_state = [xx(1:orig_endo_aux_nbr); mult];
+    steady_state = [xx(1:ramsey_orig_endo_nbr); mult];
 end
 
 function result = check_static_model(ys,M,options_,oo)
diff --git a/matlab/evaluate_steady_state.m b/matlab/evaluate_steady_state.m
index b2be3d0545..3ac5164e83 100644
--- a/matlab/evaluate_steady_state.m
+++ b/matlab/evaluate_steady_state.m
@@ -82,8 +82,7 @@ if options.ramsey_policy
         else
             [resids, ~ , jacob]= evaluate_static_model(ys,exo_ss,params,M,options);
         end
-        n_ramsey_equations=M.ramsey_eq_nbr;
-        nan_indices=find(isnan(resids(n_ramsey_equations+1:n_ramsey_equations+M.orig_eq_nbr))); % 
+        nan_indices=find(isnan(resids(M.ramsey_orig_endo_nbr+(1:M.ramsey_orig_eq_nbr))));
 
         if ~isempty(nan_indices)
             if options.debug
@@ -106,7 +105,7 @@ if options.ramsey_policy
             return
         end
 
-        if any(imag(ys(n_ramsey_equations+1:n_ramsey_equations+M.orig_eq_nbr)))
+        if any(imag(ys(M.ramsey_orig_endo_nbr+(1:M.ramsey_orig_eq_nbr))))
             if options.debug
                 fprintf('\nevaluate_steady_state: The steady state file computation for the Ramsey problem resulted in complex numbers.\n')
                 fprintf('evaluate_steady_state: The steady state was computed conditional on the following initial instrument values: \n')
@@ -121,7 +120,7 @@ if options.ramsey_policy
             return
         end
 
-        if max(abs(resids(n_ramsey_equations+1:n_ramsey_equations+M.orig_eq_nbr))) > options.solve_tolf %does it solve for all variables except for the Lagrange multipliers
+        if max(abs(resids(M.ramsey_orig_endo_nbr+(1:M.ramsey_orig_eq_nbr)))) > options.solve_tolf %does it solve for all variables except for the Lagrange multipliers
             if options.debug
                 fprintf('\nevaluate_steady_state: The steady state file does not solve the steady state for the Ramsey problem.\n')
                 fprintf('evaluate_steady_state: Conditional on the following instrument values: \n')
@@ -129,9 +128,9 @@ if options.ramsey_policy
                     fprintf('\t %s \t %f \n',options.instruments{ii},ys_init(strmatch(options.instruments{ii},M.endo_names,'exact')))
                 end
                 fprintf('evaluate_steady_state: the following equations have non-zero residuals: \n')
-                for ii=n_ramsey_equations+1:M.endo_nbr
+                for ii=M.ramsey_orig_endo_nbr+1:M.endo_nbr
                     if abs(resids(ii)) > options.solve_tolf
-                        fprintf('\t Equation number %d: %f\n',ii-n_ramsey_equations, resids(ii))
+                        fprintf('\t Equation number %d: %f\n',ii-M.ramsey_orig_endo_nbr, resids(ii))
                     end
                 end
                 skipline(2)
@@ -165,7 +164,7 @@ if options.ramsey_policy
             end
         end
         if steadystate_flag
-            nan_indices_mult=find(isnan(resids(1:n_ramsey_equations)));
+            nan_indices_mult=find(isnan(resids(1:M.ramsey_orig_endo_nbr)));
             if any(nan_indices_mult)
                 fprintf('evaluate_steady_state: The steady state results NaN for auxiliary equation %u.\n',nan_indices_mult);
                 fprintf('evaluate_steady_state: This is often a sign of problems.\n');
@@ -200,9 +199,8 @@ if options.ramsey_policy
     %check whether steady state really solves the model
     resids = evaluate_static_model(ys,exo_ss,params,M,options);
 
-    n_ramsey_equations=M.ramsey_eq_nbr;
-    nan_indices_multiplier=find(isnan(resids(1:n_ramsey_equations)));
-    nan_indices=find(isnan(resids(n_ramsey_equations+1:end)));
+    nan_indices_multiplier=find(isnan(resids(1:M.ramsey_orig_endo_nbr)));
+    nan_indices=find(isnan(resids(M.ramsey_orig_endo_nbr+1:end)));
 
     if ~isempty(nan_indices)
         if options.debug
@@ -248,14 +246,14 @@ if options.ramsey_policy
                 fprintf('\t %s \t %f \n',options.instruments{i},ys(strmatch(options.instruments{i},M.endo_names,'exact')))
             end
             fprintf('evaluate_steady_state: The following equations have non-zero residuals: \n')
-            for ii=1:n_ramsey_equations
+            for ii=1:M.ramsey_orig_endo_nbr
                 if abs(resids(ii)) > options.solve_tolf/100
                     fprintf('\t Auxiliary Ramsey equation number %d: %f\n',ii, resids(ii))
                 end
             end
-            for ii=n_ramsey_equations+1:M.endo_nbr
+            for ii=M.ramsey_orig_endo_nbr+1:M.endo_nbr
                 if abs(resids(ii)) > options.solve_tolf/100
-                    fprintf('\t Equation number %d: %f\n',ii-n_ramsey_equations, resids(ii))
+                    fprintf('\t Equation number %d: %f\n',ii-M.ramsey_orig_endo_nbr, resids(ii))
                 end
             end
             skipline(2)
diff --git a/matlab/model_diagnostics.m b/matlab/model_diagnostics.m
index 752d1416a8..845bc85860 100644
--- a/matlab/model_diagnostics.m
+++ b/matlab/model_diagnostics.m
@@ -41,8 +41,7 @@ if options.ramsey_policy
     %test whether specification matches
     inst_nbr = size(options.instruments,1);
     if inst_nbr~=0
-        orig_endo_aux_nbr = M.orig_endo_nbr + min(find([M.aux_vars.type] == 6)) - 1;
-        implied_inst_nbr = orig_endo_aux_nbr - M.orig_eq_nbr;
+        implied_inst_nbr = M.ramsey_orig_endo_nbr - M.ramsey_orig_eq_nbr;
         if inst_nbr>implied_inst_nbr
             warning('You have specified more steady state instruments than there are omitted equations. While there are use cases for this setup, it is rather unusual. Check whether this is desired.')
         elseif inst_nbr<implied_inst_nbr
diff --git a/matlab/resid.m b/matlab/resid.m
index b7586ccea1..620ace4488 100644
--- a/matlab/resid.m
+++ b/matlab/resid.m
@@ -12,7 +12,7 @@ function z = resid(options_resid_)
 % SPECIAL REQUIREMENTS
 %    none
 
-% Copyright © 2001-2022 Dynare Team
+% Copyright © 2001-2023 Dynare Team
 %
 % This file is part of Dynare.
 %
@@ -93,15 +93,25 @@ if nargout == 0
     fprintf('Residuals of the static equations%s:',disp_string)
     skipline()
     any_non_zero_residual = false;
-    for i=1:M_.orig_eq_nbr
-        if abs(z(i+M_.ramsey_eq_nbr)) < options_.solve_tolf/100
+    if options_.ramsey_policy
+        first_eq = M_.ramsey_orig_endo_nbr+1;
+        last_eq = M_.ramsey_orig_endo_nbr+M_.ramsey_orig_eq_nbr;
+    elseif options_.discretionary_policy
+        first_eq = 1;
+        last_eq = M_.discretionary_orig_eq_nbr;
+    else
+        first_eq = 1;
+        last_eq = M_.orig_endo_nbr;
+    end
+    for i=first_eq:last_eq
+        if abs(z(i)) < options_.solve_tolf/100
             tmp = 0;
         else
-            tmp = z(i+M_.ramsey_eq_nbr);
+            tmp = z(i);
             any_non_zero_residual = true;
         end
         if istag
-            tg = tags(cell2mat(tags(:,1)) == i+M_.ramsey_eq_nbr,2:3); % all tags for equation i
+            tg = tags(cell2mat(tags(:,1)) == i,2:3); % all tags for equation i
             ind = strmatch('name', cellstr( tg(:,1) ) );
         end
         if ~(non_zero && tmp == 0)
diff --git a/preprocessor b/preprocessor
index 70192aec71..4282c98527 160000
--- a/preprocessor
+++ b/preprocessor
@@ -1 +1 @@
-Subproject commit 70192aec718e5f1e13f1513a7a23e48c31f30714
+Subproject commit 4282c98527817ed587d6df58b96a5d4432aa0072
-- 
GitLab