From 20431ed312de9e59edc6dbf23c425559db8a6735 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Wed, 17 Jun 2020 16:48:40 +0200 Subject: [PATCH] Block decomposition: in the dynamic file, y and T are now vectors as in non-block mode --- matlab/dr_block.m | 2 +- matlab/dynvars_from_endo_simul.m | 26 +++++++++++++++++++ matlab/lnsrch1_wrapper_one_boundary.m | 14 +++++----- matlab/lnsrch1_wrapper_two_boundaries.m | 12 +++++---- .../solve_block_decomposed_problem.m | 4 ++- matlab/solve_one_boundary.m | 10 +++---- matlab/solve_two_boundaries.m | 4 +-- preprocessor | 2 +- 8 files changed, 51 insertions(+), 23 deletions(-) create mode 100644 matlab/dynvars_from_endo_simul.m diff --git a/matlab/dr_block.m b/matlab/dr_block.m index d65248b77..24243c27c 100644 --- a/matlab/dr_block.m +++ b/matlab/dr_block.m @@ -73,7 +73,7 @@ end if (options_.bytecode) [zz, data]= bytecode('dynamic','evaluate', z, zx, M_.params, dr.ys, 1, data); else - [r, data] = feval([M_.fname '.dynamic'], options_, M_, oo_, z', zx, M_.params, dr.ys, M_.maximum_lag+1, data); + [r, data] = feval([M_.fname '.dynamic'], options_, M_, oo_, dynvars_from_endo_simul(z, M_.maximum_lag+1, M_), zx, M_.params, dr.ys, M_.maximum_lag+1, data); end dr.full_rank = 1; dr.eigval = []; diff --git a/matlab/dynvars_from_endo_simul.m b/matlab/dynvars_from_endo_simul.m new file mode 100644 index 000000000..c8508f535 --- /dev/null +++ b/matlab/dynvars_from_endo_simul.m @@ -0,0 +1,26 @@ +function y2 = dynvars_from_endo_simul(y, it_, M_) +% Given the matrix y of paths for all endogenous (same format as +% oo_.endo_simul), and an iteration number (first simulation period corresponds +% to it_=M_.maximum_lag+1), return a vector of endogenous values in the format +% expected by the dynamic.m file (i.e. whose indices are described by +% M_.lead_lag_incidence) + +% Copyright (C) 2020 Dynare Team +% +% This file is part of Dynare. +% +% Dynare is free software: you can redistribute it and/or modify +% it under the terms of the GNU General Public License as published by +% the Free Software Foundation, either version 3 of the License, or +% (at your option) any later version. +% +% Dynare is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with Dynare. If not, see . + +y2 = y(:, it_+(-M_.maximum_endo_lag:M_.maximum_endo_lead)); +y2 = y2(find(M_.lead_lag_incidence')); diff --git a/matlab/lnsrch1_wrapper_one_boundary.m b/matlab/lnsrch1_wrapper_one_boundary.m index 6a90ab120..6b4797590 100644 --- a/matlab/lnsrch1_wrapper_one_boundary.m +++ b/matlab/lnsrch1_wrapper_one_boundary.m @@ -1,18 +1,17 @@ -function r = lnsrch1_wrapper_one_boundary(ya, y_index, fname, y, x, params, steady_state, T, it_) +function r = lnsrch1_wrapper_one_boundary(ya, ll_index, fname, y, x, params, steady_state, T, it_) % wrapper for solve_one_boundary m-file when it is used with a dynamic % model % % INPUTS % ya [vector] The endogenous of the current block -% y_index [vector of int] The index of the endogenous variables of -% the block +% ll_index [vector] M_.lead_lag_incidence(M_.maximum_endo_lag+1, :) % fname [string] name of the file containing the block % to simulate -% y [matrix] All the endogenous variables of the model +% y [vector] Dynamic endogenous variables of the model % x [matrix] All the exogenous variables of the model % params [vector] All the parameters of the model % steady_state [vector] steady state of the model -% T [matrix] Temporary terms +% T [vector] Temporary terms % OUTPUTS % r [vector] The residuals of the current block % @@ -40,6 +39,5 @@ function r = lnsrch1_wrapper_one_boundary(ya, y_index, fname, y, x, params, stea % You should have received a copy of the GNU General Public License % along with Dynare. If not, see maxit_) if is_dynamic - [r, T, g1] = feval(fname, y, x, params, steady_state, T, it_, false); + [r, T(:, it_), g1] = feval(fname, dynvars_from_endo_simul(y', it_, M), x, params, steady_state, T(:, it_), it_, false); else [r, T, g1] = feval(fname, y, x, params, T); end @@ -203,9 +203,9 @@ for it_=start:incr:finish p = -g1\r ; [ya,f,r,check]=lnsrch1(ya,f,g,p,stpmax, ... 'lnsrch1_wrapper_one_boundary',nn, ... - y_index_eq, options.solve_tolx, y_index_eq, fname, y, x, params, steady_state, T, it_); + y_index_eq, options.solve_tolx, M.lead_lag_incidence(M.maximum_endo_lag+1, :), fname, dynvars_from_endo_simul(y', it_, M), x, params, steady_state, T(:, it_), it_); %% Recompute temporary terms, since they are not given as output of lnsrch1 - [~, T] = feval(fname, y, x, params, steady_state, T, it_, false); + [~, T(:, it_)] = feval(fname, dynvars_from_endo_simul(y', it_, M), x, params, steady_state, T(:, it_), it_, false); dx = ya' - y(it_, index_eq); y(it_, index_eq) = ya'; elseif (is_dynamic && (stack_solve_algo==1 || stack_solve_algo==0)) || (~is_dynamic && options.solve_algo==6) @@ -262,8 +262,8 @@ for it_=start:incr:finish y(y_index_eq) = phat; end if is_dynamic - [r, T, g1] = feval(fname, y, x, params, ... - steady_state, T, it_, false); + [r, T(:, it_), g1] = feval(fname, dynvars_from_endo_simul(y', it_, M), x, params, ... + steady_state, T(:, it_), it_, false); else [r, T, g1] = feval(fname, y, x, params, T); end diff --git a/matlab/solve_two_boundaries.m b/matlab/solve_two_boundaries.m index aef8cf02c..7c221fbe4 100644 --- a/matlab/solve_two_boundaries.m +++ b/matlab/solve_two_boundaries.m @@ -83,7 +83,7 @@ while ~(cvg==1 || iter>maxit_) r = NaN(Blck_size, periods); g1a = spalloc(Blck_size*periods, Blck_size*periods, nze*periods); for it_ = y_kmin+(1:periods) - [r(:, it_-y_kmin), T, g1]=feval(fname, y, x, params, steady_state, T, it_, false); + [r(:, it_-y_kmin), T(:, it_), g1]=feval(fname, dynvars_from_endo_simul(y', it_, M), x, params, steady_state, T(:, it_), it_, false); if periods == 1 g1a = g1(:, Blck_size+(1:Blck_size)); elseif it_ == y_kmin+1 @@ -314,7 +314,7 @@ while ~(cvg==1 || iter>maxit_) g = (ra'*g1a)'; f = 0.5*ra'*ra; p = -g1a\ra; - [yn,f,ra,check]=lnsrch1(ya,f,g,p,stpmax,'lnsrch1_wrapper_two_boundaries',nn,nn, options.solve_tolx, fname, y, y_index,x, params, steady_state, T, periods, y_kmin, Blck_size); + [yn,f,ra,check]=lnsrch1(ya,f,g,p,stpmax,'lnsrch1_wrapper_two_boundaries',nn,nn, options.solve_tolx, fname, y, y_index,x, params, steady_state, T, periods, Blck_size, M); dx = ya - yn; y(1+y_kmin:periods+y_kmin,y_index)=reshape(yn',length(y_index),periods)'; end diff --git a/preprocessor b/preprocessor index 379be6cce..79763911b 160000 --- a/preprocessor +++ b/preprocessor @@ -1 +1 @@ -Subproject commit 379be6ccef55dc38af04edb881bf6aa57bdecd65 +Subproject commit 79763911b2af3cd3ce368afd8a7e604a0bb43715