Debian Codegraph @

tests/stochastic_simulations/pruning/AS_pruned_state_space_red_shock.mod

raw
1 % This mod file compares the functionality of Dynare's pruned_state_space.m with the
2 % external Dynare pruning toolbox of Andreasen, Fernández-Villaverde and Rubio-Ramírez (2018):
3 % "The Pruned State-Space System for Non-Linear DSGE Models: Theory and Empirical Applications",
4 % Review of Economic Studies, Volume 85, Issue 1, Pages 1–49.
5 % The model under study is taken from An and Schorfheide (2007): "Bayesian Analysis of DSGE Models",
6 % Econometric Reviews, Volume 26, Issue 2-4, Pages 113-172.
7 % Note that we use version 2 of the toolbox, i.e. the one which is called
8 % "Third-order GMM estimate package for DSGE models (version 2)" and can be
9 % downloaded from https://sites.google.com/site/mandreasendk/home-1
10 %
11 % Created by @wmutschl (Willi Mutschler, [email protected])
12 %
13 % =========================================================================
14 % Copyright © 2020 Dynare Team
15 %
16 % This file is part of Dynare.
17 %
18 % Dynare is free software: you can redistribute it and/or modify
19 % it under the terms of the GNU General Public License as published by
20 % the Free Software Foundation, either version 3 of the License, or
21 % (at your option) any later version.
22 %
23 % Dynare is distributed in the hope that it will be useful,
24 % but WITHOUT ANY WARRANTY; without even the implied warranty of
25 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 % GNU General Public License for more details.
27 %
28 % You should have received a copy of the GNU General Public License
29 % along with Dynare. If not, see <https://www.gnu.org/licenses/>.
30 % =========================================================================
31
32 % set this to 1 if you want to recompute using the Andreasen et al toolbox
33 % otherwise the results are loaded from Andreasen_et_al_2018_Dynare44Pruning_v2.mat
34 @#define Andreasen_et_al_toolbox = 0
35
36 var YGR INFL INT
37 c p R g y z; %if ordering of var is changed comparison code below needs to be adapted
38 varexo e_r e_g e_z e_junk;
39 parameters tau nu kap cyst psi1 psi2 rhor rhog rhoz rrst pist gamst;
40
41 tau = 2;
42 nu = 0.1;
43 kap = 0.33;
44 cyst = 0.85;
45 psi1 = 1.5;
46 psi2 = 0.125;
47 rhor = 0.75;
48 rhog = 0.95;
49 rhoz = 0.9;
50 rrst = 1;
51 pist = 3.2;
52 gamst = 0.55;
53 sig_r = .2;
54 sig_g = .6;
55 sig_z = .3;
56
57 model;
58 #pist2 = exp(pist/400);
59 #rrst2 = exp(rrst/400);
60 #bet = 1/rrst2;
61 #phi = tau*(1-nu)/nu/kap/pist2^2;
62 #gst = 1/cyst;
63 #cst = (1-nu)^(1/tau);
64 #yst = cst*gst;
65 #dy = y-y(-1);
66 1 = exp(-tau*c(+1)+tau*c+R-z(+1)-p(+1));
67 (1-nu)/nu/phi/(pist2^2)*(exp(tau*c)-1) = (exp(p)-1)*((1-1/2/nu)*exp(p)+1/2/nu) - bet*(exp(p(+1))-1)*exp(-tau*c(+1)+tau*c+y(+1)-y+p(+1));
68 exp(c-y) = exp(-g) - phi*pist2^2*gst/2*(exp(p)-1)^2;
69 R = rhor*R(-1) + (1-rhor)*psi1*p + (1-rhor)*psi2*(y-g) + e_r;
70 g = rhog*g(-1) + e_g;
71 z = rhoz*z(-1) + e_z;
72 YGR = gamst+100*(dy+z);
73 INFL = pist+400*p + e_junk;
74 INT = pist+rrst+4*gamst+400*R;
75 end;
76
77 shocks;
78 var e_r = sig_r^2;
79 var e_g = sig_g^2;
80 var e_z = sig_z^2;
81 var e_junk = 0;
82 end;
83
84 steady_state_model;
85 y = 0;
86 R = 0;
87 g = 0;
88 z = 0;
89 c = 0;
90 p = 0;
91 YGR = gamst;
92 INFL = pist;
93 INT = pist + rrst + 4*gamst;
94 end;
95
96 steady; check; model_diagnostics;
97
98 @#for orderApp in [1, 2, 3]
99 stoch_simul(order=@{orderApp},pruning,irf=0,periods=0);
100 pruned_state_space.order_@{orderApp} = pruned_SS.pruned_state_space_system(M_, options_, oo_.dr, [], options_.ar, 1, 0);
101 @#if Andreasen_et_al_toolbox
102 addpath('Dynare44Pruning_v2/simAndMoments3order'); %provide path to toolbox
103 optPruning.orderApp = @{orderApp};
104 outAndreasenetal.order_@{orderApp} = RunDynarePruning(optPruning,oo_,M_,[oo_.dr.ghx oo_.dr.ghu]);
105 rmpath('Dynare44Pruning_v2/simAndMoments3order');
106 close all;
107 @#endif
108 @#endfor
109
110 @#if Andreasen_et_al_toolbox
111 delete Andreasen_et_al_2018_Dynare44Pruning_v2.mat;
112 pause(3);
113 save('Andreasen_et_al_2018_Dynare44Pruning_v2.mat', 'outAndreasenetal')
114 pause(3);
115 @#endif
116
117 load('Andreasen_et_al_2018_Dynare44Pruning_v2.mat')
118
119 % Make comparisons only at orders 1 and 2
120 for iorder = 1:3
121 fprintf('ORDER %d:\n',iorder);
122 pruned = pruned_state_space.(sprintf('order_%d',iorder));
123 outAndreasen = outAndreasenetal.(sprintf('order_%d',iorder));
124 %make sure variable ordering is correct
125 if ~isequal(M_.endo_names,[outAndreasen.label_y; outAndreasen.label_v(1:M_.nspred)])
126 error('variable ordering is not the same, change declaration order');
127 end
128 norm_E_yx = norm(pruned.E_y(oo_.dr.inv_order_var) - [outAndreasen.Mean_y; outAndreasen.Mean_v(1:M_.nspred)] , Inf);
129 fprintf('max(sum(abs(E[y;x]''))): %d\n',norm_E_yx);
130 norm_Var_y = norm(pruned.Var_y(oo_.dr.inv_order_var(1:(M_.endo_nbr-M_.nspred)),oo_.dr.inv_order_var(1:(M_.endo_nbr-M_.nspred))) - outAndreasen.Var_y , Inf);
131 fprintf('max(sum(abs(Var[y]''))):: %d\n',norm_Var_y);
132 norm_Var_x = norm(pruned.Var_y(M_.nstatic+(1:M_.nspred),M_.nstatic+(1:M_.nspred)) - outAndreasen.Var_v(1:M_.nspred,1:M_.nspred) , Inf);
133 fprintf('max(sum(abs(Var[x]''))): %d\n',norm_Var_x);
134 norm_Corr_yi1 = norm(pruned.Corr_yi(oo_.dr.inv_order_var(1:(M_.endo_nbr-M_.nspred)),oo_.dr.inv_order_var(1:(M_.endo_nbr-M_.nspred)),1) - outAndreasen.Corr_y(:,:,1) , Inf);
135 fprintf('max(sum(abs(Corr[y,y(-1)]''))): %d\n',norm_Corr_yi1);
136 norm_Corr_yi2 = norm(pruned.Corr_yi(oo_.dr.inv_order_var(1:(M_.endo_nbr-M_.nspred)),oo_.dr.inv_order_var(1:(M_.endo_nbr-M_.nspred)),2) - outAndreasen.Corr_y(:,:,2) , Inf);
137 fprintf('max(sum(abs(Corr[y,y(-2)]''))): %d\n',norm_Corr_yi2);
138 norm_Corr_xi1 = norm(pruned.Corr_yi(M_.nstatic+(1:M_.nspred),M_.nstatic+(1:M_.nspred),1) - outAndreasen.Corr_v(1:M_.nspred,1:M_.nspred,1) , Inf);
139 fprintf('max(sum(abs(Corr[x,x(-1)]''))): %d\n',norm_Corr_xi1);
140 norm_Corr_xi2 = norm(pruned.Corr_yi(M_.nstatic+(1:M_.nspred),M_.nstatic+(1:M_.nspred),2) - outAndreasen.Corr_v(1:M_.nspred,1:M_.nspred,2) , Inf);
141 fprintf('max(sum(abs(Corr[x,x(-2)]''))): %d\n',norm_Corr_xi2);
142
143 if iorder < 3 && any([norm_E_yx norm_Var_y norm_Var_x norm_Corr_yi1 norm_Corr_yi2 norm_Corr_xi1 norm_Corr_xi2] > 1e-5)
144 error('Something wrong with pruned_state_space.m compared to Andreasen et al 2018 Toolbox v2 at order %d.',iorder);
145 end
146 if iorder==3
147 pruned_without_shock = load(['AnSchorfheide_pruned_state_space' filesep 'Output' filesep 'AnSchorfheide_pruned_state_space_results.mat']);
148 pruned_without_shock = pruned_without_shock.oo_.pruned;
149 norm_E_yx = norm(pruned.E_y - pruned_without_shock.E_y , Inf);
150 fprintf('max(sum(abs(E[y;x]''))): %d\n',norm_E_yx);
151 norm_Var_y = norm(pruned.Var_y - pruned_without_shock.Var_y, Inf);
152 fprintf('max(sum(abs(Var[x]''))): %d\n',norm_Var_x);
153 norm_Corr_yi1 = norm(pruned.Corr_yi(:,:,1) - pruned_without_shock.Corr_yi(:,:,1), Inf);
154 fprintf('max(sum(abs(Corr[y,y(-1)]''))): %d\n',norm_Corr_yi1);
155 norm_Corr_yi2 = norm(pruned.Corr_yi(:,:,2) - pruned_without_shock.Corr_yi(:,:,2), Inf);
156 fprintf('max(sum(abs(Corr[y,y(-2)]''))): %d\n',norm_Corr_yi2);
157 end
158 end
159 % skipline();
160 % fprintf('Note that at third order, there is an error in the computation of Var_z in Andreasen et al (2018)''s toolbox, @wmutschl is in contact to clarify this.\n');
161 % fprintf('EXAMPLE:\n')
162 % fprintf(' Consider Var[kron(kron(xf,xf),xf)] = E[kron(kron(kron(kron(kron(xf,xf),xf),xf),xf),xf)] - E[kron(kron(xf,xf),xf)]*E[kron(kron(xf,xf),xf)].''\n');
163 % fprintf(' Now note that xf=hx*xf(-1)+hu*u is Gaussian, that is E[kron(kron(xf,xf),xf)]=0, and Var[kron(kron(xf,xf),xf)] are the sixth-order product moments\n');
164 % fprintf(' which can be computed using the prodmom.m function by providing E[xf*xf''] as covariance matrix.\n');
165 % fprintf(' In order to replicate this you have to change UnconditionalMoments_3rd_Lyap.m to also output Var_z.\n')
166 %
167 % dynare_nx = M_.nspred;
168 % dynare_E_xf2 = pruned_state_space.order_3.Var_z(1:dynare_nx,1:dynare_nx);
169 % dynare_E_xf6 = pruned_state_space.order_3.Var_z((end-dynare_nx^3+1):end,(end-dynare_nx^3+1):end);
170 % dynare_E_xf6 = dynare_E_xf6(:);
171 %
172 % Andreasen_nx = M_.nspred+M_.exo_nbr;
173 % Andreasen_E_xf2 = outAndreasenetal.order_3.Var_z(1:Andreasen_nx,1:Andreasen_nx);
174 % Andreasen_E_xf6 = outAndreasenetal.order_3.Var_z((end-Andreasen_nx^3+1):end,(end-Andreasen_nx^3+1):end);
175 % Andreasen_E_xf6 = Andreasen_E_xf6(:);
176 %
177 % fprintf('Second-order product moments of xf and u are the same:\n')
178 % norm_E_xf2 = norm(dynare_E_xf2-Andreasen_E_xf2(1:M_.nspred,1:M_.nspred),Inf)
179 % norm_E_uu = norm(M_.Sigma_e-Andreasen_E_xf2(M_.nspred+(1:M_.exo_nbr),M_.nspred+(1:M_.exo_nbr)),Inf)
180 %
181 % % Compute unique sixth-order product moments of xf, i.e. unique(E[kron(kron(kron(kron(kron(xf,xf),xf),xf),xf),xf)],'stable')
182 % dynare_nx6 = dynare_nx*(dynare_nx+1)/2*(dynare_nx+2)/3*(dynare_nx+3)/4*(dynare_nx+4)/5*(dynare_nx+5)/6;
183 % dynare_Q6Px = Q6_plication(dynare_nx);
184 % dynare_COMBOS6 = flipud(allVL1(dynare_nx, 6)); %all possible (unique) combinations of powers that sum up to six
185 % dynare_true_E_xf6 = zeros(dynare_nx6,1); %only unique entries
186 % for j6 = 1:size(dynare_COMBOS6,1)
187 % dynare_true_E_xf6(j6) = prodmom(dynare_E_xf2, 1:dynare_nx, dynare_COMBOS6(j6,:));
188 % end
189 % dynare_true_E_xf6 = dynare_Q6Px*dynare_true_E_xf6; %add duplicate entries
190 % norm_dynare_E_xf6 = norm(dynare_true_E_xf6 - dynare_E_xf6, Inf);
191 %
192 % Andreasen_nx6 = Andreasen_nx*(Andreasen_nx+1)/2*(Andreasen_nx+2)/3*(Andreasen_nx+3)/4*(Andreasen_nx+4)/5*(Andreasen_nx+5)/6;
193 % Andreasen_Q6Px = Q6_plication(Andreasen_nx);
194 % Andreasen_COMBOS6 = flipud(allVL1(Andreasen_nx, 6)); %all possible (unique) combinations of powers that sum up to six
195 % Andreasen_true_E_xf6 = zeros(Andreasen_nx6,1); %only unique entries
196 % for j6 = 1:size(Andreasen_COMBOS6,1)
197 % Andreasen_true_E_xf6(j6) = prodmom(Andreasen_E_xf2, 1:Andreasen_nx, Andreasen_COMBOS6(j6,:));
198 % end
199 % Andreasen_true_E_xf6 = Andreasen_Q6Px*Andreasen_true_E_xf6; %add duplicate entries
200 % norm_Andreasen_E_xf6 = norm(Andreasen_true_E_xf6 - Andreasen_E_xf6, Inf);
201 %
202 % fprintf('Sixth-order product moments of xf and u are not the same!\n');
203 % fprintf(' Dynare maximum absolute deviations of sixth-order product moments of xf: %d\n',norm_dynare_E_xf6)
204 % fprintf(' Andreasen et al maximum absolute deviations of sixth-order product moments of xf: %d\n',norm_Andreasen_E_xf6)
205 % skipline();
206 % fprintf('Note that the standard deviations are set quite high to make the numerical differences more apparent.\n');