Debian Codegraph @

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

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