adding simul_backward and simul_backward_linear

time-shift
Michel Juillard 2016-12-05 16:17:14 +01:00
parent c2d72d2292
commit 6fd715a3b8
4 changed files with 229 additions and 0 deletions

View File

@ -0,0 +1,92 @@
function DynareOutput = simul_backward_linear_model(initial_conditions, sample_size, DynareOptions, DynareModel, DynareOutput, innovations)
%@info:
%! @deftypefn {Function File} {@var{DynareOutput} =} simul_backward_nonlinear_model (@var{sample_size},@var{DynareOptions}, @var{DynareModel}, @var{DynareOutput})
%! @anchor{@simul_backward_nonlinear_model}
%! @sp 1
%! Simulates a stochastic non linear backward looking model with arbitrary precision (a deterministic solver is used).
%! @sp 2
%! @strong{Inputs}
%! @sp 1
%! @table @ @var
%! @item sample_size
%! Scalar integer, size of the sample to be generated.
%! @item DynareOptions
%! Matlab/Octave structure (Options used by Dynare).
%! @item DynareDynareModel
%! Matlab/Octave structure (Description of the model).
%! @item DynareOutput
%! Matlab/Octave structure (Results reported by Dynare).
%! @end table
%! @sp 1
%! @strong{Outputs}
%! @sp 1
%! @table @ @var
%! @item DynareOutput
%! Matlab/Octave structure (Results reported by Dynare).
%! @end table
%! @sp 2
%! @strong{This function is called by:}
%! @sp 2
%! @strong{This function calls:}
%! @ref{dynTime}
%!
%! @end deftypefn
%@eod:
% Copyright (C) 2012-2016 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/>.
number_of_shocks = size(DynareOutput.exo_simul,2);
% Get usefull vector of indices.
ny0 = nnz(DynareModel.lead_lag_incidence(2,:));
ny1 = nnz(DynareModel.lead_lag_incidence(1,:));
iy1 = find(DynareModel.lead_lag_incidence(1,:)>0);
idx = 1:DynareModel.endo_nbr;
jdx = idx+ny1;
hdx = 1:ny1;
% Get the name of the dynamic model routine.
model_dynamic = str2func([DynareModel.fname,'_dynamic']);
% initialization of vector y.
y = NaN(length(idx)+ny1,1);
% initialization of the returned simulations.
DynareOutput.endo_simul = NaN(DynareModel.endo_nbr,sample_size+1);
if isempty(initial_conditions)
DynareOutput.endo_simul(:,1) = DynareOutput.steady_state;
else
DynareOutput.endo_simul(:,1) = initial_conditions;
end
Y = DynareOutput.endo_simul;
% get coefficients
[cst,jacob] = model_dynamic(zeros(DynareModel.endo_nbr+ny1,1), ...
zeros(2,size(DynareOutput.exo_simul, 2)), ...
DynareModel.params, ...
DynareOutput.steadystate,2);
A0inv = inv(jacob(:,jdx));
A1 = jacob(:,nonzeros(DynareModel.lead_lag_incidence(1,:)));
B = jacob(:,end-number_of_shocks+1:end);
% Simulations
for it = 2:sample_size+1
Y(:,it) = -A0inv*(cst + A1*Y(iy1,it-1) + B*DynareOutput.exo_simul(it,:)');
end
DynareOutput.endo_simul = Y;

View File

@ -0,0 +1,95 @@
function DynareOutput = simul_backward_linear_model(initial_conditions, sample_size, DynareOptions, DynareModel, DynareOutput, innovations)
%@info:
%! @deftypefn {Function File} {@var{DynareOutput} =} simul_backward_nonlinear_model (@var{sample_size},@var{DynareOptions}, @var{DynareModel}, @var{DynareOutput})
%! @anchor{@simul_backward_nonlinear_model}
%! @sp 1
%! Simulates a stochastic non linear backward looking model with arbitrary precision (a deterministic solver is used).
%! @sp 2
%! @strong{Inputs}
%! @sp 1
%! @table @ @var
%! @item sample_size
%! Scalar integer, size of the sample to be generated.
%! @item DynareOptions
%! Matlab/Octave structure (Options used by Dynare).
%! @item DynareDynareModel
%! Matlab/Octave structure (Description of the model).
%! @item DynareOutput
%! Matlab/Octave structure (Results reported by Dynare).
%! @end table
%! @sp 1
%! @strong{Outputs}
%! @sp 1
%! @table @ @var
%! @item DynareOutput
%! Matlab/Octave structure (Results reported by Dynare).
%! @end table
%! @sp 2
%! @strong{This function is called by:}
%! @sp 2
%! @strong{This function calls:}
%! @ref{dynTime}
%!
%! @end deftypefn
%@eod:
% Copyright (C) 2012-2016 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 DynareModel.maximum_lead
error(['simul_backward_nonlinear_model:: The specified model is not backward looking!'])
end
if nargin<6
% Set the covariance matrix of the structural innovations.
variances = diag(DynareModel.Sigma_e);
number_of_shocks = length(DynareModel.Sigma_e);
positive_var_indx = find(variances>0);
effective_number_of_shocks = length(positive_var_indx);
covariance_matrix = DynareModel.Sigma_e(positive_var_indx,positive_var_indx);
covariance_matrix_upper_cholesky = chol(covariance_matrix);
% Set seed to its default state.
if DynareOptions.bnlms.set_dynare_seed_to_default
set_dynare_seed('default');
end
% Simulate structural innovations.
switch DynareOptions.bnlms.innovation_distribution
case 'gaussian'
DynareOutput.bnlms.shocks = randn(sample_size,effective_number_of_shocks)*covariance_matrix_upper_cholesky;
otherwise
error(['simul_backward_nonlinear_model:: ' DynareOption.bnlms.innovation_distribution ' distribution for the structural innovations is not (yet) implemented!'])
end
% Put the simulated innovations in DynareOutput.exo_simul.
DynareOutput.exo_simul = zeros(sample_size+1,number_of_shocks);
DynareOutput.exo_simul(2:end,positive_var_indx) = DynareOutput.bnlms.shocks;
else
number_of_shocks = size(innovations,2);
DynareOutput.exo_simul = innovations;
end
if DynareOptions.linear
DynareOutput = simul_backward_linear_model(initial_conditions, sample_size, DynareOptions, ...
DynareModel, DynareOutput, innovations);
else
DynareOutput = simul_backward_nonlinear_model(initial_conditions, sample_size, DynareOptions, ...
DynareModel, DynareOutput, innovations);
end

View File

@ -240,6 +240,7 @@ MODFILES = \
ep/linearmodel1.mod \
stochastic-backward-models/solow_cd.mod \
stochastic-backward-models/solow_ces.mod \
stochastic-backward-models/backward_linear.mod \
deterministic_simulations/purely_forward/ar1.mod \
deterministic_simulations/purely_forward/nk.mod \
deterministic_simulations/purely_backward/ar1.mod \

View File

@ -0,0 +1,41 @@
var y gdp gdp_pot g pi P rs;
varexo e_y e_g e_p;
parameters alpha_1 alpha_2 beta_1 beta_2 theta gamma_1 gamma_2 pi_tar g_ss rr_bar;
alpha_1 = 0.9;
alpha_2 = -0.1;
beta_1 = 0.8;
beta_2 = 0.1;
theta = 0.9;
gamma_1 = 1.5;
gamma_2 = 0.5;
pi_tar = 2;
g_ss = 0.005;
rr_bar = 1;
model(linear);
gdp = gdp_pot + y;
y = alpha_1*y(-1) + alpha_2*(rs - pi - rr_bar) + e_y;
gdp_pot = gdp_pot(-1) + g;
g = (1 - theta)*g_ss + theta*g(-1) + e_g;
pi = (1 - beta_1)*pi_tar + beta_1*pi(-1) + beta_2*y + e_p;
P = pi/400 + P(-1);
rs = rr_bar + pi_tar + gamma_1*(pi(-1) - pi_tar) + gamma_2*y(-1);
end;
histval;
gdp_pot(0) = 1;
P(0) = 1;
g(0) = g_ss;
pi(0) = pi_tar;
end;
oo_.steadystate = NaN(7,1);
oo_ = simul_backward_model(M_.endo_histval, 10, options_, M_, oo_, zeros(11,3));
err1 = norm(abs(oo_.endo_simul([1 4 5 7],2:end) - repmat([0 0.005 2 3]',1,10)));
err2 = norm(abs(oo_.endo_simul([2 3 6],2:end) - repmat(linspace(1.005,1.05,10),3,1)));
if err1 > 1e-14 || err2 > 1e-14;
error('Error in backward_linear.mod');
end;