| 1 |
function [yhat,Estr,rcon,Rcon,u,v,d] = fn_fcstcnd(valuecon,stepcon,varcon,nstepsm,... |
| 2 |
nconstr,eq_ms,nvar,lags,phil,Sband,yfore_h,imf3s_h,Bh_h,forep) |
| 3 |
% ******* There are some BUG problems when calling this fucntion. ******* 3/15/2004. |
| 4 |
% [yhat,Estr,rcon,Rcon,u,v,d] = fn_fcstcnd(valuecon,stepcon,varcon,nstepsm,... |
| 5 |
% nconstr,eq_ms,nvar,lags,phil,Sband,yfore_h,imf3s_h,Bh_h,forep) |
| 6 |
% |
| 7 |
% Conditional forecasting in the identified model with or without error bands |
| 8 |
% It handles conditions on average values as well, so "valuecon" must be |
| 9 |
% expressed at average (NOT sum) levels (i.e., arithmetic averages of log(y) over |
| 10 |
% the period of stepcon{i}. 5/22/01. |
| 11 |
% Unconditional forecast when imf3s_h, etc is fixed and nconstr=0. |
| 12 |
% Note that length(eq_ms)==1 implies one-one mapping between MS shocks and, say, FFR |
| 13 |
% if nstepsm==nconstr. If this condition does not hold, this procedure is incorrect. |
| 14 |
% I don't have time to fix it now (3/20/99). Meantime, consult or use the old code |
| 15 |
% fidencond.m. |
| 16 |
% |
| 17 |
% valuecon: vector of values conditioned |
| 18 |
% stepcon: sequence (cell) of steps conditioned; if length(stepcon{i}) > 1, the condition |
| 19 |
% is then an arithmetic average of log(y) over the stepcon{i} period. |
| 20 |
% varcon: vector of variables conditioned |
| 21 |
% nstepsm: maximum number of steps in all DLS constraints |
| 22 |
% nconstr: number of DLS constraints |
| 23 |
% eq_ms: Equation location of MS shocks. If [], all shocks. |
| 24 |
% nvar: number of variables in the BVAR model |
| 25 |
% lags: number of lags in the BVAR model |
| 26 |
% phil: the 1-by-(nvar*lags+1) data matrix where k=nvar*lags+1 |
| 27 |
% (last period plus lags before the beginning of forecast) |
| 28 |
% Sband: 1: draws from random shocks E; 0: no random shocks For now (4/27/01), no option |
| 29 |
% for Aband because I don't think it works best to do both Aband and Sband in one function. |
| 30 |
% yfore_h: uncondtional forecasts: forep-by-nvar. Never used when nconstr=0. |
| 31 |
% In this case, may set it to []; |
| 32 |
% imf3s_h: 3-dimensional impulse responses matrix: impsteps-by-nvar shocks-by-nvar responses |
| 33 |
% Never used when nconstr=0. In this case, may set it to []; |
| 34 |
% Bh_h: reduced-form parameter matrix: k-by-nvar, y(t) = X(t)*Bh+e(t) |
| 35 |
% where X(t) is k-by-nvar and y(t) is 1-by-nvar |
| 36 |
% forep: # of forecast periods (e.g., monthly for a monthly model) |
| 37 |
% eq_Cms: equation location of MS shocks |
| 38 |
% ------ |
| 39 |
% yhat: conditional forecasts: forep-by-nvar |
| 40 |
% Estr: backed-out structural shocks (from constrained Gaussians) |
| 41 |
% rcon: vector - the difference between valuecon and log(yfore) (unconditional forecasts) |
| 42 |
% Rcon: k-by-q (q constranits and k=nvar*max(nsteps)) so that |
| 43 |
% Rcon'*e = rcon where e is k-by-1 |
| 44 |
% [u,d,v]: svd(Rcon,0) |
| 45 |
% |
| 46 |
%% See Zha's note "Forecast (1)" p. 5, RATS manual (some errors in RATS), etc. |
| 47 |
%% Some notations: y(t+1) = y(t)B1 + e(t+1)inv(A0). e(t+1) is 1-by-n. |
| 48 |
%% Let r(t+1)=e(t+1)inv(A0) + e(t+2)C + .... where inv(A0) is impulse |
| 49 |
%% response at t=1, C at t=2, etc. The row of inv(A0) or C is |
| 50 |
%% all responses to one shock. |
| 51 |
%% Let r be q-by-1 (such as r(1) = r(t+1) |
| 52 |
%% = y(t+1) (constrained) - y(t+1) (forecast)). |
| 53 |
%% Use impulse responses to find out R (k-by-q) where k=nvar*nsteps |
| 54 |
%% where nsteps the largest constrained step. The key of the program |
| 55 |
%% is to creat R using impulse responses |
| 56 |
%% Optimal solution for shock e where R'*e=r and e is k-by-1 is |
| 57 |
%% e = R*inv(R'*R)*r and k>=q |
| 58 |
% |
| 59 |
% See the old code fidencond.m. I wond't use fn_fcstidcnd?.m, 5/22/01. |
| 60 |
% Written by Tao Zha March 1998 . |
| 61 |
% Revised November 1998, May 2001 (Delete A0_h as |
| 62 |
% input arg so that previous programs may not be compatible). |
| 63 |
|
| 64 |
% |
| 65 |
% Copyright (C) 1997-2012 Tao Zha |
| 66 |
% |
| 67 |
% This free software: you can redistribute it and/or modify |
| 68 |
% it under the terms of the GNU General Public License as published by |
| 69 |
% the Free Software Foundation, either version 3 of the License, or |
| 70 |
% (at your option) any later version. |
| 71 |
% |
| 72 |
% It is distributed in the hope that it will be useful, |
| 73 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 74 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 75 |
% GNU General Public License for more details. |
| 76 |
% |
| 77 |
% If you did not received a copy of the GNU General Public License |
| 78 |
% with this software, see <http://www.gnu.org/licenses/>. |
| 79 |
% |
| 80 |
|
| 81 |
DLSIdShock = ~isempty(eq_ms); % if not empty, the MS shock is identified as in DLS |
| 82 |
|
| 83 |
impsteps=size(imf3s_h,1); |
| 84 |
if (forep<nstepsm) | (impsteps<nstepsm) |
| 85 |
disp('Increase # of forecast or impulse steps!!') |
| 86 |
disp('Or decrease # of constraints (nconstr) or constrained steps (stepcon(i))!!') |
| 87 |
error('Maximum of conditional steps > # of forecast or impulse steps!!') |
| 88 |
end |
| 89 |
kts = nvar*nstepsm; % k -- ts: total shocks some of which are restricted and others |
| 90 |
% are free. |
| 91 |
%*** initializing |
| 92 |
Rcon = zeros(kts,nconstr); % R: k-by-q |
| 93 |
Econ = zeros(kts,1); % E: k-by-1 |
| 94 |
rcon = zeros(nconstr,1); % r: q-by-1 |
| 95 |
%rcon=valuecon-diag(yfore(stepcon,varcon)); % another way is to use "loop" below. |
| 96 |
tcwc = nvar*lags; % total coefficients without constant |
| 97 |
phi=phil; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
%---------------------------------------------------- |
| 102 |
% Form rcon, Rcon, and Econ (the mean of structural shocks) |
| 103 |
%---------------------------------------------------- |
| 104 |
A0in = reshape(imf3s_h(1,:,:),nvar,nvar); % <<>>1 nvar shocks-by-nvar responses |
| 105 |
if nconstr % Conditional forecasts. |
| 106 |
for i=1:nconstr |
| 107 |
rcon(i)=length(stepcon{i})*valuecon(i) - sum(yfore_h(stepcon{i},varcon(i)),1); |
| 108 |
%<<>>2 Automatically taking care of average conditions. |
| 109 |
Rmat = zeros(nstepsm,nvar); |
| 110 |
r2mat = zeros(nstepsm,1); % simply one identified equation |
| 111 |
% Must be here inside the loop because it's matrix of one column of Rcon |
| 112 |
for j=1:length(stepcon{i}) |
| 113 |
if DLSIdShock % Assuming the Fed can't see all other shocks within a month |
| 114 |
Rmat(1:stepcon{i}(j),eq_ms) = Rmat(1:stepcon{i}(j),eq_ms) + ... |
| 115 |
imf3s_h(stepcon{i}(j):-1:1,eq_ms,varcon(i)); |
| 116 |
% Rmat: row--nstepsm, column--nvar shocks (here all shocks except |
| 117 |
% the identified one are set to zero) for a particular |
| 118 |
% endogenous variable 'varcon(i)'. See Zha Forcast (1), pp.6-7 |
| 119 |
else % Rcon random with (A0,A+) |
| 120 |
Rmat(1:stepcon{i}(j),:) = Rmat(1:stepcon{i}(j),:) + ... |
| 121 |
imf3s_h(stepcon{i}(j):-1:1,:,varcon(i)); |
| 122 |
% Rmat: row--nstepsm, column--nvar shocks (here all shocks are |
| 123 |
% *not* set to zero) for a particular endogenous |
| 124 |
% variable 'varcon(i)'. See Zha Forcast (1), pp.6-7 |
| 125 |
end |
| 126 |
end |
| 127 |
Rmatt = Rmat'; % Now, nvar-by-nstepsm. I think here is where RATS has an error |
| 128 |
% i.e. "OVERR" is not transposed when overlaid to "CAPR" |
| 129 |
Rcon(:,i)=Rmatt(:); % Rcon: k-by-q where q=nconstr |
| 130 |
end |
| 131 |
% |
| 132 |
[u d v]=svd(Rcon,0); %trial |
| 133 |
vd=v.*(ones(size(v,2),1)*diag(d)'); %trial |
| 134 |
dinv = 1./diag(d); % inv(diag(d)) |
| 135 |
vdinv=v.*(ones(size(v,2),1)*dinv'); %trial |
| 136 |
rtr=vd*vd'; % R'*R |
| 137 |
rtrinv = vdinv*vdinv'; % inv(R'*R) |
| 138 |
|
| 139 |
Econ = Rcon*rtrinv*rcon; % E = R*inv(R'R)*r; the mean of structural shocks |
| 140 |
else % Unconditional forecasts. |
| 141 |
Econ = zeros(kts,1); % the mean of shocks is zero under no variable condition |
| 142 |
Rcon = NaN; |
| 143 |
rcon = NaN; |
| 144 |
u = NaN; |
| 145 |
d = NaN; |
| 146 |
v = NaN; |
| 147 |
end |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
%--------------------------------------- |
| 152 |
% No uncertainty at all. In other words, no future shocks. |
| 153 |
%--------------------------------------- |
| 154 |
if (~Sband) %| (nconstr & (length(eq_ms)==1)) |
| 155 |
% length(eq_ms)==1 implies one-one mapping between MS shocks and, say, FFR |
| 156 |
% if nstepsm==nconstr. If this condition does not hold, this procedure |
| 157 |
% is incorrect. I don't have time to fix it now (3/20/99). So I use |
| 158 |
% this as a proximation |
| 159 |
Estr = reshape(Econ,nvar,nstepsm); |
| 160 |
Estr = Estr'; % transpose so that |
| 161 |
% Estr: structural shocks. Row--steps, Column--n shocks |
| 162 |
Estr = [Estr;zeros(forep-nstepsm,nvar)]; |
| 163 |
% Now, forep-by-nvar -- ready for forecasts |
| 164 |
Ures = Estr*A0in; % <<>>1 nstepsm-by-nvar |
| 165 |
% Ures: reduced-form residuals. Row--steps; Column--n shocks |
| 166 |
% Note: We don't use /A0_h so as to eliminate small discrepancies to be |
| 167 |
% completely compatible with the computation of Rmat and Estr, which uses A0in. |
| 168 |
|
| 169 |
% ** reconstruct x(t) for y(t+h) = x(t+h-1)*B |
| 170 |
% ** where phi = x(t+h-1) with last column being constant |
| 171 |
% |
| 172 |
yhat = zeros(forep,nvar); |
| 173 |
for k=1:forep |
| 174 |
yhat(k,:) = phi*Bh_h + Ures(k,:); |
| 175 |
phi(1,nvar+1:tcwc) = phi(1,1:tcwc-nvar); |
| 176 |
phi(1,1:nvar) = yhat(k,:); |
| 177 |
end |
| 178 |
%--------------------------------------- |
| 179 |
% With random future shocks. |
| 180 |
%--------------------------------------- |
| 181 |
else |
| 182 |
if nconstr % Conditional forecasts. |
| 183 |
%-------------- |
| 184 |
% Condition on variables with all shocks backed out. Straight DLS forecasts. No A random but S random. |
| 185 |
%-------------- |
| 186 |
if ~DLSIdShock |
| 187 |
Ome = eye(kts) - u*u'; % note, I-u*u' = I - R*inv(R'*R)*R' |
| 188 |
%[u1 d1 v1] = svd(Ome); % too slow |
| 189 |
[u1 d1] = eig(Ome); |
| 190 |
Stdcon = u1*diag(sqrt(diag(abs(d1)))); % lower triagular chol of conditional variance |
| 191 |
% see Zha's forecast (1), p.17 |
| 192 |
Estr1 = Econ + Stdcon*randn(kts,1); % Draws of constrained (conditioned) shocks. |
| 193 |
Estr2 = reshape(Estr1,nvar,nstepsm); |
| 194 |
Estr2 = Estr2'; % transpose so that |
| 195 |
% Estr2: structural shocks. Row--nstepsm, Column--n shocks |
| 196 |
Estr = [Estr2;randn(forep-nstepsm,nvar)]; % Second concatenated part: draws of free shocks. |
| 197 |
% Now, forep-by-nvar -- ready for forecasts |
| 198 |
%-------------- |
| 199 |
% Condition on variables with identified MS shocks backed out, no A random but S random. |
| 200 |
%-------------- |
| 201 |
else % other shocks are indepedent of the eq_ms shock |
| 202 |
% 3/20/99 The following may be problematic because Osk should depend |
| 203 |
% on u (A0_h and Bh_h) in general. I have NOT worked out any good version. |
| 204 |
%/* |
| 205 |
% Osk = randn(kts,1); % other shocks |
| 206 |
% for j=1:nstepsm |
| 207 |
% Osk(nvar*(j-1)+eq_ms)=0; % no shock to the MS or identified equation |
| 208 |
% end |
| 209 |
% Estr = Econ + Osk; % Econ is non zero only at position |
| 210 |
% % eq_ms*j where j=1:nstepsm |
| 211 |
% Estr = reshape(Estr,nvar,nstepsm); |
| 212 |
% Estr = Estr'; % transpose so that |
| 213 |
% % Estr: structural shocks. Row--steps, Column--n shocks |
| 214 |
% Estr = [Estr;randn(forep-nstepsm,nvar)]; |
| 215 |
% % Now, forep-by-nvar -- ready for forecasts |
| 216 |
% |
| 217 |
disp('DLS') |
| 218 |
Ome = eye(kts) - u*u'; % note, I-u*u' = I - R*inv(R'*R)*R' |
| 219 |
%[u1 d1 v1] = svd(Ome); % too slow |
| 220 |
[u1 d1] = eig(Ome); |
| 221 |
Stdcon = u1*diag(sqrt(diag(abs(d1)))); % lower triagular chol of conditional variance |
| 222 |
% see Zha's forecast (1), p.17 |
| 223 |
tmp1=zeros(nvar,nstepsm); |
| 224 |
tmp1(eq_ms,:)=randn(1,nstepsm); |
| 225 |
tmp2=tmp1(:); |
| 226 |
%Estr1 = Econ + Stdcon*randn(kts,1); |
| 227 |
%jnk = reshape(Stdcon*tmp2,nvar,nstepsm) |
| 228 |
Estr1 = Econ + Stdcon*tmp2; |
| 229 |
Estr2 = reshape(Estr1,nvar,nstepsm); |
| 230 |
Estr2 = Estr2'; % transpose so that |
| 231 |
% Estr2: structural shocks. Row--nstepsm, Column--n shocks |
| 232 |
Estr = [Estr2;randn(forep-nstepsm,nvar)]; |
| 233 |
% Now, forep-by-nvar -- ready for forecasts |
| 234 |
end |
| 235 |
else % Unconditional forecasts. |
| 236 |
Estr = randn(forep,nvar); % Unconditional draws. |
| 237 |
end |
| 238 |
|
| 239 |
Ures = Estr*A0in; % <<>>1 nstepsm-by-nvar |
| 240 |
% Ures: reduced-form residuals. Row--steps; Column--n shocks |
| 241 |
% Note: We don't use /A0_h so as to eliminate small discrepancies to be |
| 242 |
% completely compatible with the computation of Rmat and Estr, which uses A0in. |
| 243 |
|
| 244 |
% ** reconstruct x(t) for y(t+h) = x(t+h-1)*B |
| 245 |
% ** where phi = x(t+h-1) with last column being constant |
| 246 |
% |
| 247 |
yhat = zeros(forep,nvar); |
| 248 |
for k=1:forep |
| 249 |
yhat(k,:) = phi*Bh_h + Ures(k,:); |
| 250 |
phi(1,nvar+1:tcwc) = phi(1,1:tcwc-nvar); |
| 251 |
phi(1,1:nvar) = yhat(k,:); |
| 252 |
end |
| 253 |
end |