| 1 |
function [Gb,Sbd,Bh,SpH,fss,ndobs,phi,y,nvar,ncoef,xxhpc,a0indx,na0p,... |
| 2 |
idmat0,idmatpp,H0invmulti,Hpinvmulti,xxhp] = szasbvar(idfile,q_m,lags,xdgel,mu) |
| 3 |
% [Gb,Sbd,Bh,SpH,fss,ndobs,phi,y,nvar,ncoef,xxhpc,a0indx,na0p,... |
| 4 |
% idmat0,idmatpp,H0invmulti,Hpinvmulti] = szasbvar(idfile,q_m,lags,xdgel,mu) |
| 5 |
% |
| 6 |
% Estimating the Bayesian VAR of Sims and Zha with asymmetric prior (as) |
| 7 |
% |
| 8 |
% idfile: Identification filename with rows corresponding to equations. |
| 9 |
% But all the output will be transposed to columns |
| 10 |
% corresponding to equations. |
| 11 |
% q_m: quarter or month |
| 12 |
% lags: the maximum length of lag |
| 13 |
% nhp: number of haperparameters |
| 14 |
% xdgel: the general matrix of the original data (no manipulation involved) |
| 15 |
% with sample size including lags |
| 16 |
% mu: 6-by-1 vector of hyperparameters (the following numbers for Atlanta Fed's forecast) |
| 17 |
% mu(1): overall tightness and also for A0; (0.57) |
| 18 |
% mu(2): relative tightness for A+; (0.13) |
| 19 |
% mu(3): relative tightness for the constant term; (0.1) |
| 20 |
% mu(4): tightness on lag decay; (1) |
| 21 |
% mu(5): weight on nvar sums of coeffs dummy observations (unit roots); (5) |
| 22 |
% mu(6): weight on single dummy initial observation including constant |
| 23 |
% (cointegration, unit roots, and stationarity); (5) |
| 24 |
% -------------------- |
| 25 |
% -------------------- |
| 26 |
% Gb: cell(nvar,1). Each cell, when postmultiplied by A0(:,i), is used to compute A+(:,i) |
| 27 |
% where A+ is k-by-m, where k--ncnoef, m--nvar, if asymmetric prior. |
| 28 |
% In particular, Aplus(:,i)=Gb{i}*A0(:,i), so Bh = Aplus/A0 |
| 29 |
% Sbd: cell(nvar,1). Sbd=diag(S(1), ..., S(m)). Already divided by 'fss' for the |
| 30 |
% posterior of a0 or A0(:) in Waggoner and Zha when one has asymmetric prior. |
| 31 |
% Note,"bd" stands for block diagonal. |
| 32 |
% Bh: reduced form B, k(mp+1)-by-m, column--equation. Bh=NaN if asymmetric prior. |
| 33 |
% In the form of y=X*B+U where B=[B1|B2| ...|Bp|C] |
| 34 |
% where Bi: m-by-m (i=1,..,p--lag length), C: 1-by-ml, constant. |
| 35 |
% SpH: divided by T, the final S in the Waggoner-Zha exponential part of p(A0|y) |
| 36 |
% SpH=NaN if asymmetric prior |
| 37 |
% fss: in-sample-size (for forecasting). Including dummy observations |
| 38 |
% ndobs: number of dummy observations, ndobs=nvar+1 |
| 39 |
% phi: X in the form of y = X*B+U. Row: nSmaple-lags+ndobs. Column: ncoef |
| 40 |
% y: y in the form y = X*B+U. Including the dummy observations too, |
| 41 |
% T-lags+ndobs-by-nvar. |
| 42 |
% nvar: number of variables |
| 43 |
% ncoef: number of coefficients in *each* equation. RHS coefficients only, nvar*lags+1 |
| 44 |
% xxhpc: chol(X'X+inv(H_p_tilde)): upper triangular but its transpose |
| 45 |
% is lower triagular Choleski |
| 46 |
% a0indx: the index number for free parameters in A0. Column meaning equation |
| 47 |
% na0p: number of free parameters in A0 |
| 48 |
% idmat0: identification matrix for A0 with asymmetric prior; column -- equation. |
| 49 |
% idmatpp: asymmetric prior variance for A+ without constant; column -- equation. |
| 50 |
% H0invmulti: nvar-by-nvar-by-nvar; inv(H0) for different equations under asymmetric prior |
| 51 |
% Hpinvmulti: ncoef-by-ncoef-by-nvar; inv(H+) for different equations under asymmetric prior |
| 52 |
% xxhp: ncoef-by-nocef, X'X+inv(H_p_tilde) |
| 53 |
% |
| 54 |
% Revisions by CAS 9/27/96: If we transmit idmat, rather than a0indx, we can scale |
| 55 |
% the elements of idmat0 or idmatp so that it carries information about relative |
| 56 |
% prior variances. |
| 57 |
% Revsions by TZ, 10/13/96: Efficiency improvement by streamlining the previous |
| 58 |
% code according to the general setup in Sims and Zha's IER and according |
| 59 |
% to Zha's notes Forecast (0) (p.3) and Forecast (1) (p.9). |
| 60 |
% Quick Revisions: May 2003. See H1p_1 on lines 406-409. |
| 61 |
% |
| 62 |
% NOTE1: "nSample" is something I deleted as an input recently, so it may not |
| 63 |
% be compatible with old programs, 10/15/98 by TZ. |
| 64 |
% NOTE2: I put "mu" as an input arg and delete "nhp" as an input arg, so it may |
| 65 |
% not be compatible with old programs, 03/06/99 by TZ |
| 66 |
% NOTE3: added three output arguments: H0invmulti and Hpinvmulti and xxhp. 9/27/99 |
| 67 |
|
| 68 |
% |
| 69 |
% Copyright (C) 1997-2012 Christopher A. Sims and Tao Zha |
| 70 |
% |
| 71 |
% This free software: you can redistribute it and/or modify |
| 72 |
% it under the terms of the GNU General Public License as published by |
| 73 |
% the Free Software Foundation, either version 3 of the License, or |
| 74 |
% (at your option) any later version. |
| 75 |
% |
| 76 |
% It is distributed in the hope that it will be useful, |
| 77 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 78 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 79 |
% GNU General Public License for more details. |
| 80 |
% |
| 81 |
% If you did not received a copy of the GNU General Public License |
| 82 |
% with this software, see <http://www.gnu.org/licenses/>. |
| 83 |
% |
| 84 |
|
| 85 |
|
| 86 |
% |
| 87 |
%@@@ Prepared for Bayesian VAR of Sims and Zha |
| 88 |
% |
| 89 |
%* Load identification and obtain idmat0 |
| 90 |
eval(['load ' idfile '.prn -ascii']); |
| 91 |
eval(['idmat0=' idfile ';']); |
| 92 |
idmat0=idmat0'; % so that each column corresponds to an equation |
| 93 |
a0indx=find(idmat0); % column meaning equation |
| 94 |
na0p = length(a0indx); % number of free parameters in A0 |
| 95 |
nhp = length(mu); % total number of hyperparameters |
| 96 |
nfp = na0p + nhp; % total number of free parameters (including hyperparameters) |
| 97 |
[nvar,neqn]=size(idmat0); |
| 98 |
% |
| 99 |
idmatpp = ones(nvar*lags,nvar); % pp: plus without constant. Column -- equation |
| 100 |
%>>>>>> B: asymmetric prior variance for idmatpp <<<<<<<< |
| 101 |
% |
| 102 |
%for i = 1:lags |
| 103 |
% rowif = (i-1)*nvar+1; |
| 104 |
% rowil = i*nvar; |
| 105 |
% idmatw0 = 0.5; % weight assigned to idmat0 in the formation of idmatpp |
| 106 |
% if (i==1) |
| 107 |
% idmatpp(rowif:rowil,:)=(1-idmatw0)*ones(nvar)+idmatw0*idmat0; % first lag |
| 108 |
% % note: idmat1 is already transposed. Column -- equation |
| 109 |
% else |
| 110 |
% %idmatpp(rowif:rowil,1:nvar) = (1-idmatw0)*ones(nvar)+idmatw0*idmat0; |
| 111 |
% % <<<<<<< toggle + |
| 112 |
% % Note: already transposed, since idmat0 is transposed. |
| 113 |
% % Meaning: column implies equation |
| 114 |
% idmatpp(rowif:rowil,1:nvar) = ones(nvar); |
| 115 |
% % >>>>>>> toggle - |
| 116 |
% end |
| 117 |
%end |
| 118 |
% |
| 119 |
%>>>>>> E: asymmetric prior variance for idmatpp <<<<<<<< |
| 120 |
|
| 121 |
|
| 122 |
%$$$ available computations |
| 123 |
% |
| 124 |
%@@@ original |
| 125 |
% total number of the sample under study, including lags, etc. -- original sample size |
| 126 |
nSample = size(xdgel,1); % the sample size (including lags, of course) |
| 127 |
sb = lags+1; % original beginning without dummies |
| 128 |
sl = nSample; % original last period without dummies |
| 129 |
ssp = nSample - lags; % original number of observations |
| 130 |
% |
| 131 |
ndobs=nvar+1; % number of dummy observations |
| 132 |
ncoef = nvar*lags+1; % number of coefficients in *each* equation, RHS coefficients only. |
| 133 |
%*** initializing for global variables |
| 134 |
%%GlbAph=zeros(nvar,ncoef); |
| 135 |
%Aplus=zeros(ncoef,nvar); |
| 136 |
Gb = cell(nvar,1); % Storing potential reduced-form Bh (k-by-m) or prepared |
| 137 |
% for computing Aplus (k-by-m), where k--ncnoef, m--nvar |
| 138 |
Sbd = cell(nvar,1); % Storing for diag(S(1), ..., S(m)) for the posterior of a0 |
| 139 |
% or vec(A0) when one has asymmetric prior. Note, |
| 140 |
% "bd" stands for block diagonal. |
| 141 |
SpH = ones(nvar,nvar); |
| 142 |
%* flp: forecast last period (with dummies); |
| 143 |
%* fbp: forecast beginning period (with dummies). |
| 144 |
flp = sl+ndobs; |
| 145 |
%fbp = ndobs+lags+1; % <<>> true begining by skipping dummies |
| 146 |
fss = flp-lags; % forecast sample size (with dummies). |
| 147 |
|
| 148 |
% ** hyperparameters |
| 149 |
% mu = zeros(nhp,1); |
| 150 |
% mu(1) = 0.57; |
| 151 |
% mu(2) = 0.13; |
| 152 |
% mu(3) = 0.1; |
| 153 |
% mu(4) = 1; |
| 154 |
% mu(5) = 5; %10; |
| 155 |
% mu(6) = 5; %10; |
| 156 |
% |
| 157 |
% mu(1): overall tightness and also for A0; |
| 158 |
% mu(2): relative tightness for A+; |
| 159 |
% mu(3): relative tightness for the constant term; |
| 160 |
% mu(4): tightness on lag decay; |
| 161 |
% mu(5): weight on nvar sums of coeffs dummy observations (unit roots); |
| 162 |
% mu(6): weight on single dummy initial observation including constant (cointegration, unit |
| 163 |
% roots, and stationarity). |
| 164 |
|
| 165 |
|
| 166 |
% ** monthly lag decay in order to match quarterly decay: a*exp(bl) where |
| 167 |
% ** l is the monthly lag. Suppose quarterly decay is 1/x where x=1,2,3,4. |
| 168 |
% ** Let the decay of l1 (a*exp(b*l1)) match that of x1 (say, beginning: 1/1) |
| 169 |
% ** and the decay of l2 (a*exp(b*l2)) match that of x2 (say, end: 1/5), |
| 170 |
% ** we can solve for a and b which are |
| 171 |
% ** b = (log_x1-log_x2)/(l1-l2), and a = x1*exp(-b*l1). |
| 172 |
if q_m==12 |
| 173 |
l1 = 1; % 1st month == 1st quarter |
| 174 |
xx1 = 1; % 1st quarter |
| 175 |
l2 = lags; % last month |
| 176 |
xx2 = 1/((ceil(lags/3))^mu(4)); % last quarter |
| 177 |
%xx2 = 1/6; % last quarter |
| 178 |
% 3rd quarter: i.e., we intend to let decay of the 6th month match |
| 179 |
% that of the 3rd quarter, so that the 6th month decays a little |
| 180 |
% faster than the second quarter which is 1/2. |
| 181 |
if lags==1 |
| 182 |
b = 0; |
| 183 |
else |
| 184 |
b = (log(xx1)-log(xx2))/(l1-l2); |
| 185 |
end |
| 186 |
a = xx1*exp(-b*l1); |
| 187 |
end |
| 188 |
|
| 189 |
% |
| 190 |
% ** now run the VAR with |
| 191 |
% |
| 192 |
% **** nvar prior dummy observations with the sum of coefficients |
| 193 |
% ** construct X for Y = X*B + U where phi = X: (T-lags)*k, Y: (T-lags)*nvar |
| 194 |
% ** columns: k = # of [nvar for 1st lag, ..., nvar for last lag, const] |
| 195 |
% ** Now, T=T+ndobs -- added with "ndobs" dummy observations |
| 196 |
% |
| 197 |
phi = zeros(fss,ncoef); |
| 198 |
const = ones(fss,1); |
| 199 |
const(1:nvar) = zeros(nvar,1); |
| 200 |
phi(:,ncoef) = const; % the first nvar periods: no or zero constant! |
| 201 |
% |
| 202 |
xdgelint = mean(xdgel(1:lags,:),1); % mean of the first lags initial conditions |
| 203 |
%* Dummies |
| 204 |
for k=1:nvar |
| 205 |
for m=1:lags |
| 206 |
phi(ndobs,nvar*(m-1)+k) = xdgelint(k); |
| 207 |
phi(k,nvar*(m-1)+k) = xdgelint(k); |
| 208 |
% <<>> multiply hyperparameter later |
| 209 |
end |
| 210 |
end |
| 211 |
%* True data |
| 212 |
for k=1:lags |
| 213 |
phi(ndobs+1:fss,nvar*(k-1)+1:nvar*k) = xdgel(sb-k:sl-k,:); |
| 214 |
% row: T-lags; column: [nvar for 1st lag, ..., nvar for last lag, const] |
| 215 |
% Thus, # of columns is nvar*lags+1 = ncoef. |
| 216 |
end |
| 217 |
% |
| 218 |
% ** Y with "ndobs" dummies added |
| 219 |
y = zeros(fss,nvar); |
| 220 |
%* Dummies |
| 221 |
for k=1:nvar |
| 222 |
y(ndobs,k) = xdgelint(k); |
| 223 |
y(k,k) = xdgelint(k); |
| 224 |
% multiply hyperparameter later |
| 225 |
end |
| 226 |
%* True data |
| 227 |
y(ndobs+1:fss,:) = xdgel(sb:sl,:); |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
% |
| 232 |
% *** specify the prior for each equation separately, SZ method, |
| 233 |
% *** |
| 234 |
% *** obtaining the residuals for the univariate processes: |
| 235 |
% *** a total of "nvar" equations |
| 236 |
% *** obtain the posterior peak of A0 which is Sims iden1 (Sims 1986) |
| 237 |
% ** get the residuals from univariate regressions. |
| 238 |
% |
| 239 |
sgh = zeros(nvar,1); % square root |
| 240 |
sgsh = sgh; % square |
| 241 |
yu = xdgel; |
| 242 |
C = ones(nSample,1); |
| 243 |
for k=1:nvar |
| 244 |
[Bk,ek,junk1,junk2,junk3,junk4] = sye([yu(:,k) C],lags); |
| 245 |
clear Bk junk1 junk2 junk3 junk4; |
| 246 |
sgsh(k) = ek'*ek/ssp; |
| 247 |
%% sgh(k) = ek'*ek/(fss-7); to match "sqrt(ess)" in RATS univariate regression |
| 248 |
sgh(k) = sqrt(sgsh(k)); |
| 249 |
end |
| 250 |
% ** prior variance for alpha0, same for all equations!!! |
| 251 |
al0b = zeros(nvar*neqn,1); % prior mean for all A0 |
| 252 |
sg0bid = zeros(nvar,1); % Sigma0_bar diagonal only |
| 253 |
for j=1:nvar |
| 254 |
sg0bid(j) = 1/sgsh(j); % sgsh = sigmai^2 |
| 255 |
end |
| 256 |
% ** prior variance for alpha_plus, same for all equations |
| 257 |
sgpbid = zeros(ncoef,1); % Sigma_plus_bar, diagonal |
| 258 |
for i = 1:lags |
| 259 |
if (q_m==12) |
| 260 |
lagdecay = a*exp(b*i); |
| 261 |
end |
| 262 |
% |
| 263 |
for j = 1:nvar |
| 264 |
if (q_m==12) |
| 265 |
% exponential decay to match quarterly decay |
| 266 |
sgpbid((i-1)*nvar+j) = lagdecay^2/sgsh(j); |
| 267 |
%%sgpbid((i-1)*nvar+j) = (1/i)^2/sgsh(j); |
| 268 |
elseif (q_m==4) |
| 269 |
sgpbid((i-1)*nvar+j) = (1/i^mu(4))^2/sgsh(j); |
| 270 |
else |
| 271 |
error('Incompatibility with lags, check the possible errors!!!') |
| 272 |
%warning('Incompatibility with lags, check the possible errors!!!') |
| 273 |
%return |
| 274 |
end |
| 275 |
end |
| 276 |
end |
| 277 |
%%A0bd = sqrt(sg0bid); |
| 278 |
% commented out by T.A. Zha, 10/3/96, to avoid double-counting of scaling and the problem |
| 279 |
% of potential multiple peaks. |
| 280 |
A0bd = zeros(nvar,1); |
| 281 |
A0b = sparse(1:nvar,1:nvar,A0bd,nvar,nvar); |
| 282 |
A0b = A0b'; % making a column in A0b correspond to an equation |
| 283 |
|
| 284 |
% |
| 285 |
|
| 286 |
|
| 287 |
%================================================= |
| 288 |
% Computing the (prior) covariance matrix for the posterior of A0, no data yet |
| 289 |
%================================================= |
| 290 |
% |
| 291 |
% |
| 292 |
% ** weight prior dummy observations |
| 293 |
%phi(1:nvar,:) = (mu(5)^2/mu(1)^2)*phi(1:nvar,:); |
| 294 |
%y(1:nvar,:) = (mu(5)^2/mu(1)^2)*y(1:nvar,:); |
| 295 |
%phi(ndobs,:) = mu |
| 296 |
% modified by CAS 8/6/96. The dummy obs weights are naturally in std units, not var units. |
| 297 |
% |
| 298 |
phi(1:nvar,:) = 1*mu(5)*phi(1:nvar,:); % standard Sims and Zha prior |
| 299 |
y(1:nvar,:) = mu(5)*y(1:nvar,:); % standard Sims and Zha prior |
| 300 |
%----- The following prior designed for GLZ |
| 301 |
%phi(1,:) = 1.00*mu(5)*phi(1,:); |
| 302 |
%phi(2:nvar,:) = 1.02*mu(7)*phi(2:nvar,:); |
| 303 |
%y(1,:) = mu(5)*y(1,:); |
| 304 |
%y(2:nvar,:) = mu(7)*y(2:nvar,:); |
| 305 |
%---------------------------- |
| 306 |
% |
| 307 |
phi(nvar+1,:) = mu(6)*phi(nvar+1,:); |
| 308 |
y(nvar+1,:) = mu(6)*y(nvar+1,:); |
| 309 |
|
| 310 |
|
| 311 |
% ** set up the conditional prior variance sg0bi and sgpbi. |
| 312 |
sg0bida = mu(1)^2*sg0bid; |
| 313 |
sgpbida = mu(1)^2*mu(2)^2*sgpbid; |
| 314 |
sgpbida(ncoef) = mu(1)^2*mu(3)^2; |
| 315 |
sgppbd = sgpbida(nvar+1:ncoef); % corresponding to A++, in a Sims-Zha paper |
| 316 |
%%sgppbdi = 1./sgppbd; |
| 317 |
%sgppb = diag(sgppbd); |
| 318 |
|
| 319 |
Hptd = zeros(ncoef); |
| 320 |
Hptdi=Hptd; |
| 321 |
%%Hptd(nvar+1:ncoef,nvar+1:ncoef)=diag(sgppbd); |
| 322 |
%%Hptdi(nvar+1:ncoef,nvar+1:ncoef)=diag(sgppbdi); |
| 323 |
Hptd(ncoef,ncoef)=sgppbd(ncoef-nvar); |
| 324 |
Hptdi(ncoef,ncoef)=1/sgppbd(ncoef-nvar); |
| 325 |
% condtional on A0i, H_plus_tilde |
| 326 |
|
| 327 |
% ** some common terms |
| 328 |
[u d v]=svd(phi,0); %trial |
| 329 |
% xtx = phi'*phi; %trial |
| 330 |
vd=v.*(ones(size(v,2),1)*diag(d)'); %trial |
| 331 |
xtx=vd*vd'; |
| 332 |
% M = eye(fss) - phi*(xtx\phi'); % not used except in line below %trial |
| 333 |
yu = y'*u; %trial |
| 334 |
cxyxx=yu*yu'; %trial |
| 335 |
yty=y'*y; |
| 336 |
ymy=yty-cxyxx; %trial |
| 337 |
%ymy = y'*M*y; |
| 338 |
%cxy = phi'*y; %trial |
| 339 |
cxy = vd*yu'; %trial |
| 340 |
cyx = cxy'; |
| 341 |
%cxpy = xtx\cxy; %not used further except in line below |
| 342 |
%cxyxx = cxy'*cxpy; |
| 343 |
|
| 344 |
% ** estimate A+_hat conditional on A0, ncoef*nvar, but not using full prior |
| 345 |
%GlbAph = cxpy*A0h; |
| 346 |
|
| 347 |
|
| 348 |
%================================================= |
| 349 |
% Computing the final covariance matrix (S1,...,Sm) for the posterior of A0, |
| 350 |
% with the data, and final Gb=(G1,...,Gm) for A+ if asymmetric prior or for |
| 351 |
% B if symmetric prior for A+ |
| 352 |
%================================================= |
| 353 |
% |
| 354 |
H0invmulti=zeros(nvar,nvar,nvar); % inv(H0) for different equations under asymmetric prior |
| 355 |
Hpinvmulti=zeros(ncoef,ncoef,nvar); % inv(H+) for different equations under asymmetric prior |
| 356 |
for i = 1:nvar |
| 357 |
stri = find(idmat0(:,i)); |
| 358 |
% CAS change 9/24/96, to avoid slim chance of an exact zero in x vector |
| 359 |
% at some point during an optimization. |
| 360 |
%strm = length(stri); % TZ, 11/27/97, no use any more |
| 361 |
|
| 362 |
%A0hb = A0h(stri,i)-A0b(stri,i); |
| 363 |
|
| 364 |
% ** set up the conditional prior variance sg0bi and sgpbi. |
| 365 |
% sg0bd = zeros(stri,1); Commented out by CAS, 9/24/96. Looks like this |
| 366 |
% meant to have strm where it has stri, and in any case it is "overwritten" below. |
| 367 |
|
| 368 |
%------------------------------ |
| 369 |
% Introduce prior information on which variables "belong" in various equations. |
| 370 |
% In this first trial, we just introduce this information here, in a model-specific way. |
| 371 |
% Eventually this info has to be passed parametricly. In our first shot, we just damp down |
| 372 |
% all coefficients except those on the diagonal. |
| 373 |
factor0=idmat0(:,i); |
| 374 |
sg0bd = sg0bida; % added by TZ, 2/26/98. I think at each equation, sg0bd must |
| 375 |
% be refreshed. Note, this only works for the prior variance Sg(i) |
| 376 |
% of a0(i) being diagonal. If the prior variance Sg(i) is not |
| 377 |
% diagonal, we have to work on inv(Sg(i)) or sg0bdi directly. |
| 378 |
sg0bd(stri) = sg0bida(stri).*factor0(stri); |
| 379 |
sg0bdi = 1./sg0bd; |
| 380 |
% |
| 381 |
factor1=idmatpp(1:nvar,i); |
| 382 |
sg1bd = sgpbida(1:nvar).*factor1; |
| 383 |
sg1bdi = 1./sg1bd; |
| 384 |
% |
| 385 |
if lags>1 |
| 386 |
factorpp=idmatpp(nvar+1:ncoef-1,i); |
| 387 |
sgpp_cbd = sgppbd(1:ncoef-nvar-1) .* factorpp; |
| 388 |
sgpp_cbdi = 1./sgpp_cbd; |
| 389 |
end |
| 390 |
|
| 391 |
% ** set up the unconditional prior variance on A0i and A+i |
| 392 |
%XX = zeros(nvar,strm); |
| 393 |
%XX(stri,:) = eye(strm); % XX in Gelman, etel, on p479 |
| 394 |
% Commented out by TZ, 11/27/97. Streamline. No need for these any more. |
| 395 |
|
| 396 |
|
| 397 |
% * final conditional prior variance on A+i give that on A0i, and |
| 398 |
% * unconditional variance on A0+ |
| 399 |
H0td = diag(sg0bd); % unconditional |
| 400 |
% H_~: td: tilde for ~ |
| 401 |
% ** inverse and chol decomp |
| 402 |
H0tdi = diag(sg0bdi); |
| 403 |
H0invmulti(:,:,i)=H0tdi; |
| 404 |
|
| 405 |
% |
| 406 |
Hptd(1:nvar,1:nvar)=diag(sg1bd); |
| 407 |
Hptdi(1:nvar,1:nvar)=diag(sg1bdi); |
| 408 |
if lags>1 |
| 409 |
Hptd(nvar+1:ncoef-1,nvar+1:ncoef-1)=diag(sgpp_cbd); |
| 410 |
Hptdi(nvar+1:ncoef-1,nvar+1:ncoef-1)=diag(sgpp_cbdi); |
| 411 |
% condtional on A0i, H_plus_tilde |
| 412 |
end |
| 413 |
Hpinvmulti(:,:,i)=Hptdi; |
| 414 |
|
| 415 |
% common terms |
| 416 |
xxhp = xtx+Hptdi; |
| 417 |
%Lxxhpc = chol(inv(xxhp))'; % lower triangular |
| 418 |
xxhpc = chol(xxhp); % upper triangular but its transpose is lower triagular Choleski |
| 419 |
%A0hbH = A0hb'*H0tdi; |
| 420 |
%====== The following two lines seem incorrect. The third line is supposed to correc the mistake. May 2003. |
| 421 |
% H1p_1 = zeros(ncoef,nvar); |
| 422 |
% H1p_1(1:nvar,:) = Hptdi(1:nvar,1:nvar); |
| 423 |
H1p_1 = Hptdi(:,1:nvar); |
| 424 |
%%Hm = (cyx+H1p_1')*(xxhp\(cxy+H1p_1)); |
| 425 |
Hm1 = (cyx+H1p_1')/xxhp; % if symmetric prior, Bh=Hm1' -- reduced form k-by-m. |
| 426 |
Gb{i} = Hm1'; % k-by-m, where k--ncoef, m--nvar. |
| 427 |
% If asymmetric prior, Gb is used to compute A+ where A+(i) = Gb(i)*a0(i) |
| 428 |
% where a0(i) is m-by-1 for the ith column of A0 or the ith equation. |
| 429 |
%Hm2 = cxy+H1p_1; |
| 430 |
Hm = Hm1*(cxy+H1p_1); |
| 431 |
%GlbAph(i,:) = A0h(stri,i)'*XX'*Hm1; % alpha_plus_*_transpose |
| 432 |
%%alpMpyh = A0h(stri,i)'*XX'*(yty+Hptdi(1:nvar,1:nvar)-Hm)*XX; |
| 433 |
%Hss = XX'*(yty+Hptdi(1:nvar,1:nvar)-Hm)*XX; |
| 434 |
Hss = yty+Hptdi(1:nvar,1:nvar)-Hm; |
| 435 |
Sbd{i} = (H0tdi+Hss)./fss; % m-by-m, where m--nvar. If asymmetric prior, Scd is |
| 436 |
% usde to form diag(Sbd{1}, ..., Sbd{m}) for a0 or vec(A0) in the |
| 437 |
% posterior of A0. Note, "bd" stands for block diagonal and divided by |
| 438 |
% "fss" already to make it compatible with SpH used in "a0lhfun" and make it |
| 439 |
% it easier according to the notations by Waggoner and Zha |
| 440 |
end |
| 441 |
|
| 442 |
|
| 443 |
%%========================================= |
| 444 |
%% check if there is asymmetric prior on both A+ and A0 |
| 445 |
%%========================================= |
| 446 |
% |
| 447 |
spindx=1; % A+ index for asymmetric prior |
| 448 |
s0indx=1; % A0 index for asymmetric prior |
| 449 |
for i=2:nvar |
| 450 |
diffGb = Gb{i}-Gb{i-1}; |
| 451 |
diffSbd = Sbd{i}-Sbd{i-1}; |
| 452 |
if ~any(any(diffGb)) |
| 453 |
spindx=spindx+1; |
| 454 |
end |
| 455 |
if ~any(any(diffSbd)) |
| 456 |
s0indx=s0indx+1; |
| 457 |
end |
| 458 |
end |
| 459 |
% |
| 460 |
if (spindx==nvar) |
| 461 |
Bh = Gb{1}; % reduced-form parameter. Note: does not depend on A0 |
| 462 |
else |
| 463 |
Bh = NaN; |
| 464 |
end |
| 465 |
% |
| 466 |
if (s0indx==nvar) |
| 467 |
SpH = (H0tdi+Hss)/fss; % the final S in the exponential part of p(A0|y) |
| 468 |
% divided by nobs in order to make Choleski decomp without optimization |
| 469 |
% or in the form conformable to Waggoner and Zha |
| 470 |
else |
| 471 |
SpH=NaN; |
| 472 |
end |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
%---------------------------------------------- |
| 479 |
% |
| 480 |
|
| 481 |
% *** |
| 482 |
% *** Form the inverse of covarinace to draw from t- or Gaussian distribution |
| 483 |
% *** |
| 484 |
%SpHs = SpH*fss; % for the Wishart draw |
| 485 |
%SpHsc = chol(SpHs); % upper triangular where Sphsc' is |
| 486 |
% % lower triangular Choleski, for Wishart draw |
| 487 |
%SpHsic = chol(inv(SpHs))'; % inverse Choleski decomposition -- lower triangular |
| 488 |
%A0hin = chol(SpH); % upper triangular, inverse of A0h, each column |
| 489 |
% corresponds to an equation. |
| 490 |
|
| 491 |
|
| 492 |
%@@@ The following can be used for other purposes such as forecasting |
| 493 |
% |
| 494 |
%swish = A0hin'; % each row corresponds to an equation |
| 495 |
%A0h = inv(A0hin); % each column corresponds to an equation |
| 496 |
%xa0 = A0h(a0indx); |
| 497 |
|
| 498 |
%%*** form Bh (ncoef*nvar) |
| 499 |
%Aplus = Hm1t*A0h; % estimate of Aplus -- the same as Astrar |
| 500 |
%Bhp = Hm1t; |