Debian Codegraph @

contrib/ms-sbvar/TZcode/MatlabFiles/imfvdscksim.m

raw
1 function imfvdscksim(xinput,sa0indx,simfindx)
2 % imfvdscksim(xinput,sa0indx,simfindx)
3 % Save the simulated pdfs of impulse responses, vd, shocks, and A0's;
4 % Ref: Waggoner and Zha "Does Normalization Matter for Inference"
5 % See note Forecast (2)
6 %
7 % xinput{1}: nfp -- total number of free parameters
8 % xinput{2}: nvar -- number of variables
9 % xinput{3}: xhat -- ML estimate of free parameters in A0
10 % xinput{4}: hess -- Hessian of -logLH
11 % xinput{5}:Indxv -- index for selected variables of interest; normall first 2 are of our interest
12 % to select variables, always check idmat0 to make sure
13 % it plots: (1) pdf of 1st v for every buffer, (2) scattered plot of 1st and 2nd for every buffer,
14 % (2) pdf of 1st v for all sequences; (4) scattered plot of 3rd and 4th for all sequences
15 % (5) scattered plot of 1st and 2nd for al sequences.
16 % xinput{6}: IndxGraph - 1: plot graphs; 0: no graphs
17 % xinput{7}: idmat0 -- Index for non-zero elements in A0 with column to equation
18 % xinput{8}: nstarts -- # of starting points in Gibbs sampling
19 % xinput{9}: ndraws1 -- # of 1st loop to be discarded
20 % xinput{10}: ndraws2 -- # of 2nd loop for saving A0 draws
21 % xinput{11}: imndraws=nstarts*ndraws2
22 % xinput{12}: a0indx -- index number for non-zero elements in A0
23 % xinput{13}: tdf -- degrees of freedom for t-distribution
24 % xinput{14}: nbuffer -- interval for printing, plotting, and saving
25 % xinput{15}: Sbd -- nvar-by-nvar S{1}, ..., S{n} -- kind of covariance matrix for each simultaneous equation
26 % xinput{16}: nSample -- the original sample size including lags
27 % xinput{17}: IndxNmlr -- index for which normalization rule to choose
28 % xinput{18}: IndxGibbs -- index for WZ Gibbs; 1; Gibbs; 0: Metropolis
29 % xinput{19}: scf -- reduction scale factor for Metropolis jumping kernel
30 % xinput{20}: H_sr -- covariance matrix for free elements in A0 (nfp-by-nfp)
31 % xinput{21}: fss -- effective sample size == nSample-lags+# of dummy observations
32 % xinput{22}: idfile1 -- calls "iden6std." Save stds. of both data and impulse responses in idfile1
33 % xinput{23}: xxhpc -- chol(X'X+inv(H_p_tilde)): upper triangular but its transpose
34 % is lower triagular Choleski
35 % xinput{24}: ImfErr -- if 1, impulse response simulation; if 0, disable this simulation
36 % xinput{25}: ninv -- number of bins pre-specified to put each draw of impulse response
37 % into a proper bin (or small interval)
38 % xinput{26}: imstp -- # of steps for impulse responses
39 % xinput{27}: forep -- forecast periods (# of steps)
40 % xinput{28}: yact -- actual data (in log except R, U, etc.)
41 % xinput{29}: yactqg -- quarterly annualized growth in actual data
42 % xinput{30}: yactCalyg -- calendar annual growth in actual data
43 % xinput{31}: imfml -- imstp-by-nvar^2 ML impulse responses
44 % xinput{32}: forepq -- forecast periods for quarterly growth
45 % xinput{33}: forepy -- forecast periods for annual growth
46 % xinput{34}: ncoef -- k: # of coeffients per equation
47 % xinput{35}: Bhml -- ML reduced form parameter B (nvar-by-k)
48 % xinput{36}: lags -- # of lags
49 % xinput{37}: Psuedo -- 1: for Pseudo out-of-sample; 0: for in-sample (or real-time out-of-sample)
50 % xinput{38}: q_m = 4 or 12 -- quarterly (4) or monthly (12)
51 % xinput{39}: imf3ml -- ML impulse responses with row--steps, column--nvar responses,
52 % 3rd dimension--nvar shocks
53 % xinput{40}: vlistlog -- sub index for log in vlist
54 % xinput{41}: vlistper -- sub index for percent in vlist
55 % xinput{42}: phi -- X in the form of y = X*B+U. Row: nSmaple-lags+ndobs. Column: ncoef
56 % xinput{43}: actup -- acutal periods for backing out structural shocks
57 % xinput{44}: A0ml -- ML A0; column-equation
58 % xinput{45}: Bhml -- ML Bh (k-by-nvar)
59 % xinput{46}: yrEnd -- the end year for the estimation period
60 % xinput{47}: qmEnd -- the end month or quarter for the estimation period
61 % xinput{48}: yrStart -- the start year for the estimation period
62 % xinput{49}: qmStart -- the start month or quarter for the estimation period
63 % sa0indx: special a0 index. 1: use this; 0: disenable
64 % simfindx: special impulse response index. 1: use it; 0: disenable
65 %------------------
66 % E.G.: imfcntmulti: (ninv+2,imstp*nvar^2,nstarts) matrix
67 % cnt: count; for impulse responses; multi (nstarts) sequences
68 % All output is saved in outB_W, including Range5, invc, ninv, imfcntmulti,
69 % sckcorcntmulti, Avhxcntmulti, lzvdcntmulti
70 %
71 % Written by TAZ 1999
72 %
73 % Copyright (C) 1997-2012 Tao Zha
74 %
75 % This free software: you can redistribute it and/or modify
76 % it under the terms of the GNU General Public License as published by
77 % the Free Software Foundation, either version 3 of the License, or
78 % (at your option) any later version.
79 %
80 % It is distributed in the hope that it will be useful,
81 % but WITHOUT ANY WARRANTY; without even the implied warranty of
82 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
83 % GNU General Public License for more details.
84 %
85 % If you did not received a copy of the GNU General Public License
86 % with this software, see <http://www.gnu.org/licenses/>.
87 %
88
89
90 nfp=xinput{1}; nvar=xinput{2}; xhat=xinput{3}; hess=xinput{4}; Indxv=xinput{5};
91 IndxGraph=xinput{6}; idmat0=xinput{7}; nstarts=xinput{8}; ndraws1=xinput{9}; ndraws2=xinput{10};
92 imndraws=xinput{11}; a0indx=xinput{12}; tdf=xinput{13}; nbuffer=xinput{14}; Sbd=xinput{15};
93 nSample = xinput{16}; IndxNmlr=xinput{17}; IndxGibbs=xinput{18}; scf=xinput{19}; H_sr=xinput{20};
94 fss=xinput{21}; idfile1=xinput{22}; xxhpc=xinput{23}; ImfErr=xinput{24}; ninv=xinput{25};
95 imstp=xinput{26}; forep=xinput{27}; yact=xinput{28}; yactqg=xinput{29}; yactCalyg=xinput{30};
96 imfml=xinput{31}; forepq=xinput{32}; forepy=xinput{33}; ncoef=xinput{34}; Bhml=xinput{35};
97 lags=xinput{36}; Psuedo=xinput{37}; q_m=xinput{38}; imf3ml=xinput{39}; vlistlog=xinput{40};
98 vlistper=xinput{41}; phi=xinput{42}; actup=xinput{43}; A0ml=xinput{44}; Bhml=xinput{45};
99 yrEnd=xinput{46}; qmEnd=xinput{47}; yrStart=xinput{48}; qmStart=xinput{49};
100
101
102 if Psuedo
103 disp('Make sure (1) Psuedo=0 in msstart.m and (2) "actuap=nSample-lags" for strucutral shocks')
104 disp('Press ctrl-c to abort now')
105 end
106
107 A0_h = zeros(nvar);
108 A0gbs = A0_h; % drawn A0 in Gibbs sampling
109 Avhxml = xhat; % ML estimate
110 Avhxmean = zeros(nfp,1);
111 Avhxs = Avhxmean;
112
113 A0xhat = zeros(nvar);
114 A0xhat(a0indx) = xhat;
115 % A0hatw = zeros(nvar^2,nbuffer);
116
117 countJump = zeros(nstarts,1);
118 imstpyer = floor(imstp/q_m); % yearly
119
120 imfmean = zeros(imstp,nvar^2);
121 imfs = imfmean;
122 imfyermean = zeros(imstpyer,nvar^2);
123 imfyers = imfyermean;
124 imfyer3ml = zeros(imstpyer,nvar,nvar);
125 imfyer3_h = zeros(imstpyer,nvar,nvar);
126
127
128 imfcntmulti = zeros(ninv+2,imstp*nvar^2,nstarts);
129 % cnt: count; for impulse responses; multi (nstarts) sequences
130 Avhxcntmulti = zeros(ninv+2,nfp,nstarts);
131 % cnt: count; for A0; multi (nstarts) sequences
132 imfyercntmulti = zeros(ninv+2,imstpyer*nvar^2,nstarts);
133 % cnt: count; for impulse responses; multi (nstarts) sequences
134 lzvdcntmulti = zeros(ninv+2,imstp*nvar^2,nstarts);
135 % cnt: count; for lz vd; multi (nstarts) sequences
136 lzvdyercntmulti = zeros(ninv+2,imstpyer*nvar^2,nstarts);
137 % cnt: count; for lz vd; multi (nstarts) sequences
138 sckcorcntmulti = zeros(ninv+2,nvar^2,nstarts);
139
140 %---------------------------------------------------
141 % Specify the range for counting the empirical distribution
142 %
143 %** load the standard deviations of 6 variables, one for log(y), one for gq, and
144 %** the third one for yg
145 eval(['load ' idfile1 '.prn -ascii']);
146 eval(['ABstd=' idfile1 ';']);
147 Range5 = cell(9,1); % 8: log, qg, yg, imf, Avhx, imfyer (yearly), lzvd,
148 % lzvdyer, sckcorv,
149
150 %@@@ Tony's trick to expand the matrix
151 %
152 %** In order of log(y), qg, and yg for Range5{i} for i=1:3
153 Range5{1} =zeros(forep,nvar,2); % 2: min and max
154 Range5{1}(:,:,1) = repmat(yact(length(yact(:,1)),:)-10*ABstd(1,:),[forep 1]); % min, 30 std.
155 Range5{1}(:,:,2) = repmat(yact(length(yact(:,1)),:)+10*ABstd(1,:),[forep 1]); % max, 30 std.
156 %
157 Range5{2} =zeros(forepq,nvar,2); % 2: min and max
158 Range5{2}(:,:,1) = repmat(yactqg(length(yactqg(:,1)),:)-10*ABstd(2,:),[forepq 1]); % min, 30 std.
159 Range5{2}(:,:,2) = repmat(yactqg(length(yactqg(:,1)),:)+10*ABstd(2,:),[forepq 1]); % max, 30 std.
160 %
161 Range5{3} =zeros(forepy,nvar,2); % 2: min and max
162 Range5{3}(:,:,1) = repmat(yactCalyg(length(yactCalyg(:,1)),:)-10*ABstd(3,:),[forepy 1]); % min, 30 std.
163 Range5{3}(:,:,2) = repmat(yactCalyg(length(yactCalyg(:,1)),:)+10*ABstd(3,:),[forepy 1]); % max, 30 std.
164 %
165 Range5{4} =zeros(imstp,nvar^2,2); % 2: min and max
166 imfscale = repmat(ABstd(4,:),[1 nvar]); % because nvar variables to 1, ..., nvar shocks
167 Range5{4}(:,:,1) = repmat(imfml(1,:)-5*imfscale,[imstp 1]); % min, 5 std.
168 Range5{4}(:,:,2) = repmat(imfml(1,:)+5*imfscale,[imstp 1]); % max, 5 std.
169 % Range5(4)(:,:,1): imstp-by-nvar^2. Column: nvar responses to 1st shock,
170 % nvar responses to 2nd shock, ...
171 %
172 %*** for parameters A0's
173 Range5{5} =zeros(nfp,2); % 2: min and max
174 Avhxscale = abs(Avhxml);
175 Range5{5}(:,1) = Avhxml-5*Avhxscale; % min, 5 std.
176 Range5{5}(:,2) = Avhxml+5*Avhxscale; % max, 5 std.
177
178 %
179 %*** for yearly impulse responses
180 yer3 = zeros(1+imstpyer,nvar,nvar);
181 for k=1:imstpyer
182 yer3(k+1,:,:) = mean(imf3ml(1+q_m*(k-1):q_m*k,:,:),1); % annual average
183 % yer3: row--steps, column--nvar responses, 3rd dimension--nvar shocks
184 end
185 imfyer3ml(:,vlistlog,:) = ( exp(yer3(2:1+imstpyer,vlistlog,:)-yer3(1:imstpyer,vlistlog,:)) - 1 ).*100;
186 imfyer3ml(:,vlistper,:) = yer3(2:1+imstpyer,vlistper,:) .* 100;
187 % imfyer3ml: row--steps, column--nvar responses, 3rd dimension--nvar shocks
188 tmp = max(squeeze(max(imfyer3ml,[],1)),[],2); % nvar-by-1
189 tmp = tmp'; % 1-by-nvar (variables)
190 imfyerml = reshape(imfyer3ml,imstpyer,nvar^2);
191 imfyermlmax = max(abs(imfyerml)); % for error bands (imfyercnt) later
192 %*** for annual impulse responses
193 Range5{6} =zeros(imstpyer,nvar^2,2); % 2: min and max
194 tmp = repmat(tmp,[1 nvar]); % because nvar variables to 1, ..., nvar shocks
195 imfyerscl = repmat(tmp,[imstpyer,1]); % imstpyer-by-nvar^2
196 Range5{6}(:,:,1) = imfyerml-5*imfyerscl; % min, 5 std.
197 Range5{6}(:,:,2) = imfyerml+5*imfyerscl; % max, 5 std.
198 % Range5(6)(:,:,1): imstpyer-by-nvar^2. Column: nvar responses to 1st shock,
199 % nvar responses to 2nd shock, ...
200
201 %*** lz variance decomposition (nunlike the traditional, non-cumulative).
202 tmp0=abs(imf3ml);
203 tmp1 = repmat(sum(tmp0,3),[1 1 nvar]); % imstp-by-nvar^2
204 % imf3: row--steps, column--nvar responses, 3rd dimension--nvar shocks
205 lzvd3ml = 100*(tmp0./tmp1);
206 lzvdml = reshape(lzvd3ml,imstp,nvar^2);
207 %** for lz vd (non-cumulative)
208 Range5{7} =zeros(imstp,nvar^2,2); % 2: min and max
209 Range5{7}(:,:,1) = zeros(imstp,nvar^2); % min, 5 std.
210 Range5{7}(:,:,2) = 100*ones(imstp,nvar^2); % max, 5 std.
211 % Range5(7)(:,:,1): imstp-by-nvar^2. Column: nvar responses to 1st shock,
212 % nvar responses to 2nd shock, ...
213
214 %*** lz annual variance decomposition (nunlike the traditional, non-cumulative).
215 tmp0=abs(imfyer3ml);
216 tmp1 = repmat(sum(tmp0,3),[1 1 nvar]); % imstp-by-nvar^2
217 % imf3: row--steps, column--nvar responses, 3rd dimension--nvar shocks
218 lzvdyer3ml = 100*(tmp0./tmp1);
219 lzvdyerml = reshape(lzvdyer3ml,imstpyer,nvar^2);
220 %** for lz annual vd
221 Range5{8} =zeros(imstpyer,nvar^2,2); % 2: min and max
222 Range5{8}(:,:,1) = zeros(imstpyer,nvar^2); % min, 5 std.
223 Range5{8}(:,:,2) = 100*ones(imstpyer,nvar^2); % max, 5 std.
224 % Range5(8)(:,:,1): imstpyer-by-nvar^2. Column: nvar responses to 1st shock,
225 % nvar responses to 2nd shock, ...
226
227 %**** Correlations among structural shocks
228 philr = phi(size(phi,1)-actup+1,:); % +1 is absolutely needed
229 yexa = yact(1:actup,:);
230 Estrexaml = fidcndexa(yexa,philr,A0ml,Bhml,nvar,lags,actup); % actup-by-nvar
231 sckvarml = (Estrexaml'*Estrexaml)/actup;
232 sckcorml = corr(sckvarml);
233 %** for shock correlation sckcor
234 Range5{9} =zeros(nvar,nvar,2); % 2: min and max
235 Range5{9}(:,:,1) = (-1)*ones(nvar,nvar); % min, 5 std.
236 Range5{9}(:,:,2) = ones(nvar,nvar); % max, 5 std.
237 % Range5(9)(:,:,1): nvar^2. Correlation among structural shocks
238
239
240
241
242 %**
243 invc = cell(9,1); % interval length (used for counting later).
244 for i=[1:4 6:9]
245 invc{i} = Range5{i}(:,:,2) - Range5{i}(:,:,1);
246 end
247 invc{5} = Range5{5}(:,2) - Range5{5}(:,1);
248 %
249 imfhbin = invc{4} ./ ninv; % bin size for each point of impulse responses
250 imfloor = Range5{4}(:,:,1); % lowest point next to -infinity
251 Avhxhbin = invc{5} ./ ninv; % bin size for each point of A0
252 Avhxfloor = Range5{5}(:,1); % lowest point next to -infinity
253 imfyerhbin = invc{6} ./ ninv; % bin size for each point of annual impulse responses
254 imfyerfloor = Range5{6}(:,:,1); % lowest point next to -infinity
255 lzvdhbin = invc{7} ./ ninv; % bin size for each point of variance decompositions
256 lzvdfloor = Range5{7}(:,:,1); % lowest point next to -infinity
257 lzvdyerhbin = invc{8} ./ ninv; % bin size for each point of annul variance decompositions
258 lzvdyerfloor = Range5{8}(:,:,1); % lowest point next to -infinity
259 sckcorhbin = invc{9} ./ ninv; % bin size for each point of shock correlations
260 sckcorfloor = Range5{9}(:,:,1); % lowest point next to -infinity
261
262
263
264
265 %*** <<>> Specific requests
266 %*** compute prob(parameter>0) or joint prob. of sign matches in MP and MD
267 if sa0indx
268 csix1a0 = [-1 -1 1 1 1 -1 1]'; % for 7 parameters in MP and MD
269 % from 7th to 13th in Avhx_norm
270 nspeca0 = 7+3; % number of specific requests
271 % additional 3: 1 for all MP parameters; 2 for all MD parameters;
272 % 3 for all paramters in MP and MD
273 cspeca0 = zeros(nspeca0,1);
274 end
275 %
276 if simfindx
277 csix1imf = [-1 -1 1 -1 -1 1]'; % for 6 variables to MP shock
278 % maybe at different horizons (esp. for inflation)
279 csix1pimf = [1 1 1 24 36 24]'; % the periods for the 6 varialbes to MP shock
280 % Pcm, M2, FFR, y, P, U.
281 nspecimf = nvar+2; % number of specific requests
282 % additional 1: 1 for opposite signs of M2 and R
283 cspecimf = zeros(nspecimf,1);
284 end
285
286
287
288
289 %===================================
290 % Here begins with the big loop
291 %===================================
292 H1 = chol(hess); % upper triangular so that H1' is a lower triangular decomp
293 baseW = H_sr; %inv(H1); %H_sr; % covariance matrix without scaling
294 nswitch=0; %<<>> total number of sign switches
295 A0inxhat = inv(A0xhat); % inverse of ML estimate
296 a0indx0 = find(idmat0==0); % index for all zero's in A0;
297 nn=[nvar lags imstp];
298
299 [cT,vR,kdf] = gibbsglb(Sbd,idmat0,nvar,fss);
300
301 tic
302 for starts = 1:nstarts
303 starts
304 if starts == 1
305 A0gbs(a0indx) = xhat; % from "load ..."
306 if ~IndxGibbs % Metropolist
307 Avhx = xhat;
308 hAvhx = a0asfun(Avhx,Sbd,fss,nvar,a0indx);
309 hAvhx = -hAvhx; % converted to logLH
310 end
311 else
312 Avhx = baseW*randn(nfp,1); %H_sr*randn(nfp,1); % D: discarded sequence
313 csq=randn(tdf,1);
314 csq=sum(csq .* csq);
315 Avhx = xhat+Avhx/sqrt(csq/tdf);
316 %** Normalization by the choice of IndxNmlr
317 A0gbs(a0indx) = Avhx;
318 if ~IndxNmlr(5)
319 [A0gbs,jnk] = nmlzvar(A0gbs,A0xhat,A0inxhat,IndxNmlr,nswitch,[]);
320 else
321 A0ingbs = inv(A0gbs);
322 [A0gbs,jnk,jnk1] = nmlzvar(A0gbs,A0xhat,A0inxhat,IndxNmlr,nswitch,A0ingbs);
323 end
324 %
325 if ~IndxGibbs % Metropolist
326 Avhx = A0gbs(a0indx);
327 hAvhx = a0asfun(Avhx,Sbd,fss,nvar,a0indx);
328 hAvhx = -hAvhx; % converted to logLH
329 end
330 end
331 %
332 cJump = 0;
333
334 imfcnt = zeros(ninv+2,imstp*nvar^2); % cnt: count; for impulse responses
335 Avhxcnt = zeros(ninv+2,nfp); % cnt: count; for A0's
336 imfyercnt = zeros(ninv+2,imstpyer*nvar^2); % cnt: count; for impulse responses
337 lzvdcnt = zeros(ninv+2,imstp*nvar^2); % cnt: count; for lz vd (non-cumulative)
338 lzvdyercnt = zeros(ninv+2,imstpyer*nvar^2); % cnt: count; for lz annual vd (non-cumulative)
339 sckcorcnt = zeros(ninv+2,nvar^2); % cnt: count; for shock correlations
340
341 for draws = 1:ndraws1
342 if IndxGibbs
343 A0gbs = gibbsvar(A0gbs,cT,vR,nvar,fss,kdf);
344 else % Metropolis
345 [Avhx,hAvhx,cJump] = smtplis(Avhx,hAvhx,tdf,cJump,scf,...
346 baseW,nfp,Sbd,fss,nvar,a0indx);
347 end
348 end
349
350 wdraws=(starts-1)*ndraws2+0;
351 for draws = 1:ndraws2
352 drawsc = (starts-1)*ndraws2+draws;
353 if IndxGibbs
354 A0gbs = gibbsvar(A0gbs,cT,vR,nvar,fss,kdf);
355 A0gbs(a0indx0) = 0; % set all zeros in A0gbs clean to avoid possible cumulative round-off errors
356 else % Metropolis
357 [Avhx,hAvhx,cJump] = smtplis(Avhx,hAvhx,tdf,cJump,scf,...
358 baseW,nfp,Sbd,fss,nvar,a0indx);
359 A0gbs(a0indx) = Avhx;
360 end
361
362 %*** call normalization so that A0_h is normalized
363 if ~IndxNmlr(5)
364 [A0_h,nswitch] = nmlzvar(A0gbs,A0xhat,A0inxhat,IndxNmlr,nswitch,[]);
365 A0_hin = inv(A0_h);
366 else
367 A0ingbs = inv(A0gbs);
368 [A0_h,nswitch,A0_hin] = nmlzvar(A0gbs,A0xhat,A0inxhat,IndxNmlr,nswitch,A0ingbs);
369 end
370 Avhx_norm = A0_h(a0indx);
371
372 if sa0indx
373 for k=1:7
374 cspeca0(k) = cspeca0(k) + ((csix1a0(k)*Avhx_norm(6+k))>0);
375 end
376 %*** Joint tests
377 j1=csix1a0;
378 j2=Avhx_norm;
379 %** MP equation
380 mpall = ((j1(2)*j2(8))/(j1(3)*j2(9)))<0; % & ((j1(1)*j2(7))/(j1(3)*j2(9)))<0;
381
382 cspeca0(8) = cspeca0(8) + mpall;
383 %** MD equation
384 mdall = ((j1(4)*j2(10))/(j1(5)*j2(11)))<0; %& ((j1(4)*j2(10))/(j1(6)*j2(12)))<0;
385 cspeca0(9) = cspeca0(9) + mdall;
386 mpdall = mpall & mdall;
387 cspeca0(10) = cspeca0(10) + mpdall;
388 end
389
390 %
391 % *** normal draws for posterior Aplus conditional on A0h
392 %
393 %** the mean is Aplushm, and the covariance is inv(xxhp)=Lxxhpc*Lxxhpc'
394 Apindm = randn(ncoef,nvar);
395 %
396 if ~all(all(finite(Bhml)))
397 Aplushm=zeros(ncoef,nvar);
398 for i=1:nvar
399 Aplushm(:,i)=Gb{i}*A0_h(:,i); % see Zha's forecast (1) p.9
400 % Here, Gb is used to compute A+ where A+(i) = Gb(i)*a0(i)
401 end
402 Bh_h = (Aplushm + xxhpc\Apindm)*A0_hin;
403 else
404 Bh_h = Bhml + (xxhpc\Apindm)*A0_hin;
405 end
406
407 if ImfErr
408 swish_h = A0_hin'; % Switching back to the form A0*y(t) = e(t)
409 imf_h = zimpulse(Bh_h,swish_h,nn); % in the form that is congenial to RATS
410 imf3_h=reshape(imf_h,size(imf_h,1),nvar,nvar);
411 % imf3: row--steps, column--nvar responses, 3rd dimension--nvar shocks
412 imfmean = imfmean + imf_h; % posterior mean
413 imfs = imfs + imf_h.^2; % posterior 2nd moment
414 imfcnt = empdfsort(imfcnt,imf_h,imfloor,imfhbin,ninv);
415 % sorted counts (prob.) in bins
416
417 %**** annula impulse responses
418 for k=1:imstpyer
419 yer3(k+1,:,:) = mean(imf3_h(1+q_m*(k-1):q_m*k,:,:),1); % annual average
420 % yer3: initialized earlier already
421 % yer3: row--steps, column--nvar responses, 3rd dimension--nvar shocks
422 end
423 imfyer3_h(:,vlistlog,:) = ( exp(yer3(2:1+imstpyer,vlistlog,:)-yer3(1:imstpyer,vlistlog,:)) - 1 ).*100;
424 imfyer3_h(:,vlistper,:) = yer3(2:1+imstpyer,vlistper,:) .* 100;
425 % imfyer3_h: row--steps, column--nvar responses, 3rd dimension--nvar shocks
426 imfyer_h = reshape(imfyer3_h,imstpyer,nvar^2);
427 imfyermean = imfyermean + imfyer_h; % posterior mean
428 imfyers = imfyers + imfyer_h.^2; % posterior 2nd moment
429 imfyercnt = empdfsort(imfyercnt,imfyer_h,imfyerfloor,imfyerhbin,ninv);
430 % sorted counts (prob.) in bins
431
432 %**** Leeper-Zha variance decomposition (non-cumulative)
433 tmp0=abs(imf3_h);
434 tmp = repmat(sum(tmp0,3),[1 1 nvar]); % imstp-by-nvar^2
435 % imf3: row--steps, column--nvar responses, 3rd dimension--nvar shocks
436 lzvd3_h = 100*(tmp0./tmp);
437 lzvd_h = reshape(lzvd3_h,imstp,nvar^2);
438 lzvdcnt = empdfsort(lzvdcnt,lzvd_h,lzvdfloor,lzvdhbin,ninv);
439 % sorted counts (prob.) in bins
440
441 %**** Leeper-Zha annual variance decomposition (non-cumulative)
442 tmp0=abs(imfyer3_h);
443 tmp = repmat(sum(tmp0,3),[1 1 nvar]); % imstp-by-nvar^2
444 % imf3: row--steps, column--nvar responses, 3rd dimension--nvar shocks
445 lzvdyer3_h = 100*(tmp0./tmp);
446 lzvdyer_h = reshape(lzvdyer3_h,imstpyer,nvar^2);
447 lzvdyercnt = empdfsort(lzvdyercnt,lzvdyer_h,lzvdyerfloor,lzvdyerhbin,ninv);
448 % sorted counts (prob.) in bins
449
450 %**** Correlations among structural shocks
451 Estrexa_h = fidcndexa(yexa,philr,A0_h,Bh_h,nvar,lags,actup); % actup-by-nvar
452 sckvar_h = (Estrexa_h'*Estrexa_h)/actup;
453 sckcor_h = corr(sckvar_h);
454 sckcorcnt = empdfsort(sckcorcnt,sckcor_h,sckcorfloor,sckcorhbin,ninv);
455
456 if simfindx
457 for k=1:6
458 cspecimf(k) = cspecimf(k) + ((csix1a0(k)*imf_h(1,nvar+k))>0);
459 % 1st dim in imf_h: periods
460 end
461 %*** Joint tests
462 j1=csix1imf;
463 j2=imf_h;
464 j3=csix1pimf;
465 %** M2 and R in MP equation
466 mpall = ((j1(2)*j2(j3(2),nvar+2))/(j1(3)*j2(j3(3),nvar+3)))>0; % & ((j1(1)*j2(7))/(j1(3)*j2(9)))<0;
467 % 1st dim in j2: periods
468 cspecimf(nvar+1) = cspecimf(nvar+1) + mpall;
469 %** R and P in MP equation
470 mpall = ((j1(3)*j2(j3(3),nvar+3))/(j1(5)*j2(j3(5),nvar+5)))>0; %
471 % 1st dim in j2: periods
472 cspecimf(nvar+2) = cspecimf(nvar+2) + mpall;
473 %
474 % %** MD equation
475 % mdall = ((j1(4)*j2(10))/(j1(5)*j2(11)))<0; %& ((j1(4)*j2(10))/(j1(6)*j2(12)))<0;
476 % cspeca0(9) = cspeca0(9) + mdall;
477 % mpdall = mpall & mdall;
478 % cspeca0(10) = cspeca0(10) + mpdall;
479 end
480 end
481
482 Avhxmean = Avhxmean + Avhx_norm; % 1st step to overall mean of parameter
483 Avhxs = Avhxs + Avhx_norm.^2; % 1st step to overall 2nd moment of parameter
484 Avhxcnt = empdfsort(Avhxcnt,Avhx_norm,Avhxfloor,Avhxhbin,ninv);
485
486
487 % A0hatw(:,drawsc-wdraws) = A0_h(:);
488 if ~mod(draws,nbuffer)
489 starts
490 draws
491 wdraws=drawsc
492 % fwriteid = fopen('outA0.bin','a');
493 % count = fwrite(fwriteid,A0hatw,'double');
494 % status = fclose('all');
495 end
496 end
497 %
498 imfcntmulti(:,:,starts) = imfcnt;
499 Avhxcntmulti(:,:,starts) = Avhxcnt;
500 imfyercntmulti(:,:,starts) = imfyercnt;
501 lzvdcntmulti(:,:,starts) = lzvdcnt;
502 lzvdyercntmulti(:,:,starts) = lzvdyercnt;
503 sckcorcntmulti(:,:,starts) = sckcorcnt;
504
505 if ~IndxGibbs
506 countJump(starts,1) = cJump;
507 end
508 end
509 timend = toc
510 timeminutes=timend/60
511
512 if ~IndxGibbs
513 countJump = countJump/ndraws2
514 end
515
516 Avhxmean = Avhxmean/(imndraws);
517 Avhxs = Avhxs/(imndraws);
518 Avhxs = sqrt(Avhxs - Avhxmean.^2); % stardard deviation
519 A0hm = zeros(nvar);
520 A0hm(a0indx) = Avhxmean % mean
521 A0hs = zeros(nvar);
522 A0hs(a0indx) = Avhxs; % standar deviation
523
524 imfmean = imfmean/(imndraws);
525 imfs = imfs/imndraws;
526 imfs = sqrt(imfs - imfmean.^2); % standard deviation
527
528 imfyermean = imfyermean/(imndraws);
529 imfyers = imfyers/imndraws;
530 imfyers = sqrt(imfyers - imfyermean.^2); % standard deviation
531
532
533 save outB_W nstarts ndraws1 ndraws2 imndraws timeminutes Avhxml Avhxmean Avhxs ...
534 Avhxcntmulti A0xhat A0hm A0hs IndxGibbs countJump nvar Range5 ...
535 ninv invc nfp a0indx nswitch actup nSample lags yrEnd qmEnd ...
536 yrStart qmStart q_m sckcorml sckcorcntmulti
537
538 if ImfErr
539 if simfindx
540 cspecimf = cspecimf/imndraws;
541 save outB_W cspecimf -append
542 end
543 save outB_W imfml imfmean imfs imfcntmulti imstp cspecimf ...
544 imfyerml imfyermean imfyers imfyercntmulti imstpyer ...
545 lzvdml lzvdcntmulti lzvdyerml lzvdyercntmulti -append
546 end
547 %
548 if sa0indx
549 cspeca0 = cspeca0/imndraws;
550 save outB_W cspeca0 -append
551 end
552
553