Merge branch 'new_ep'

time-shift
Michel Juillard 2015-07-23 15:46:09 +02:00
commit 53fef04e29
10 changed files with 443 additions and 138 deletions

View File

@ -76,7 +76,9 @@ if ~isempty(i)
return;
end
if max(abs(fvec)) < tolf
% this test doesn't check complementarity conditions and is not used for
% mixed complementarity problems
if (options.solve_algo ~= 10) && (max(abs(fvec)) < tolf)
return ;
end

35
matlab/ep/ep_notes.org Normal file
View File

@ -0,0 +1,35 @@
debug: 0
memory: 0
verbosity: 0
use_bytecode: 0
init: 0
maxit: 500
periods: 200
step: 50
check_stability: 0
lp: 5
fp: 2
innovation_distribution: 'gaussian'
set_dynare_seed_to_default: 1
stack_solve_algo: 4
stochastic: [1x1 struct]
IntegrationAlgorithm: 'Tensor-Gaussian-Quadrature'
stochastic:
method: ''
algo: 0
quadrature: [1x1 struct]
order: 1
hybrid_order: 0
homotopic_steps: 1
nodes: 3
quadrature:
ortpol: 'hermite'
nodes: 5
pruned: [1x1 struct]
pruned:
ortpol: 'hermite'
nodes: 5
pruned: [1x1 struct]

View File

