2014-04-09 17:57:17 +02:00
|
|
|
function perfect_foresight_solver()
|
2010-09-21 13:35:55 +02:00
|
|
|
% Computes deterministic simulations
|
2016-03-24 22:42:44 +01:00
|
|
|
%
|
2006-10-29 18:27:48 +01:00
|
|
|
% INPUTS
|
2010-09-21 13:35:55 +02:00
|
|
|
% None
|
2016-03-24 22:42:44 +01:00
|
|
|
%
|
2006-10-29 18:27:48 +01:00
|
|
|
% OUTPUTS
|
2010-11-17 17:09:39 +01:00
|
|
|
% none
|
2016-03-24 22:42:44 +01:00
|
|
|
%
|
2006-10-29 18:27:48 +01:00
|
|
|
% ALGORITHM
|
2016-03-24 22:42:44 +01:00
|
|
|
%
|
2006-10-29 18:27:48 +01:00
|
|
|
% SPECIAL REQUIREMENTS
|
|
|
|
% none
|
2008-08-01 14:40:33 +02:00
|
|
|
|
2022-04-13 13:15:19 +02:00
|
|
|
% Copyright © 1996-2022 Dynare Team
|
2008-08-01 14:40:33 +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/>.
|
2005-02-18 20:54:39 +01:00
|
|
|
|
2010-09-21 13:35:55 +02:00
|
|
|
global M_ options_ oo_
|
2005-02-18 20:54:39 +01:00
|
|
|
|
2014-12-19 16:33:55 +01:00
|
|
|
check_input_arguments(options_, M_, oo_);
|
2014-04-09 17:57:17 +02:00
|
|
|
|
2011-02-10 15:54:23 +01:00
|
|
|
if isempty(options_.scalv) || options_.scalv == 0
|
2013-12-09 15:06:06 +01:00
|
|
|
options_.scalv = oo_.steady_state;
|
2009-12-16 18:17:34 +01:00
|
|
|
end
|
|
|
|
|
2019-04-26 15:36:33 +02:00
|
|
|
periods = options_.periods;
|
|
|
|
|
2013-12-09 15:06:06 +01:00
|
|
|
options_.scalv= 1;
|
2005-02-18 20:54:39 +01:00
|
|
|
|
2013-07-21 00:02:09 +02:00
|
|
|
if options_.debug
|
2018-06-27 17:02:13 +02:00
|
|
|
model_static = str2func([M_.fname,'.static']);
|
2013-07-21 00:02:09 +02:00
|
|
|
for ii=1:size(oo_.exo_simul,1)
|
|
|
|
[residual(:,ii)] = model_static(oo_.steady_state, oo_.exo_simul(ii,:),M_.params);
|
|
|
|
end
|
|
|
|
problematic_periods=find(any(isinf(residual)) | any(isnan(residual)))-M_.maximum_endo_lag;
|
2016-03-24 22:42:44 +01:00
|
|
|
if ~isempty(problematic_periods)
|
2013-07-21 00:02:09 +02:00
|
|
|
period_string=num2str(problematic_periods(1));
|
|
|
|
for ii=2:length(problematic_periods)
|
|
|
|
period_string=[period_string, ', ', num2str(problematic_periods(ii))];
|
|
|
|
end
|
2016-03-24 22:42:44 +01:00
|
|
|
fprintf('\n\nWARNING: Value for the exogenous variable(s) in period(s) %s inconsistent with the static model.\n',period_string);
|
2013-07-21 00:02:09 +02:00
|
|
|
fprintf('WARNING: Check for division by 0.\n')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-13 16:00:17 +01:00
|
|
|
initperiods = 1:M_.maximum_lag;
|
2019-04-26 15:36:33 +02:00
|
|
|
lastperiods = (M_.maximum_lag+periods+1):(M_.maximum_lag+periods+M_.maximum_lead);
|
2015-02-12 15:27:02 +01:00
|
|
|
|
2015-07-23 14:27:55 +02:00
|
|
|
oo_ = perfect_foresight_solver_core(M_,options_,oo_);
|
2015-02-12 15:27:02 +01:00
|
|
|
|
|
|
|
% If simulation failed try homotopy.
|
|
|
|
if ~oo_.deterministic_simulation.status && ~options_.no_homotopy
|
2016-03-24 22:42:44 +01:00
|
|
|
|
2019-09-12 14:28:35 +02:00
|
|
|
if ~options_.noprint
|
|
|
|
fprintf('\nSimulation of the perfect foresight model failed!')
|
|
|
|
fprintf('Switching to a homotopy method...\n')
|
|
|
|
end
|
2016-03-24 22:42:44 +01:00
|
|
|
|
2021-05-28 12:09:02 +02:00
|
|
|
if ~M_.maximum_lag && M_.maximum_lead>0
|
2015-06-04 12:22:48 +02:00
|
|
|
disp('Homotopy not implemented for purely forward models!')
|
|
|
|
disp('Failed to solve the model!')
|
|
|
|
disp('Return with empty oo_.endo_simul.')
|
|
|
|
oo_.endo_simul = [];
|
|
|
|
return
|
|
|
|
end
|
2021-05-28 12:09:02 +02:00
|
|
|
|
|
|
|
if ~M_.maximum_lead && M_.maximum_lag>0
|
2015-06-04 12:22:48 +02:00
|
|
|
disp('Homotopy not implemented for purely backward models!')
|
|
|
|
disp('Failed to solve the model!')
|
|
|
|
disp('Return with empty oo_.endo_simul.')
|
|
|
|
oo_.endo_simul = [];
|
|
|
|
return
|
|
|
|
end
|
2016-03-24 22:42:44 +01:00
|
|
|
|
2021-05-28 12:09:02 +02:00
|
|
|
if ~M_.maximum_lead && ~M_.maximum_lag
|
|
|
|
disp('Homotopy not implemented for purely static models!')
|
|
|
|
disp('Failed to solve the model!')
|
|
|
|
disp('Return with empty oo_.endo_simul.')
|
|
|
|
oo_.endo_simul = [];
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2015-02-18 23:58:37 +01:00
|
|
|
% Disable warnings if homotopy
|
2015-04-22 12:23:20 +02:00
|
|
|
warning_old_state = warning;
|
2015-02-18 23:58:37 +01:00
|
|
|
warning off all
|
|
|
|
% Do not print anything
|
|
|
|
oldverbositylevel = options_.verbosity;
|
|
|
|
options_.verbosity = 0;
|
2016-03-24 22:42:44 +01:00
|
|
|
|
|
|
|
% Set initial paths for the endogenous and exogenous variables.
|
2019-04-26 15:36:33 +02:00
|
|
|
endoinit = repmat(oo_.steady_state, 1,M_.maximum_lag+periods+M_.maximum_lead);
|
|
|
|
exoinit = repmat(oo_.exo_steady_state',M_.maximum_lag+periods+M_.maximum_lead,1);
|
2016-03-24 22:42:44 +01:00
|
|
|
|
|
|
|
% Copy the current paths for the exogenous and endogenous variables.
|
|
|
|
exosim = oo_.exo_simul;
|
2014-04-10 16:38:39 +02:00
|
|
|
endosim = oo_.endo_simul;
|
|
|
|
|
2016-03-24 22:42:44 +01:00
|
|
|
current_weight = 0; % Current weight of target point in convex combination.
|
|
|
|
step = .5; % Set default step size.
|
2014-04-10 16:38:39 +02:00
|
|
|
success_counter = 0;
|
2015-02-12 15:27:02 +01:00
|
|
|
iteration = 0;
|
2014-04-10 16:38:39 +02:00
|
|
|
|
2019-09-12 14:28:35 +02:00
|
|
|
if ~options_.noprint
|
|
|
|
fprintf('Iter. \t | Lambda \t | status \t | Max. residual\n')
|
|
|
|
fprintf('++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n')
|
|
|
|
end
|
2014-04-10 16:38:39 +02:00
|
|
|
while (step > options_.dynatol.x)
|
|
|
|
|
2015-01-07 12:37:07 +01:00
|
|
|
if ~isequal(step,1)
|
|
|
|
options_.verbosity = 0;
|
|
|
|
end
|
|
|
|
|
2015-02-12 15:27:02 +01:00
|
|
|
iteration = iteration+1;
|
2014-04-10 16:38:39 +02:00
|
|
|
new_weight = current_weight + step; % Try this weight, and see if it succeeds
|
2015-02-12 15:27:02 +01:00
|
|
|
|
2014-04-10 16:38:39 +02:00
|
|
|
if new_weight >= 1
|
|
|
|
new_weight = 1; % Don't go beyond target point
|
|
|
|
step = new_weight - current_weight;
|
|
|
|
end
|
|
|
|
|
|
|
|
% Compute convex combination for exo path and initial/terminal endo conditions
|
|
|
|
% But take care of not overwriting the computed part of oo_.endo_simul
|
|
|
|
oo_.exo_simul = exosim*new_weight + exoinit*(1-new_weight);
|
2016-03-24 22:42:44 +01:00
|
|
|
oo_.endo_simul(:,[initperiods, lastperiods]) = new_weight*endosim(:,[initperiods, lastperiods])+(1-new_weight)*endoinit(:,[initperiods, lastperiods]);
|
|
|
|
|
|
|
|
% Detect Nans or complex numbers in the solution.
|
2015-02-13 16:00:17 +01:00
|
|
|
path_with_nans = any(any(isnan(oo_.endo_simul)));
|
|
|
|
path_with_cplx = any(any(~isreal(oo_.endo_simul)));
|
2016-03-24 22:42:44 +01:00
|
|
|
|
|
|
|
if isequal(iteration, 1)
|
|
|
|
% First iteration, same initial guess as in the first call to perfect_foresight_solver_core routine.
|
2019-04-26 15:36:33 +02:00
|
|
|
oo_.endo_simul(:,M_.maximum_lag+1:end-M_.maximum_lead) = endoinit(:,1:periods);
|
2015-02-13 16:00:17 +01:00
|
|
|
elseif path_with_nans || path_with_cplx
|
2016-03-24 22:42:44 +01:00
|
|
|
% If solver failed with NaNs or complex number, use previous solution as an initial guess.
|
2015-05-27 12:22:32 +02:00
|
|
|
oo_.endo_simul(:,M_.maximum_lag+1:end-M_.maximum_lead) = saved_endo_simul(:,1+M_.maximum_lag:end-M_.maximum_lead);
|
2015-02-13 16:00:17 +01:00
|
|
|
end
|
2016-03-24 22:42:44 +01:00
|
|
|
|
|
|
|
% Make a copy of the paths.
|
2014-04-23 16:45:09 +02:00
|
|
|
saved_endo_simul = oo_.endo_simul;
|
|
|
|
|
2016-03-24 22:42:44 +01:00
|
|
|
% Solve for the paths of the endogenous variables.
|
2015-07-23 14:27:55 +02:00
|
|
|
[oo_,me] = perfect_foresight_solver_core(M_,options_,oo_);
|
2015-02-14 11:54:57 +01:00
|
|
|
|
2022-03-23 16:49:05 +01:00
|
|
|
if oo_.deterministic_simulation.status
|
2014-04-10 16:38:39 +02:00
|
|
|
current_weight = new_weight;
|
|
|
|
if current_weight >= 1
|
2019-09-12 14:28:35 +02:00
|
|
|
if ~options_.noprint
|
|
|
|
fprintf('%i \t | %1.5f \t | %s \t | %e\n', iteration, new_weight, 'succeeded', me)
|
|
|
|
end
|
2014-04-10 16:38:39 +02:00
|
|
|
break
|
|
|
|
end
|
|
|
|
success_counter = success_counter + 1;
|
|
|
|
if success_counter >= 3
|
|
|
|
success_counter = 0;
|
|
|
|
step = step * 2;
|
|
|
|
end
|
2019-09-12 14:28:35 +02:00
|
|
|
if ~options_.noprint
|
|
|
|
fprintf('%i \t | %1.5f \t | %s \t | %e\n', iteration, new_weight, 'succeeded', me)
|
|
|
|
end
|
2014-04-10 16:38:39 +02:00
|
|
|
else
|
2016-03-24 22:42:44 +01:00
|
|
|
% If solver failed, then go back.
|
2014-04-23 16:45:09 +02:00
|
|
|
oo_.endo_simul = saved_endo_simul;
|
2014-04-10 16:38:39 +02:00
|
|
|
success_counter = 0;
|
|
|
|
step = step / 2;
|
2019-09-12 14:28:35 +02:00
|
|
|
if ~options_.noprint
|
|
|
|
if isreal(me)
|
|
|
|
fprintf('%i \t | %1.5f \t | %s \t | %e\n', iteration, new_weight, 'failed', me)
|
|
|
|
else
|
|
|
|
fprintf('%i \t | %1.5f \t | %s \t | %s\n', iteration, new_weight, 'failed', 'Complex')
|
|
|
|
end
|
2015-02-21 13:58:52 +01:00
|
|
|
end
|
2014-04-10 16:38:39 +02:00
|
|
|
end
|
|
|
|
end
|
2019-09-12 14:28:35 +02:00
|
|
|
if ~options_.noprint
|
|
|
|
fprintf('++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\n')
|
|
|
|
end
|
2015-02-18 23:58:37 +01:00
|
|
|
options_.verbosity = oldverbositylevel;
|
2015-04-22 12:23:20 +02:00
|
|
|
warning(warning_old_state);
|
2014-04-10 16:38:39 +02:00
|
|
|
end
|
|
|
|
|
2022-05-17 21:28:16 +02:00
|
|
|
%If simulated paths are complex, take real part and recompute the residuals to check whether this is actually a solution
|
2019-06-24 17:52:09 +02:00
|
|
|
if ~isreal(oo_.endo_simul(:)) % cannot happen with bytecode or the perfect_foresight_problem DLL
|
2022-05-17 21:28:16 +02:00
|
|
|
ny = size(oo_.endo_simul, 1);
|
2019-06-24 17:52:09 +02:00
|
|
|
if M_.maximum_lag > 0
|
|
|
|
y0 = real(oo_.endo_simul(:, M_.maximum_lag));
|
2019-04-26 15:36:33 +02:00
|
|
|
else
|
2019-06-24 17:52:09 +02:00
|
|
|
y0 = NaN(ny, 1);
|
2019-04-26 15:36:33 +02:00
|
|
|
end
|
2019-06-24 17:52:09 +02:00
|
|
|
if M_.maximum_lead > 0
|
|
|
|
yT = real(oo_.endo_simul(:, M_.maximum_lag+periods+1));
|
|
|
|
else
|
|
|
|
yT = NaN(ny, 1);
|
|
|
|
end
|
2022-05-17 21:28:16 +02:00
|
|
|
if M_.maximum_lag~=0 && M_.maximum_lead~=0
|
|
|
|
yy = real(oo_.endo_simul(:,M_.maximum_lag+(1:periods)));
|
|
|
|
residuals = perfect_foresight_problem(yy(:), y0, yT, oo_.exo_simul, M_.params, oo_.steady_state, periods, M_, options_);
|
|
|
|
else
|
|
|
|
%The perfect_foresight_problem MEX only works on models with lags and leads
|
|
|
|
i_cols = find(M_.lead_lag_incidence');
|
|
|
|
residuals=NaN(ny,periods);
|
|
|
|
yy=real(oo_.endo_simul);
|
|
|
|
for it = (M_.maximum_lag+1):(M_.maximum_lag+periods)
|
|
|
|
residuals(:,it) = feval([M_.fname '.dynamic'],yy(i_cols), oo_.exo_simul, M_.params, oo_.steady_state, it);
|
|
|
|
i_cols = i_cols + ny;
|
|
|
|
end
|
|
|
|
residuals=residuals(:);
|
|
|
|
end
|
2019-06-24 17:52:09 +02:00
|
|
|
|
2016-03-23 21:20:43 +01:00
|
|
|
if max(abs(residuals))< options_.dynatol.f
|
2022-03-23 16:49:05 +01:00
|
|
|
oo_.deterministic_simulation.status = true;
|
2016-03-23 21:20:43 +01:00
|
|
|
oo_.endo_simul=real(oo_.endo_simul);
|
|
|
|
else
|
2022-03-23 16:49:05 +01:00
|
|
|
oo_.deterministic_simulation.status = false;
|
2016-03-23 21:20:43 +01:00
|
|
|
disp('Simulation terminated with imaginary parts in the residuals or endogenous variables.')
|
2017-05-16 15:10:20 +02:00
|
|
|
end
|
2016-03-23 21:20:43 +01:00
|
|
|
end
|
|
|
|
|
2022-03-23 16:49:05 +01:00
|
|
|
if oo_.deterministic_simulation.status
|
2019-09-12 14:28:35 +02:00
|
|
|
if ~options_.noprint
|
|
|
|
fprintf('Perfect foresight solution found.\n\n')
|
|
|
|
end
|
2014-04-10 16:38:39 +02:00
|
|
|
else
|
2019-09-12 14:28:35 +02:00
|
|
|
fprintf('Failed to solve perfect foresight model\n\n')
|
2014-04-10 16:38:39 +02:00
|
|
|
end
|
|
|
|
|
2019-09-10 17:02:20 +02:00
|
|
|
dyn2vec(M_, oo_, options_);
|
2014-04-10 16:38:39 +02:00
|
|
|
|
2022-01-21 16:45:45 +01:00
|
|
|
if isfield(oo_, 'initval_series') && ~isempty(oo_.initval_series)
|
|
|
|
initial_period = oo_.initval_series.dates(1)+(M_.orig_maximum_lag-1);
|
|
|
|
elseif ~isdates(options_.initial_period) && isnan(options_.initial_period)
|
2014-07-16 17:02:58 +02:00
|
|
|
initial_period = dates(1,1);
|
|
|
|
else
|
|
|
|
initial_period = options_.initial_period;
|
|
|
|
end
|
|
|
|
|
2022-01-16 16:49:06 +01:00
|
|
|
ts = dseries([transpose(oo_.endo_simul(1:M_.orig_endo_nbr,:)), oo_.exo_simul], initial_period, [M_.endo_names(1:M_.orig_endo_nbr); M_.exo_names]);
|
2022-01-21 16:45:45 +01:00
|
|
|
|
|
|
|
if isfield(oo_, 'initval_series') && ~isempty(oo_.initval_series)
|
|
|
|
names = ts.name;
|
|
|
|
ts = merge(oo_.initval_series{names{:}}, ts);
|
|
|
|
end
|
|
|
|
|
2019-05-07 18:31:47 +02:00
|
|
|
assignin('base', 'Simulated_time_series', ts);
|
2022-01-21 16:45:45 +01:00
|
|
|
|
2020-03-03 11:45:38 +01:00
|
|
|
if oo_.deterministic_simulation.status
|
|
|
|
oo_.gui.ran_perfect_foresight = true;
|
2022-03-23 16:49:05 +01:00
|
|
|
end
|