Debian Codegraph @

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

raw
1 function [yhat,Estr,rcon,Rcon,u,v,d] = fcstidcnd(valuecon,stepcon,varcon,nstepsm,...
2 nconstr,eq_ms,nvar,lags,phil,Aband,Sband,yfore_h,imf3s_h,A0_h,Bh_h,...
3 forep,TLindx,TLnumber,nCms,eq_Cms)
4 % [yhat,Estr,rcon,Rcon,u,v,d] = fcstidcnd(valuecon,stepcon,varcon,nstepsm,...
5 % nconstr,eq_ms,nvar,lags,phil,Aband,Sband,yfore_h,imf3s_h,A0_h,Bh_h,...
6 % forep,TLindx,TLnumber,nCms,eq_Cms)
7 %
8 % Conditional forecasting in the identified model with or without error bands
9 % It handles conditions on average values as well, so "valuecon" must be
10 % expressed at average (NOT sum) level.
11 % Aband is used only once when nconstr>0 and Bband=1 where Gibbs sampler may be used
12 % Unconditional forecast when imf3s_h, etc is fixed and nconstr=0.
13 %
14 % valuecon: vector of values conditioned
15 % stepcon: sequence (cell) of steps conditioned; if length(stepcon{i}) > 1, the condition
16 % is then an arithmetic average of log(y) over the stepcon{i} period.
17 % varcon: vector of variables conditioned
18 % nconstr: number of DLS constraints
19 % nstepsm: maximum number of steps in all DLS constraints
20 % nvar: number of variables in the BVAR model
21 % lags: number of lags in the BVAR model
22 % phil: the 1-by-(nvar*lags+1) data matrix where k=nvar*lags+1
23 % (last period plus lags before the beginning of forecast)
24 % Aband: 1: draws from A0 and Bh; 0: no draws
25 % Sband: 1: draws from random shocks E; 0: no random shocks
26 % yfore_h: uncondtional forecasts: forep-by-nvar. Never used when nconstr=0.
27 % In this case, set it to [];
28 % imf3s_h: 3-dimensional impulse responses matrix: impsteps-by-nvar shocks-by-nvar responses
29 % Never used when nconstr=0. In this case, set it to [];
30 % A0_h: A0 contemporaneous parameter matrix
31 % Bh_h: reduced-form parameter matrix: k-by-nvar, y(t) = X(t)*Bh+e(t)
32 % where X(t) is k-by-nvar and y(t) is 1-by-nvar
33 % forep: # of forecast periods (e.g., monthly for a monthly model)
34 % TLindx: 1-by-nCms vector of 1's and 0's, indicating tight or loose; 1: tighter, 0: looser
35 % Used only when /* (MS draws) is activated. Right now, MS shocks are deterministic.
36 % TLnumber: 1-by-nCms vector, lower bound for tight and upper bound for loose
37 % nCms: # of LZ conditions
38 % eq_Cms: equation location of MS shocks
39 % ------
40 % yhat: conditional forecasts: forep-by-nvar
41 % Estr: backed-out structural shocks (from N(0,1))
42 % rcon: vector - the difference between valuecon and log(yfore) (unconditional forecasts)
43 % Rcon: k-by-q (q constranits and k=nvar*max(nsteps)) so that
44 % Rcon'*e = rcon where e is k-by-1
45 % [u,d,v]: svd(Rcon,0)
46 %
47 %% See Zha's note "Forecast (1)" p. 5, RATS manual (some errors in RATS), etc.
48 %
49 %% Some notations: y(t+1) = y(t)B1 + e(t+1)inv(A0). e(t+1) is 1-by-n.
50 %% Let r(t+1)=e(t+1)inv(A0) + e(t+2)C + .... where inv(A0) is impulse
51 %% response at t=1, C at t=2, etc. The row of inv(A0) or C is
52 %% all responses to one shock.
53 %% Let r be q-by-1 (such as r(1) = r(t+1)
54 %% = y(t+1) (constrained) - y(t+1) (forecast)).
55 %% Use impulse responses to find out R (k-by-q) where k=nvar*nsteps
56 %% where nsteps the largest constrained step. The key of the program
57 %% is to creat R using impulse responses
58 %% Optimal solution for shock e where R'*e=r and e is k-by-1 is
59 %% e = R*inv(R'*R)*r and k>=q
60 %
61 % Written by Tao Zha. Revised November 1998;
62 % 3/20/99 Disenabled draws of MS shcoks. To enable it, activate /* part
63 % 3/20/99 Added A0_h and forep and deleted Cms as input argument. Previous
64 % programs may not be compatible.
65 %
66 %
67 % Copyright (C) 1997-2012 Tao Zha
68 %
69 % This free software: you can redistribute it and/or modify
70 % it under the terms of the GNU General Public License as published by
71 % the Free Software Foundation, either version 3 of the License, or
72 % (at your option) any later version.
73 %
74 % It is distributed in the hope that it will be useful,
75 % but WITHOUT ANY WARRANTY; without even the implied warranty of
76 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
77 % GNU General Public License for more details.
78 %
79 % If you did not received a copy of the GNU General Public License
80 % with this software, see <http://www.gnu.org/licenses/>.
81 %
82
83
84 DLSIdShock = ~isempty(eq_ms); % if not empty, the MS shock is identified as in DLS
85
86 impsteps=size(imf3s_h,1);
87 if (forep<nstepsm) | (impsteps<nstepsm)
88 disp('Increase # of forecast or impulse steps!!')
89 disp('Or decrease # of constraints (nconstr) or constrained steps (stepcon(i))!!')
90 error('Maximum of conditional steps > # of forecast or impulse steps!!')
91 end
92 kts = nvar*nstepsm; % k -- ts: total shocks some of which are restricted and others
93 % are free.
94 %*** initializing
95 Rcon = zeros(kts,nconstr); % R: k-by-q
96 Econ = zeros(kts,1); % E: k-by-1
97 rcon = zeros(nconstr,1); % r: q-by-1
98 %rcon=valuecon-diag(yfore(stepcon,varcon)); % another way is to use "loop" below.
99 tcwc = nvar*lags; % total coefficients without constant
100 phi=phil;
101
102
103
104 %----------------------------------------------------
105 % Form rcon, Rcon, and Econ (the mean of structural shocks)
106 %----------------------------------------------------
107 if nconstr
108 A0in = reshape(imf3s_h(1,:,:),nvar,nvar); % nvar shocks-by-nvar responses
109 for i=1:nconstr
110 rcon(i)=length(stepcon{i})*valuecon(i) - ...
111 sum(yfore_h(stepcon{i},varcon(i)),1); %<<>>
112 Rmat = zeros(nstepsm,nvar);
113 r2mat = zeros(nstepsm,1); % simply one identified equation
114 % Must be here inside the loop because it's matrix of one column of Rcon
115 for j=1:length(stepcon{i})
116 if DLSIdShock % Assuming the Fed can't see all other shocks within a month
117 Rmat(1:stepcon{i}(j),eq_ms) = Rmat(1:stepcon{i}(j),eq_ms) + ...
118 imf3s_h(stepcon{i}(j):-1:1,eq_ms,varcon(i));
119 % Rmat: row--nstepsm, column--nvar shocks (here all shocks except
120 % the identified one are set to zero) for a particular
121 % endogenous variable 'varcon(i)'. See Zha Forcast (1), pp.6-7
122 else % Rcon random with (A0,A+)
123 Rmat(1:stepcon{i}(j),:) = Rmat(1:stepcon{i}(j),:) + ...
124 imf3s_h(stepcon{i}(j):-1:1,:,varcon(i));
125 % Rmat: row--nstepsm, column--nvar shocks (here all shocks are
126 % *not* set to zero) for a particular endogenous
127 % variable 'varcon(i)'. See Zha Forcast (1), pp.6-7
128 end
129 end
130 Rmatt = Rmat'; % Now, nvar-by-nstepsm. I think here is where RATS has an error
131 % i.e. "OVERR" is not transposed when overlaid to "CAPR"
132 Rcon(:,i)=Rmatt(:); % Rcon: k-by-q where q=nconstr
133 end
134
135 [u d v]=svd(Rcon,0); %trial
136 %???? Can we reduce the time by computing inv(R'*R) directly?
137 % rtr = Rcon'*Rcon; %trial
138 % rtrinv = inv(Rcon'*Rcon); %trial
139 vd=v.*(ones(size(v,2),1)*diag(d)'); %trial
140 dinv = 1./diag(d); % inv(diag(d))
141 vdinv=v.*(ones(size(v,2),1)*dinv'); %trial
142 rtr=vd*vd'; % R'*R
143 rtrinv = vdinv*vdinv'; % inv(R'*R)
144
145 Econ = Rcon*rtrinv*rcon; % E = R*inv(R'R)*r; the mean of structural shocks
146 else
147 Econ = zeros(kts,1); % the mean of shocks is zero under no variable condition
148 Rcon = NaN;
149 rcon = NaN;
150 u = NaN;
151 d = NaN;
152 v = NaN;
153 end
154
155
156
157 %---------------------------------------
158 % No uncertainty at all or only random (A0,A+)
159 % In other words, no future shocks
160 %---------------------------------------
161 if (~Sband) %| (nconstr & (length(eq_ms)==1))
162 % length(eq_ms)==1 implies one-one mapping between MS shocks and, say, FFR
163 % if nstepsm==nconstr. If this condition does not hold, this procedure
164 % is incorrect. I don't have time to fix it now (3/20/99). So I use
165 % this as a proximation
166 Estr = reshape(Econ,nvar,nstepsm);
167 Estr = Estr'; % transpose so that
168 % Estr: structural shocks. Row--steps, Column--n shocks
169 Estr = [Estr;zeros(forep-nstepsm,nvar)];
170 % Now, forep-by-nvar -- ready for forecasts
171 Estr(1:nCms,eq_Cms) = TLnumber(:);
172 Ures = Estr/A0_h; % nstepsm-by-nvar
173 % Ures: reduced-form residuals. Row--steps; Column--n shocks
174
175 % ** reconstruct x(t) for y(t+h) = x(t+h-1)*B
176 % ** where phi = x(t+h-1) with last column being constant
177 %
178 yhat = zeros(forep,nvar);
179 for k=1:forep
180 yhat(k,:) = phi*Bh_h + Ures(k,:);
181 phi(1,nvar+1:tcwc) = phi(1,1:tcwc-nvar);
182 phi(1,1:nvar) = yhat(k,:);
183 %
184 end
185
186 %---------------------------------------
187 % With random future shocks and possibly (A0,A+) depending
188 % on if imf3s_h is random
189 %---------------------------------------
190 else
191 %--------------
192 % Condition on variables and A random
193 %--------------
194 if nconstr & Aband
195 warning(' ')
196 disp('This situation (both E and A random) is still under construction')
197 disp('It is closely related to Waggoner and Zha ReStat Gibbs sampling method')
198 disp('Please press ctrl-c to abort')
199 pause
200 elseif nconstr
201 %--------------
202 % Condition on variables and DLS MS shock, no A random but S random
203 %--------------
204 if DLSIdShock % other shocks are indepedent of the eq_ms shock
205 % 3/20/99 The following may be problematic because Osk should depend
206 % on u (A0_h and Bh_h) in general. I haven't worked out any good version
207 %/*
208 % Osk = randn(kts,1); % other shocks
209 % for j=1:nstepsm
210 % Osk(nvar*(j-1)+eq_ms)=0; % no shock to the MS or identified equation
211 % end
212 % Estr = Econ + Osk; % Econ is non zero only at position
213 % % eq_ms*j where j=1:nstepsm
214 % Estr = reshape(Estr,nvar,nstepsm);
215 % Estr = Estr'; % transpose so that
216 % % Estr: structural shocks. Row--steps, Column--n shocks
217 % Estr = [Estr;randn(forep-nstepsm,nvar)];
218 % % Now, forep-by-nvar -- ready for forecasts
219 %
220 disp('DLS')
221 Ome = eye(kts) - u*u'; % note, I-u*u' = I - R*inv(R'*R)*R'
222 %[u1 d1 v1] = svd(Ome); % too slow
223 [u1 d1] = eig(Ome);
224 Stdcon = u1*diag(sqrt(diag(abs(d1)))); % lower triagular chol of conditional variance
225 % see Zha's forecast (1), p.17
226 tmp1=zeros(nvar,nstepsm);
227 tmp1(eq_ms,:)=randn(1,nstepsm);
228 tmp2=tmp1(:);
229 %Estr1 = Econ + Stdcon*randn(kts,1);
230 %jnk = reshape(Stdcon*tmp2,nvar,nstepsm)
231 Estr1 = Econ + Stdcon*tmp2;
232 Estr2 = reshape(Estr1,nvar,nstepsm);
233 Estr2 = Estr2'; % transpose so that
234 % Estr2: structural shocks. Row--nstepsm, Column--n shocks
235 Estr = [Estr2;randn(forep-nstepsm,nvar)];
236 % Now, forep-by-nvar -- ready for forecasts
237 else
238 Ome = eye(kts) - u*u'; % note, I-u*u' = I - R*inv(R'*R)*R'
239 %[u1 d1 v1] = svd(Ome); % too slow
240 [u1 d1] = eig(Ome);
241 Stdcon = u1*diag(sqrt(diag(abs(d1)))); % lower triagular chol of conditional variance
242 % see Zha's forecast (1), p.17
243 %--------------
244 % Condition on variables and LZ MS shock, no A random but S random
245 % This section has not be tested yet, 10/14/98
246 %--------------
247 if nCms
248 Estr1 = Econ + Stdcon*randn(kts,1);
249 Estr2 = reshape(Estr1,nvar,nstepsm);
250 Estr2 = Estr2'; % transpose so that
251 % Estr2: structural shocks. Row--nstepsm, Column--n shocks
252 Estr = [Estr2;randn(forep-nstepsm,nvar)];
253 % Now, forep-by-nvar -- ready for forecasts
254 Estr(1:nCms,eq_Cms) = TLnumber(:);
255
256 %/* draw MS shocks
257 % for k=1:nCms
258 % if TLindx(k) % tighter
259 % while (Estr(k,eq_Cms)<TLnumber(k))
260 % Estr(k,eq_Cms) = randn(1,1);
261 % end
262 % else % looser
263 % while (Estr(k,eq_Cms)>TLnumber(k))
264 % Estr(k,eq_Cms) = randn(1,1);
265 % end
266 % end
267 % end
268 %--------------
269 % Condition on variables only, no A random but S random
270 %--------------
271 else
272 Estr1 = Econ + Stdcon*randn(kts,1);
273 Estr2 = reshape(Estr1,nvar,nstepsm);
274 Estr2 = Estr2'; % transpose so that
275 % Estr2: structural shocks. Row--nstepsm, Column--n shocks
276 Estr = [Estr2;randn(forep-nstepsm,nvar)];
277 % Now, forep-by-nvar -- ready for forecasts
278 end
279 end
280 %--------------
281 % Condition on LZ MS shocks only, S random and possibly A random depending on
282 % if A0_h and Bh_h are random
283 %--------------
284 else
285 if nCms
286 Estr = randn(forep,nvar);
287 % Now, forep-by-nvar -- ready for forecasts
288 Estr(1:nCms,eq_Cms) = TLnumber(:);
289
290 %/* draw MS shocks
291 % for k=1:nCms
292 % if TLindx(k) % tighter
293 % while (Estr(k,eq_Cms)<TLnumber(k))
294 % Estr(k,eq_Cms) = randn(1,1);
295 % end
296 % else % looser
297 % while (Estr(k,eq_Cms)>TLnumber(k))
298 % Estr(k,eq_Cms) = randn(1,1);
299 % end
300 % end
301 % end
302 else
303 Estr = randn(forep,nvar); % Unconditional forecast
304 % Now, forep-by-nvar -- ready for forecasts
305 end
306 end
307 %
308
309
310 Ures = Estr/A0_h; % nstepsm-by-nvar
311 % Ures: reduced-form residuals. Row--steps; Column--n shocks
312
313 % ** reconstruct x(t) for y(t+h) = x(t+h-1)*B
314 % ** where phi = x(t+h-1) with last column being constant
315 %
316 yhat = zeros(forep,nvar);
317 for k=1:forep
318 yhat(k,:) = phi*Bh_h + Ures(k,:);
319 phi(1,nvar+1:tcwc) = phi(1,1:tcwc-nvar);
320 phi(1,1:nvar) = yhat(k,:);
321 end
322 end