@ -1,4 +1,4 @@
function ts = extended_path(initial_conditions,sample_size)
function [ts,results] = extended_path(initial_conditions,sample_size)
% Stochastic simulation of a non linear DSGE model using the Extended Path method (Fair and Taylor 1983). A time
% series of size T is obtained by solving T perfect foresight models.
%
@ -30,26 +30,30 @@ function ts = extended_path(initial_conditions,sample_size)
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
global M_ options_ oo_
global M_ options_ oo_
options_.verbosity = options_.ep.verbosity;
verbosity = options_.ep.verbosity+options_.ep.debug;
ep = options_.ep;
options_.verbosity = ep.verbosity;
verbosity = ep.verbosity+ep.debug;
% Set maximum number of iterations for the deterministic solver.
options_.simul.maxit = options_.ep.maxit;
options_.simul.maxit = ep.maxit;
% Prepare a structure needed by the matlab implementation of the perfect foresight model solver
pfm = setup_stochastic_perfect_foresight_model_solver(M_,options_,oo_);
endo_nbr = M_.endo_nbr;
exo_nbr = M_.exo_nbr;
ep = options_.ep;
maximum_lag = M_.maximum_lag;
maximum_lead = M_.maximum_lead;
epreplic_nbr = ep.replic_nbr;
steady_state = oo_.steady_state;
dynatol = options_.dynatol;
% Set default initial conditions.
if isempty(initial_conditions)
if isempty(M_.endo_histval)
initial_conditions = oo_.steady_state;
initial_conditions = steady_state;
else
initial_conditions = M_.endo_histval;
end
@ -57,18 +61,19 @@ end
% Set the number of periods for the perfect foresight model
periods = options_.ep.periods;
periods = ep.periods;
pfm.periods = periods;
pfm.i_upd = pfm.ny+(1:pfm.periods*pfm.ny);
pfm.block = options_.block;
% keep a copy of pfm.i_upd
i_upd = pfm.i_upd;
% Set the algorithm for the perfect foresight solver
options_.stack_solve_algo = options_.ep.stack_solve_algo;
options_.stack_solve_algo = ep.stack_solve_algo;
% Set check_stability flag
do_not_check_stability_flag = ~options_.ep.check_stability;
do_not_check_stability_flag = ~ep.check_stability;
% Compute the first order reduced form if needed.
%
@ -76,22 +81,15 @@ do_not_check_stability_flag = ~options_.ep.check_stability;
% all the globals in a mat file called linear_reduced_form.mat;
dr = struct();
if options_.ep.init
if ep.init
options_.order = 1;
[dr,Info,M_,options_,oo_] = resol(1,M_,options_,oo_);
oo_.dr=set_state_space(dr,M_,options_);
[dr,Info,M_,options_,oo_] = resol(0,M_,options_,oo_);
end
% Do not use a minimal number of perdiods for the perfect foresight solver (with bytecode and blocks)
options_.minimal_solving_period = 100;%options_.ep.periods;
% Initialize the exogenous variables.
% !!!!!!!! Needs to fixed
options_.periods = periods;
make_ex_;
% Initialize the endogenous variables.
make_y_;
% Initialize the output array.
time_series = zeros(M_.endo_nbr,sample_size);
@ -107,19 +105,37 @@ covariance_matrix_upper_cholesky = chol(covariance_matrix);
%exo_nbr = effective_number_of_shocks;
% Set seed.
if options_.ep.set_dynare_seed_to_default
if ep.set_dynare_seed_to_default
set_dynare_seed('default');
end
% Set bytecode flag
bytecode_flag = options_.ep.use_bytecode;
bytecode_flag = ep.use_bytecode;
% Set number of replications
replic_nbr = ep.replic_nbr;
% Simulate shocks.
switch options_.ep.innovation_distribution
switch ep.innovation_distribution
case 'gaussian'
oo_.ep.shocks = transpose(transpose(covariance_matrix_upper_cholesky)*randn(effective_number_of_shocks,sample_size));
shocks = transpose(transpose(covariance_matrix_upper_cholesky)* ...
randn(effective_number_of_shocks,sample_size* ...
replic_nbr));
shocks(:,positive_var_indx) = shocks;
case 'calibrated'
replic_nbr = 1;
shocks = zeros(sample_size,M_.exo_nbr);
for i = 1:length(M_.unanticipated_det_shocks)
k = M_.unanticipated_det_shocks(i).periods;
ivar = M_.unanticipated_det_shocks(i).exo_id;
v = M_.unanticipated_det_shocks(i).value;
if ~M_.unanticipated_det_shocks(i).multiplicative
shocks(k,ivar) = v;
else
socks(k,ivar) = shocks(k,ivar) * v;
end
end
otherwise
error(['extended_path:: ' options_.ep.innovation_distribution ' distribution for the structural innovations is not (yet) implemented!'])
error(['extended_path:: ' ep.innovation_distribution ' distribution for the structural innovations is not (yet) implemented!'])
end
@ -128,7 +144,7 @@ hh = dyn_waitbar(0,'Please wait. Extended Path simulations...');
set(hh,'Name','EP simulations.');
% hybrid correction
pfm.hybrid_order = options_.ep.stochastic.hybrid_order;
pfm.hybrid_order = ep.stochastic.hybrid_order;
if pfm.hybrid_order
oo_.dr = set_state_space(oo_.dr,M_,options_);
options = options_;
@ -142,16 +158,16 @@ end
pfm.nnzA = M_.NNZDerivatives(1);
% setting up integration nodes if order > 0
if options_.ep.stochastic.order > 0
if ep.stochastic.order > 0
[nodes,weights,nnodes] = setup_integration_nodes(options_.ep,pfm);
pfm.nodes = nodes;
pfm.weights = weights;
pfm.nnodes = nnodes;
% compute number of blocks
[block_nbr,pfm.world_nbr] = get_block_world_nbr(options_.ep.stochastic.algo,nnodes,options_.ep.stochastic.order,options_.ep.periods);
[block_nbr,pfm.world_nbr] = get_block_world_nbr(ep.stochastic.algo,nnodes,ep.stochastic.order,ep.periods);
else
block_nbr = options_.ep.periods
block_nbr = ep.periods;
end
@ -167,74 +183,56 @@ oo_.ep.failures.previous_period = cell(0);
oo_.ep.failures.shocks = cell(0);
% Initializes some variables.
t = 0;
t = 1;
tsimul = 1;
for k = 1:replic_nbr
results{k} = zeros(endo_nbr,sample_size+1);
results{k}(:,1) = initial_conditions;
end
make_ex_;
exo_simul_ = zeros(maximum_lag+sample_size+maximum_lead,exo_nbr);
exo_simul_(1:size(oo_.exo_simul,1),1:size(oo_.exo_simul,2)) = oo_.exo_simul;
% Main loop.
while (t<sample_size)
while (t <= sample_size)
if ~mod(t,10)
dyn_waitbar(t/sample_size,hh,'Please wait. Extended Path simulations...');
end
% Set period index.
t = t+1;
% Put shocks in oo_.exo_simul (second line).
exo_simul_1 = zeros(periods+2,exo_nbr);
exo_simul_1(2,positive_var_indx) = oo_.exo_simul(2,positive_var_indx) + oo_.ep.shocks(t,:);
if ep.init% Compute first order solution (Perturbation)...
initial_path = simult_(initial_conditions,dr,exo_simul_1(2:end,:),1);
endo_simul_1(:,1:end-1) = initial_path(:,1:end-1)*ep.init+endo_simul_1(:,1:end-1)*(1-ep.init);
if replic_nbr > 1 && ep.parallel_1
parfor k = 1:replic_nbr
exo_simul = repmat(oo_.exo_steady_state',periods+2,1);
% exo_simul(1:sample_size+3-t,:) = exo_simul_(t:end,:);
exo_simul(2,:) = exo_simul_(M_.maximum_lag+t,:) + ...
shocks((t-2)*replic_nbr+k,:);
initial_conditions = results{k}(:,t-1);
results{k}(:,t) = extended_path_core(ep.periods,endo_nbr,exo_nbr,positive_var_indx, ...
exo_simul,ep.init,initial_conditions,...
maximum_lag,maximum_lead,steady_state, ...
ep.verbosity,bytecode_flag,ep.stochastic.order,...
M_.params,pfm,ep.stochastic.algo,ep.stock_solve_algo,...
options_.lmmcp,options_,oo_);
end
else
if t==1
endo_simul_1 = repmat(steady_state,1,periods+2);
for k = 1:replic_nbr
exo_simul = repmat(oo_.exo_steady_state',periods+maximum_lag+ ...
maximum_lead,1);
% exo_simul(1:sample_size+maximum_lag+maximum_lead-t+1,:) = ...
% exo_simul_(t:end,:);
exo_simul(maximum_lag+1,:) = ...
exo_simul_(maximum_lag+t,:) + shocks((t-2)*replic_nbr+k,:);
initial_conditions = results{k}(:,t-1);
results{k}(:,t) = extended_path_core(ep.periods,endo_nbr,exo_nbr,positive_var_indx, ...
exo_simul,ep.init,initial_conditions,...
maximum_lag,maximum_lead,steady_state, ...
ep.verbosity,bytecode_flag,ep.stochastic.order,...
M_,pfm,ep.stochastic.algo,ep.stack_solve_algo,...
options_.lmmcp,options_,oo_);
end
end
% Solve a perfect foresight model.
% Keep a copy of endo_simul_1
endo_simul = endo_simul_1;
if verbosity
save ep_test_1 endo_simul_1 exo_simul_1
end
if bytecode_flag && ~options_.ep.stochastic.order
[flag,tmp] = bytecode('dynamic',endo_simul_1,exo_simul_1, M_.params, endo_simul_1, options_.ep.periods);
else
flag = 1;
end
if flag
if options_.ep.stochastic.order == 0
[flag,tmp,err] = solve_perfect_foresight_model(endo_simul_1,exo_simul_1,pfm);
else
switch(options_.ep.stochastic.algo)
case 0
[flag,tmp] = ...
solve_stochastic_perfect_foresight_model(endo_simul_1,exo_simul_1,pfm,options_.ep.stochastic.quadrature.nodes,options_.ep.stochastic.order);
case 1
[flag,tmp] = ...
solve_stochastic_perfect_foresight_model_1(endo_simul_1,exo_simul_1,options_,pfm,options_.ep.stochastic.order);
end
end
end
info_convergence = ~flag;
if verbosity
if info_convergence
disp(['Time: ' int2str(t) '. Convergence of the perfect foresight model solver!'])
else
disp(['Time: ' int2str(t) '. No convergence of the perfect foresight model solver!'])
end
end
endo_simul_1 = tmp;
if info_convergence
% Save results of the perfect foresight model solver.
time_series(:,tsimul) = endo_simul_1(:,2);
endo_simul_1(:,1:end-1) = endo_simul_1(:,2:end);
endo_simul_1(:,1) = time_series(:,tsimul);
endo_simul_1(:,end) = oo_.steady_state;
tsimul = tsimul+1;
else
oo_.ep.failures.periods = [oo_.ep.failures.periods t];
oo_.ep.failures.previous_period = [oo_.ep.failures.previous_period endo_simul_1(:,1)];
oo_.ep.failures.shocks = [oo_.ep.failures.shocks shocks];
endo_simul_1 = repmat(steady_state,1,periods+2);
endo_simul_1(:,1) = time_series(:,tsimul-1);
end
end% (while) loop over t
dyn_waitbar_close(hh);
@ -242,14 +240,105 @@ dyn_waitbar_close(hh);
if isnan(options_.initial_period)
initial_period = dates(1,1);
else
initial_period = optins_.initial_period;
initial_period = options_.initial_period;
end
if nargout
ts = dseries(transpose([initial_conditions, time_series]),initial_period,cellstr(M_.endo_names));
if ~isnan(results{1})
ts = dseries(transpose([results{1}]), ...
initial_period,cellstr(M_.endo_names));
else
ts = NaN;
end
else
oo_.endo_simul = [initial_conditions, time_series];
ts = dseries(transpose(oo_.endo_simul),initial_period,cellstr(M_.endo_names));
dyn2vec;
if ~isnan(results{1})
oo_.endo_simul = results{1};
ts = dseries(transpose(results{1}),initial_period, ...
cellstr(M_.endo_names));
else
oo_.endo_simul = NaN;
ts = NaN;
end
end
assignin('base', 'Simulated_time_series', ts);
function y = extended_path_core(periods,endo_nbr,exo_nbr,positive_var_indx, ...
exo_simul,init,initial_conditions,...
maximum_lag,maximum_lead,steady_state, ...
verbosity,bytecode_flag,order,M,pfm,algo,stack_solve_algo,...
olmmcp,options,oo)
ep = options.ep;
if init% Compute first order solution (Perturbation)...
endo_simul = simult_(initial_conditions,oo.dr,exo_simul(2:end,:),1);
else
endo_simul = [initial_conditions repmat(steady_state,1,periods+1)];
end
oo.endo_simul = endo_simul;
oo_.endo_simul = endo_simul;
% Solve a perfect foresight model.
% Keep a copy of endo_simul_1
if verbosity
save ep_test_1 endo_simul exo_simul
end
if bytecode_flag && ~ep.stochastic.order
[flag,tmp] = bytecode('dynamic',endo_simul,exo_simul, M_.params, endo_simul, periods);
else
flag = 1;
end
if flag
if order == 0
options.periods = periods;
options.block = pfm.block;
oo.endo_simul = endo_simul;
oo.exo_simul = exo_simul;
oo.steady_state = steady_state;
options.bytecode = bytecode_flag;
options.lmmcp = olmmcp;
options.stack_solve_algo = stack_solve_algo;
[tmp,flag] = perfect_foresight_solver_core(M,options,oo);
if ~flag && ~options.no_homotopy
exo_orig = oo.exo_simul;
endo_simul = repmat(steady_state,1,periods+1);
for i = 1:10
weight = i/10;
oo.endo_simul = [weight*initial_conditions + (1-weight)*steady_state ...
endo_simul];
oo.exo_simul = repmat((1-weight)*oo.exo_steady_state', ...
size(oo.exo_simul,1),1) + weight*exo_orig;
[tmp,flag] = perfect_foresight_solver_core(M,options,oo);
disp([i,flag])
if ~flag
break
end
endo_simul = tmp.endo_simul;
end
end
info_convergence = flag;
else
switch(algo)
case 0
[flag,tmp] = ...
solve_stochastic_perfect_foresight_model(endo_simul,exo_simul,pfm,ep.stochastic.quadrature.nodes,ep.stochastic.order);
case 1
[flag,tmp] = ...
solve_stochastic_perfect_foresight_model_1(endo_simul,exo_simul,options_,pfm,ep.stochastic.order);
end
endo_simul = tmp;
info_convergence = ~flag;
end
end
if verbosity
if info_convergence
disp(['Time: ' int2str(t) '. Convergence of the perfect foresight model solver!'])
else
disp(['Time: ' int2str(t) '. No convergence of the perfect foresight model solver!'])
end
end
if info_convergence
y = endo_simul(:,2);
else
y = NaN(size(endo_nbr,1));
end

View File

@ -20,7 +20,9 @@ function pfm = setup_stochastic_perfect_foresight_model_solver(DynareModel,Dynar
pfm.lead_lag_incidence = DynareModel.lead_lag_incidence;
pfm.ny = DynareModel.endo_nbr;
pfm.Sigma = DynareModel.Sigma_e;
pfm.Omega = chol(pfm.Sigma,'upper'); % Sigma = Omega'*Omega
if det(pfm.Sigma) > 0
pfm.Omega = chol(pfm.Sigma,'upper'); % Sigma = Omega'*Omega
end
pfm.number_of_shocks = length(pfm.Sigma);
pfm.stochastic_order = DynareOptions.ep.stochastic.order;
pfm.max_lag = DynareModel.maximum_endo_lag;

View File

@ -195,7 +195,11 @@ ep.innovation_distribution = 'gaussian';
% Set flag for the seed
ep.set_dynare_seed_to_default = 1;
% Set algorithm for the perfect foresight solver
ep.stack_solve_algo = 4;
ep.stack_solve_algo = 7;
% Number of replications
ep.replic_nbr = 1;
% Parallel execution of replications
ep.parallel_1 = false;
% Stochastic extended path related options.
ep.stochastic.method = '';
ep.stochastic.algo = 0;

View File

@ -53,6 +53,6 @@ else
end
% the first NaNs take care of the case where there are lags > 1 on
% exogenous variables
oo_.endo_simul = [NaN(M_.endo_nbr,M_.maximum_lag-1) M_.endo_histval ...
oo_.endo_simul = [M_.endo_histval ...
oo_.steady_state*ones(1,options_.periods+M_.maximum_lead)];
end

View File

@ -58,7 +58,7 @@ end
initperiods = 1:M_.maximum_lag;
lastperiods = (M_.maximum_lag+options_.periods+1):(M_.maximum_lag+options_.periods+M_.maximum_lead);
oo_ = simulation_core(options_, M_, oo_);
oo_ = perfect_foresight_solver_core(M_,options_,oo_);
% If simulation failed try homotopy.
if ~oo_.deterministic_simulation.status && ~options_.no_homotopy
@ -135,7 +135,7 @@ if ~oo_.deterministic_simulation.status && ~options_.no_homotopy
saved_endo_simul = oo_.endo_simul;
[oo_, me] = simulation_core(options_, M_, oo_);
[oo_,me] = perfect_foresight_solver_core(M_,options_,oo_);
if oo_.deterministic_simulation.status == 1
current_weight = new_weight;

View File

@ -0,0 +1,176 @@
function [oo_, maxerror] = perfect_foresight_solver_core(M_, options_, oo_)
%function [oo_, maxerror] = simulation_core(M_, options_, oo_)
% Copyright (C) 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 <http://www.gnu.org/licenses/>.
if options_.linear_approximation && ~(isequal(options_.stack_solve_algo,0) || isequal(options_.stack_solve_algo,7))
error('perfect_foresight_solver: Option linear_approximation is only available with option stack_solve_algo equal to 0.')
end
if options_.linear && isequal(options_.stack_solve_algo,0)
options_.linear_approximation = 1;
end
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
oo_.deterministic_simulation.status = false;
else
oo_.endo_simul = tmp;
oo_.deterministic_simulation.status = true;
end
if options_.no_homotopy
mexErrCheck('bytecode', info);
end
else
oo_ = feval([M_.fname '_dynamic'], options_, M_, oo_);
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
oo_.deterministic_simulation.status = false;
else
oo_.endo_simul = tmp;
oo_.deterministic_simulation.status = true;
end
if options_.no_homotopy
mexErrCheck('bytecode', info);
end
else
if M_.maximum_endo_lead == 0 % Purely backward model
oo_ = sim1_purely_backward(options_, M_, oo_);
elseif M_.maximum_endo_lag == 0 % Purely forward model
oo_ = sim1_purely_forward(options_, M_, oo_);
else % General case
if options_.stack_solve_algo == 0
if options_.linear_approximation
oo_ = sim1_linear(options_, M_, oo_);
else
oo_ = sim1(M_, options_, oo_);
end
elseif options_.stack_solve_algo == 6
oo_ = sim1_lbj(options_, M_, oo_);
elseif options_.stack_solve_algo == 7
periods = options_.periods;
if ~isfield(options_.lmmcp,'lb')
[lb,ub,pfm.eq_index] = get_complementarity_conditions(M_,options_.ramsey_policy);
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,junk,i_cols_j] = find(illi(:));
illi = illi(:,2:3);
[i_cols_J1,junk,i_cols_1] = find(illi(:));
i_cols_T = nonzeros(M_.lead_lag_incidence(1:2,:)');
if options_.linear_approximation
y_steady_state = oo_.steady_state;
x_steady_state = transpose(oo_.exo_steady_state);
ip = find(M_.lead_lag_incidence(1,:)');
ic = find(M_.lead_lag_incidence(2,:)');
in = find(M_.lead_lag_incidence(3,:)');
% Evaluate the Jacobian of the dynamic model at the deterministic steady state.
model_dynamic = str2func([M_.fname,'_dynamic']);
[d1,jacobian] = model_dynamic(y_steady_state([ip; ic; in]), x_steady_state, M_.params, y_steady_state, 1);
% Check that the dynamic model was evaluated at the steady state.
if max(abs(d1))>1e-12
error('Jacobian is not evaluated at the steady state!')
end
nyp = nnz(M_.lead_lag_incidence(1,:)) ;
ny0 = nnz(M_.lead_lag_incidence(2,:)) ;
nyf = nnz(M_.lead_lag_incidence(3,:)) ;
nd = nyp+ny0+nyf; % size of y (first argument passed to the dynamic file).
jexog = transpose(nd+(1:M_.exo_nbr));
jendo = transpose(1:nd);
z = bsxfun(@minus,z,y_steady_state);
x = bsxfun(@minus,oo_.exo_simul,x_steady_state);
[y,info] = dynare_solve(@linear_perfect_foresight_problem,z(:), options_, ...
jacobian, y0-y_steady_state, yT-y_steady_state, ...
x, M_.params, y_steady_state, ...
M_.maximum_lag, options_.periods, M_.endo_nbr, i_cols, ...
i_cols_J1, i_cols_1, i_cols_T, i_cols_j, ...
M_.NNZDerivatives(1),jendo,jexog);
else
[y,info] = dynare_solve(@perfect_foresight_problem,z(:),options_, ...
str2func([M_.fname '_dynamic']),y0,yT, ...
oo_.exo_simul,M_.params,oo_.steady_state, ...
M_.maximum_lag,options_.periods,M_.endo_nbr,i_cols, ...
i_cols_J1, i_cols_1, i_cols_T, i_cols_j, ...
M_.NNZDerivatives(1));
end
if all(imag(y)<.1*options_.dynatol.f)
if ~isreal(y)
y = real(y);
end
else
info = 1;
end
if options_.linear_approximation
oo_.endo_simul = [y0 bsxfun(@plus,reshape(y,M_.endo_nbr,periods),y_steady_state) yT];
else
oo_.endo_simul = [y0 reshape(y,M_.endo_nbr,periods) yT];
end
if info == 1
oo_.deterministic_simulation.status = false;
else
oo_.deterministic_simulation.status = true;
end
end
end
end
end
if nargout>1
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,junk,i_cols_j] = find(illi(:));
illi = illi(:,2:3);
[i_cols_J1,junk,i_cols_1] = find(illi(:));
i_cols_T = nonzeros(M_.lead_lag_incidence(1:2,:)');
end
if options_.block && ~options_.bytecode
maxerror = oo_.deterministic_simulation.error;
else
if options_.bytecode
[chck, residuals, junk]= bytecode('dynamic','evaluate', oo_.endo_simul, oo_.exo_simul, M_.params, oo_.steady_state, 1);
else
residuals = perfect_foresight_problem(yy(:),str2func([M_.fname '_dynamic']), y0, yT, ...
oo_.exo_simul,M_.params,oo_.steady_state, ...
M_.maximum_lag,options_.periods,M_.endo_nbr,i_cols, ...
i_cols_J1, i_cols_1, i_cols_T, i_cols_j, ...
M_.NNZDerivatives(1));
end
maxerror = max(max(abs(residuals)));
end
end

View File

@ -1,4 +1,4 @@
function oo_ = sim1(options_, M_, oo_)
function oo = sim1(M, options, oo)
% function sim1
% Performs deterministic simulations with lead or lag on one period.
% Uses sparse matrices.
@ -30,18 +30,18 @@ function oo_ = sim1(options_, M_, oo_)
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
verbose = options_.verbosity;
verbose = options.verbosity;
endogenous_terminal_period = options_.endogenous_terminal_period;
vperiods = options_.periods*ones(1,options_.simul.maxit);
azero = options_.dynatol.f/1e7;
endogenous_terminal_period = options.endogenous_terminal_period;
vperiods = options.periods*ones(1,options.simul.maxit);
azero = options.dynatol.f/1e7;
lead_lag_incidence = M_.lead_lag_incidence;
lead_lag_incidence = M.lead_lag_incidence;
ny = M_.endo_nbr;
ny = M.endo_nbr;
maximum_lag = M_.maximum_lag;
max_lag = M_.maximum_endo_lag;
maximum_lag = M.maximum_lag;
max_lag = M.maximum_endo_lag;
nyp = nnz(lead_lag_incidence(1,:)) ;
ny0 = nnz(lead_lag_incidence(2,:)) ;
@ -50,11 +50,11 @@ nyf = nnz(lead_lag_incidence(3,:)) ;
nd = nyp+ny0+nyf;
stop = 0 ;
periods = options_.periods;
steady_state = oo_.steady_state;
params = M_.params;
endo_simul = oo_.endo_simul;
exo_simul = oo_.exo_simul;
periods = options.periods;
steady_state = oo.steady_state;
params = M.params;
endo_simul = oo.endo_simul;
exo_simul = oo.exo_simul;
i_cols_1 = nonzeros(lead_lag_incidence(2:3,:)');
i_cols_A1 = find(lead_lag_incidence(2:3,:)');
i_cols_T = nonzeros(lead_lag_incidence(1:2,:)');
@ -72,20 +72,18 @@ if verbose
skipline()
end
model_dynamic = str2func([M_.fname,'_dynamic']);
model_dynamic = str2func([M.fname,'_dynamic']);
z = Y(find(lead_lag_incidence'));
[d1,jacobian] = model_dynamic(z,oo_.exo_simul, params, ...
[d1,jacobian] = model_dynamic(z,oo.exo_simul, params, ...
steady_state,maximum_lag+1);
res = zeros(periods*ny,1);
o_periods = periods;
ZERO = zeros(length(i_upd),1);
h1 = clock ;
iA = zeros(periods*M_.NNZDerivatives(1),3);
for iter = 1:options_.simul.maxit
iA = zeros(periods*M.NNZDerivatives(1),3);
for iter = 1:options.simul.maxit
h2 = clock ;
i_rows = (1:ny)';
@ -131,7 +129,7 @@ for iter = 1:options_.simul.maxit
err = max(abs(res));
if options_.debug
if options.debug
fprintf('\nLargest absolute residual at iteration %d: %10.3f\n',iter,err);
if any(isnan(res)) || any(isinf(res)) || any(isnan(Y)) || any(isinf(Y))
fprintf('\nWARNING: NaN or Inf detected in the residuals or endogenous variables.\n');
@ -147,8 +145,7 @@ for iter = 1:options_.simul.maxit
disp(str);
end
if err < options_.dynatol.f
if err < options.dynatol.f
stop = 1 ;
break
end
@ -168,18 +165,18 @@ for iter = 1:options_.simul.maxit
end
if endogenous_terminal_period
err = evaluate_max_dynamic_residual(model_dynamic, Y, oo_.exo_simul, params, steady_state, o_periods, ny, max_lag, lead_lag_incidence);
err = evaluate_max_dynamic_residual(model_dynamic, Y, oo.exo_simul, params, steady_state, o_periods, ny, max_lag, lead_lag_incidence);
periods = o_periods;
end
if stop
if any(isnan(res)) || any(isinf(res)) || any(isnan(Y)) || any(isinf(Y)) || ~isreal(res) || ~isreal(Y)
oo_.deterministic_simulation.status = false;% NaN or Inf occurred
oo_.deterministic_simulation.error = err;
oo_.deterministic_simulation.iterations = iter;
oo_.deterministic_simulation.periods = vperiods(1:iter);
oo_.endo_simul = reshape(Y,ny,periods+maximum_lag+M_.maximum_lead);
oo.deterministic_simulation.status = false;% NaN or Inf occurred
oo.deterministic_simulation.error = err;
oo.deterministic_simulation.iterations = iter;
oo.deterministic_simulation.periods = vperiods(1:iter);
oo.endo_simul = reshape(Y,ny,periods+maximum_lag+M.maximum_lead);
if verbose
skipline()
disp(sprintf('Total time of simulation: %s.', num2str(etime(clock,h1))))
@ -197,11 +194,11 @@ if stop
disp(sprintf('Total time of simulation: %s', num2str(etime(clock,h1))))
printline(56)
end
oo_.deterministic_simulation.status = true;% Convergency obtained.
oo_.deterministic_simulation.error = err;
oo_.deterministic_simulation.iterations = iter;
oo_.deterministic_simulation.periods = vperiods(1:iter);
oo_.endo_simul = reshape(Y,ny,periods+maximum_lag+M_.maximum_lead);
oo.deterministic_simulation.status = true;% Convergency obtained.
oo.deterministic_simulation.error = err;
oo.deterministic_simulation.iterations = iter;
oo.deterministic_simulation.periods = vperiods(1:iter);
oo.endo_simul = reshape(Y,ny,periods+maximum_lag+M.maximum_lead);
end
elseif ~stop
if verbose
@ -210,10 +207,10 @@ elseif ~stop
disp('Maximum number of iterations is reached (modify option maxit).')
printline(62)
end
oo_.deterministic_simulation.status = false;% more iterations are needed.
oo_.deterministic_simulation.error = err;
oo_.deterministic_simulation.periods = vperiods(1:iter);
oo_.deterministic_simulation.iterations = options_.simul.maxit;
oo.deterministic_simulation.status = false;% more iterations are needed.
oo.deterministic_simulation.error = err;
oo.deterministic_simulation.periods = vperiods(1:iter);
oo.deterministic_simulation.iterations = options.simul.maxit;
end
if verbose

View File

@ -274,7 +274,7 @@ HistValStatement::writeOutput(ostream &output, const string &basename, bool mini
output << "%" << endl
<< "% HISTVAL instructions" << endl
<< "%" << endl
<< "M_.endo_histval = zeros(M_.endo_nbr,M_.maximum_endo_lag);" << endl;
<< "M_.endo_histval = zeros(M_.endo_nbr,M_.maximum_lag);" << endl;
for (hist_values_t::const_iterator it = hist_values.begin();
it != hist_values.end(); it++)
@ -310,7 +310,7 @@ HistValStatement::writeOutput(ostream &output, const string &basename, bool mini
int tsid = symbol_table.getTypeSpecificID(symb_id) + 1;
if (type == eEndogenous)
output << "M_.endo_histval( " << tsid << ", M_.maximum_endo_lag + " << lag << ") = ";
output << "M_.endo_histval( " << tsid << ", M_.maximum_lag + " << lag << ") = ";
else if (type == eExogenous)
output << "oo_.exo_simul( M_.maximum_lag + " << lag << ", " << tsid << " ) = ";
else if (type != eExogenousDet)