Debian Codegraph @

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

raw
1 % 10/24/97
2 % Distance Method of Waggoner and Zha
3 % Modified from Sims and Zha's code
4 %
5 %
6 % Copyright (C) 1997-2012 Tao Zha
7 %
8 % This free software: you can redistribute it and/or modify
9 % it under the terms of the GNU General Public License as published by
10 % the Free Software Foundation, either version 3 of the License, or
11 % (at your option) any later version.
12 %
13 % It is distributed in the hope that it will be useful,
14 % but WITHOUT ANY WARRANTY; without even the implied warranty of
15 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 % GNU General Public License for more details.
17 %
18 % If you did not received a copy of the GNU General Public License
19 % with this software, see <http://www.gnu.org/licenses/>.
20 %
21
22 % ** ONLY UNDER UNIX SYSTEM
23 %path(path,'/usr2/f1taz14/mymatlab')
24
25
26 %global xxhp Hm1t Hm1 Hm SpH FRESHFUNCTION
27 %
28 %* =================================================
29 %* ====== Beginning of the script ==================
30 %* =================================================
31 %
32 %* The available data considered
33 %
34 q_m = 12; % quarters or months
35 yrBin=59; % beginning of the year
36 qmBin=1; % begining of the quarter or month
37 yrFin=97; % final year
38 qmFin=12; % final quarter
39 %tnvar = 2; % total number of variables
40 nData=(yrFin-yrBin)*q_m + (qmFin-qmBin+1);
41 % total number of the available data -- this is all you have
42 %
43 %* Load data and series
44 %
45 load xd24a % the default name for the variable is "xdd".
46 [nt,ndv]=size(xdd);
47 if nt~=nData
48 warning(sprintf('nt=%d, Caution: not equal to the length in the data',nt));
49 %disp(sprintf('nt=%d, Caution: not equal to the length in the data',nt));
50 return
51 end
52 %1 CPI-U
53 %2 FFR
54 %3 T-bill3
55 %4 Treasure note 10
56 %5 M2
57 %6 M1
58 %7 Nominal PCE
59 %8 real PCE
60 %9 Unemployment Rate
61 %10 IMF Commodity Price Index
62 %11 Civilians Employed: 16 & over
63 %12 Nonfarm Payroll Employment
64 %13 IP
65 %14 Retail Sales (Nominal)
66 %15 NAPM Composit Index
67 %16 Total Reserves
68 %17 PPI-finished goods
69 %18 PPI-Crude materials
70 %19 PPI-Crude materials less energy
71 %20 CRB Spot Commodity Index -- all commodities
72 %21 CRB Spot Commodity Index -- raw industrials
73 %22 PCE price index
74 %23 rgdpmon Real GDP (monthly, chain $92)
75 %24 dgdpmon Deflator GDP (monthly, chain $92)
76
77
78 %1 IMF CP
79 %2 M2
80 %3 FFR
81 %3 real GDP
82 %4 CPI-U
83 %5 U
84
85 %>>>>>>>>>>>>>>>>>>
86 logindx = [1 5:8 10:14 16:24];
87 xdd(:,logindx) = log(xdd(:,logindx));
88 pctindx = [2:4 9 15];
89 xdd(:,pctindx)=.01*xdd(:,pctindx); % make it a general term for the following use
90 %
91 vlist = [10 5 2 23 1 9]; % regarding "xdd", IMF-CP, M2, FFR, GDP, CPI, U
92 vlistlog = [1 2 4 5]; % subset of "vlist"
93 vlistper = [3 6]; % subset of "vlist"
94 %<<<<<<<<<<<<<<<<<<<
95
96 idfile='iden6';
97
98 xlab = {'Inf'
99 'MS'
100 'FFR'
101 'y'
102 'P'
103 'U'};
104
105 ylab = {'Pcm'
106 'M2'
107 'FFR'
108 'y'
109 'P'
110 'U'};
111
112 xdd_per = xdd(:,vlist);
113
114 x1 = 'Pcm';
115 x2 = 'M2';
116 x3 = 'FFR';
117 x4 = 'GDP';
118 x5 = 'CPI';
119 x6 = 'U';
120 x7 = 'R10';
121
122
123 baddata = find(isnan(xdd_per));
124 if ~isempty(baddata)
125 warning('Some data are actually unavailable.')
126 disp('Hit any key to continue, or ctrl-c to abort')
127 pause
128 end
129 %
130
131 %* A specific sample is considered for estimation
132 %* Sample period 59:7-82:9, forecast period 82:10-84:9
133 yrStart=59;
134 qmStart=1;
135 [yrEnd,qmEnd,forep,forepy,forelabel] = pararc;
136 nSample=(yrEnd-yrStart)*q_m + (qmEnd-qmStart+1);
137 if qmEnd == q_m % end of the year
138 nSampleCal=nSample; % Cal: calendar year
139 else
140 nSampleCal=(yrEnd-1-yrStart)*q_m + (q_m-qmStart+1); % Cal: calendar year
141 end
142
143 %* More script variables
144 %
145 lags = 13; % <<>>
146 % automatic decay code (monthly data), only two options: lags = 6 or 13
147 forepq = forep/3; % quarterly
148 actup = 5*48; % <<>> actual periods before forecasting (20 years)
149 %actup = 12*floor(nSample/12); % <<>> actual periods before forecasting (8 years)
150 actup = 48; % <<>> actual periods before forecasting (4 years)
151 actupq = actup/3; % quarterly
152 actupy = actup/12; % four years
153 imstp = 48; % <<>> impulse responses (4 years)
154 ninv = 1000; % the number of intervals for counting impulse responses
155 nhp = 6; % <<>> number of hyperparameters for estimation
156 %%scf = 2.4/sqrt(nvar); % scf^2*Sigma (covaraince)
157 scf = 0.25; % scf^2*Sigma (covaraince)
158 ndraws1=15; % 1500, 1st part of Monte Carlo draws
159 ndraws2=2*ndraws1; % 2nd part of Monte Carlo draws
160 ndraws=3*ndraws2 % a total number of Monte Carlo draws
161 nstarts=3; % number of starting points
162 imndraws = nstarts*ndraws2;
163 tdf = 3; % degrees of freedom for t-dist
164 ga = tdf/2; % asymmetry parameter in Gamma
165 gb = 2/tdf; % normalized parameter in Gamma
166 %
167 %* =================================================
168 %* ====== End of the script ========================
169 %* =================================================
170
171
172 if (q_m==12)
173 nStart=(yrStart-yrBin)*12+qmStart-qmBin; % positive number of months at the start
174 nEnd=(yrEnd-yrFin)*12+qmEnd-qmFin; % negative number of months towards end
175 elseif (q_m==4)
176 nStart=(yrStart-yrBin)*4+qmStart-qmBin; % positive number of months at the start
177 nEnd=(yrEnd-yrFin)*4+qmEnd-qmFin; % negative number of months towards end
178 else
179 disp('Warning: this code is only good for monthly/quarterly data!!!')
180 return
181 end
182 %
183 if nEnd>0 | nStart<0
184 disp('Warning: this particular sample consider is out of bounds of the data!!!')
185 return
186 end
187 %
188 xdgel=xdd(nStart+1:nData+nEnd,vlist); % gel: general term for selected xdd
189 xdata=xdd(nStart+1:nData,vlist);
190
191 [Gb,Sbd,Bh,SpH,fss,ndobs,phi,y,nvar,ncoef,xxhpc,a0indx,na0p,...
192 idmat0,idmatpp] = szasbvar(idfile,q_m,lags,nSample,nhp,xdgel);
193
194 % * the largest matrix in this file <<>>
195 yforew = zeros(ndraws,forep*nvar); % preallocating
196 yforeqgw = zeros(ndraws,forepq*nvar); % preallocating
197 yforeCalygw = zeros(ndraws,forepy*nvar); % preallocating
198 % * the largest matrix in this file <<>>
199 %%imfcnt = zeros(ninv+2,imstp*nvar^2); % cnt: count
200
201 load idenml % xhat ghat fhat, etc.
202 %load outiden % xhat ghat fhat
203
204 %==================
205 % Impulse responses first
206 %==================
207 %A0 = zeros(nvar);
208 %A0(a0indx)=xhat;
209 %A0(4,2) = -xhat(7); % output in MD
210 %A0(5,2)=-xhat(7); % price in MD
211 A0in = inv(A0);
212 swish = A0in'; % each row corresponds to an equation
213 %Bh = Hm1t; % no longer have Hm1t in this new szasbvar
214
215 % ** impulse responses
216 nn = [nvar lags imstp];
217 imf = zimpulse(Bh,swish,nn); % in the form that is congenial to RATS
218 %[vd,str,imf] = errors(Bh,swish,nn);
219
220 scaleout = imcgraph(imf,nvar,imstp,xlab,ylab)
221
222
223 %%%%
224 %$$$ Out-of-sample forecasts. Note: Hm1t does not change with A0.
225 %%%%
226 %
227 % ** out-of-sample forecast, from 82:4 to 84:3 (flp+1:flp+forep)
228 % * updating the last row of X (phi) with the current (last row of) y.
229 phil = phi(size(phi,1),:);
230 phil(nvar+1:ncoef-1) = phil(1:ncoef-1-nvar);
231 phil(1:nvar) = y(size(y,1),:);
232 ylast = y(size(y,1),:);
233 indx12 = size(y,1)-q_m+1:size(y,1);
234 ylast12 = y(indx12,:); % last 12 months data
235 nn = [nvar lags forep];
236 %
237 yfore = forecast(Bh,phil,nn); % forep-by-nvar
238
239
240 %>>>>>>>>>>>>>>>
241 yforel=yfore;
242 yforel(:,vlistlog) = exp(yfore(:,vlistlog));
243 figure;
244 t2=1:forep;
245 for i = 1:nvar
246 subplot(nvar/2,2,i)
247 plot(t2,yforel(:,i),'--')
248 %title('solid-actual, dotted-forecast');
249 %title(eval(['forelabel']));
250 %ylabel(eval(['x' int2str(i)]));
251 ylabel(char(ylab(i)))
252 end
253 %<<<<<<<<<<<<<<<
254
255 %%%%%%%%
256 %
257 %% See Zha's note "Forecast (1)" p. 5, RATS manual (some errors in RATS), etc.
258 %
259 %% Some notations: y(t+1) = y(t)B1 + e(t+1)inv(A0). e(t+1) is 1-by-n.
260 %% Let r(t+1)=e(t+1)inv(A0) + e(t+2)C + .... where inv(A0) is impulse
261 %% response at t=1, C at t=2, etc. The row of inv(A0) or C is
262 %% all responses to one shock.
263 %% Let r be q-by-1 (such as r(1) = r(t+1)
264 %% = y(t+1) (constrained) - y(t+1) (forecast)).
265 %% Use impulse responses to find out R (k-by-q) where k=nvar*nsteps
266 %% where nsteps the largest constrained step. The key of the program
267 %% is to creat R using impulse responses
268 %% Optimal solution for shock e where R'*e=r and e is k-by-1 is
269 %% e = R*inv(R'*R)*r.
270 %
271 %%%%%%%%
272
273 nconstr=4; % q: 4 years -- 4*12 months
274 eq_ms = 2; % location of MS equation
275 %eq_ms = []; % all shocks
276 %*** initializing
277 stepcon=cell(nconstr,1); % initializing, value y conditioned
278 valuecon=zeros(nconstr,1); % initializing, value y conditioned
279 varcon=zeros(nconstr,1); % initializing, endogous variables conditioned
280 %
281 stepcon{1}=[1:12]'; % average over 12 months.
282 stepcon{2}=[13:24]'; % average over 12 months.
283 stepcon{3}=[25:36]'; % average over 12 months.
284 stepcon{4}=[37:48]'; % average over 12 months.
285 %
286 %for i=1:nconstr
287 % stepcon{i}=i;
288 %end
289 %
290 chk1 = mean(yfore(stepcon{1},3))
291 chk2 = mean(yfore(stepcon{2},3))
292 chk3 = mean(yfore(stepcon{3},3))
293 chk4 = mean(yfore(stepcon{4},3))
294 Ro=[chk1 chk2 chk3 chk4];
295 %
296 chk1 = exp( (sum(yfore(stepcon{1},5))-sum(ylast12(:,5))) ./ q_m )
297 chk2 = exp( (sum(yfore(stepcon{2},5))-sum(yfore(stepcon{1},5))) ./ q_m )
298 chk3 = exp( (sum(yfore(stepcon{3},5))-sum(yfore(stepcon{2},5))) ./ q_m )
299 chk4 = exp( (sum(yfore(stepcon{4},5))-sum(yfore(stepcon{3},5))) ./ q_m )
300 %
301 %valuecon(:)=0.055;
302 %
303 %>>>>>>>>>>>>>>>>> E: Condition on funds rate path >>>>>>>>>>>>> Toggle
304 %delta=0.0010;
305 %valuecon(1) = mean(yfore(stepcon{1},3))+2*delta;
306 %valuecon(2) = mean(yfore(stepcon{2},3))+2*delta;
307 %valuecon(3) = mean(yfore(stepcon{3},3))-2*delta;
308 %valuecon(4) = mean(yfore(stepcon{4},3))-2*delta;
309 %valuecon(1) = 0.055;
310 %valuecon(2) = 0.050;
311 %valuecon(3) = 0.0475;
312 %valuecon(4) = 0.045;
313 %>>>>>>>>>>>>>>>>> E: Condition on funds rate path >>>>>>>>>>>>>
314 %
315 %<<<<<<<<<<<<<<<< B: Condition on inflation path <<<<<<<<<< Toggle
316 %delta=0.0010;
317 %valuecon(1)=mean(ylast12(:,5))+log(chk1-0*delta);
318 %valuecon(2)=valuecon(1)+log(chk2-2*delta);
319 %valuecon(3)=valuecon(2)+log(chk3-6*delta);
320 %valuecon(4)=valuecon(3)+log(chk4-12*delta);
321 % % 5: CPI; 2.5%: annual inflation over 12 months, geometric means
322 %$$$ very good results -- following
323 valuecon(1)=mean(ylast12(:,5))+log(chk1);
324 valuecon(2)=valuecon(1)+log(1.020);
325 valuecon(3)=valuecon(2)+log(1.02);
326 valuecon(4)=valuecon(3)+log(1.02);
327 % % 5: CPI; 2.5%: annual inflation over 12 months, geometric means
328 %<<<<<<<<<<<<<<<< E: Condition on inflation path <<<<<<<<<<
329
330 nstepsm = 0; % initializing, the maximum step in all constraints
331 for i=1:nconstr
332 nstepsm = max([nstepsm max(stepcon{i})]);
333 end
334 varcon(:)=5; % 3: FFR; 5: CPI
335 %
336 imf3=reshape(imf,size(imf,1),nvar,nvar);
337 % imf3: row-steps, column-nvar responses, 3rd dimension-nvar shocks
338 imf3s=permute(imf3,[1 3 2]);
339 % imf3s: permuted so that row-steps, column-nvar shocks,
340 % 3rd dimension-nvar responses
341
342 [yhat,Estr] = fidencond(valuecon,stepcon,varcon,nconstr,nstepsm,nvar,lags,...
343 yfore,imf3s,phil,Bh,eq_ms);
344
345 chk1 = mean(yhat(stepcon{1},3))
346 chk2 = mean(yhat(stepcon{2},3))
347 chk3 = mean(yhat(stepcon{3},3))
348 chk4 = mean(yhat(stepcon{4},3))
349 Rh=[chk1 chk2 chk3 chk4];
350
351 chk1 = exp( (sum(yhat(stepcon{1},5))-sum(ylast12(:,5))) ./ q_m )
352 chk2 = exp( (sum(yhat(stepcon{2},5))-sum(yhat(stepcon{1},5))) ./ q_m )
353 chk3 = exp( (sum(yhat(stepcon{3},5))-sum(yhat(stepcon{2},5))) ./ q_m )
354 chk4 = exp( (sum(yhat(stepcon{4},5))-sum(yhat(stepcon{3},5))) ./ q_m )
355
356 %chk1 = mean(yhat(1:12,3))
357 %chk2 = mean(yhat(13:24,3))
358 %chk3 = mean(yhat(25:36,3))
359 %chk4 = mean(yhat(36:48,3))
360 %Rh=[chk1 chk2 chk3 chk4];
361
362 %chk1 = exp( (sum(yhat(1:12,5))-sum(ylast12(:,5))) ./ q_m )
363 %chk2 = exp( (sum(yhat(13:24,5))-sum(yhat(1:12,5))) ./ q_m )
364 %chk3 = exp( (sum(yhat(25:36,5))-sum(yhat(13:24,5))) ./ q_m )
365 %chk4 = exp( (sum(yhat(37:48,5))-sum(yhat(25:36,5))) ./ q_m )
366
367
368 idiff=mean(yfore(:,3))-mean(yhat(:,3))
369 mean(yfore(:,3))
370 mean(yhat(:,3))
371 figure
372 plot(1:48,yfore(:,3),1:48,yhat(:,3),':')
373 figure
374 plot(1:4,Ro,1:4,Rh,':')
375
376 %>>>>>>>>>>>>>>>
377 yhatl=yhat;
378 yhatl(:,vlistlog) = exp(yhat(:,vlistlog));
379 figure;
380 t2=1:forep;
381 for i = 1:nvar
382 subplot(nvar/2,2,i)
383 plot(t2,yhatl(:,i),'--')
384 %title('solid-actual, dotted-forecast');
385 %title(eval(['forelabel']));
386 %ylabel(eval(['x' int2str(i)]));
387 ylabel(char(ylab(i)))
388 end
389 %<<<<<<<<<<<<<<<
390
391
392 % inputs needed.
393 %yfore=yhat;
394
395 %===================================================
396 %%% Converting to calendar years and all at level
397 %===================================================
398 [yforeml,yforeqgml,yforeCalygml] = fore_cal(yhat,xdata,nvar,nSample,...
399 nSampleCal,forep,forepq,forepy,q_m,qmEnd,vlist,vlistlog);
400
401 %==================
402 % Note
403 %=================
404 % ? 1--median; l--lower bound; h--upper bound: between the bound: 2/3 probability
405 % yfore? % monthly, level
406 % yforeqg? % quarterly, growth rate
407 % yforeCalyg? % calendar year, growth rate
408
409 %save outw ndraws yfore1 yforeqg1 yforeCalyg1 yforel yforeqgl yforeh yforeqgh ...
410 % yforeCalygl yforeCalygh IUbeta
411
412 yforeCalygml
413
414
415 %----------------------------------------------------------------------
416 %================= Graphics =====================
417 %----------------------------------------------------------------------
418 %
419
420 %[yactCalyg,yforeCalygml,yAg,yFg] = fore_gh(xdata,nvar,nSample,nSampleCal,...
421 % yforeml,yforeqgml,yforeCalygml,actup,actupq,actupy,...
422 % vlist,vlistlog,vlistper,q_m,forep,ylab);
423
424 xinput = cell(16,1);
425 xinput{1}=xdata; xinput{2}=nvar; xinput{3}=nSample; xinput{4}=nSampleCal;
426 xinput{5}=yforeml; xinput{6}=yforeqgml; xinput{7}=yforeCalygml; xinput{8}=actup;
427 xinput{9}=actupq; xinput{10}=actupy; xinput{11}=vlist; xinput{12}=vlistlog;
428 xinput{13}=vlistper; xinput{14}=q_m; xinput{15}=forep; xinput{16}=ylab;
429 [yactCalyg,yforeCalygml,yAg,yFg] = fore_gh(xinput);
430
431
432 % Key Macroeconomic Variables: GDP, CPI, U
433 %******* From Goldbook July 1-2, 1997 FOMC
434 yforeBlue = [
435 3.6 2.4 5.0
436 2.5 2.6 5.0
437 ]; % real GDP, CPI-U, U,
438 yforeMacro = [
439 3.7 2.3 4.9
440 2.1 2.4 4.9
441 ]; % real GDP, CPI-U, U
442 yforeGold = [
443 3.8 2.4 5.0
444 2.5 2.5 4.8
445 2.4 2.7 4.7
446 ]; % real GDP, CPI-U, U
447 t3 = yAg+1:yAg+length(yforeBlue(:,1));
448 t4 = yAg+1:yAg+length(yforeGold(:,1));
449 %
450 keyindx = [4:nvar 3 2]; % GdP, CPI, U, FFR, M2
451 count=0;
452 t1 = 1:yAg;
453 t2 = yAg:yAg+yFg;
454 for i = keyindx
455 count = count+1;
456 subplot(3,2,count)
457 %plot(t1,yactqg(:,i),t2,yforeqg(:,i),':')
458 if (i==3) | (i==2)
459 plot(t1,yactCalyg(:,i),t2,[yactCalyg(length(t1),i);yforeCalygml(:,i)],'--')
460 %title('solid-actual, dotted-forecast');
461 %xlabel(eval(['forelabel']));
462 %ylabel(eval(['x' int2str(i)]));
463 ylabel(char(ylab(i)))
464 else
465 plot(t1,yactCalyg(:,i),t2,[yactCalyg(length(t1),i);yforeCalygml(:,i)],'--',...
466 t3,yforeBlue(:,count),'o',t3,yforeMacro(:,count),'d',...
467 t4,yforeGold(:,count),'^')
468 %title('solid-actual, dotted-forecast');
469 %xlabel(eval(['forelabel']));
470 %ylabel(eval(['x' int2str(i)]));
471 ylabel(char(ylab(i)))
472 end
473 end
474
475 actual=yactCalyg(:,keyindx) % GDP, CPI, U, M2
476 mode=yforeCalygml(:,keyindx) % GDP, CPI, U, M2
477 %low=yforeCalygl(:,keyindx)
478 %high=yforeCalygh(:,keyindx)
479 yforeBlue
480 yforeMacro
481 yforeGold