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