From 085ab360cc0b21361aa042df0060821b8e277c8f Mon Sep 17 00:00:00 2001 From: Michel Juillard Date: Fri, 6 Feb 2015 12:35:00 +0100 Subject: [PATCH] perfect foresight: making simulation_core into a separate file, perfect_foresight_solver_core.m --- matlab/perfect_foresight_solver.m | 74 +------------- matlab/perfect_foresight_solver_core.m | 133 +++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 72 deletions(-) create mode 100644 matlab/perfect_foresight_solver_core.m diff --git a/matlab/perfect_foresight_solver.m b/matlab/perfect_foresight_solver.m index db0f265d6..0e03d9646 100644 --- a/matlab/perfect_foresight_solver.m +++ b/matlab/perfect_foresight_solver.m @@ -86,7 +86,7 @@ end % Effectively compute simulation, possibly with homotopy if options_.no_homotopy - simulation_core; + [oo_.endo_simul,oo_.deterministic_simulation.status] = perfect_foresight_solver_core(M_,oo_,options_); else exosim = oo_.exo_simul; exoinit = repmat(oo_.exo_steady_state',M_.maximum_lag+options_.periods+M_.maximum_lead,1); @@ -114,7 +114,7 @@ else saved_endo_simul = oo_.endo_simul; - simulation_core; + [oo_.endo_simul,oo_.deterministic_simulation.status] = perfect_foresight_solver_core(M_,oo_,options_); if oo_.deterministic_simulation.status == 1 current_weight = new_weight; @@ -158,73 +158,3 @@ assignin('base', 'Simulated_time_series', ts); end -function simulation_core() - -global M_ oo_ options_ - -if(options_.block) - if(options_.bytecode) - [info, oo_.endo_simul] = bytecode('dynamic'); - if info == 1 - oo_.deterministic_simulation.status = 0; - else - oo_.deterministic_simulation.status = 1; - end - mexErrCheck('bytecode', info); - else - eval([M_.fname '_dynamic']); - end -else - if(options_.bytecode) - [info, oo_.endo_simul]=bytecode('dynamic'); - if info == 1 - oo_.deterministic_simulation.status = 0; - else - oo_.deterministic_simulation.status = 1; - end; - mexErrCheck('bytecode', info); - else - if M_.maximum_endo_lead == 0 % Purely backward model - sim1_purely_backward; - elseif M_.maximum_endo_lag == 0 % Purely forward model - sim1_purely_forward; - else % General case - 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]; - if info == 1 - oo_.deterministic_simulation.status = 0; - else - oo_.deterministic_simulation.status = 1; - end; - end - end - end -end - -end diff --git a/matlab/perfect_foresight_solver_core.m b/matlab/perfect_foresight_solver_core.m new file mode 100644 index 000000000..2ac6adaf7 --- /dev/null +++ b/matlab/perfect_foresight_solver_core.m @@ -0,0 +1,133 @@ +function [endo_simul, status] = perfect_foresight_solver_core(M,oo,options) +% Core function to compute deterministic simulations +% +% INPUTS +% M: model structure +% oo: output structure +% options: options structure +% +% OUTPUTS +% endo_simul: matrix endogenous variables +% deterministic_simulation: simulation status +% +% ALGORITHM +% +% various +% +% SPECIAL REQUIREMENTS +% none + +% Copyright (C) 1996-2015 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 . + +endo_simul = oo.endo_simul; +status = 0; +deterministic_simulation = struct(); + +if(options.block) + if(options.bytecode) + [info, endo_simul] = bytecode('dynamic'); + if info == 1 + status = 0; + else + status = 1; + end + mexErrCheck('bytecode', info); + else + eval([M.fname '_dynamic']); + end +else + if(options.bytecode) + [info, endo_simul]=bytecode('dynamic'); + if info == 1 + status = 0; + else + status = 1; + end; + mexErrCheck('bytecode', info); + else + if M.maximum_endo_lead == 0 + % Purely backward model + global oo_ + oo_ = oo; + sim1_purely_backward; + endo_simul = oo_.endo_simul; + if oo_.deterministic_simulation.status == 1 + status = 1; + end + elseif M.maximum_endo_lag == 0 + % Purely forward model + global oo_ + oo_ = oo; + sim1_purely_forward; + endo_simul = oo_.endo_simul; + if oo_.deterministic_simulation.status == 1 + status = 1; + end + else + % General case + if options.stack_solve_algo == 0 + global oo_ + oo_ = oo; + sim1; + endo_simul = oo_.endo_simul; + if oo_.deterministic_simulation.status == 1 + status = 1; + end + elseif options.stack_solve_algo == 6 + global oo_ + oo_ = oo; + sim1_lbj; + endo_simul = oo_.endo_simul; + if oo_.deterministic_simulation.status == 1 + status = 1; + end + 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 = 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)); + endo_simul = [y0 reshape(y,M.endo_nbr,periods) yT]; + if info == 1 + status = 1; + else + status = 0; + end; + end + end + end +end + +