Debian Codegraph @

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

raw
1 function [A0hin,Hm1t,fss,ndobs,phi,y,nvar,ncoef,SpH,SpHsc,xxhpc,a0indx,na0p,...
2 idmat0] = szbvar(idfile,q_m,lags,nSample,nhp,xdgel)
3 % Estimating the Bayesian VAR of Sims and Zha:
4 % function [A0hin,Hm1t,fss,ndobs,phi,y,nvar,ncoef,SpH,SpHsc,xxhpc,a0indx,na0p,...
5 % idmat0] = szbvar(idfile,q_m,lags,nSample,nhp,xdgel)
6 %
7 % idfile: Identification filename such as "iden4" (with the extension ".prn").
8 % q_m: quarter or month
9 % lags: the maximum length of lag
10 % nSample: the sample size (including lags, of course)
11 % nhp: number of haperparameters
12 % imstp: number of impusle response steps
13 % xdgel: the general matrix of the original data (no manipulation involved)
14 % ------
15 % A0hin: chol(SpH) -- inverse of A0h, upper triangular, each column corresponds to an equation
16 % Hm1t: reduced form B
17 % fss: in-sample-size (for forecasting). Including dummy observations
18 % ndobs: number of dummy observations, ndobs=nvar+1
19 % phi: X in the form of y = X*B+U. Row: nSmaple-lags+ndobs. Column: ncoef
20 % y: y in the form y = X*B+U. Including the dummy observations too,
21 % T-lags+ndobs-by-nvar.
22 % nvar: number of variables
23 % ncoef: number of coefficients in *each* equation. RHS coefficients only, nvar*lags+1
24 % SpH: divided by T, the final S in the Waggoner-Zha exponential part of p(A0|y)
25 % SpHsc: upper triagular in the lower triangular Choleski of SpH*fss, for Wishart
26 % xxhpc: chol(X'X+inv(H_p_tilde)): upper triangular but its transpose
27 % is lower triagular Choleski
28 % a0indx: the index number for free parameters in A0. Column meaning equation
29 % na0p: number of free parameters in A0
30 % idmat0: identification matrix for A0; only 1's and 0's. Column meaning equation.
31 %
32 % Quick Revisions: May 2003. See H1p_1 on lines 301-304.
33 %
34 %
35 % Copyright (C) 1997-2012 Christopher A. Sims and Tao Zha
36 %
37 % This free software: you can redistribute it and/or modify
38 % it under the terms of the GNU General Public License as published by
39 % the Free Software Foundation, either version 3 of the License, or
40 % (at your option) any later version.
41 %
42 % It is distributed in the hope that it will be useful,
43 % but WITHOUT ANY WARRANTY; without even the implied warranty of
44 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 % GNU General Public License for more details.
46 %
47 % If you did not received a copy of the GNU General Public License
48 % with this software, see <http://www.gnu.org/licenses/>.
49 %
50
51 %
52 %@@@ Prepared for Bayesian VAR of Sims and Zha
53 %
54 %* Load identification and obtain idmat0
55 eval(['load ' idfile '.prn -ascii']);
56 eval(['idmat0=' idfile ';']);
57 idmat0=idmat0'; % so that each column corresponds to an equation
58 a0indx=find(idmat0); % column meaning equation
59 na0p = length(a0indx); % number of free parameters in A0
60 nfp = na0p + nhp; % total number of free parameter
61 [nvar,neqn]=size(idmat0);
62
63 %$$$ available computations
64 %
65 %@@@ original
66 % total number of the sample under study, including lags, etc. -- original sample size
67 sb = lags+1; % original beginning without dummies
68 sl = nSample; % original last period without dummies
69 ssp = nSample - lags; % original number of observations
70 %
71 ndobs=nvar+1; % number of dummy observations
72 ncoef = nvar*lags+1; % number of coefficients in *each* equation, RHS coefficients only.
73 %*** initializing for global variables
74 %%GlbAph=zeros(nvar,ncoef);
75 Aplus=zeros(ncoef,nvar);
76 SpH = ones(nvar,nvar);
77 %* flp: forecast last period (with dummies);
78 %* fbp: forecast beginning period (with dummies).
79 flp = sl+ndobs;
80 fbp = ndobs+lags+1; % <<>> true begining by skipping dummies
81 fss = flp-lags; % forecast sample size (with dummies).
82
83 % ** hyperparameters
84 mu = zeros(nhp,1);
85 mu(1) = 0.57;
86 mu(2) = 0.13;
87 mu(3) = 0.1;
88 mu(4) = 1;
89 mu(5) = 5;
90 mu(6) = 5;
91 %
92 % mu(1): overall tightness and also for A0;
93 % mu(2): relative tightness for A+;
94 % mu(3): relative tightness for the constant term;
95 % mu(4): tightness on lag decay;
96 % mu(5): weight on nvar sums of coeffs dummy observations (unit roots);
97 % mu(6): weight on single dummy initial observation including constant (cointegration, unit
98 % roots, and stationarity).
99
100
101 % ** monthly lag decay in order to match quarterly decay: a*exp(bl) where
102 % ** l is the monthly lag. Suppose quarterly decay is 1/x where x=1,2,3,4.
103 % ** Let the decay of l1 match that of x1 (say, beginning) and the decay
104 % ** of l2 match that of x2 (say, end), we can solve for a and b which are
105 % ** b = (log_x1-log_x2)/(l1-l2), and a = x1*exp(-b*l1).
106 if q_m==12
107 l1 = 1; % 1st month == 1st quarter
108 xx1 = 1; % 1st quarter
109 l2 = lags; % last month
110 xx2 = 1/((ceil(lags/3))^mu(4)); % last quarter
111 %xx2 = 1/6; % last quarter
112 % 3rd quarter: i.e., we intend to let decay of the 6th month match
113 % that of the 3rd quarter, so that the 6th month decays a little
114 % faster than the second quarter which is 1/2.
115 if lags==1
116 b = 0;
117 else
118 b = (log(xx1)-log(xx2))/(l1-l2);
119 end
120 a = xx1*exp(-b*l1);
121 end
122
123 %
124 % ** now run the VAR with
125 %
126 % **** nvar prior dummy observations with the sum of coefficients
127 % ** construct X for Y = X*B + U where phi = X: (T-lags)*k, Y: (T-lags)*nvar
128 % ** columns: k = # of [nvar for 1st lag, ..., nvar for last lag, const]
129 % ** Now, T=T+ndobs -- added with "ndobs" dummy observations
130 %
131 phi = zeros(fss,ncoef);
132 const = ones(fss,1);
133 const(1:nvar) = zeros(nvar,1);
134 phi(:,ncoef) = const; % the first nvar periods: no or zero constant!
135 %
136 xdgelint = mean(xdgel(1:lags,:),1);
137 % mean of the first lags initial conditions
138 for k=1:nvar
139 for m=1:lags
140 phi(ndobs,nvar*(m-1)+k) = xdgelint(k);
141 phi(k,nvar*(m-1)+k) = xdgelint(k);
142 % <<>> multiply hyperparameter later
143 end
144 end
145 for k=1:lags
146 phi(ndobs+1:fss,nvar*(k-1)+1:nvar*k) = xdgel(sb-k:sl-k,:);
147 % row: T-lags; column: [nvar for 1st lag, ..., nvar for last lag, const]
148 % Thus, # of columns is nvar*lags+1 = ncoef.
149 end
150 %
151 % ** Y with "ndobs" dummies added
152 y = zeros(fss,nvar);
153 for k=1:nvar
154 y(ndobs,k) = xdgelint(k);
155 y(k,k) = xdgelint(k);
156 % <<>> place hyperparameter later
157 end
158 y(ndobs+1:fss,:) = xdgel(sb:sl,:);
159
160 %
161 % *** specify the prior for each equation separately, SZ method,
162 % ***
163 % *** obtaining the residuals for the univariate processes:
164 % *** a total of "nvar" equations
165 % *** obtain the posterior peak of A0 which is Sims iden1 (Sims 1986)
166 % ** get the residuals from univariate regressions.
167 %
168 sgh = zeros(nvar,1); % square root
169 sgsh = sgh; % square
170 nn = [1 lags nSample];
171 yu = xdgel;
172 C = ones(nSample,1);
173 for k=1:nvar
174 [Bk,ek,junk1,junk2,junk3,junk4] = sye([yu(:,k) C],nn);
175 clear Bk junk1 junk2 junk3 junk4;
176 sgsh(k) = ek'*ek/ssp;
177 %% sgh(k) = ek'*ek/(fss-7); to match "sqrt(ess)" in RATS univariate regression
178 sgh(k) = sqrt(sgsh(k));
179 end
180 % ** prior variance for alpha0, same for all equations!!!
181 al0b = zeros(nvar*neqn,0); % prior mean for all A0
182 sg0bid = zeros(nvar,1); % Sigma0_bar diagonal only
183 for j=1:nvar
184 sg0bid(j) = 1/sgsh(j); % sgsh = sigmai^2
185 end
186 % ** prior variance for alpha_plus, same for all equations
187 sgpbid = zeros(ncoef,1); % Sigma_plus_bar, diagonal
188 for i = 1:lags
189 if (q_m==12)
190 lagdecay = a*exp(b*i);
191 end
192 %
193 for j = 1:nvar
194 if (q_m==12)
195 % exponential decay to match quarterly decay
196 sgpbid((i-1)*nvar+j) = lagdecay^2/sgsh(j);
197 %%sgpbid((i-1)*nvar+j) = (1/i)^2/sgsh(j);
198 elseif (q_m==4)
199 sgpbid((i-1)*nvar+j) = (1/i^mu(4))^2/sgsh(j);
200 else
201 warning('Incompatibility with lags, check the possible errors!!!')
202 return
203 end
204 end
205 end
206 %%A0bd = sqrt(sg0bid);
207 % commented out by T.A. Zha, 10/3/96, to avoid double-counting of scaling and the problem
208 % of potential multiple peaks.
209 A0bd = zeros(nvar,1);
210 A0b = sparse(1:nvar,1:nvar,A0bd,nvar,nvar);
211 A0b = A0b'; % making a column in A0b correspond to an equation
212
213 %
214
215
216 %=================================================
217 % Computing the (prior) covariance matrix for the posterior of A0, no data yet
218 %=================================================
219 %
220 %
221 % ** weight prior dummy observations
222 %phi(1:nvar,:) = (mu(5)^2/mu(1)^2)*phi(1:nvar,:);
223 %y(1:nvar,:) = (mu(5)^2/mu(1)^2)*y(1:nvar,:);
224 %phi(ndobs,:) = mu
225 % modified by CAS 8/6/96. The dummy obs weights are naturally in std units, not var units.
226 %
227 phi(1:nvar,:) = mu(5)*phi(1:nvar,:); % standard Sims and Zha prior
228 y(1:nvar,:) = mu(5)*y(1:nvar,:); % standard Sims and Zha prior
229 %----- The following prior designed for GLZ
230 %phi(1,:) = 1.00*mu(5)*phi(1,:);
231 %phi(2:nvar,:) = 1.02*mu(7)*phi(2:nvar,:);
232 %y(1,:) = mu(5)*y(1,:);
233 %y(2:nvar,:) = mu(7)*y(2:nvar,:);
234 %----------------------------
235 %
236 phi(nvar+1,:) = mu(6)*phi(nvar+1,:);
237 y(nvar+1,:) = mu(6)*y(nvar+1,:);
238
239
240 % ** set up the conditional prior variance sg0bi and sgpbi.
241 sg0bida = mu(1)^2*sg0bid;
242 sgpbida = mu(1)^2*mu(2)^2*sgpbid;
243 sgpbida(ncoef) = mu(1)^2*mu(3)^2;
244 sgppbd = sgpbida(nvar+1:ncoef); % corresponding to A++, in a Sims-Zha paper
245 %%sgppbdi = 1./sgppbd;
246 %sgppb = diag(sgppbd);
247
248 Hptd = zeros(ncoef);
249 Hptdi=Hptd;
250 %%Hptd(nvar+1:ncoef,nvar+1:ncoef)=diag(sgppbd);
251 %%Hptdi(nvar+1:ncoef,nvar+1:ncoef)=diag(sgppbdi);
252 Hptd(ncoef,ncoef)=sgppbd(ncoef-nvar);
253 Hptdi(ncoef,ncoef)=1/sgppbd(ncoef-nvar);
254 % condtional on A0i, H_plus_tilde
255 sg0bd = sg0bida;
256 sg0bdi = 1./sg0bd;
257 sg1bd = sgpbida(1:nvar);
258 sg1bdi = 1./sg1bd;
259 if lags>1
260 sgpp_cbd = sgppbd(1:ncoef-nvar-1);
261 sgpp_cbdi = 1./sgpp_cbd; % _cbd: c -- constant, b -- barred (without), d -- diagonal
262 end
263
264
265 % ** set up the unconditional prior variance on A0i and A+i
266 %%XX = zeros(nvar,strm);
267 %%XX(stri,:) = eye(strm); % XX in Gelman, etel, on p479
268 %
269 % * final conditional prior variance on A+i give that on A0i, and
270 % * unconditional variance on A0+
271 H0td = diag(sg0bd); % unconditional
272 % H_~: td: tilde for ~
273 % ** inverse and chol decomp
274 H0tdi = diag(sg0bdi);
275
276 %
277 Hptd(1:nvar,1:nvar)=diag(sg1bd);
278 Hptdi(1:nvar,1:nvar)=diag(sg1bdi);
279 if lags>1
280 Hptd(nvar+1:ncoef-1,nvar+1:ncoef-1)=diag(sgpp_cbd);
281 Hptdi(nvar+1:ncoef-1,nvar+1:ncoef-1)=diag(sgpp_cbdi);
282 % condtional on A0i, H_plus_tilde
283 end
284
285
286 %=================================================
287 % Computing the final covariance matrix for the posterior of A0, with the data
288 %=================================================
289 %
290 % ** some common terms
291 [u d v]=svd(phi,0); %trial
292 % xtx = phi'*phi; %trial
293 vd=v.*(ones(size(v,2),1)*diag(d)'); %trial
294 xtx=vd*vd';
295 % M = eye(fss) - phi*(xtx\phi'); % not used except in line below %trial
296 yu = y'*u; %trial
297 cxyxx=yu*yu'; %trial
298 yty=y'*y;
299 ymy=yty-cxyxx; %trial
300 %ymy = y'*M*y;
301 %cxy = phi'*y; %trial
302 cxy = vd*yu'; %trial
303 cyx = cxy';
304 %cxpy = xtx\cxy; %not used further except in line below
305 %cxyxx = cxy'*cxpy;
306
307 % ** estimate A+_hat conditional on A0, ncoef*nvar, but not using full prior
308 %GlbAph = cxpy*A0h;
309
310
311 % common terms
312 xxhp = xtx+Hptdi;
313 %Lxxhpc = chol(inv(xxhp))'; % lower triangular
314 xxhpc = chol(xxhp); % upper triangular but its transpose is lower triagular Choleski
315 %A0hbH = A0hb'*H0tdi;
316 %====== The following two lines seem incorrect. The third line is supposed to correc the mistake. May 2003.
317 % H1p_1 = zeros(ncoef,nvar);
318 % H1p_1(1:nvar,:) = Hptdi(1:nvar,1:nvar);
319 H1p_1 = Hptdi(:,1:nvar);
320 %%Hm = (cyx+H1p_1')*(xxhp\(cxy+H1p_1));
321 Hm1 = (cyx+H1p_1')/xxhp;
322 Hm1t = Hm1';
323 %Hm2 = cxy+H1p_1;
324 Hm = Hm1*(cxy+H1p_1);
325 %%alpMpyh = A0h(stri,i)'*XX'*(yty+Hptdi(1:nvar,1:nvar)-Hm)*XX;
326 Hss = yty+Hptdi(1:nvar,1:nvar)-Hm;
327 SpH = (H0tdi+Hss)/fss; % the final S in the exponential part of p(A0|y)
328 % divided by nobs in order to make Choleski decomp without optimization
329 % or in the form conformable to Waggoner and Zha
330
331 % ***
332 % *** Form the inverse of covarinace to draw from t- or Gaussian distribution
333 % ***
334 SpHs = SpH*fss; % for the Wishart draw
335 SpHsc = chol(SpHs); % upper triangular where Sphsc' is lower triangular Choleski
336 %SpHsic = chol(inv(SpHs))'; % inverse Choleski decomposition -- lower triangular
337
338 A0hin = chol(SpH); % upper triangular, inverse of A0h, each column
339 % corresponds to an equation.
340
341
342 %----------------------------------------------
343 %
344 %@@@ The following can be used for other purposes such as forecasting
345 %
346 %swish = A0hin'; % each row corresponds to an equation
347 %A0h = inv(A0hin); % each column corresponds to an equation
348 %xa0 = A0h(a0indx);
349
350 %%*** form Bh (ncoef*nvar)
351 %Aplus = Hm1t*A0h; % estimate of Aplus -- the same as Astrar
352 %Bhp = Hm1t;