2019-09-10 17:02:20 +02:00
|
|
|
function y_=simult_(M_,options_,y0,dr,ex_,iorder)
|
2010-05-25 14:00:08 +02:00
|
|
|
% Simulates the model using a perturbation approach, given the path for the exogenous variables and the
|
2008-12-19 17:49:52 +01:00
|
|
|
% decision rules.
|
2005-02-18 20:54:39 +01:00
|
|
|
%
|
2007-12-21 18:13:52 +01:00
|
|
|
% INPUTS
|
2019-09-10 17:02:20 +02:00
|
|
|
% M_ [struct] model
|
|
|
|
% options_ [struct] options
|
2017-05-16 15:10:20 +02:00
|
|
|
% y0 [double] n*1 vector, initial value (n is the number of declared endogenous variables plus the number
|
2015-03-07 12:37:36 +01:00
|
|
|
% of auxilliary variables for lags and leads); must be in declaration order, i.e. as in M_.endo_names
|
2010-05-25 14:00:08 +02:00
|
|
|
% dr [struct] matlab's structure where the reduced form solution of the model is stored.
|
|
|
|
% ex_ [double] T*q matrix of innovations.
|
|
|
|
% iorder [integer] order of the taylor approximation.
|
2007-12-21 18:13:52 +01:00
|
|
|
%
|
|
|
|
% OUTPUTS
|
2010-05-25 14:00:08 +02:00
|
|
|
% y_ [double] n*(T+1) time series for the endogenous variables.
|
2007-12-21 18:13:52 +01:00
|
|
|
%
|
|
|
|
% SPECIAL REQUIREMENTS
|
|
|
|
% none
|
2008-08-01 14:40:33 +02:00
|
|
|
|
2022-04-13 13:15:19 +02:00
|
|
|
% Copyright © 2001-2021 Dynare Team
|
2008-08-01 14:40:33 +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
|
2021-06-09 17:33:48 +02:00
|
|
|
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
2005-02-18 20:54:39 +01:00
|
|
|
|
2009-12-16 18:17:34 +01:00
|
|
|
iter = size(ex_,1);
|
2012-07-31 21:50:28 +02:00
|
|
|
endo_nbr = M_.endo_nbr;
|
|
|
|
exo_nbr = M_.exo_nbr;
|
2010-05-25 14:00:08 +02:00
|
|
|
|
2010-11-19 16:30:55 +01:00
|
|
|
y_ = zeros(size(y0,1),iter+M_.maximum_lag);
|
2010-05-25 14:00:08 +02:00
|
|
|
y_(:,1) = y0;
|
2009-12-16 18:17:34 +01:00
|
|
|
|
2015-03-07 12:37:36 +01:00
|
|
|
if options_.loglinear && ~options_.logged_steady_state
|
2021-05-11 14:25:44 +02:00
|
|
|
k = get_all_variables_but_lagged_leaded_exogenous(M_);
|
|
|
|
dr.ys(k)=log(dr.ys(k));
|
2015-03-07 12:37:36 +01:00
|
|
|
end
|
|
|
|
|
2013-05-11 12:14:11 +02:00
|
|
|
if ~options_.k_order_solver || (options_.k_order_solver && options_.pruning) %if k_order_pert is not used or if we do not use Dynare++ with k_order_pert
|
2010-11-18 14:55:07 +01:00
|
|
|
if iorder==1
|
2010-11-20 11:34:18 +01:00
|
|
|
y_(:,1) = y_(:,1)-dr.ys;
|
2010-11-18 14:55:07 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-25 12:04:44 +02:00
|
|
|
if options_.k_order_solver && ~options_.pruning % Call dynare++ routines.
|
2021-08-20 11:28:46 +02:00
|
|
|
if options_.order~=iorder
|
|
|
|
error(['The k_order_solver requires the specified approximation order to be '...
|
|
|
|
'consistent with the one used for computing the decision rules'])
|
|
|
|
end
|
2021-10-11 16:11:45 +02:00
|
|
|
y_start=y_(:,1); %store first period required for output
|
2021-11-26 16:39:38 +01:00
|
|
|
y_ = k_order_simul(iorder,M_.nstatic,M_.npred,M_.nboth,M_.nfwrd,exo_nbr, ...
|
|
|
|
y_start(dr.order_var,:),ex_',dr.ys(dr.order_var),dr);
|
2009-12-16 18:17:34 +01:00
|
|
|
y_(dr.order_var,:) = y_;
|
2021-10-11 16:11:45 +02:00
|
|
|
y_=[y_start y_];
|
2009-12-16 18:17:34 +01:00
|
|
|
else
|
2010-11-20 16:11:42 +01:00
|
|
|
if options_.block
|
2011-06-18 17:43:45 +02:00
|
|
|
if M_.maximum_lag > 0
|
|
|
|
k2 = dr.state_var;
|
|
|
|
else
|
|
|
|
k2 = [];
|
2017-05-16 12:42:01 +02:00
|
|
|
end
|
2012-07-31 21:50:28 +02:00
|
|
|
order_var = 1:endo_nbr;
|
2011-06-18 17:43:45 +02:00
|
|
|
dr.order_var = order_var;
|
2010-11-20 16:11:42 +01:00
|
|
|
else
|
|
|
|
k2 = dr.kstate(find(dr.kstate(:,2) <= M_.maximum_lag+1),[1 2]);
|
2012-07-31 21:50:28 +02:00
|
|
|
k2 = k2(:,1)+(M_.maximum_lag+1-k2(:,2))*endo_nbr;
|
2010-11-20 16:11:42 +01:00
|
|
|
order_var = dr.order_var;
|
2017-05-16 12:42:01 +02:00
|
|
|
end
|
2017-05-16 15:10:20 +02:00
|
|
|
|
2010-05-25 14:00:08 +02:00
|
|
|
switch iorder
|
|
|
|
case 1
|
2010-11-20 11:34:18 +01:00
|
|
|
if isempty(dr.ghu)% For (linearized) deterministic models.
|
2010-11-19 16:30:55 +01:00
|
|
|
for i = 2:iter+M_.maximum_lag
|
2010-11-20 16:11:42 +01:00
|
|
|
yhat = y_(order_var(k2),i-1);
|
|
|
|
y_(order_var,i) = dr.ghx*yhat;
|
2009-12-16 18:17:34 +01:00
|
|
|
end
|
2010-11-20 11:34:18 +01:00
|
|
|
elseif isempty(dr.ghx)% For (linearized) purely forward variables (no state variables).
|
|
|
|
y_(dr.order_var,:) = dr.ghu*transpose(ex_);
|
2009-12-16 18:17:34 +01:00
|
|
|
else
|
2010-11-20 11:34:18 +01:00
|
|
|
epsilon = dr.ghu*transpose(ex_);
|
2010-11-19 16:30:55 +01:00
|
|
|
for i = 2:iter+M_.maximum_lag
|
2010-11-20 16:11:42 +01:00
|
|
|
yhat = y_(order_var(k2),i-1);
|
|
|
|
y_(order_var,i) = dr.ghx*yhat + epsilon(:,i-1);
|
2009-12-16 18:17:34 +01:00
|
|
|
end
|
|
|
|
end
|
2010-11-18 14:55:07 +01:00
|
|
|
y_ = bsxfun(@plus,y_,dr.ys);
|
2010-05-25 14:00:08 +02:00
|
|
|
case 2
|
2010-11-20 16:11:42 +01:00
|
|
|
constant = dr.ys(order_var)+.5*dr.ghs2;
|
2013-04-25 12:04:44 +02:00
|
|
|
if options_.pruning
|
2010-05-25 14:00:08 +02:00
|
|
|
y__ = y0;
|
2010-11-19 16:30:55 +01:00
|
|
|
for i = 2:iter+M_.maximum_lag
|
2010-11-20 16:11:42 +01:00
|
|
|
yhat1 = y__(order_var(k2))-dr.ys(order_var(k2));
|
|
|
|
yhat2 = y_(order_var(k2),i-1)-dr.ys(order_var(k2));
|
2010-05-25 14:00:08 +02:00
|
|
|
epsilon = ex_(i-1,:)';
|
2020-01-10 17:55:57 +01:00
|
|
|
abcOut1 = A_times_B_kronecker_C(.5*dr.ghxx,yhat1);
|
|
|
|
abcOut2 = A_times_B_kronecker_C(.5*dr.ghuu,epsilon);
|
|
|
|
abcOut3 = A_times_B_kronecker_C(dr.ghxu,yhat1,epsilon);
|
2010-11-20 16:11:42 +01:00
|
|
|
y_(order_var,i) = constant + dr.ghx*yhat2 + dr.ghu*epsilon ...
|
2010-09-21 10:40:18 +02:00
|
|
|
+ abcOut1 + abcOut2 + abcOut3;
|
2010-11-20 16:11:42 +01:00
|
|
|
y__(order_var) = dr.ys(order_var) + dr.ghx*yhat1 + dr.ghu*epsilon;
|
2010-05-25 14:00:08 +02:00
|
|
|
end
|
|
|
|
else
|
2010-11-19 16:30:55 +01:00
|
|
|
for i = 2:iter+M_.maximum_lag
|
2010-11-20 16:11:42 +01:00
|
|
|
yhat = y_(order_var(k2),i-1)-dr.ys(order_var(k2));
|
2010-05-25 14:00:08 +02:00
|
|
|
epsilon = ex_(i-1,:)';
|
2020-01-10 17:55:57 +01:00
|
|
|
abcOut1 = A_times_B_kronecker_C(.5*dr.ghxx,yhat);
|
|
|
|
abcOut2 = A_times_B_kronecker_C(.5*dr.ghuu,epsilon);
|
|
|
|
abcOut3 = A_times_B_kronecker_C(dr.ghxu,yhat,epsilon);
|
2010-05-25 14:00:08 +02:00
|
|
|
y_(dr.order_var,i) = constant + dr.ghx*yhat + dr.ghu*epsilon ...
|
2010-09-21 10:40:18 +02:00
|
|
|
+ abcOut1 + abcOut2 + abcOut3;
|
2010-05-25 14:00:08 +02:00
|
|
|
end
|
2017-05-16 15:10:20 +02:00
|
|
|
end
|
2012-07-31 21:50:28 +02:00
|
|
|
case 3
|
|
|
|
% only with pruning
|
2013-04-21 21:27:52 +02:00
|
|
|
% the third moments of the shocks are assumed null. We don't have
|
|
|
|
% an interface for specifying them
|
2012-07-31 21:50:28 +02:00
|
|
|
ghx = dr.ghx;
|
|
|
|
ghu = dr.ghu;
|
|
|
|
ghxx = dr.ghxx;
|
|
|
|
ghxu = dr.ghxu;
|
|
|
|
ghuu = dr.ghuu;
|
|
|
|
ghs2 = dr.ghs2;
|
|
|
|
ghxxx = dr.ghxxx;
|
|
|
|
ghxxu = dr.ghxxu;
|
|
|
|
ghxuu = dr.ghxuu;
|
|
|
|
ghuuu = dr.ghuuu;
|
|
|
|
ghxss = dr.ghxss;
|
|
|
|
ghuss = dr.ghuss;
|
2012-11-16 20:05:13 +01:00
|
|
|
nspred = M_.nspred;
|
|
|
|
ipred = M_.nstatic+(1:nspred);
|
2013-04-25 12:04:44 +02:00
|
|
|
%construction follows Andreasen et al (2013), Technical
|
|
|
|
%Appendix, Formulas (65) and (66)
|
|
|
|
%split into first, second, and third order terms
|
|
|
|
yhat1 = y0(order_var(k2))-dr.ys(order_var(k2));
|
|
|
|
yhat2 = zeros(nspred,1);
|
|
|
|
yhat3 = zeros(nspred,1);
|
|
|
|
for i=2:iter+M_.maximum_lag
|
|
|
|
u = ex_(i-1,:)';
|
|
|
|
%construct terms of order 2 from second order part, based
|
|
|
|
%on linear component yhat1
|
2020-01-10 17:55:57 +01:00
|
|
|
gyy = A_times_B_kronecker_C(ghxx,yhat1);
|
|
|
|
guu = A_times_B_kronecker_C(ghuu,u);
|
|
|
|
gyu = A_times_B_kronecker_C(ghxu,yhat1,u);
|
2013-04-25 12:04:44 +02:00
|
|
|
%construct terms of order 3 from second order part, based
|
|
|
|
%on order 2 component yhat2
|
2020-01-10 17:55:57 +01:00
|
|
|
gyy12 = A_times_B_kronecker_C(ghxx,yhat1,yhat2);
|
|
|
|
gy2u = A_times_B_kronecker_C(ghxu,yhat2,u);
|
2017-05-16 15:10:20 +02:00
|
|
|
%construct terms of order 3, all based on first order component yhat1
|
2013-04-25 12:04:44 +02:00
|
|
|
y2a = kron(yhat1,yhat1);
|
2020-01-10 17:55:57 +01:00
|
|
|
gyyy = A_times_B_kronecker_C(ghxxx,y2a,yhat1);
|
2013-04-25 12:04:44 +02:00
|
|
|
u2a = kron(u,u);
|
2020-01-10 17:55:57 +01:00
|
|
|
guuu = A_times_B_kronecker_C(ghuuu,u2a,u);
|
2013-04-25 12:04:44 +02:00
|
|
|
yu = kron(yhat1,u);
|
2020-01-10 17:55:57 +01:00
|
|
|
gyyu = A_times_B_kronecker_C(ghxxu,yhat1,yu);
|
|
|
|
gyuu = A_times_B_kronecker_C(ghxuu,yu,u);
|
2013-04-25 12:04:44 +02:00
|
|
|
%add all terms of order 3, linear component based on third
|
|
|
|
%order yhat3
|
2013-04-26 09:50:56 +02:00
|
|
|
yhat3 = ghx*yhat3 +gyy12 ... % prefactor is 1/2*2=1, see (65) Appendix Andreasen et al.
|
|
|
|
+ gy2u ... % prefactor is 1/2*2=1, see (65) Appendix Andreasen et al.
|
2013-04-25 12:04:44 +02:00
|
|
|
+ 1/6*(gyyy + guuu + 3*(gyyu + gyuu + ghxss*yhat1 + ghuss*u)); %note: s is treated as variable, thus xss and uss are third order
|
|
|
|
yhat2 = ghx*yhat2 + 1/2*(gyy + guu + 2*gyu + ghs2);
|
|
|
|
yhat1 = ghx*yhat1 + ghu*u;
|
|
|
|
y_(order_var,i) = dr.ys(order_var)+yhat1 + yhat2 + yhat3; %combine terms again
|
|
|
|
yhat1 = yhat1(ipred);
|
|
|
|
yhat2 = yhat2(ipred);
|
|
|
|
yhat3 = yhat3(ipred);
|
2017-05-16 15:10:20 +02:00
|
|
|
end
|
2019-04-15 18:50:16 +02:00
|
|
|
otherwise
|
2021-06-10 14:40:19 +02:00
|
|
|
error(['pruning not available for order = ' int2str(iorder)])
|
2009-12-16 18:17:34 +01:00
|
|
|
end
|
2012-11-16 20:05:13 +01:00
|
|
|
end
|