Debian Codegraph @

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

raw
1 function [Pi,H0multi,Hpmulti,H0invmulti,Hpinvmulti,sgh] ...
2 = fn_rnrprior_covres_dobs(nvar,q_m,lags,xdgel,mu,indxDummy,hpmsmd,indxmsmdeqn,nexo,asym0,asymp)
3 % Differs from fn_rnrprior_covres_dobs_tv(): no linear restrictions (Ui and Vi) have applied yet to this function, but
4 % linear restrictions are incorported in fn_rnrprior_covres_dobs_tv().
5 %
6 % Only works for the nexo=1 (constant term) case. To extend this to other exogenous variables, see fn_dataxy.m. 01/14/03.
7 % Differs from fn_rnrprior_covres.m in that dummy observations are included as part of the explicit prior. See Forcast II, pp.68-69b.
8 % More general than fn_rnrprior.m because when hpmsmd=0, fn_rnrprior_covres() is the same as fn_rnrprior().
9 % Allows for prior covariances for the MS and MD equations to achieve liquidity effects.
10 % Exports random Bayesian prior of Sims and Zha with asymmetric rior (but no linear restrictions yet)
11 % See Waggoner and Zha's Gibbs sampling paper and TVBVAR NOTES p. 71k.0.
12 %
13 % nvar: number of endogenous variables
14 % q_m: quarter or month
15 % lags: the maximum length of lag
16 % xdgel: T*nvar endogenous-variable matrix of raw or original data (no manipulation involved) with sample size including lags.
17 % Order of columns: (1) nvar endogenous variables; (2) constants will be automatically put in the last column.
18 % Used only to get variances of residuals for mu(1)-mu(5) and for dummy observations mu(5) and mu(6).
19 % mu: 6-by-1 vector of hyperparameters (the following numbers for Atlanta Fed's forecast), where
20 % mu(5) and mu(6) are NOT used here. See fn_dataxy.m for using mu(5) and mu(6).
21 % mu(1): overall tightness and also for A0; (0.57)
22 % mu(2): relative tightness for A+; (0.13)
23 % mu(3): relative tightness for the constant term; (0.1). NOTE: for other
24 % exogenous terms, the variance of each exogenous term must be taken into
25 % acount to eliminate the scaling factor.
26 % mu(4): tightness on lag decay; (1)
27 % mu(5): weight on nvar sums of coeffs dummy observations (unit roots); (5)
28 % mu(6): weight on single dummy initial observation including constant
29 % (cointegration, unit roots, and stationarity); (5)
30 % indxDummy: 1: uses dummy observations to form part of an explicit prior; 0: no dummy observations as part of the prior.
31 % hpmsmd: 2-by-1 hyperparameters with -1<h1=hpmsmd(1)<=0 for the MS equation and 0<=h2=hpmsmd(2)<1 the MD equation. Consider a1*R + a2*M.
32 % The term h1*var(a1)*var(a2) is the prior covariance of a1 and a2 for MS, equivalent to penalizing the same sign of a1 and a2.
33 % The term h2*var(a1)*var(a2) is the prior covariance of a1 and a2 for MD, equivalent to penalizing opposite signs of a1 and a2.
34 % This will give us a liquidity effect.
35 % indxmsmdeqn: 4-by-1 index for the locations of the MS and MD equation and for the locations of M and R.
36 % indxmsmdeqn(1) for MS and indxmsmdeqn(2) for MD.
37 % indxmsmdeqn(3) for M and indxmsmdeqn(4) for R.
38 % nexo: number of exogenous variables (if not specified, nexo=1 (constant) by default).
39 % The constant term is always put to the last of all endogenous and exogenous variables.
40 % asym0: nvar-by-nvar asymmetric prior on A0. Column -- equation.
41 % If ones(nvar,nvar), symmetric prior; if not, relative (asymmetric) tightness on A0.
42 % asymp: ncoef-1-by-nvar asymmetric prior on A+ bar constant. Column -- equation.
43 % If ones(ncoef-1,nvar), symmetric prior; if not, relative (asymmetric) tightness on A+.
44 % --------------------
45 % Pi: ncoef-by-nvar matrix for the ith equation under random walk. Same for all equations
46 % H0multi: nvar-by-nvar-by-nvar; H0 for different equations under asymmetric prior
47 % Hpmulti: ncoef-by-ncoef-by-nvar; H+ for different equations under asymmetric prior
48 % H0invmulti: nvar-by-nvar-by-nvar; inv(H0) for different equations under asymmetric prior
49 % Hpinvmulti: ncoef-by-ncoef-by-nvar; inv(H+) for different equations under asymmetric prior
50 % sgh: nvar-by-1 standard deviations of residuals for each equation.
51 %
52 % Tao Zha, February 2000. Revised, September 2000, February, May 2003.
53
54 %
55 % Copyright (C) 1997-2012 Tao Zha
56 %
57 % This free software: you can redistribute it and/or modify
58 % it under the terms of the GNU General Public License as published by
59 % the Free Software Foundation, either version 3 of the License, or
60 % (at your option) any later version.
61 %
62 % It is distributed in the hope that it will be useful,
63 % but WITHOUT ANY WARRANTY; without even the implied warranty of
64 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
65 % GNU General Public License for more details.
66 %
67 % If you did not received a copy of the GNU General Public License
68 % with this software, see <http://www.gnu.org/licenses/>.
69 %
70
71
72 if (nargin<=8), nexo=1; end
73 ncoef = nvar*lags+nexo; % number of coefficients in *each* equation, RHS coefficients only.
74
75 H0multi=zeros(nvar,nvar,nvar); % H0 for different equations under asymmetric prior
76 Hpmulti=zeros(ncoef,ncoef,nvar); % H+ for different equations under asymmetric prior
77 H0invmulti=zeros(nvar,nvar,nvar); % inv(H0) for different equations under asymmetric prior
78 Hpinvmulti=zeros(ncoef,ncoef,nvar); % inv(H+) for different equations under asymmetric prior
79
80 %*** Constructing Pi for the ith equation under the random walk assumption
81 Pi = zeros(ncoef,nvar); % same for all equations
82 Pi(1:nvar,1:nvar) = eye(nvar); % random walk
83
84 %
85 %@@@ Prepared for Bayesian prior
86 %
87 %
88 % ** monthly lag decay in order to match quarterly decay: a*exp(bl) where
89 % ** l is the monthly lag. Suppose quarterly decay is 1/x where x=1,2,3,4.
90 % ** Let the decay of l1 (a*exp(b*l1)) match that of x1 (say, beginning: 1/1)
91 % ** and the decay of l2 (a*exp(b*l2)) match that of x2 (say, end: 1/5),
92 % ** we can solve for a and b which are
93 % ** b = (log_x1-log_x2)/(l1-l2), and a = x1*exp(-b*l1).
94 if q_m==12
95 l1 = 1; % 1st month == 1st quarter
96 xx1 = 1; % 1st quarter
97 l2 = lags; % last month
98 xx2 = 1/((ceil(lags/3))^mu(4)); % last quarter
99 %xx2 = 1/6; % last quarter
100 % 3rd quarter: i.e., we intend to let decay of the 6th month match
101 % that of the 3rd quarter, so that the 6th month decays a little
102 % faster than the second quarter which is 1/2.
103 if lags==1
104 b = 0;
105 else
106 b = (log(xx1)-log(xx2))/(l1-l2);
107 end
108 a = xx1*exp(-b*l1);
109 end
110
111
112
113 %
114 % *** specify the prior for each equation separately, SZ method,
115 % ** get the residuals from univariate regressions.
116 %
117 sgh = zeros(nvar,1); % square root
118 sgsh = sgh; % square
119 nSample=size(xdgel,1); % sample size-lags
120 yu = xdgel;
121 C = ones(nSample,1);
122 for k=1:nvar
123 [Bk,ek,junk1,junk2,junk3,junk4] = sye([yu(:,k) C],lags);
124 clear Bk junk1 junk2 junk3 junk4;
125 sgsh(k) = ek'*ek/(nSample-lags);
126 sgh(k) = sqrt(sgsh(k));
127 end
128 % ** prior variance for A0(:,1), same for all equations!!!
129 sg0bid = zeros(nvar,1); % Sigma0_bar diagonal only for the ith equation
130 for j=1:nvar
131 sg0bid(j) = 1/sgsh(j); % sgsh = sigmai^2
132 end
133 % ** prior variance for lagged and exogeous variables, same for all equations
134 sgpbid = zeros(ncoef,1); % Sigma_plus_bar, diagonal, for the ith equation
135 for i = 1:lags
136 if (q_m==12)
137 lagdecay = a*exp(b*i*mu(4));
138 end
139 %
140 for j = 1:nvar
141 if (q_m==12)
142 % exponential decay to match quarterly decay
143 sgpbid((i-1)*nvar+j) = lagdecay^2/sgsh(j); % ith equation
144 elseif (q_m==4)
145 sgpbid((i-1)*nvar+j) = (1/i^mu(4))^2/sgsh(j); % ith equation
146 else
147 error('Incompatibility with lags, check the possible errors!!!')
148 %warning('Incompatibility with lags, check the possible errors!!!')
149 %return
150 end
151 end
152 end
153 %
154
155 if indxDummy % Dummy observations as part of the explicit prior.
156 ndobs=nvar+1; % Number of dummy observations: nvar unit roots and 1 cointegration prior.
157 phibar = zeros(ndobs,ncoef);
158 %* constant term
159 const = ones(nvar+1,1);
160 const(1:nvar) = 0.0;
161 phibar(:,ncoef) = const; % the first nvar periods: no or zero constant!
162
163 xdgelint = mean(xdgel(1:lags,:),1); % mean of the first lags initial conditions
164 %* Dummies
165 for k=1:nvar
166 for m=1:lags
167 phibar(ndobs,nvar*(m-1)+k) = xdgelint(k);
168 phibar(k,nvar*(m-1)+k) = xdgelint(k);
169 % <<>> multiply hyperparameter later
170 end
171 end
172 phibar(1:nvar,:) = 1*mu(5)*phibar(1:nvar,:); % standard Sims and Zha prior
173 phibar(ndobs,:) = mu(6)*phibar(ndobs,:);
174 [phiq,phir]=qr(phibar,0);
175 xtxbar=phir'*phir; % phibar'*phibar. ncoef-by-ncoef. Reduced (not full) rank. See Forcast II, pp.69-69b.
176 end
177
178 %=================================================
179 % Computing the (prior) covariance matrix for A0, no data yet.
180 % As proved in pp.69a-69b, Forecast II, the following prior covariance of A0
181 % will remain the same after the dummy observations prior is incorporated.
182 % The dummy observation prior only affects the prior covariance of A+|A0.
183 % See pp.69a-69b for the proof.
184 %=================================================
185 %
186 %
187 % ** set up the conditional prior variance sg0bi and sgpbi.
188 sg0bida = mu(1)^2*sg0bid; % ith equation
189 sgpbida = mu(1)^2*mu(2)^2*sgpbid;
190 sgpbida(ncoef-nexo+1:ncoef) = mu(1)^2*mu(3)^2;
191 %<<>> No scaling adjustment has been made for exogenous terms other than constant
192 sgppbd = sgpbida(nvar+1:ncoef); % corresponding to A++, in a Sims-Zha paper
193
194 Hptd = zeros(ncoef);
195 Hptdi=Hptd;
196 Hptd(ncoef,ncoef)=sgppbd(ncoef-nvar);
197 Hptdinv(ncoef,ncoef)=1./sgppbd(ncoef-nvar);
198 % condtional on A0i, H_plus_tilde
199
200
201 if nargin<10 % the default is no asymmetric information
202 asym0 = ones(nvar,nvar); % if not ones, then we have relative (asymmetric) tightness
203 asymp = ones(ncoef-1,nvar); % for A+. Column -- equation
204 end
205
206 %**** Asymmetric Information
207 %asym0 = ones(nvar,nvar); % if not ones, then we have relative (asymmetric) tightness
208 %asymp = ones(ncoef-1,nvar); % pp: plus without constant. Column -- equation
209 %>>>>>> B: asymmetric prior variance for asymp <<<<<<<<
210 %
211 %for i = 1:lags
212 % rowif = (i-1)*nvar+1;
213 % rowil = i*nvar;
214 % idmatw0 = 0.5; % weight assigned to idmat0 in the formation of asymp
215 % if (i==1)
216 % asymp(rowif:rowil,:)=(1-idmatw0)*ones(nvar)+idmatw0*idmat0; % first lag
217 % % note: idmat1 is already transposed. Column -- equation
218 % else
219 % %asymp(rowif:rowil,1:nvar) = (1-idmatw0)*ones(nvar)+idmatw0*idmat0;
220 % % <<<<<<< toggle +
221 % % Note: already transposed, since idmat0 is transposed.
222 % % Meaning: column implies equation
223 % asymp(rowif:rowil,1:nvar) = ones(nvar);
224 % % >>>>>>> toggle -
225 % end
226 %end
227 %
228 %>>>>>> E: asymmetric prior variance for asymp <<<<<<<<
229
230
231 %=================================================
232 % Computing the final covariance matrix (S1,...,Sm) for the prior of A0,
233 % and final Gb=(G1,...,Gm) for A+ if asymmetric prior or for
234 % B if symmetric prior for A+
235 %=================================================
236 %
237 for i = 1:nvar
238 %------------------------------
239 % Introduce prior information on which variables "belong" in various equations.
240 % In this first trial, we just introduce this information here, in a model-specific way.
241 % Eventually this info has to be passed parametricly. In our first shot, we just damp down
242 % all coefficients except those on the diagonal.
243
244 %*** For A0
245 factor0=asym0(:,i);
246 sg0bd = sg0bida.*factor0; % Note, this only works for the prior variance Sg(i)
247 % of a0(i) being diagonal. If the prior variance Sg(i) is not
248 % diagonal, we have to the inverse to get inv(Sg(i)).
249 %sg0bdinv = 1./sg0bd;
250 % * unconditional variance on A0+
251 H0td = diag(sg0bd); % unconditional
252 %=== Correlation in the MS equation to get a liquidity effect.
253 if (i==indxmsmdeqn(1))
254 H0td(indxmsmdeqn(3),indxmsmdeqn(4)) = hpmsmd(1)*sqrt(sg0bida(indxmsmdeqn(3))*sg0bida(indxmsmdeqn(4)));
255 H0td(indxmsmdeqn(4),indxmsmdeqn(3)) = hpmsmd(1)*sqrt(sg0bida(indxmsmdeqn(3))*sg0bida(indxmsmdeqn(4)));
256 elseif (i==indxmsmdeqn(2))
257 H0td(indxmsmdeqn(3),indxmsmdeqn(4)) = hpmsmd(2)*sqrt(sg0bida(indxmsmdeqn(3))*sg0bida(indxmsmdeqn(4)));
258 H0td(indxmsmdeqn(4),indxmsmdeqn(3)) = hpmsmd(2)*sqrt(sg0bida(indxmsmdeqn(3))*sg0bida(indxmsmdeqn(4)));
259 end
260 H0tdinv = inv(H0td);
261 %H0tdinv = diag(sg0bdinv);
262 %
263 H0multi(:,:,i)=H0td;
264 H0invmulti(:,:,i)=H0tdinv;
265
266
267 %*** For A+
268 if ~(lags==0) % For A1 to remain random walk properties
269 factor1=asymp(1:nvar,i);
270 sg1bd = sgpbida(1:nvar).*factor1;
271 sg1bdinv = 1./sg1bd;
272 %
273 Hptd(1:nvar,1:nvar)=diag(sg1bd);
274 Hptdinv(1:nvar,1:nvar)=diag(sg1bdinv);
275 if lags>1
276 factorpp=asymp(nvar+1:ncoef-1,i);
277 sgpp_cbd = sgppbd(1:ncoef-nvar-1) .* factorpp;
278 sgpp_cbdinv = 1./sgpp_cbd;
279 Hptd(nvar+1:ncoef-1,nvar+1:ncoef-1)=diag(sgpp_cbd);
280 Hptdinv(nvar+1:ncoef-1,nvar+1:ncoef-1)=diag(sgpp_cbdinv);
281 % condtional on A0i, H_plus_tilde
282 end
283 end
284 %---------------
285 % The dummy observation prior affects only the prior covariance of A+|A0,
286 % but not the covariance of A0. See pp.69a-69b for the proof.
287 %---------------
288 if indxDummy % Dummy observations as part of the explicit prior.
289 Hpinvmulti(:,:,i)=Hptdinv + xtxbar;
290 Hpmulti(:,:,i) = inv(Hpinvmulti(:,:,i));
291 else
292 Hpmulti(:,:,i)=Hptd;
293 Hpinvmulti(:,:,i)=Hptdinv;
294 end
295 end
296
297