Use dynare_solve to simulate purely forward deterministic models.

fix-nonlinear-solvers
Stéphane Adjemian (Ryûk) 2022-04-10 09:39:28 +02:00
parent ef2bb4e669
commit 23a72d7aaa
Signed by: stepan
GPG Key ID: 295C1FE89E17EB3C
5 changed files with 42 additions and 11 deletions

View File

@ -0,0 +1,27 @@
function [r, J] = dynamic_forward_model_for_simulation(z, dynamicmodel, ylead, x, params, steady_state, it_)
% Copyright © 2022 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 <https://www.gnu.org/licenses/>.
if nargout>1
% Compute residuals and jacobian of the full dynamic model.
[r, J] = feval(dynamicmodel, [z; ylead], x, params, steady_state, it_);
J = J(:,1:length(z)); % Remove derivatives with respect to shocks.
else
% Compute residuals.
r = feval(dynamicmodel, [z; ylead], x, params, steady_state, it_);
end

View File

@ -30,7 +30,7 @@ if ismember(options.solve_algo, [12,14]) && ~M.possible_to_use_solve_algo_12_14
error(M.message_solve_algo_12_14)
end
dynamicmodel = str2func([M.fname,'.dynamic']);
dynamicmodel = str2func(sprintf('%s.%s', M.fname, 'dynamic'));
dynamicmodel_s = str2func('dynamic_backward_model_for_simulation');
info.status = true;

View File

@ -1,7 +1,7 @@
function [endogenousvariables, info] = sim1_purely_forward(endogenousvariables, exogenousvariables, steadystate, M, options)
% Performs deterministic simulation of a purely forward model
% Copyright (C) 2012-2017 Dynare Team
% Copyright © 2012-2022 Dynare Team
%
% This file is part of Dynare.
%
@ -25,21 +25,25 @@ if ny0 ~= M.endo_nbr
error('All endogenous variables must appear at the current period!')
end
dynamicmodel = str2func([M.fname,'.dynamic']);
dynamicmodel = str2func(sprintf('%s.%s', M.fname, 'dynamic'));
dynamicmodel_s = str2func('dynamic_forward_model_for_simulation');
info.status = 1;
for it = options.periods:-1:1
yf = endogenousvariables(:,it+1); % Values at next period, also used as guess value for current period
yf1 = yf(iyf);
% TODO Call dynare_solve instead.
[tmp, check] = solve1(dynamicmodel, [yf; yf1], 1:M.endo_nbr, 1:M.endo_nbr, ...
1, options.gstep, options.dynatol.f, ...
options.dynatol.x, options.simul.maxit, [], ...
options.debug, exogenousvariables, M.params, steadystate, ...
it+M.maximum_lag);
if ismember(options.solve_algo, [12,14])
[tmp, check, ~, ~, errorcode] = dynare_solve(dynamicmodel_s, yf, options, M.isloggedlhs, M.isauxdiffloggedrhs, M.endo_names, M.lhs, ...
dynamicmodel, yf(iyf), exogenousvariables, M.params, steadystate, it);
else
[tmp, check, ~, ~, errorcode] = dynare_solve(dynamicmodel_s, yf, options, ...
dynamicmodel, yf(iyf), exogenousvariables, M.params, steadystate, it);
end
if check
info.status = 0;
if option.debug
dprintf('sim1_purely_forward: Nonlinear solver routine failed with errorcode=%i in period %i.', errorcode, it)
end
end
endogenousvariables(:,it) = tmp(1:M.endo_nbr);
end

View File

@ -27,7 +27,7 @@ if ismember(options.solve_algo, [12,14]) && ~M.possible_to_use_solve_algo_12_14
error(M.message_solve_algo_12_14)
end
dynamicmodel = str2func([M.fname,'.dynamic']);
dynamicmodel = str2func(sprintf('%s.%s', M.fname, 'dynamic'));
dynamicmodel_s = str2func('dynamic_static_model_for_simulation');
info.status = true;