From 4b76d76175bd7c37fce035b0a56ac05d153163e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Tue, 23 Nov 2021 16:31:50 +0100 Subject: [PATCH] evaluate_static_model.m: fix the block without bytecode case The routine would not put elements at the right place in the residuals vector (it would use variable indices instead of equation indices). Also change the routine so that it computes a residual on evaluated equations (instead of systematically returning zero for those). Finally, simplify resid.m by calling this routine instead of embedding a similar code. Ref. #1823 --- matlab/evaluate_static_model.m | 17 ++++++----------- matlab/resid.m | 30 ++++-------------------------- 2 files changed, 10 insertions(+), 37 deletions(-) diff --git a/matlab/evaluate_static_model.m b/matlab/evaluate_static_model.m index e505ce6ae..e3758378d 100644 --- a/matlab/evaluate_static_model.m +++ b/matlab/evaluate_static_model.m @@ -53,18 +53,13 @@ else residuals = zeros(M.endo_nbr,1); T = NaN(M.block_structure_stat.tmp_nbr, 1); for b = 1:length(M.block_structure_stat.block) - mfsb = M.block_structure_stat.block(b).variable; - % blocks that can be directly evaluated (mfsb is empty) - % have zero residuals by construction - if M.block_structure_stat.block(b).Simulation_Type ~= 1 && ... - M.block_structure_stat.block(b).Simulation_Type ~= 2 - [r, ~, T] = feval(fh_static,b,ys,exo_ss,params,T); - residuals(mfsb) = r; - else - %need to evaluate the recursive blocks to compute the - %temporary terms - [~, ~, T] = feval(fh_static,b,ys,exo_ss,params,T); + [r, yy, T] = feval(fh_static,b,ys,exo_ss,params,T); + if M.block_structure_stat.block(b).Simulation_Type == 1 || ... % evaluateForward + M.block_structure_stat.block(b).Simulation_Type == 2 % evaluateBackward + vidx = M.block_structure_stat.block(b).variable; + r = yy(vidx) - ys(vidx); end + residuals(M.block_structure_stat.block(b).equation) = r; end if nargout==3 jacob=NaN(length(ys)); diff --git a/matlab/resid.m b/matlab/resid.m index 22cc2f874..19d316b56 100644 --- a/matlab/resid.m +++ b/matlab/resid.m @@ -67,31 +67,9 @@ if options_.steadystate_flag end % Compute the residuals -if options_.block && ~options_.bytecode - z = zeros(M_.endo_nbr,1); - T = NaN(M_.block_structure_stat.tmp_nbr, 1); - for i = 1:length(M_.block_structure_stat.block) - [r, yy, T, g] = feval([M_.fname '.static'],... - i,... - oo_.steady_state,... - [oo_.exo_steady_state; ... - oo_.exo_det_steady_state], M_.params, T); - if M_.block_structure_stat.block(i).Simulation_Type == 1 || ... % evaluateForward - M_.block_structure_stat.block(i).Simulation_Type == 2 % evaluateBackward - vidx = M_.block_structure_stat.block(i).variable; - r = yy(vidx) - oo_.steady_state(vidx); - end - idx = M_.block_structure_stat.block(i).equation; - z(idx) = r; - end -elseif options_.bytecode - z = bytecode('evaluate','static'); -else - z = feval([M_.fname '.static'],... - oo_.steady_state,... - [oo_.exo_steady_state; ... - oo_.exo_det_steady_state], M_.params); -end +z = evaluate_static_model(oo_.steady_state, [oo_.exo_steady_state; ... + oo_.exo_det_steady_state], ... + M_.params, M_, options_); M_.Sigma_e = Sigma_e; @@ -123,4 +101,4 @@ if nargout == 0 skipline(2) end -oo_.steady_state = steady_state_old; \ No newline at end of file +oo_.steady_state = steady_state_old;