2014-04-09 17:57:17 +02:00
|
|
|
function perfect_foresight_solver()
|
2010-09-21 13:35:55 +02:00
|
|
|
% Computes deterministic simulations
|
2006-10-29 18:27:48 +01:00
|
|
|
%
|
|
|
|
% INPUTS
|
2010-09-21 13:35:55 +02:00
|
|
|
% None
|
2006-10-29 18:27:48 +01:00
|
|
|
%
|
|
|
|
% OUTPUTS
|
2010-11-17 17:09:39 +01:00
|
|
|
% none
|
|
|
|
%
|
2006-10-29 18:27:48 +01:00
|
|
|
% ALGORITHM
|
2010-11-17 17:09:39 +01:00
|
|
|
%
|
2006-10-29 18:27:48 +01:00
|
|
|
% SPECIAL REQUIREMENTS
|
|
|
|
% none
|
2008-08-01 14:40:33 +02:00
|
|
|
|
2014-04-09 17:57:17 +02:00
|
|
|
% Copyright (C) 1996-2014 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
|
|
|
|
% along with Dynare. If not, see <http://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-05-18 21:12:38 +02:00
|
|
|
if options_.stack_solve_algo < 0 || options_.stack_solve_algo > 7
|
|
|
|
error('PERFECT_FORESIGHT_SOLVER: stack_solve_algo must be between 0 and 7')
|
2010-10-22 12:09:53 +02:00
|
|
|
end
|
|
|
|
|
2012-06-04 17:23:14 +02:00
|
|
|
if ~options_.block && ~options_.bytecode && options_.stack_solve_algo ~= 0 ...
|
2014-05-18 21:12:38 +02:00
|
|
|
&& options_.stack_solve_algo ~= 6 && options_.stack_solve_algo ~= 7
|
2014-04-09 17:57:17 +02:00
|
|
|
error('PERFECT_FORESIGHT_SOLVER: you must use stack_solve_algo=0 or stack_solve_algo=6 when not using block nor bytecode option')
|
2010-10-22 12:09:53 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if options_.block && ~options_.bytecode && options_.stack_solve_algo == 5
|
2014-04-09 17:57:17 +02:00
|
|
|
error('PERFECT_FORESIGHT_SOLVER: you can''t use stack_solve_algo = 5 without bytecode option')
|
2010-10-22 12:09:53 +02:00
|
|
|
end
|
|
|
|
|
2012-06-04 17:23:14 +02:00
|
|
|
if (options_.block || options_.bytecode) && options_.stack_solve_algo == 6
|
2014-04-09 17:57:17 +02:00
|
|
|
error('PERFECT_FORESIGHT_SOLVER: you can''t use stack_solve_algo = 6 with block or bytecode option')
|
2012-06-04 17:23:14 +02:00
|
|
|
end
|
|
|
|
|
2013-11-04 10:54:45 +01:00
|
|
|
if isoctave && options_.stack_solve_algo == 2
|
2014-04-09 17:57:17 +02:00
|
|
|
error('PERFECT_FORESIGHT_SOLVER: you can''t use stack_solve_algo = 2 under Octave')
|
2010-10-22 12:09:53 +02:00
|
|
|
end
|
|
|
|
|
2005-02-18 20:54:39 +01:00
|
|
|
|
2014-04-09 17:57:17 +02:00
|
|
|
if isempty(oo_.endo_simul) || any(size(oo_.endo_simul) ~= [ M_.endo_nbr, M_.maximum_endo_lag+options_.periods+M_.maximum_endo_lead ])
|
|
|
|
error('PERFECT_FORESIGHT_SOLVER: ''oo_.endo_simul'' has wrong size. Did you run ''perfect_foresight_setup'' ?')
|
2005-02-18 20:54:39 +01:00
|
|
|
end
|
|
|
|
|
2014-04-09 17:57:17 +02:00
|
|
|
if isempty(oo_.exo_simul) || any(size(oo_.exo_simul) ~= [ M_.maximum_lag+options_.periods+M_.maximum_lead, M_.exo_nbr ])
|
|
|
|
error('PERFECT_FORESIGHT_SOLVER: ''oo_.exo_simul'' has wrong size. Did you run ''perfect_foresight_setup'' ?')
|
2009-12-16 18:17:34 +01:00
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
model_static = str2func([M_.fname,'_static']);
|
|
|
|
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;
|
|
|
|
if ~isempty(problematic_periods)
|
|
|
|
period_string=num2str(problematic_periods(1));
|
|
|
|
for ii=2:length(problematic_periods)
|
|
|
|
period_string=[period_string, ', ', num2str(problematic_periods(ii))];
|
|
|
|
end
|
|
|
|
fprintf('\n\nWARNING: Value for the exogenous variable(s) in period(s) %s inconsistent with the static model.\n',period_string);
|
|
|
|
fprintf('WARNING: Check for division by 0.\n')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-10 16:38:39 +02:00
|
|
|
% Effectively compute simulation, possibly with homotopy
|
|
|
|
if options_.no_homotopy
|
|
|
|
simulation_core;
|
|
|
|
else
|
|
|
|
exosim = oo_.exo_simul;
|
|
|
|
exoinit = repmat(oo_.exo_steady_state',M_.maximum_lag+options_.periods+M_.maximum_lead,1);
|
|
|
|
endosim = oo_.endo_simul;
|
|
|
|
endoinit = repmat(oo_.steady_state, 1,M_.maximum_endo_lag+options_.periods+M_.maximum_endo_lead);
|
|
|
|
|
|
|
|
current_weight = 0; % Current weight of target point in convex combination
|
|
|
|
step = 1;
|
|
|
|
success_counter = 0;
|
|
|
|
|
|
|
|
while (step > options_.dynatol.x)
|
|
|
|
|
|
|
|
new_weight = current_weight + step; % Try this weight, and see if it succeeds
|
|
|
|
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);
|
|
|
|
endocombi = endosim*new_weight + endoinit*(1-new_weight);
|
|
|
|
oo_.endo_simul(:,1:M_.maximum_endo_lag) = endocombi(:,1:M_.maximum_endo_lag);
|
|
|
|
oo_.endo_simul(:,(end-M_.maximum_endo_lead):end) = endocombi(:,(end-M_.maximum_endo_lead):end);
|
|
|
|
|
2014-04-23 16:45:09 +02:00
|
|
|
saved_endo_simul = oo_.endo_simul;
|
|
|
|
|
2014-04-10 16:38:39 +02:00
|
|
|
simulation_core;
|
|
|
|
|
|
|
|
if oo_.deterministic_simulation.status == 1
|
|
|
|
current_weight = new_weight;
|
|
|
|
if current_weight >= 1
|
|
|
|
break
|
|
|
|
end
|
|
|
|
success_counter = success_counter + 1;
|
|
|
|
if success_counter >= 3
|
|
|
|
success_counter = 0;
|
|
|
|
step = step * 2;
|
2014-04-23 15:04:59 +02:00
|
|
|
disp([ 'Homotopy step succeeded, doubling step size (completed ' sprintf('%.1f', current_weight*100) '%, step size ' sprintf('%.3g', step) ')' ])
|
2014-04-10 16:38:39 +02:00
|
|
|
else
|
2014-04-23 15:04:59 +02:00
|
|
|
disp([ 'Homotopy step succeeded (completed ' sprintf('%.1f', current_weight*100) '%, step size ' sprintf('%.3g', step) ')' ])
|
2014-04-10 16:38:39 +02:00
|
|
|
end
|
|
|
|
else
|
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;
|
2014-04-23 15:04:59 +02:00
|
|
|
disp([ 'Homotopy step failed, halving step size (completed ' sprintf('%.1f', current_weight*100) '%, step size ' sprintf('%.3g', step) ')' ])
|
2014-04-10 16:38:39 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if oo_.deterministic_simulation.status == 1
|
|
|
|
disp('Perfect foresight solution found.')
|
|
|
|
else
|
|
|
|
disp('Failed to solve perfect foresight model')
|
|
|
|
end
|
|
|
|
|
|
|
|
dyn2vec;
|
|
|
|
|
2014-07-16 17:02:58 +02:00
|
|
|
if isnan(options_.initial_period)
|
|
|
|
initial_period = dates(1,1);
|
|
|
|
else
|
|
|
|
initial_period = options_.initial_period;
|
|
|
|
end
|
|
|
|
|
|
|
|
ts = dseries(transpose(oo_.endo_simul),initial_period,cellstr(M_.endo_names));
|
2014-04-10 16:38:39 +02:00
|
|
|
assignin('base', 'Simulated_time_series', ts);
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function simulation_core()
|
|
|
|
|
|
|
|
global M_ oo_ options_
|
|
|
|
|
2009-12-16 18:17:34 +01:00
|
|
|
if(options_.block)
|
|
|
|
if(options_.bytecode)
|
2010-09-24 12:52:58 +02:00
|
|
|
[info, oo_.endo_simul] = bytecode('dynamic');
|
2013-01-18 17:05:10 +01:00
|
|
|
if info == 1
|
|
|
|
oo_.deterministic_simulation.status = 0;
|
|
|
|
else
|
|
|
|
oo_.deterministic_simulation.status = 1;
|
2013-12-09 15:06:06 +01:00
|
|
|
end
|
2010-09-24 12:52:58 +02:00
|
|
|
mexErrCheck('bytecode', info);
|
2009-12-16 18:17:34 +01:00
|
|
|
else
|
|
|
|
eval([M_.fname '_dynamic']);
|
2013-12-09 15:06:06 +01:00
|
|
|
end
|
2009-12-16 18:17:34 +01:00
|
|
|
else
|
2010-01-22 11:03:29 +01:00
|
|
|
if(options_.bytecode)
|
2010-09-24 12:52:58 +02:00
|
|
|
[info, oo_.endo_simul]=bytecode('dynamic');
|
2013-12-13 16:49:00 +01:00
|
|
|
if info == 1
|
|
|
|
oo_.deterministic_simulation.status = 0;
|
|
|
|
else
|
|
|
|
oo_.deterministic_simulation.status = 1;
|
|
|
|
end;
|
2010-09-24 12:52:58 +02:00
|
|
|
mexErrCheck('bytecode', info);
|
2009-12-16 18:17:34 +01:00
|
|
|
else
|
2012-06-04 16:14:01 +02:00
|
|
|
if M_.maximum_endo_lead == 0 % Purely backward model
|
|
|
|
sim1_purely_backward;
|
|
|
|
elseif M_.maximum_endo_lag == 0 % Purely forward model
|
2012-06-04 15:25:01 +02:00
|
|
|
sim1_purely_forward;
|
2012-06-04 16:14:01 +02:00
|
|
|
else % General case
|
2014-05-18 21:12:38 +02:00
|
|
|
if options_.stack_solve_algo == 0
|
|
|
|
sim1;
|
|
|
|
elseif options_.stack_solve_algo == 6
|
|
|
|
sim1_lbj;
|
|
|
|
elseif options_.stack_solve_algo == 7
|
|
|
|
periods = options_.periods;
|
|
|
|
if ~isfield(options_.lmmcp,'lb')
|
|
|
|
[lb,ub,pfm.eq_index] = get_complementarity_conditions(M_);
|
|
|
|
options_.lmmcp.lb = repmat(lb,periods,1);
|
|
|
|
options_.lmmcp.ub = repmat(ub,periods,1);
|
|
|
|
end
|
|
|
|
|
|
|
|
y = oo_.endo_simul;
|
|
|
|
y0 = y(:,1);
|
|
|
|
yT = y(:,periods+2);
|
|
|
|
z = y(:,2:periods+1);
|
|
|
|
illi = M_.lead_lag_incidence';
|
|
|
|
[i_cols,~,i_cols_j] = find(illi(:));
|
|
|
|
illi = illi(:,2:3);
|
|
|
|
[i_cols_J1,~,i_cols_1] = find(illi(:));
|
|
|
|
i_cols_T = nonzeros(M_.lead_lag_incidence(1:2,:)');
|
|
|
|
[y,info] = dynare_solve(@perfect_foresight_problem,z(:),1, ...
|
|
|
|
str2func([M_.fname '_dynamic']),y0,yT, ...
|
|
|
|
oo_.exo_simul,M_.params,oo_.steady_state, ...
|
|
|
|
options_.periods,M_.endo_nbr,i_cols, ...
|
|
|
|
i_cols_J1, i_cols_1, i_cols_T, i_cols_j, ...
|
|
|
|
M_.NNZDerivatives(1));
|
|
|
|
oo_.endo_simul = [y0 reshape(y,M_.endo_nbr,periods) yT];
|
2014-05-14 09:29:45 +02:00
|
|
|
if info == 1
|
|
|
|
oo_.deterministic_simulation.status = 0;
|
|
|
|
else
|
|
|
|
oo_.deterministic_simulation.status = 1;
|
|
|
|
end;
|
2012-06-04 17:23:14 +02:00
|
|
|
end
|
2012-06-04 16:14:01 +02:00
|
|
|
end
|
2013-12-09 15:06:06 +01:00
|
|
|
end
|
|
|
|
end
|
2005-02-18 20:54:39 +01:00
|
|
|
|
2014-04-10 16:38:39 +02:00
|
|
|
end
|