Allow linear_approximation option with stack_solve_algo=7.

time-shift
Stéphane Adjemian (Charybdis) 2015-07-07 15:08:38 +02:00
parent 27922a349c
commit 17f3583151
2 changed files with 110 additions and 8 deletions

View File

@ -0,0 +1,69 @@
function [residuals,JJacobian] = perfect_foresight_problem(y, dynamicjacobian, Y0, YT, ...
exo_simul, params, steady_state, ...
maximum_lag, T, ny, i_cols, ...
i_cols_J1, i_cols_1, i_cols_T, ...
i_cols_j,nnzJ,jendo,jexog)
% function [residuals,JJacobian] = perfect_foresight_problem(x, model_dynamic, Y0, YT,exo_simul,
% params, steady_state, maximum_lag, periods, ny, i_cols,i_cols_J1, i_cols_1,
% i_cols_T, i_cols_j, nnzA)
% computes the residuals and th Jacobian matrix
% for a perfect foresight problem over T periods.
%
% INPUTS
% ...
% OUTPUTS
% ...
% ALGORITHM
% ...
%
% SPECIAL REQUIREMENTS
% None.
% Copyright (C) 2015 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 <http://www.gnu.org/licenses/>.
YY = [Y0; y; YT];
residuals = zeros(T*ny,1);
z = zeros(columns(dynamicjacobian), 1);
if nargout == 2
JJacobian = sparse([],[],[],T*ny,T*ny,T*nnzJ);
end
i_rows = 1:ny;
i_cols_J = i_cols;
for it = maximum_lag+(1:T)
z(jendo) = YY(i_cols);
z(jexog) = transpose(exo_simul(it,:));
residuals(i_rows) = dynamicjacobian*z;
if nargout == 2
if it == 2
JJacobian(i_rows,i_cols_J1) = dynamicjacobian(:,i_cols_1);
elseif it == T + 1
JJacobian(i_rows,i_cols_J(i_cols_T)) = dynamicjacobian(:,i_cols_T);
else
JJacobian(i_rows,i_cols_J) = dynamicjacobian(:,i_cols_j);
i_cols_J = i_cols_J + ny;
end
end
i_rows = i_rows + ny;
i_cols = i_cols + ny;
end

View File

@ -18,7 +18,7 @@ function [oo_, maxerror] = simulation_core(options_, M_, oo_)
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
if options_.linear_approximation && ~isequal(options_.stack_solve_algo,0)
if options_.linear_approximation && ~(isequal(options_.stack_solve_algo,0) || isequal(options_.stack_solve_algo,7))
error('perfect_foresight_solver: Option linear_approximation is only available with option stack_solve_algo equal to 0.')
end
@ -91,12 +91,41 @@ else
illi = illi(:,2:3);
[i_cols_J1,junk,i_cols_1] = find(illi(:));
i_cols_T = nonzeros(M_.lead_lag_incidence(1:2,:)');
[y,info] = dynare_solve(@perfect_foresight_problem,z(:),options_, ...
str2func([M_.fname '_dynamic']),y0,yT, ...
oo_.exo_simul,M_.params,oo_.steady_state, ...
M_.maximum_lag,options_.periods,M_.endo_nbr,i_cols, ...
i_cols_J1, i_cols_1, i_cols_T, i_cols_j, ...
M_.NNZDerivatives(1));
if options_.linear_approximation
y_steady_state = oo_.steady_state;
x_steady_state = transpose(oo_.exo_steady_state);
ip = find(M_.lead_lag_incidence(1,:)');
ic = find(M_.lead_lag_incidence(2,:)');
in = find(M_.lead_lag_incidence(3,:)');
% Evaluate the Jacobian of the dynamic model at the deterministic steady state.
model_dynamic = str2func([M_.fname,'_dynamic']);
[d1,jacobian] = model_dynamic(y_steady_state([ip; ic; in]), x_steady_state, M_.params, y_steady_state, 1);
% Check that the dynamic model was evaluated at the steady state.
if max(abs(d1))>1e-12
error('Jacobian is not evaluated at the steady state!')
end
nyp = nnz(M_.lead_lag_incidence(1,:)) ;
ny0 = nnz(M_.lead_lag_incidence(2,:)) ;
nyf = nnz(M_.lead_lag_incidence(3,:)) ;
nd = nyp+ny0+nyf; % size of y (first argument passed to the dynamic file).
jexog = transpose(nd+(1:M_.exo_nbr));
jendo = transpose(1:nd);
z = bsxfun(@minus,z,y_steady_state);
x = bsxfun(@minus,oo_.exo_simul,x_steady_state);
[y,info] = dynare_solve(@linear_perfect_foresight_problem,z(:), options_, ...
jacobian, y0-y_steady_state, yT-y_steady_state, ...
x, M_.params, y_steady_state, ...
M_.maximum_lag, options_.periods, M_.endo_nbr, i_cols, ...
i_cols_J1, i_cols_1, i_cols_T, i_cols_j, ...
M_.NNZDerivatives(1),jendo,jexog);
else
[y,info] = dynare_solve(@perfect_foresight_problem,z(:),options_, ...
str2func([M_.fname '_dynamic']),y0,yT, ...
oo_.exo_simul,M_.params,oo_.steady_state, ...
M_.maximum_lag,options_.periods,M_.endo_nbr,i_cols, ...
i_cols_J1, i_cols_1, i_cols_T, i_cols_j, ...
M_.NNZDerivatives(1));
end
if all(imag(y)<.1*options_.dynatol.f)
if ~isreal(y)
y = real(y);
@ -104,7 +133,11 @@ else
else
info = 1;
end
oo_.endo_simul = [y0 reshape(y,M_.endo_nbr,periods) yT];
if options_.linear_approximation
oo_.endo_simul = [y0 bsxfun(@plus,reshape(y,M_.endo_nbr,periods),y_steady_state) yT];
else
oo_.endo_simul = [y0 reshape(y,M_.endo_nbr,periods) yT];
end
if info == 1
oo_.deterministic_simulation.status = false;
else