2017-07-27 15:40:19 +02:00
function DynareOutput = simul_backward_nonlinear_model ( varargin)
2012-06-18 14:59:52 +02:00
2017-05-04 18:17:15 +02:00
% Simulates a stochastic non linear backward looking model with arbitrary precision (a deterministic solver is used).
%
2017-05-16 15:10:20 +02:00
% INPUTS
2017-05-04 18:17:15 +02:00
% - initial_conditions [double] n*1 vector, initial conditions for the endogenous variables.
% - sample_size [integer] scalar, number of periods for the simulation.
% - DynareOptions [struct] Dynare's options_ global structure.
% - DynareModel [struct] Dynare's M_ global structure.
% - DynareOutput [struct] Dynare's oo_ global structure.
% - innovations [double] T*q matrix, innovations to be used for the simulation.
%
2017-05-16 15:10:20 +02:00
% OUTPUTS
2017-05-04 18:17:15 +02:00
% - DynareOutput [struct] Dynare's oo_ global structure.
%
2017-05-16 15:10:20 +02:00
% REMARKS
2017-05-04 18:17:15 +02:00
% [1] The innovations used for the simulation are saved in DynareOutput.exo_simul, and the resulting paths for the endogenous
% variables are saved in DynareOutput.endo_simul.
% [2] The last input argument is not mandatory. If absent we use random draws and rescale them with the informations provided
% through the shocks block.
% [3] If the first input argument is empty, the endogenous variables are initialized with 0, or if available with the informations
% provided thrtough the histval block.
2012-06-18 14:59:52 +02:00
2017-05-04 18:17:15 +02:00
% Copyright (C) 2012-2017 Dynare Team
2012-06-18 14:59:52 +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/>.
2017-07-27 15:40:19 +02:00
[ initialconditions , samplesize , innovations , DynareOptions , DynareModel , DynareOutput , nx , ny1 , iy1 , jdx , model_dynamic , y ] = ...
simul_backward_model_init ( varargin { : } ) ;
2012-06-18 14:59:52 +02:00
2017-05-06 16:32:48 +02:00
model_dynamic_s = str2func ( ' dynamic_backward_model_for_simulation' ) ;
2012-06-18 14:59:52 +02:00
% initialization of the returned simulations.
2017-07-29 11:28:28 +02:00
DynareOutput . endo_simul = NaN ( DynareModel . endo_nbr , samplesize ) ;
2017-07-27 15:40:19 +02:00
if isempty ( initialconditions )
2017-06-01 15:16:00 +02:00
if isfield ( DynareModel , ' endo_histval' ) && ~ isempty ( DynareModel . endo_histval )
2017-07-29 11:28:28 +02:00
DynareOutput . endo_simul = [ DynareModel . endo_histval , DynareOutput . endo_simul ] ;
2016-12-01 13:29:36 +01:00
else
2017-07-29 11:28:28 +02:00
warning ( ' simul_backward_nonlinear_model:: Initial condition is zero for all variables! The model simulation may fail with the default initialization.' )
DynareOutput . endo_simul = [ zeros ( DynareModel . endo_nbr , DynareModel . max_lag_orig ) , DynareOutput . endo_simul ] ;
2016-12-01 13:29:36 +01:00
end
2016-03-14 20:24:07 +01:00
else
2017-07-29 11:28:28 +02:00
if ~ isequal ( size ( initialconditions , 2 ) , DynareModel . max_lag_orig )
error ( [ ' simul_backward_nonlinear_model:: First argument should have %s columns!' ] , DynareModel . max_lag_orig )
end
DynareOutput . endo_simul = [ initialconditions , DynareOutput . endo_simul ] ;
2016-03-14 20:24:07 +01:00
end
2016-02-26 21:31:50 +01:00
Y = DynareOutput . endo_simul ;
2017-07-29 11:28:28 +02:00
if ~ DynareModel . max_exo_lag_orig
if DynareModel . max_endo_lag_orig > 1
DynareOutput . exo_simul = [ zeros ( DynareModel . max_endo_lag_orig - 1 , DynareModel . exo_nbr ) ; DynareOutput . exo_simul ] ;
end
end
2017-06-01 15:16:00 +02:00
2012-06-18 14:59:52 +02:00
% Simulations (call a Newton-like algorithm for each period).
2017-07-29 11:28:28 +02:00
for it = DynareModel . max_lag_orig + ( 1 : samplesize )
2017-05-06 16:32:48 +02:00
ylag = Y ( iy1 , it - 1 ) ; % Set lagged variables.
y = Y ( : , it - 1 ) ; % A good guess for the initial conditions is the previous values for the endogenous variables.
2017-07-29 11:28:28 +02:00
Y ( : , it ) = dynare_solve ( model_dynamic_s , y , DynareOptions , model_dynamic , ylag , DynareOutput . exo_simul , DynareModel . params , DynareOutput . steady_state , it ) ;
2016-02-26 21:31:50 +01:00
end
2016-02-29 15:46:40 +01:00
DynareOutput . endo_simul = Y ;