Debian Codegraph @

tests/moments/fs2000_post_moments.mod

raw
1 /*
2 * This file replicates the estimation of the cash in advance model (termed M1
3 * in the paper) described in Frank Schorfheide (2000): "Loss function-based
4 * evaluation of DSGE models", Journal of Applied Econometrics, 15(6), 645-670.
5 *
6 * The data are in file "fsdat_simul.m", and have been artificially generated.
7 * They are therefore different from the original dataset used by Schorfheide.
8 *
9 * The prior distribution follows the one originally specified in Schorfheide's
10 * paper, except for parameter rho. In the paper, the elicited beta prior for rho
11 * implies an asymptote and corresponding prior mode at 0. It is generally
12 * recommended to avoid this extreme type of prior. Some optimizers, for instance
13 * mode_compute=12 (Mathworks' particleswarm algorithm) may find a posterior mode
14 * with rho equal to zero. We lowered the value of the prior standard deviation
15 * (changing .223 to .100) to remove the asymptote.
16 *
17 * The equations are taken from J. Nason and T. Cogley (1994): "Testing the
18 * implications of long-run neutrality for monetary business cycle models",
19 * Journal of Applied Econometrics, 9, S37-S70.
20 * Note that there is an initial minus sign missing in equation (A1), p. S63.
21 *
22 * This implementation was originally written by Michel Juillard. Please note that the
23 * following copyright notice only applies to this Dynare implementation of the
24 * model.
25 */
26
27 /*
28 * Copyright © 2004-2017 Dynare Team
29 *
30 * This file is part of Dynare.
31 *
32 * Dynare is free software: you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation, either version 3 of the License, or
35 * (at your option) any later version.
36 *
37 * Dynare is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
41 *
42 * You should have received a copy of the GNU General Public License
43 * along with Dynare. If not, see <https://www.gnu.org/licenses/>.
44 */
45
46 var m P c e W R k d n l gy_obs gp_obs y dA;
47 varexo e_a e_m;
48
49 parameters alp bet gam mst rho psi del;
50
51 alp = 0.33;
52 bet = 0.99;
53 gam = 0.003;
54 mst = 1.011;
55 rho = 0.7;
56 psi = 0.787;
57 del = 0.02;
58
59 model;
60 dA = exp(gam+e_a);
61 log(m) = (1-rho)*log(mst) + rho*log(m(-1))+e_m;
62 -P/(c(+1)*P(+1)*m)+bet*P(+1)*(alp*exp(-alp*(gam+log(e(+1))))*k^(alp-1)*n(+1)^(1-alp)+(1-del)*exp(-(gam+log(e(+1)))))/(c(+2)*P(+2)*m(+1))=0;
63 W = l/n;
64 -(psi/(1-psi))*(c*P/(1-n))+l/n = 0;
65 R = P*(1-alp)*exp(-alp*(gam+e_a))*k(-1)^alp*n^(-alp)/W;
66 1/(c*P)-bet*P*(1-alp)*exp(-alp*(gam+e_a))*k(-1)^alp*n^(1-alp)/(m*l*c(+1)*P(+1)) = 0;
67 c+k = exp(-alp*(gam+e_a))*k(-1)^alp*n^(1-alp)+(1-del)*exp(-(gam+e_a))*k(-1);
68 P*c = m;
69 m-1+d = l;
70 e = exp(e_a);
71 y = k(-1)^alp*n^(1-alp)*exp(-alp*(gam+e_a));
72 gy_obs = dA*y/y(-1);
73 gp_obs = (P/P(-1))*m(-1)/dA;
74 end;
75
76 shocks;
77 var e_a; stderr 0.014;
78 var e_m; stderr 0.005;
79 end;
80
81 steady_state_model;
82 dA = exp(gam);
83 gst = 1/dA;
84 m = mst;
85 khst = ( (1-gst*bet*(1-del)) / (alp*gst^alp*bet) )^(1/(alp-1));
86 xist = ( ((khst*gst)^alp - (1-gst*(1-del))*khst)/mst )^(-1);
87 nust = psi*mst^2/( (1-alp)*(1-psi)*bet*gst^alp*khst^alp );
88 n = xist/(nust+xist);
89 P = xist + nust;
90 k = khst*n;
91
92 l = psi*mst*n/( (1-psi)*(1-n) );
93 c = mst/P;
94 d = l - mst + 1;
95 y = k^alp*n^(1-alp)*gst^alp;
96 R = mst/bet;
97 W = l/n;
98 ist = y-c;
99 q = 1 - d;
100
101 e = 1;
102
103 gp_obs = m/dA;
104 gy_obs = dA;
105 end;
106
107 steady;
108
109 check;
110
111 estimated_params;
112 alp, beta_pdf, 0.356, 0.02;
113 bet, beta_pdf, 0.993, 0.002;
114 gam, normal_pdf, 0.0085, 0.003;
115 mst, normal_pdf, 1.0002, 0.007;
116 rho, beta_pdf, 0.129, 0.100;
117 psi, beta_pdf, 0.65, 0.05;
118 del, beta_pdf, 0.01, 0.005;
119 stderr e_a, inv_gamma_pdf, 0.035449, inf;
120 stderr e_m, inv_gamma_pdf, 0.008862, inf;
121 end;
122
123 varobs gp_obs gy_obs;
124
125 estimation(order=1,mode_compute=5,silent_optimizer, datafile='../fs2000/fsdat_simul.m', nobs=192, loglinear, mh_replic=20, mh_nblocks=1, mh_jscale=0.8,moments_varendo,
126 conditional_variance_decomposition=[2,2000],consider_all_endogenous,sub_draws=2);
127
128 stoch_simul(order=1,conditional_variance_decomposition=[2,2000],noprint,nograph);
129 par=load([M_.fname filesep 'metropolis' filesep M_.fname '_posterior_draws1']);
130
131 for par_iter=1:size(par.pdraws,1)
132 M_=set_parameters_locally(M_,par.pdraws{par_iter,1});
133 [info, oo_, options_, M_]=stoch_simul(M_, options_, oo_, var_list_);
134 correlation(:,:,par_iter)=cell2mat(oo_.autocorr);
135 covariance(:,:,par_iter)=oo_.var;
136 conditional_variance_decomposition(:,:,:,par_iter)=oo_.conditional_variance_decomposition;
137 variance_decomposition(:,:,par_iter)=oo_.variance_decomposition;
138 end
139
140 correlation=mean(correlation,3);
141 nvars=M_.orig_endo_nbr;
142 for var_iter_1=1:nvars
143 for var_iter_2=1:nvars
144 if max(abs(correlation(var_iter_1,var_iter_2:nvars:end)'-oo_.PosteriorTheoreticalMoments.dsge.correlation.Mean.(M_.endo_names{var_iter_1}).(M_.endo_names{var_iter_2})))>1e-8
145 error('Correlations do not match')
146 end
147 end
148 end
149
150 covariance=mean(covariance,3);
151 nvars=M_.orig_endo_nbr;
152 for var_iter_1=1:nvars
153 for var_iter_2=var_iter_1:nvars
154 if max(abs(covariance(var_iter_1,var_iter_2)-oo_.PosteriorTheoreticalMoments.dsge.covariance.Mean.(M_.endo_names{var_iter_1}).(M_.endo_names{var_iter_2})))>1e-8
155 error('Covariances do not match')
156 end
157 end
158 end
159
160 variance_decomposition=mean(variance_decomposition,3);
161 nvars=M_.orig_endo_nbr;
162 for var_iter_1=1:nvars
163 for shock_iter=1:M_.exo_nbr
164 if max(abs(variance_decomposition(var_iter_1,shock_iter)/100-oo_.PosteriorTheoreticalMoments.dsge.VarianceDecomposition.Mean.(M_.endo_names{var_iter_1}).(M_.exo_names{shock_iter})))>1e-8
165 error('Variance decomposition does not match')
166 end
167 end
168 end
169
170 conditional_variance_decomposition=mean(conditional_variance_decomposition,4);
171 nvars=M_.orig_endo_nbr;
172 horizon_size=size(conditional_variance_decomposition,2);
173 for var_iter_1=1:nvars
174 for shock_iter=1:M_.exo_nbr
175 for horizon_iter=1:horizon_size
176 if max(abs(conditional_variance_decomposition(var_iter_1,horizon_iter,shock_iter)-oo_.PosteriorTheoreticalMoments.dsge.ConditionalVarianceDecomposition.Mean.(M_.endo_names{var_iter_1}).(M_.exo_names{shock_iter})(horizon_iter)))>1e-8
177 error('Conditional Variance decomposition does not match')
178 end
179 end
180 end
181 end
182
183 // case with measurement error
184 estimated_params(overwrite);
185 alp, beta_pdf, 0.356, 0.02;
186 bet, beta_pdf, 0.993, 0.002;
187 gam, normal_pdf, 0.0085, 0.003;
188 mst, normal_pdf, 1.0002, 0.007;
189 rho, beta_pdf, 0.129, 0.100;
190 psi, beta_pdf, 0.65, 0.05;
191 del, beta_pdf, 0.01, 0.005;
192 stderr e_a, inv_gamma_pdf, 0.035449, inf;
193 stderr e_m, inv_gamma_pdf, 0.008862, inf;
194 stderr gp_obs, inv_gamma_pdf, 0.003, inf;
195 end;
196
197 estimation(order=1,mode_compute=5,silent_optimizer, datafile='../fs2000/fsdat_simul.m', nobs=192, loglinear, mh_replic=20, mh_nblocks=1, mh_jscale=0.8,moments_varendo,
198 conditional_variance_decomposition=[2,2000],consider_all_endogenous,sub_draws=2);
199
200 stoch_simul(order=1,conditional_variance_decomposition=[2,2000],noprint,nograph);
201 par=load([M_.fname filesep 'metropolis' filesep M_.fname '_posterior_draws1']);
202
203 for par_iter=1:size(par.pdraws,1)
204 M_=set_parameters_locally(M_,par.pdraws{par_iter,1});
205 [info, oo_, options_, M_]=stoch_simul(M_, options_, oo_, var_list_);
206 correlation(:,:,par_iter)=cell2mat(oo_.autocorr);
207 covariance(:,:,par_iter)=oo_.var;
208 conditional_variance_decomposition(:,:,:,par_iter)=oo_.conditional_variance_decomposition;
209 conditional_variance_decomposition_ME(:,:,:,par_iter)=oo_.conditional_variance_decomposition_ME;
210 variance_decomposition(:,:,par_iter)=oo_.variance_decomposition;
211 variance_decomposition_ME(:,:,par_iter)=oo_.variance_decomposition_ME;
212 [~,obs_order]=sort(options_.varobs_id);
213 end
214
215 correlation=mean(correlation,3);
216 nvars=M_.orig_endo_nbr;
217 for var_iter_1=1:nvars
218 for var_iter_2=1:nvars
219 if max(abs(correlation(var_iter_1,var_iter_2:nvars:end)'-oo_.PosteriorTheoreticalMoments.dsge.correlation.Mean.(M_.endo_names{var_iter_1}).(M_.endo_names{var_iter_2})))>1e-8
220 error('Correlations do not match')
221 end
222 end
223 end
224
225 covariance=mean(covariance,3);
226 nvars=M_.orig_endo_nbr;
227 for var_iter_1=1:nvars
228 for var_iter_2=var_iter_1:nvars
229 if max(abs(covariance(var_iter_1,var_iter_2)-oo_.PosteriorTheoreticalMoments.dsge.covariance.Mean.(M_.endo_names{var_iter_1}).(M_.endo_names{var_iter_2})))>1e-8
230 error('Covariances do not match')
231 end
232 end
233 end
234
235 variance_decomposition=mean(variance_decomposition,3);
236 nvars=M_.orig_endo_nbr;
237 for var_iter_1=1:nvars
238 for shock_iter=1:M_.exo_nbr
239 if max(abs(variance_decomposition(var_iter_1,shock_iter)/100-oo_.PosteriorTheoreticalMoments.dsge.VarianceDecomposition.Mean.(M_.endo_names{var_iter_1}).(M_.exo_names{shock_iter})))>1e-8
240 error('Variance decomposition does not match')
241 end
242 end
243 end
244
245 if isoctave
246 tol = 2.2e-3;
247 else
248 tol = 1e-8;
249 end
250
251 variance_decomposition_ME=mean(variance_decomposition_ME,3);
252 nvars=length(options_.varobs);
253 for var_iter_1=1:nvars
254 for shock_iter=1:M_.exo_nbr
255 if max(abs(variance_decomposition_ME(obs_order(var_iter_1),shock_iter)/100-oo_.PosteriorTheoreticalMoments.dsge.VarianceDecompositionME.Mean.(options_.varobs{var_iter_1}).(M_.exo_names{shock_iter}))) > tol
256 error('Variance decomposition does not match')
257 end
258 end
259 end
260
261 conditional_variance_decomposition=mean(conditional_variance_decomposition,4);
262 nvars=M_.orig_endo_nbr;
263 horizon_size=size(conditional_variance_decomposition,2);
264 for var_iter_1=1:nvars
265 for shock_iter=1:M_.exo_nbr
266 for horizon_iter=1:horizon_size
267 if max(abs(conditional_variance_decomposition(var_iter_1,horizon_iter,shock_iter)-oo_.PosteriorTheoreticalMoments.dsge.ConditionalVarianceDecomposition.Mean.(M_.endo_names{var_iter_1}).(M_.exo_names{shock_iter})(horizon_iter))) > tol
268 error('Conditional Variance decomposition does not match')
269 end
270 end
271 end
272 end
273
274 conditional_variance_decomposition_ME=mean(conditional_variance_decomposition_ME,4);
275 exo_names=[M_.exo_names;'ME'];
276 nvars=length(options_.varobs);
277 horizon_size=size(conditional_variance_decomposition_ME,2);
278 for var_iter_1=1:nvars
279 for shock_iter=1:M_.exo_nbr+1
280 for horizon_iter=1:horizon_size
281 if max(abs(conditional_variance_decomposition_ME(obs_order(var_iter_1),horizon_iter,shock_iter)-oo_.PosteriorTheoreticalMoments.dsge.ConditionalVarianceDecompositionME.Mean.(options_.varobs{var_iter_1}).(exo_names{shock_iter})(horizon_iter))) > tol
282 error('Conditional Variance decomposition does not match')
283 end
284 end
285 end
286 end
287
288
289 options_.prior_mc=100;
290 options_.qz_criterium = 1+1e-6;
291 oo_ = compute_moments_varendo('prior',options_,M_,oo_,estim_params_,M_.endo_names(1:M_.orig_endo_nbr));
292
293
294 /*
295 * The following lines were used to generate the data file. If you want to
296 * generate another random data file, comment the "estimation" line and uncomment
297 * the following lines.
298 */
299
300 //stoch_simul(periods=200, order=1);
301 //datatomfile('fsdat_simul', char('gy_obs', 'gp_obs'));