106 lines
4.1 KiB
Matlab
106 lines
4.1 KiB
Matlab
function [endo_simul,status, maxerror] = perfect_foresight_simulation_core(M_, options_, oo_)
|
|
endo_simul = [];
|
|
status = false;
|
|
maxerror = [];
|
|
|
|
if options_.block
|
|
if options_.bytecode
|
|
try
|
|
[info, tmp] = bytecode('dynamic', oo_.endo_simul, oo_.exo_simul, M_.params, repmat(oo_.steady_state,1,options_.periods+2), options_.periods);
|
|
catch
|
|
info = 0;
|
|
end
|
|
if info
|
|
status = false;
|
|
else
|
|
endo_simul = tmp;
|
|
status = true;
|
|
end
|
|
mexErrCheck('bytecode', info);
|
|
else
|
|
eval([M_.fname '_dynamic']);
|
|
end
|
|
else
|
|
if options_.bytecode
|
|
try
|
|
[info, tmp] = bytecode('dynamic', oo_.endo_simul, oo_.exo_simul, M_.params, repmat(oo_.steady_state,1,options_.periods+2), options_.periods);
|
|
catch
|
|
info = 0;
|
|
end
|
|
if info
|
|
status = false;
|
|
else
|
|
endo_simul = tmp;
|
|
status = true;
|
|
end;
|
|
mexErrCheck('bytecode', info);
|
|
else
|
|
if M_.maximum_endo_lead == 0 % Purely backward model
|
|
oo = sim1_purely_backward(options_, M_, oo_);
|
|
endo_simul = oo.endo_simul;
|
|
status = oo.deterministic_simulation.status;
|
|
elseif M_.maximum_endo_lag == 0 % Purely forward model
|
|
oo = sim1_purely_forward(options_, M_, oo_);
|
|
endo_simul = oo.endo_simul;
|
|
status = oo.deterministic_simulation.status;
|
|
else % General case
|
|
if options_.stack_solve_algo == 0
|
|
oo = sim1(M_, options_, oo_);
|
|
endo_simul = oo.endo_simul;
|
|
status = oo.deterministic_simulation.status;
|
|
elseif options_.stack_solve_algo == 6
|
|
oo = sim1_lbj(options_, M_, oo_);
|
|
endo_simul = oo.endo_simul;
|
|
status = oo.deterministic_simulation.status;
|
|
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(:),options_, ...
|
|
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));
|
|
endo_simul = [y0 reshape(y,M_.endo_nbr,periods) yT];
|
|
if info == 1
|
|
status = false;
|
|
else
|
|
status = true;
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
if nargout > 2
|
|
y0 = oo_.endo_simul(:,1);
|
|
yT = oo_.endo_simul(:,options_.periods+2);
|
|
yy = oo_.endo_simul(:,2:options_.periods+1);
|
|
if ~exist('illi')
|
|
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,:)');
|
|
end
|
|
residuals = perfect_foresight_problem(yy(:),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));
|
|
maxerror = max(max(abs(residuals)));
|
|
end
|