LMMCP / linear perfect foresight: fix bug for models with a single equation

The routines use the find() function applied to a subset of columns of the
Jacobian, which in this case is a row vector. When passed a row vector, find()
returns row vectors (while it returns column vectors when passed a column
vector or a matrix). This case was not correctly handled.
time-shift
Sébastien Villemot 2020-10-20 17:34:11 +02:00
parent df58037feb
commit 87cc519321
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
2 changed files with 42 additions and 2 deletions

View File

@ -16,7 +16,7 @@ function [residuals,JJacobian] = linear_perfect_foresight_problem(y, dynamicjaco
% SPECIAL REQUIREMENTS % SPECIAL REQUIREMENTS
% None. % None.
% Copyright (C) 2015-2019 Dynare Team % Copyright (C) 2015-2020 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
@ -55,15 +55,35 @@ for it = maximum_lag+(1:T)
if nargout == 2 if nargout == 2
if T==1 && it==maximum_lag+1 if T==1 && it==maximum_lag+1
[rows, cols, vals] = find(dynamicjacobian(:,i_cols_0)); [rows, cols, vals] = find(dynamicjacobian(:,i_cols_0));
if size(dynamicjacobian, 1) == 1 % find() will return row vectors in this case
rows = rows';
cols = cols';
vals = vals';
end
iJacobian{1} = [rows, i_cols_J0(cols), vals]; iJacobian{1} = [rows, i_cols_J0(cols), vals];
elseif it == maximum_lag+1 elseif it == maximum_lag+1
[rows,cols,vals] = find(dynamicjacobian(:,i_cols_1)); [rows,cols,vals] = find(dynamicjacobian(:,i_cols_1));
if size(dynamicjacobian, 1) == 1 % find() will return row vectors in this case
rows = rows';
cols = cols';
vals = vals';
end
iJacobian{1} = [offset+rows, i_cols_J1(cols), vals]; iJacobian{1} = [offset+rows, i_cols_J1(cols), vals];
elseif it == maximum_lag+T elseif it == maximum_lag+T
[rows,cols,vals] = find(dynamicjacobian(:,i_cols_T)); [rows,cols,vals] = find(dynamicjacobian(:,i_cols_T));
if size(dynamicjacobian, 1) == 1 % find() will return row vectors in this case
rows = rows';
cols = cols';
vals = vals';
end
iJacobian{T} = [offset+rows, i_cols_J(i_cols_T(cols)), vals]; iJacobian{T} = [offset+rows, i_cols_J(i_cols_T(cols)), vals];
else else
[rows,cols,vals] = find(dynamicjacobian(:,i_cols_j)); [rows,cols,vals] = find(dynamicjacobian(:,i_cols_j));
if size(dynamicjacobian, 1) == 1 % find() will return row vectors in this case
rows = rows';
cols = cols';
vals = vals';
end
iJacobian{it-maximum_lag} = [offset+rows, i_cols_J(cols), vals]; iJacobian{it-maximum_lag} = [offset+rows, i_cols_J(cols), vals];
i_cols_J = i_cols_J + ny; i_cols_J = i_cols_J + ny;
end end

View File

@ -44,7 +44,7 @@ function [residuals,JJacobian] = perfect_foresight_mcp_problem(y, dynamic_functi
% SPECIAL REQUIREMENTS % SPECIAL REQUIREMENTS
% None. % None.
% Copyright (C) 1996-2019 Dynare Team % Copyright (C) 1996-2020 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
@ -83,15 +83,35 @@ for it = maximum_lag+(1:T)
residuals(i_rows) = res(eq_index); residuals(i_rows) = res(eq_index);
if T==1 && it==maximum_lag+1 if T==1 && it==maximum_lag+1
[rows, cols, vals] = find(jacobian(:,i_cols_0)); [rows, cols, vals] = find(jacobian(:,i_cols_0));
if size(jacobian, 1) == 1 % find() will return row vectors in this case
rows = rows';
cols = cols';
vals = vals';
end
iJacobian{1} = [rows, i_cols_J0(cols), vals]; iJacobian{1} = [rows, i_cols_J0(cols), vals];
elseif it == maximum_lag+1 elseif it == maximum_lag+1
[rows,cols,vals] = find(jacobian(eq_index,i_cols_1)); [rows,cols,vals] = find(jacobian(eq_index,i_cols_1));
if numel(eq_index) == 1 % find() will return row vectors in this case
rows = rows';
cols = cols';
vals = vals';
end
iJacobian{1} = [offset+rows, i_cols_J1(cols), vals]; iJacobian{1} = [offset+rows, i_cols_J1(cols), vals];
elseif it == maximum_lag+T elseif it == maximum_lag+T
[rows,cols,vals] = find(jacobian(eq_index,i_cols_T)); [rows,cols,vals] = find(jacobian(eq_index,i_cols_T));
if numel(eq_index) == 1 % find() will return row vectors in this case
rows = rows';
cols = cols';
vals = vals';
end
iJacobian{T} = [offset+rows, i_cols_J(i_cols_T(cols)), vals]; iJacobian{T} = [offset+rows, i_cols_J(i_cols_T(cols)), vals];
else else
[rows,cols,vals] = find(jacobian(eq_index,i_cols_j)); [rows,cols,vals] = find(jacobian(eq_index,i_cols_j));
if numel(eq_index) == 1 % find() will return row vectors in this case
rows = rows';
cols = cols';
vals = vals';
end
iJacobian{it-maximum_lag} = [offset+rows, i_cols_J(cols), vals]; iJacobian{it-maximum_lag} = [offset+rows, i_cols_J(cols), vals];
i_cols_J = i_cols_J + ny; i_cols_J = i_cols_J + ny;
end end