| 1 |
function [yhat,Estr,rcon,Rcon,u,v,d] = fidcnderr(valuecon,stepcon,varcon,nconstr,... |
| 2 |
nstepsm,nvar,lags,phil,eq_iden,Aband,Sband,yforeml,imf3sml,Bhml,... |
| 3 |
yfore_h,imf3s_h,Bh_h,Cms,TLindx,TLnumber,nCms,eq_Cms) |
| 4 |
%function [yhat,Estr,rcon,Rcon,u,v,d] = fidcnderr(valuecon,stepcon,varcon,nconstr,... |
| 5 |
% nstepsm,nvar,lags,phil,eq_iden,Aband,Sband,yforeml,imf3sml,Bhml,... |
| 6 |
% yfore_h,imf3s_h,Bh_h,Cms,TLindx,TLnumber,nCms,eq_Cms) |
| 7 |
% To be done (8/25/98): (1) yforeml, imf3sml, and Bhml should not be there. |
| 8 |
% (2) MS stuff need to be generalized. |
| 9 |
% (3) perhaps, imf3s_h needs to be re-examined. |
| 10 |
% Conditional forecasting in the identified model with one draw for error bands |
| 11 |
% |
| 12 |
% valuecon: vector of values conditioned |
| 13 |
% stepcon: sequence (cell) of steps conditioned; if length(stepcon{i}) > 1, the condition |
| 14 |
% is then an arithmetic average of log(y) over the stepcon{i} period. |
| 15 |
% varcon: vector of variables conditioned |
| 16 |
% nconstr: number of constraints |
| 17 |
% nstepsm: maximum number of steps in all constraints |
| 18 |
% nvar: number of variables in the BVAR model |
| 19 |
% lags: number of lags in the BVAR model |
| 20 |
% phil: the 1-by-(nvar*lags+1) data matrix where k=nvar*lags+1 |
| 21 |
% (last period plus lags before the beginning of forecast) |
| 22 |
% eq_iden: identified equation or shock (in terms of number). If equ_iden=[], then |
| 23 |
% 'fidencond' is, similar to RATS, to compute forecasts with *all* shocks. |
| 24 |
% Sband: 1: generate error bands from random shocks E; 0: no random shocks |
| 25 |
% yfore: uncondtional forecasts: forep-by-nvar |
| 26 |
% imf3s: 3-dimensional impulse responses matrix: |
| 27 |
% impsteps-by-nvar shocks-by-nvar responses |
| 28 |
% Bh: reduced-form parameter matrix: k-by-nvar, y(t) = X(t)*Bh+e(t) |
| 29 |
% where X(t) is k-by-nvar and y(t) is 1-by-nvar |
| 30 |
% ------ |
| 31 |
% yhat: conditional forecasts: forep-by-nvar |
| 32 |
% Estr: backed-out structural shocks (from N(0,1)) |
| 33 |
% rcon: vector - the difference between valuecon and log(yfore) (unconditional forecasts) |
| 34 |
% Rcon: k-by-q (q constranits and k=nvar*max(nsteps)) so that |
| 35 |
% Rcon'*e = rcon where e is k-by-1 |
| 36 |
% [u,d,v]: svd(Rcon,0) |
| 37 |
% |
| 38 |
%% See Zha's note "Forecast (1)" p. 5, RATS manual (some errors in RATS), etc. |
| 39 |
% |
| 40 |
%% Some notations: y(t+1) = y(t)B1 + e(t+1)inv(A0). e(t+1) is 1-by-n. |
| 41 |
%% Let r(t+1)=e(t+1)inv(A0) + e(t+2)C + .... where inv(A0) is impulse |
| 42 |
%% response at t=1, C at t=2, etc. The row of inv(A0) or C is |
| 43 |
%% all responses to one shock. |
| 44 |
%% Let r be q-by-1 (such as r(1) = r(t+1) |
| 45 |
%% = y(t+1) (constrained) - y(t+1) (forecast)). |
| 46 |
%% Use impulse responses to find out R (k-by-q) where k=nvar*nsteps |
| 47 |
%% where nsteps the largest constrained step. The key of the program |
| 48 |
%% is to creat R using impulse responses |
| 49 |
%% Optimal solution for shock e where R'*e=r and e is k-by-1 is |
| 50 |
%% e = R*inv(R'*R)*r. |
| 51 |
% |
| 52 |
% |
| 53 |
% Copyright (C) 1997-2012 Tao Zha |
| 54 |
% |
| 55 |
% This free software: you can redistribute it and/or modify |
| 56 |
% it under the terms of the GNU General Public License as published by |
| 57 |
% the Free Software Foundation, either version 3 of the License, or |
| 58 |
% (at your option) any later version. |
| 59 |
% |
| 60 |
% It is distributed in the hope that it will be useful, |
| 61 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 62 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 63 |
% GNU General Public License for more details. |
| 64 |
% |
| 65 |
% If you did not received a copy of the GNU General Public License |
| 66 |
% with this software, see <http://www.gnu.org/licenses/>. |
| 67 |
% |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
IdenShock = ~isempty(eq_iden); % if not empty, the shock is identified |
| 72 |
|
| 73 |
forep=size(yfore_h,1); |
| 74 |
impsteps=size(imf3s_h,1); |
| 75 |
if (forep<nstepsm) | (impsteps<nstepsm) |
| 76 |
disp('Increase # of forecast or impulse steps!!') |
| 77 |
disp('Or decrease # of constraints (nconstr) or constrained steps (stepcon(i))!!') |
| 78 |
error('Maximum of conditional steps > # of forecast or impulse steps!!') |
| 79 |
end |
| 80 |
kfs = nvar*nstepsm; % k -- fs: free shocks |
| 81 |
%*** initializing |
| 82 |
Rcon = zeros(kfs,nconstr); % R: k-by-q |
| 83 |
Econ = zeros(kfs,1); % E: k-by-1 |
| 84 |
rcon = zeros(nconstr,1); % r: q-by-1 |
| 85 |
%rcon=valuecon-diag(yfore(stepcon,varcon)); % another way is to use "loop" below. |
| 86 |
A0in = reshape(imf3s_h(1,:,:),nvar,nvar); % nvar shocks-by-nvar responses |
| 87 |
|
| 88 |
|
| 89 |
for i=1:nconstr |
| 90 |
if IdenShock |
| 91 |
rcon(i)=length(stepcon{i})*valuecon(i) - ... |
| 92 |
sum(yforeml(stepcon{i},varcon(i)),1); % <<>> |
| 93 |
else |
| 94 |
rcon(i)=length(stepcon{i})*valuecon(i) - ... |
| 95 |
sum(yfore_h(stepcon{i},varcon(i)),1); %<<>> |
| 96 |
end |
| 97 |
Rmat = zeros(nstepsm,nvar); |
| 98 |
r2mat = zeros(nstepsm,1); % simply one identified equation |
| 99 |
% Must be here inside the loop because it's matrix of one column of Rcon |
| 100 |
for j=1:length(stepcon{i}) |
| 101 |
if IdenShock % Rcon only at ML; assuming the Fed can't see all other shocks |
| 102 |
Rmat(1:stepcon{i}(j),eq_iden) = Rmat(1:stepcon{i}(j),eq_iden) + ... |
| 103 |
imf3sml(stepcon{i}(j):-1:1,eq_iden,varcon(i)); |
| 104 |
% Rmat: row--nstepsm, column--nvar shocks (here all shocks except |
| 105 |
% the identified one are set to zero) for a particular |
| 106 |
% endogenous variable 'varcon(i)'. See Zha Forcast (1), pp.6-7 |
| 107 |
else % Rcon random with (A0,A+) |
| 108 |
Rmat(1:stepcon{i}(j),:) = Rmat(1:stepcon{i}(j),:) + ... |
| 109 |
imf3s_h(stepcon{i}(j):-1:1,:,varcon(i)); |
| 110 |
% Rmat: row--nstepsm, column--nvar shocks (here all shocks are |
| 111 |
% *not* set to zero) for a particular endogenous |
| 112 |
% variable 'varcon(i)'. See Zha Forcast (1), pp.6-7 |
| 113 |
end |
| 114 |
end |
| 115 |
Rmatt = Rmat'; % Now, nvar-by-nstepsm. I think here is where RATS has an error |
| 116 |
% i.e. "OVERR" is not transposed when overlaid to "CAPR" |
| 117 |
Rcon(:,i)=Rmatt(:); % Rcon: k-by-q where q=nconstr |
| 118 |
end |
| 119 |
|
| 120 |
if nconstr |
| 121 |
[u d v]=svd(Rcon,0); %trial |
| 122 |
% rtr = Rcon'*Rcon; %trial |
| 123 |
% rtrinv = inv(Rcon'*Rcon); %trial |
| 124 |
vd=v.*(ones(size(v,2),1)*diag(d)'); %trial |
| 125 |
dinv = 1./diag(d); % inv(diag(d)) |
| 126 |
vdinv=v.*(ones(size(v,2),1)*dinv'); %trial |
| 127 |
rtr=vd*vd'; % R'*R |
| 128 |
rtrinv = vdinv*vdinv'; % inv(R'*R) |
| 129 |
|
| 130 |
Econ = Rcon*rtrinv*rcon; % E = R*inv(R'R)*r; mean |
| 131 |
else |
| 132 |
Econ = zeros(kfs,1); |
| 133 |
Rcon = NaN; |
| 134 |
rcon = NaN; |
| 135 |
u = NaN; |
| 136 |
d = NaN; |
| 137 |
v = NaN; |
| 138 |
end |
| 139 |
|
| 140 |
|
| 141 |
tcwc = nvar*lags; % total coefficients without constant |
| 142 |
phi=phil; |
| 143 |
phis=phil; % for exact backed out shocks |
| 144 |
% |
| 145 |
if (Sband==0) % no random or random only from (A0,A+) |
| 146 |
Estr = reshape(Econ,nvar,nstepsm); |
| 147 |
Estr = Estr'; % transpose so that |
| 148 |
% Estr: structural shocks. Row--steps, Column--n shocks |
| 149 |
Estr = [Estr;randn(forep-nstepsm,nvar)]; |
| 150 |
% Now, forep-by-nvar -- ready for forecasts |
| 151 |
Ures = Estr*A0in; % nstepsm-by-nvar |
| 152 |
% Ures: reduced-form residuals. Row--steps; Column--n shocks |
| 153 |
|
| 154 |
% ** reconstruct x(t) for y(t+h) = x(t+h-1)*B |
| 155 |
% ** where phi = x(t+h-1) with last column being constant |
| 156 |
% |
| 157 |
yhat = zeros(forep,nvar); |
| 158 |
for k=1:forep |
| 159 |
yhat(k,:) = phi*Bh_h + Ures(k,:); |
| 160 |
phi(1,nvar+1:tcwc) = phi(1,1:tcwc-nvar); |
| 161 |
phi(1,1:nvar) = yhat(k,:); |
| 162 |
% |
| 163 |
end |
| 164 |
else % random from shocks E and possibly (A0,A+) depending on if imf3s is random |
| 165 |
if nconstr |
| 166 |
if IdenShock % other shocks are indepedent of the eq_iden shock |
| 167 |
Osk = randn(kfs,1); % other shocks |
| 168 |
for j=1:nstepsm |
| 169 |
Osk(nvar*(j-1)+eq_iden)=0; % no shock to the MS or identified equation |
| 170 |
end |
| 171 |
Estr = Econ + Osk; % Econ is non zero only at position |
| 172 |
% eq_iden*j where j=1:nstepsm |
| 173 |
else |
| 174 |
Ome = eye(kfs) - u*u'; % note, I-u*u' = I - R*inv(R'*R)*R' |
| 175 |
%[u1 d1 v1] = svd(Ome); % too slow |
| 176 |
[u1 d1] = eig(Ome); |
| 177 |
Stdcon = u1*diag(sqrt(diag(abs(d1)))); % lower triagular chol of conditional variance |
| 178 |
% see Zha's forecast (1), p.17 |
| 179 |
if Cms |
| 180 |
if TLindx % tight |
| 181 |
Estr1 = Econ + Stdcon*randn(kfs,1); |
| 182 |
Estr2 = reshape(Estr1,nvar,nstepsm); |
| 183 |
Estr2 = Estr2'; % transpose so that |
| 184 |
% Estr2: structural shocks. Row--nstepsm, Column--n shocks |
| 185 |
Estr = [Estr2;randn(forep-nstepsm,nvar)]; |
| 186 |
% Now, forep-by-nvar -- ready for forecasts |
| 187 |
Sindx = find(Estr(1:nCms,eq_Cms)<TLnumber); |
| 188 |
while ~isempty(Sindx) |
| 189 |
Estr1 = Econ + Stdcon*randn(kfs,1); |
| 190 |
Estr2 = reshape(Estr1,nvar,nstepsm); |
| 191 |
Estr2 = Estr2'; % transpose so that |
| 192 |
% Estr2: structural shocks. Row--nstepsm, Column--n shocks |
| 193 |
Estr = [Estr2;randn(forep-nstepsm,nvar)]; |
| 194 |
% Now, forep-by-nvar -- ready for forecasts |
| 195 |
Sindx = find(Estr(1:nCms,eq_Cms)<TLnumber); |
| 196 |
end |
| 197 |
else % loose |
| 198 |
Estr1 = Econ + Stdcon*randn(kfs,1); |
| 199 |
Estr2 = reshape(Estr1,nvar,nstepsm); |
| 200 |
Estr2 = Estr2'; % transpose so that |
| 201 |
% Estr2: structural shocks. Row--nstepsm, Column--n shocks |
| 202 |
Estr = [Estr2;randn(forep-nstepsm,nvar)]; |
| 203 |
% Now, forep-by-nvar -- ready for forecasts |
| 204 |
Sindx = find(Estr(1:nCms,eq_Cms)>TLnumber); |
| 205 |
while ~isempty(Sindx) |
| 206 |
Estr1 = Econ + Stdcon*randn(kfs,1); |
| 207 |
Estr2 = reshape(Estr1,nvar,nstepsm); |
| 208 |
Estr2 = Estr2'; % transpose so that |
| 209 |
% Estr2: structural shocks. Row--nstepsm, Column--n shocks |
| 210 |
Estr = [Estr2;randn(forep-nstepsm,nvar)]; |
| 211 |
% Now, forep-by-nvar -- ready for forecasts |
| 212 |
Sindx = find(Estr(1:nCms,eq_Cms)>TLnumber); |
| 213 |
end |
| 214 |
end |
| 215 |
else |
| 216 |
Estr1 = Econ + Stdcon*randn(kfs,1); |
| 217 |
Estr2 = reshape(Estr1,nvar,nstepsm); |
| 218 |
Estr2 = Estr2'; % transpose so that |
| 219 |
% Estr2: structural shocks. Row--nstepsm, Column--n shocks |
| 220 |
Estr = [Estr2;randn(forep-nstepsm,nvar)]; |
| 221 |
% Now, forep-by-nvar -- ready for forecasts |
| 222 |
end |
| 223 |
end |
| 224 |
else |
| 225 |
if Cms |
| 226 |
if TLindx % tight |
| 227 |
Estr = randn(forep,nvar); |
| 228 |
% Now, forep-by-nvar -- ready for forecasts |
| 229 |
Sindx = find(Estr(1:nCms,eq_Cms)<TLnumber); |
| 230 |
while ~isempty(Sindx) |
| 231 |
Estr = randn(forep,nvar); |
| 232 |
% Now, forep-by-nvar -- ready for forecasts |
| 233 |
Sindx = find(Estr(1:nCms,eq_Cms)<TLnumber); |
| 234 |
end |
| 235 |
else % loose |
| 236 |
Estr = randn(forep,nvar); |
| 237 |
% Now, forep-by-nvar -- ready for forecasts |
| 238 |
Sindx = find(Estr(1:nCms,eq_Cms)>TLnumber); |
| 239 |
while ~isempty(Sindx) |
| 240 |
Estr = randn(forep,nvar); |
| 241 |
% Now, forep-by-nvar -- ready for forecasts |
| 242 |
Sindx = find(Estr(1:nCms,eq_Cms)>TLnumber); |
| 243 |
end |
| 244 |
end |
| 245 |
else |
| 246 |
Estr = randn(forep,nvar); |
| 247 |
% Now, forep-by-nvar -- ready for forecasts |
| 248 |
end |
| 249 |
end |
| 250 |
% |
| 251 |
%disp('HERE') |
| 252 |
|
| 253 |
Ures = Estr*A0in; % nstepsm-by-nvar |
| 254 |
% Ures: reduced-form residuals. Row--steps; Column--n shocks |
| 255 |
|
| 256 |
% ** reconstruct x(t) for y(t+h) = x(t+h-1)*B |
| 257 |
% ** where phi = x(t+h-1) with last column being constant |
| 258 |
% |
| 259 |
yhat = zeros(forep,nvar); |
| 260 |
for k=1:forep |
| 261 |
yhat(k,:) = phi*Bh_h + Ures(k,:); |
| 262 |
phi(1,nvar+1:tcwc) = phi(1,1:tcwc-nvar); |
| 263 |
phi(1,1:nvar) = yhat(k,:); |
| 264 |
end |
| 265 |
end |