From 3c1087a369163cd70453a28c079732ac8345a30e Mon Sep 17 00:00:00 2001 From: Willi Mutschler Date: Fri, 20 Nov 2020 22:18:03 +0100 Subject: [PATCH] Fix for sylvester3a for purely forward-looking models MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In purely forward-looking models ghx is empty and sylvester3a gives a "Operands to the || and && operators must be convertible to logical scalar values" error, as e becomes a "1×0 empty double row vector" and not a logical. --- matlab/sylvester3a.m | 4 +- tests/Makefile.am | 1 + .../forward_looking/forward_looking.mod | 70 +++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100755 tests/identification/forward_looking/forward_looking.mod diff --git a/matlab/sylvester3a.m b/matlab/sylvester3a.m index f286c8bdf..cddcbc1ef 100644 --- a/matlab/sylvester3a.m +++ b/matlab/sylvester3a.m @@ -1,7 +1,7 @@ function [x0, flag]=sylvester3a(x0,a,b,c,dd) % solves iteratively ax+bxc=d -% Copyright (C) 2005-2017 Dynare Team +% Copyright (C) 2005-2017,2020 Dynare Team % % This file is part of Dynare. % @@ -25,7 +25,7 @@ for j=1:size(dd,3) d = a_1*dd(:,:,j); e = 1; iter = 1; - while e > 1e-8 && iter < 500 + while all(e > 1e-8) && iter < 500 %use all() to get a logical in case e is empty x = d-b*x0(:,:,j)*c; e = max(max(abs(x-x0(:,:,j)))); x0(:,:,j) = x; diff --git a/tests/Makefile.am b/tests/Makefile.am index ff4b8a4ee..297de6211 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -217,6 +217,7 @@ MODFILES = \ identification/rbc_ident/rbc_ident_std_as_structural_par.mod \ identification/rbc_ident/rbc_ident_varexo_only.mod \ identification/correlated_errors/fs2000_corr.mod \ + identification/forward_looking/forward_looking.mod \ simul/example1.mod \ simul/Solow_no_varexo.mod \ simul/simul_ZLB_purely_forward.mod \ diff --git a/tests/identification/forward_looking/forward_looking.mod b/tests/identification/forward_looking/forward_looking.mod new file mode 100755 index 000000000..bf7c18d3b --- /dev/null +++ b/tests/identification/forward_looking/forward_looking.mod @@ -0,0 +1,70 @@ +% Forward-looking example model from Koop, Pesaran, Smith (2013, JBES) +% created by Willi Mutschler (@wmutschl, willi@mutschler.eu) +% ========================================================================= +% 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 . +% ========================================================================= +var r x p; +varexo e_M e_D e_S; +varobs r x p; + +parameters PSI TAU BETA KAPPA; + +PSI=1.1; +TAU=2; +BETA=0.9; +KAPPA=0.6; + +model; +r = PSI*p + e_M; +x = x(+1) - 1/TAU*(r-p(+1)) + e_D; +p = BETA*p(+1) + KAPPA*x + e_S; +end; + +shocks; +var e_M = 1; +var e_D = 1; +var e_S = 1; +end; + +steady; +check; + +estimated_params; +PSI, 1.1; +TAU, 2; +BETA, 0.9; +KAPPA, 0.6; +end; + +identification; %this triggers sylvester3a with empty ghx + +% as a side note, we have the true solution: +% [r;x;p] = TRUE_SOLUTION*[e_M;e_D;e_S] (ghx is empty) +A = [1 0 -PSI; 1/TAU 1 0; 0 -KAPPA 1]; +TRUE_SOLUTION1 = inv(A); +TRUE_SOLUTION2 = 1/(KAPPA*PSI/TAU +1)*[1 KAPPA*PSI PSI; + -1/TAU 1 -PSI/TAU; + -KAPPA/TAU KAPPA 1]; +% note that BETA drops out from the solution + +if max(max(abs(TRUE_SOLUTION1 - oo_.dr.ghu))) > 1e-15 + error('Something wrong with perturbation'); +end +if max(max(abs(TRUE_SOLUTION2 - oo_.dr.ghu))) > 1e-15 + error('Something wrong with perturbation'); +end \ No newline at end of file