Debian Codegraph @

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

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