From 146e8c353f44f461c14a9d932495d1826971fc1c Mon Sep 17 00:00:00 2001
From: Johannes Pfeifer <jpfeifer@gmx.de>
Date: Fri, 5 Nov 2021 12:22:43 +0100
Subject: [PATCH] evaluate_steady_state.m: allow debugging of Ramsey equations

(cherry picked from commit 9a32720ac074ab156ec00f8bd610958c511df393)
---
 matlab/evaluate_steady_state.m | 30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)

diff --git a/matlab/evaluate_steady_state.m b/matlab/evaluate_steady_state.m
index 15e4e42497..17e3131ed8 100644
--- a/matlab/evaluate_steady_state.m
+++ b/matlab/evaluate_steady_state.m
@@ -57,7 +57,11 @@ if options.ramsey_policy
         [ys,params,info] = evaluate_steady_state_file(ys_init,exo_ss,M, ...
                                                       options,steadystate_check_flag);
         %test whether it solves model conditional on the instruments
-        resids = evaluate_static_model(ys,exo_ss,params,M,options);
+        if ~options.debug
+            resids = evaluate_static_model(ys,exo_ss,params,M,options);
+        else
+            [resids, ~ , jacob]= evaluate_static_model(ys,exo_ss,params,M,options);
+        end
         n_multipliers=M.ramsey_eq_nbr;
         nan_indices=find(isnan(resids(n_multipliers+1:end)));
 
@@ -132,6 +136,30 @@ if options.ramsey_policy
                 fprintf('%s\n',M.endo_names{nanrow(iter)});
             end
         end
+        nan_indices_mult=find(isnan(resids(1:n_multipliers)));
+        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');            
+        end
+        [infrow,infcol]=find(isinf(jacob));
+
+        if ~isempty(infrow)
+            fprintf('\nevaluate_steady_state: The Jacobian of the dynamic model contains Inf. The problem is associated with:\n\n')
+            display_problematic_vars_Jacobian(infrow,infcol,M,ys,'static','evaluate_steady_state: ')
+        end
+        
+        if ~isreal(jacob)
+            [imagrow,imagcol]=find(abs(imag(jacob))>1e-15);
+            fprintf('\nevaluate_steady_state: The Jacobian of the dynamic model contains imaginary parts. The problem arises from: \n\n')
+            display_problematic_vars_Jacobian(imagrow,imagcol,M,ys,'static','evaluate_steady_state: ')
+        end
+        
+        [nanrow,nancol]=find(isnan(jacob));
+        if ~isempty(nanrow)
+            fprintf('\nevaluate_steady_state: The Jacobian of the dynamic model contains NaN. The problem is associated with:\n\n')
+            display_problematic_vars_Jacobian(nanrow,nancol,M,ys,'static','evaluate_steady_state: ')
+        end
+
     end
     %either if no steady state file or steady state file without problems
     [ys,params,info] = dyn_ramsey_static(ys_init,M,options,oo);
-- 
GitLab