From 9a32720ac074ab156ec00f8bd610958c511df393 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Fri, 5 Nov 2021 12:22:43 +0100 Subject: [PATCH 1/4] evaluate_steady_state.m: allow debugging of Ramsey equations --- 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 15e4e4249..17e3131ed 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); From ce899af24dc1469e9e3a9ddf88446c4f98f4fc5f Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Fri, 5 Nov 2021 12:27:12 +0100 Subject: [PATCH 2/4] evaluate_static_model.m: provide promised output argument --- matlab/evaluate_static_model.m | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/matlab/evaluate_static_model.m b/matlab/evaluate_static_model.m index cc8a256c9..afb9bc26a 100644 --- a/matlab/evaluate_static_model.m +++ b/matlab/evaluate_static_model.m @@ -20,7 +20,7 @@ function [residuals,check1,jacob] = evaluate_static_model(ys,exo_ss,params,M,opt % SPECIAL REQUIREMENTS % none -% Copyright (C) 2001-2020 Dynare Team +% Copyright (C) 2001-2021 Dynare Team % % This file is part of Dynare. % @@ -61,6 +61,10 @@ else end end else - residuals = feval(fh_static,ys,exo_ss,params); + if nargout<3 + residuals = feval(fh_static,ys,exo_ss,params); + else + [residuals, jacob] = feval(fh_static,ys,exo_ss,params); + end end end From 7cffd34b66361b5458172bdfa4221bf2f601fc92 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Fri, 5 Nov 2021 12:39:06 +0100 Subject: [PATCH 3/4] Ramsey: do not check static model if steady state file is present Check will typically crash due to initialization with 0 --- matlab/dyn_ramsey_static.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matlab/dyn_ramsey_static.m b/matlab/dyn_ramsey_static.m index 67d90b7e2..7f3ae8fb1 100644 --- a/matlab/dyn_ramsey_static.m +++ b/matlab/dyn_ramsey_static.m @@ -43,7 +43,7 @@ options_.steadystate.nocheck = 1; %locally disable checking because Lagrange mul nl_func = @(x) dyn_ramsey_static_1(x,M,options_,oo); % check_static_model is a subfunction -if check_static_model(ys_init,M,options_,oo) && ~options_.steadystate_flag +if ~options_.steadystate_flag && check_static_model(ys_init,M,options_,oo) steady_state = ys_init; return elseif options_.steadystate_flag From 1174cad1f8a1d129ba1c5b47dce17d6145c6b6ad Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Mon, 8 Nov 2021 14:31:24 +0100 Subject: [PATCH 4/4] evaluate_static_model.m: provide jacobian output with bytecode Set to NaN for block --- matlab/evaluate_static_model.m | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/matlab/evaluate_static_model.m b/matlab/evaluate_static_model.m index afb9bc26a..e505ce6ae 100644 --- a/matlab/evaluate_static_model.m +++ b/matlab/evaluate_static_model.m @@ -39,8 +39,14 @@ function [residuals,check1,jacob] = evaluate_static_model(ys,exo_ss,params,M,opt check1 = 0; if options.bytecode - residuals = bytecode('evaluate','static',ys,... + if nargout<3 + [residuals]= bytecode('evaluate','static',ys,... exo_ss, params, ys, 1); + else + [residuals, junk]= bytecode('evaluate','static',ys,... + exo_ss, params, ys, 1); + jacob = junk.g1; + end else fh_static = str2func([M.fname '.static']); if options.block @@ -60,6 +66,9 @@ else [~, ~, T] = feval(fh_static,b,ys,exo_ss,params,T); end end + if nargout==3 + jacob=NaN(length(ys)); + end else if nargout<3 residuals = feval(fh_static,ys,exo_ss,params);