2015-07-23 14:27:55 +02:00
|
|
|
function [oo_, maxerror] = perfect_foresight_solver_core(M_, options_, oo_)
|
2019-04-26 15:36:33 +02:00
|
|
|
|
2016-07-05 19:41:24 +02:00
|
|
|
% Core function calling solvers for perfect foresight model
|
|
|
|
%
|
2017-05-16 15:10:20 +02:00
|
|
|
% INPUTS
|
2016-07-05 19:41:24 +02:00
|
|
|
% - M_ [struct] contains a description of the model.
|
|
|
|
% - options_ [struct] contains various options.
|
|
|
|
% - oo_ [struct] contains results
|
|
|
|
%
|
2017-05-16 15:10:20 +02:00
|
|
|
% OUTPUTS
|
2016-07-05 19:41:24 +02:00
|
|
|
% - oo_ [struct] contains results
|
|
|
|
% - maxerror [double] contains the maximum absolute error
|
2015-07-23 14:27:55 +02:00
|
|
|
|
2021-04-20 16:53:54 +02:00
|
|
|
% Copyright (C) 2015-2021 Dynare Team
|
2015-07-23 14:27:55 +02:00
|
|
|
%
|
|
|
|
% 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
|
2021-06-09 17:33:48 +02:00
|
|
|
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
2015-07-23 14:27:55 +02:00
|
|
|
|
2015-12-22 15:07:38 +01:00
|
|
|
if options_.lmmcp.status
|
|
|
|
options_.stack_solve_algo=7;
|
|
|
|
options_.solve_algo = 10;
|
|
|
|
end
|
|
|
|
|
2019-04-26 15:36:33 +02:00
|
|
|
periods = options_.periods;
|
|
|
|
|
2015-07-23 14:27:55 +02:00
|
|
|
if options_.linear_approximation && ~(isequal(options_.stack_solve_algo,0) || isequal(options_.stack_solve_algo,7))
|
2019-04-26 15:36:33 +02:00
|
|
|
error('perfect_foresight_solver: Option linear_approximation is only available with option stack_solve_algo equal to 0 or 7.')
|
2015-07-23 14:27:55 +02:00
|
|
|
end
|
|
|
|
|
2019-07-04 18:37:27 +02:00
|
|
|
if options_.endogenous_terminal_period && options_.stack_solve_algo ~= 0
|
|
|
|
error('perfect_foresight_solver: option endogenous_terminal_period is only available with option stack_solve_algo equal to 0')
|
|
|
|
end
|
|
|
|
|
2019-04-26 15:36:33 +02:00
|
|
|
if options_.linear && (isequal(options_.stack_solve_algo, 0) || isequal(options_.stack_solve_algo, 7))
|
|
|
|
options_.linear_approximation = true;
|
2015-07-23 14:27:55 +02:00
|
|
|
end
|
2015-02-15 16:49:33 +01:00
|
|
|
|
2015-02-14 11:54:57 +01:00
|
|
|
if options_.block
|
|
|
|
if options_.bytecode
|
|
|
|
try
|
2020-01-10 17:55:57 +01:00
|
|
|
oo_.endo_simul = bytecode('dynamic', oo_.endo_simul, oo_.exo_simul, M_.params, repmat(oo_.steady_state,1, periods+2), periods);
|
2015-07-23 14:27:55 +02:00
|
|
|
oo_.deterministic_simulation.status = true;
|
2020-01-10 17:55:57 +01:00
|
|
|
catch ME
|
|
|
|
disp(ME.message)
|
|
|
|
if options_.no_homotopy
|
|
|
|
error('Error in bytecode')
|
|
|
|
end
|
|
|
|
oo_.deterministic_simulation.status = false;
|
2015-02-14 11:54:57 +01:00
|
|
|
end
|
|
|
|
else
|
2020-06-02 17:45:28 +02:00
|
|
|
oo_ = solve_block_decomposed_problem(options_, M_, oo_);
|
2015-02-14 11:54:57 +01:00
|
|
|
end
|
|
|
|
else
|
|
|
|
if options_.bytecode
|
|
|
|
try
|
2020-01-10 17:55:57 +01:00
|
|
|
oo_.endo_simul = bytecode('dynamic', oo_.endo_simul, oo_.exo_simul, M_.params, repmat(oo_.steady_state, 1, periods+2), periods);
|
2015-07-23 14:27:55 +02:00
|
|
|
oo_.deterministic_simulation.status = true;
|
2020-01-10 17:55:57 +01:00
|
|
|
catch ME
|
|
|
|
disp(ME.message)
|
|
|
|
if options_.no_homotopy
|
|
|
|
error('Error in bytecode')
|
|
|
|
end
|
|
|
|
oo_.deterministic_simulation.status = false;
|
2015-07-23 14:27:55 +02:00
|
|
|
end
|
2015-02-14 11:54:57 +01:00
|
|
|
else
|
2021-05-28 12:09:02 +02:00
|
|
|
if M_.maximum_endo_lead == 0 && M_.maximum_endo_lag>0 && ~options_.lmmcp.status % Purely backward model
|
2015-12-16 15:05:39 +01:00
|
|
|
[oo_.endo_simul, oo_.deterministic_simulation] = ...
|
|
|
|
sim1_purely_backward(oo_.endo_simul, oo_.exo_simul, oo_.steady_state, M_, options_);
|
2021-05-28 12:09:02 +02:00
|
|
|
elseif M_.maximum_endo_lag == 0 && M_.maximum_endo_lead>0 && ~options_.lmmcp.status % Purely forward model
|
|
|
|
[oo_.endo_simul, oo_.deterministic_simulation] = ...
|
|
|
|
sim1_purely_forward(oo_.endo_simul, oo_.exo_simul, oo_.steady_state, M_, options_);
|
|
|
|
elseif M_.maximum_endo_lag == 0 && M_.maximum_endo_lead == 0 && ~options_.lmmcp.status % Purely static model
|
|
|
|
[oo_.endo_simul, oo_.deterministic_simulation] = ...
|
|
|
|
sim1_purely_static(oo_.endo_simul, oo_.exo_simul, oo_.steady_state, M_, options_);
|
2015-02-14 11:54:57 +01:00
|
|
|
else % General case
|
2015-12-28 17:40:58 +01:00
|
|
|
switch options_.stack_solve_algo
|
|
|
|
case 0
|
2015-07-23 14:27:55 +02:00
|
|
|
if options_.linear_approximation
|
2015-12-28 16:59:12 +01:00
|
|
|
[oo_.endo_simul, oo_.deterministic_simulation] = ...
|
|
|
|
sim1_linear(oo_.endo_simul, oo_.exo_simul, oo_.steady_state, oo_.exo_steady_state, M_, options_);
|
2015-07-23 14:27:55 +02:00
|
|
|
else
|
2015-12-15 17:53:13 +01:00
|
|
|
[oo_.endo_simul, oo_.deterministic_simulation] = ...
|
|
|
|
sim1(oo_.endo_simul, oo_.exo_simul, oo_.steady_state, M_, options_);
|
2015-07-23 14:27:55 +02:00
|
|
|
end
|
2015-12-28 17:40:58 +01:00
|
|
|
case 6
|
2016-05-24 22:32:02 +02:00
|
|
|
if options_.linear_approximation
|
2015-12-28 17:40:58 +01:00
|
|
|
error('Invalid value of stack_solve_algo option!')
|
|
|
|
end
|
2015-12-16 11:30:15 +01:00
|
|
|
[oo_.endo_simul, oo_.deterministic_simulation] = ...
|
|
|
|
sim1_lbj(oo_.endo_simul, oo_.exo_simul, oo_.steady_state, M_, options_);
|
2015-12-28 17:40:58 +01:00
|
|
|
case 7
|
2015-07-23 14:27:55 +02:00
|
|
|
if options_.linear_approximation
|
2020-12-18 13:59:29 +01:00
|
|
|
if isequal(options_.solve_algo, 10)
|
|
|
|
if options_.ramsey_policy && isfield(M_,'ramsey_model_constraints') && ~isempty(M_.ramsey_model_constraints)
|
|
|
|
warning('Due to ramsey_constraints you should not specify your model as model(linear)!')
|
2021-04-20 16:53:54 +02:00
|
|
|
elseif options_.lmmcp.status
|
|
|
|
warning('Due to lmmcp option, you should not specify your model as model(linear)!')
|
2020-12-18 13:59:29 +01:00
|
|
|
else
|
|
|
|
warning('It would be more efficient to set option solve_algo equal to 0!')
|
|
|
|
end
|
2015-12-28 17:40:58 +01:00
|
|
|
end
|
2015-12-28 17:33:06 +01:00
|
|
|
[oo_.endo_simul, oo_.deterministic_simulation] = ...
|
|
|
|
solve_stacked_linear_problem(oo_.endo_simul, oo_.exo_simul, oo_.steady_state, oo_.exo_steady_state, M_, options_);
|
2015-02-14 11:54:57 +01:00
|
|
|
else
|
2015-12-28 17:33:06 +01:00
|
|
|
[oo_.endo_simul, oo_.deterministic_simulation] = ...
|
|
|
|
solve_stacked_problem(oo_.endo_simul, oo_.exo_simul, oo_.steady_state, M_, options_);
|
2015-02-14 11:54:57 +01:00
|
|
|
end
|
2015-12-28 17:40:58 +01:00
|
|
|
otherwise
|
|
|
|
error('Invalid value of stack_solve_algo option!')
|
2015-02-14 11:54:57 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-23 14:27:55 +02:00
|
|
|
if nargout>1
|
2020-10-20 18:25:55 +02:00
|
|
|
if options_.lmmcp.status
|
|
|
|
maxerror = NaN; % Could be improved
|
|
|
|
elseif options_.block && ~options_.bytecode
|
2015-07-23 14:27:55 +02:00
|
|
|
maxerror = oo_.deterministic_simulation.error;
|
|
|
|
else
|
|
|
|
if options_.bytecode
|
2020-01-10 17:55:57 +01:00
|
|
|
residuals = bytecode('dynamic','evaluate', oo_.endo_simul, oo_.exo_simul, M_.params, oo_.steady_state, 1);
|
2015-07-23 14:27:55 +02:00
|
|
|
else
|
2019-06-24 17:52:09 +02:00
|
|
|
if M_.maximum_lag > 0
|
|
|
|
y0 = oo_.endo_simul(:, M_.maximum_lag);
|
|
|
|
else
|
|
|
|
y0 = NaN(ny, 1);
|
|
|
|
end
|
|
|
|
if M_.maximum_lead > 0
|
|
|
|
yT = oo_.endo_simul(:, M_.maximum_lag+periods+1);
|
|
|
|
else
|
|
|
|
yT = NaN(ny, 1);
|
|
|
|
end
|
|
|
|
yy = oo_.endo_simul(:,M_.maximum_lag+(1:periods));
|
|
|
|
|
2019-07-09 14:05:45 +02:00
|
|
|
residuals = perfect_foresight_problem(yy(:), y0, yT, oo_.exo_simul, M_.params, oo_.steady_state, periods, M_, options_);
|
2015-07-23 14:27:55 +02:00
|
|
|
end
|
|
|
|
maxerror = max(max(abs(residuals)));
|
|
|
|
end
|
2015-02-14 11:54:57 +01:00
|
|
|
end
|