Debian Codegraph @

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

raw
1 function [Pi_bar,H0tldcell_inv,Hptldcell_inv] ...
2 = fn_rnrprior_covres_tv(nvar,nStates,q_m,lags,xdgel,mu,Ui,Vi,hpmsmd,indxmsmdeqn,nexo,asym0,asymp)
3 % [Pi_bar,H0tldcell_inv,Hptldcell_inv] ...
4 % = fn_rnrprior_covres_tv(nvar,nStates,q_m,lags,xdgel,mu,Ui,Vi,hpmsmd,indxmsmdeqn,nexo,asym0,asymp)
5 %
6 % More general than fn_rnrprior_tv() because, when hpmsmd=0, fn_rnrprior_covres_tv() is the same as fn_rnrprior_tv().
7 % Exports random Bayesian prior of Sims and Zha with asymmetric rior with linear restrictions already applied
8 % but without dummy observations (i.e., mu(5) and mu(6)) yet.
9 % This function allows for prior covariances for the MS and MD equations to achieve liquidity effects.
10 % See Waggoner and Zha's Gibbs sampling paper and TVBVAR NOTES pp. 71k.0 and 50-61.
11 %
12 % nvar: number of endogenous variables
13 % nStates: Number of states.
14 % q_m: quarter or month
15 % lags: the maximum length of lag
16 % xdgel: the general matrix of the original data (no manipulation involved)
17 % with sample size including lags. Used only to get variances of residuals for
18 % the scaling purpose; NOT used for 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 % NOTE: for this function, mu(5) and mu(6) are not used. See fn_dataxy.m for using mu(5) and mu(6).
31 % Ui: nvar-by-1 cell. In each cell, nvar-by-qi*si orthonormal basis for the null of the ith
32 % equation contemporaneous restriction matrix where qi is the number of free parameters
33 % within the state and si is the number of free states.
34 % With this transformation, we have ai = Ui*bi or Ui'*ai = bi where ai is a vector
35 % of total original parameters and bi is a vector of free parameters. When no
36 % restrictions are imposed, we have Ui = I. There must be at least one free
37 % parameter left for the ith equation in the order of [a_i for 1st state, ..., a_i for last state].
38 % Vi: nvar-by-1 cell. In each cell, k-by-ri*ti orthonormal basis for the null of the ith
39 % equation lagged restriction matrix where k is a total of exogenous variables and
40 % ri is the number of free parameters within the state and ti is the number of free states.
41 % With this transformation, we have fi = Vi*gi
42 % or Vi'*fi = gi where fi is a vector of total original parameters and gi is a
43 % vector of free parameters. The ith equation is in the order of [nvar variables
44 % for 1st lag and 1st state, ..., nvar variables for last lag and 1st state, const for 1st state, nvar
45 % variables for 1st lag and 2nd state, nvar variables for last lag and 2nd state, const for 2nd state, and so on].
46 % 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.
47 % 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.
48 % 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.
49 % This will give us a liquidity effect. If hpmsmd=0, no such restrictions will be imposed.
50 % indxmsmdeqn: 4-by-1 index for the locations of the MS and MD equation and for the locations of M and R.
51 % indxmsmdeqn(1) for MS and indxmsmdeqn(2) for MD.
52 % indxmsmdeqn(3) for M and indxmsmdeqn(4) for R.
53 % nexo: number of exogenous variables (if not specified, nexo=1 (constant) by default).
54 % The constant term is always put to the last of all endogenous and exogenous variables.
55 % asym0: nvar-by-nvar asymmetric prior on A0. Column -- equation.
56 % If ones(nvar,nvar), symmetric prior; if not, relative (asymmetric) tightness on A0.
57 % asymp: ncoef-1-by-nvar asymmetric prior on A+ bar constant. Column -- equation.
58 % If ones(ncoef-1,nvar), symmetric prior; if not, relative (asymmetric) tightness on A+.
59 % --------------------
60 % Pi_bar: ncoef-by-nvar matrix for the ith equation under random walk. Same for all equations
61 % H0tldcell_inv: cell(nvar,1). The ith cell represents the ith equation, where the dim is
62 % qi*si-by-qi*si. The inverse of H0tld on p.60.
63 % Hptldcell_inv: cell(nvar,1). The ith cell represents the ith equation, where the dim is
64 % ri*ti-by-ri*ti.The inverse of Hptld on p.60.
65 %
66 % Tao Zha, February 2000. Revised, September 2000, 2001, February, May 2003.
67 %
68 % Copyright (C) 1997-2012 Tao Zha
69 %
70 % This free software: you can redistribute it and/or modify
71 % it under the terms of the GNU General Public License as published by
72 % the Free Software Foundation, either version 3 of the License, or
73 % (at your option) any later version.
74 %
75 % It is distributed in the hope that it will be useful,
76 % but WITHOUT ANY WARRANTY; without even the implied warranty of
77 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
78 % GNU General Public License for more details.
79 %
80 % If you did not received a copy of the GNU General Public License
81 % with this software, see <http://www.gnu.org/licenses/>.
82 %
83
84
85 if nargin==10, nexo=1; end % <<>>1
86 ncoef = nvar*lags+nexo; % Number of coefficients in *each* equation for each state, RHS coefficients only.
87 ncoefsts = nStates*ncoef; % Number of coefficients in *each* equation in all states, RHS coefficients only.
88
89 H0tldcell_inv=cell(nvar,1); % inv(H0tilde) for different equations under asymmetric prior.
90 Hptldcell_inv=cell(nvar,1); % inv(H+tilde) for different equations under asymmetric prior.
91
92 %*** Constructing Pi_bar for the ith equation under the random walk assumption
93 Pi_bar = zeros(ncoef,nvar); % same for all equations
94 Pi_bar(1:nvar,1:nvar) = eye(nvar); % random walk
95
96 %
97 %@@@ Prepared for Bayesian prior
98 %
99 %
100 % ** monthly lag decay in order to match quarterly decay: a*exp(bl) where
101 % ** l is the monthly lag. Suppose quarterly decay is 1/x where x=1,2,3,4.
102 % ** Let the decay of l1 (a*exp(b*l1)) match that of x1 (say, beginning: 1/1)
103 % ** and the decay of l2 (a*exp(b*l2)) match that of x2 (say, end: 1/5),
104 % ** 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
125 %
126 % *** specify the prior for each equation separately, SZ method,
127 % ** get the residuals from univariate regressions.
128 %
129 sgh = zeros(nvar,1); % square root
130 sgsh = sgh; % square
131 nSample=size(xdgel,1); % sample size-lags
132 yu = xdgel;
133 C = ones(nSample,1);
134 for k=1:nvar
135 [Bk,ek,junk1,junk2,junk3,junk4] = sye([yu(:,k) C],lags);
136 clear Bk junk1 junk2 junk3 junk4;
137 sgsh(k) = ek'*ek/(nSample-lags);
138 sgh(k) = sqrt(sgsh(k));
139 end
140 % ** prior variance for A0(:,1), same for all equations!!!
141 sg0bid = zeros(nvar,1); % Sigma0_bar diagonal only for the ith equation
142 for j=1:nvar
143 sg0bid(j) = 1/sgsh(j); % sgsh = sigmai^2
144 end
145 % ** prior variance for lagged and exogeous variables, same for all equations
146 sgpbid = zeros(ncoef,1); % Sigma_plus_bar, diagonal, for the ith equation
147 for i = 1:lags
148 if (q_m==12)
149 lagdecay = a*exp(b*i*mu(4));
150 end
151 %
152 for j = 1:nvar
153 if (q_m==12)
154 % exponential decay to match quarterly decay
155 sgpbid((i-1)*nvar+j) = lagdecay^2/sgsh(j); % ith equation
156 elseif (q_m==4)
157 sgpbid((i-1)*nvar+j) = (1/i^mu(4))^2/sgsh(j); % ith equation
158 else
159 error('Incompatibility with lags, check the possible errors!!!')
160 %warning('Incompatibility with lags, check the possible errors!!!')
161 %return
162 end
163 end
164 end
165 %
166
167
168 %=================================================
169 % Computing the (prior) covariance matrix for the posterior of A0, no data yet
170 %=================================================
171 %
172 %
173 % ** set up the conditional prior variance sg0bi and sgpbi.
174 sg0bida = mu(1)^2*sg0bid; % ith equation
175 sgpbida = mu(1)^2*mu(2)^2*sgpbid;
176 sgpbida(ncoef-nexo+1:ncoef) = mu(1)^2*mu(3)^2;
177 %<<>> No scaling adjustment has been made for exogenous terms other than constant
178 sgppbd = sgpbida(nvar+1:ncoef); % corresponding to A++, in a Sims-Zha paper
179
180 Hptd = zeros(ncoef);
181 Hptdi=Hptd;
182 Hptd(ncoef,ncoef)=sgppbd(ncoef-nvar);
183 Hptdinv(ncoef,ncoef)=1./sgppbd(ncoef-nvar);
184 % condtional on A0i, H_plus_tilde
185
186
187 if nargin<12 % <<>>1 Default is no asymmetric information
188 asym0 = ones(nvar,nvar); % if not ones, then we have relative (asymmetric) tightness
189 asymp = ones(ncoef-1,nvar); % for A+. Column -- equation
190 end
191
192 %**** Asymmetric Information
193 %asym0 = ones(nvar,nvar); % if not ones, then we have relative (asymmetric) tightness
194 %asymp = ones(ncoef-1,nvar); % pp: plus without constant. Column -- equation
195 %>>>>>> B: asymmetric prior variance for asymp <<<<<<<<
196 %
197 %for i = 1:lags
198 % rowif = (i-1)*nvar+1;
199 % rowil = i*nvar;
200 % idmatw0 = 0.5; % weight assigned to idmat0 in the formation of asymp
201 % if (i==1)
202 % asymp(rowif:rowil,:)=(1-idmatw0)*ones(nvar)+idmatw0*idmat0; % first lag
203 % % note: idmat1 is already transposed. Column -- equation
204 % else
205 % %asymp(rowif:rowil,1:nvar) = (1-idmatw0)*ones(nvar)+idmatw0*idmat0;
206 % % <<<<<<< toggle +
207 % % Note: already transposed, since idmat0 is transposed.
208 % % Meaning: column implies equation
209 % asymp(rowif:rowil,1:nvar) = ones(nvar);
210 % % >>>>>>> toggle -
211 % end
212 %end
213 %
214 %>>>>>> E: asymmetric prior variance for asymp <<<<<<<<
215
216
217 %=================================================
218 % Computing the final covariance matrix (S1,...,Sm) for the prior of A0,
219 % and final Gb=(G1,...,Gm) for A+ if asymmetric prior or for
220 % B if symmetric prior for A+
221 %=================================================
222 %
223 for i = 1:nvar
224 %------------------------------
225 % Introduce prior information on which variables "belong" in various equations.
226 % In this first trial, we just introduce this information here, in a model-specific way.
227 % Eventually this info has to be passed parametricly. In our first shot, we just damp down
228 % all coefficients except those on the diagonal.
229
230 %*** For A0
231 factor0=asym0(:,i);
232
233 sg0bd = sg0bida.*factor0; % Note, this only works for the prior variance Sg(i)
234 % of a0(i) being diagonal. If the prior variance Sg(i) is not
235 % diagonal, we have to the inverse to get inv(Sg(i)).
236 %sg0bdinv = 1./sg0bd;
237 % * unconditional variance on A0+
238 H0td = diag(sg0bd); % unconditional
239 %=== Correlation in the MS equation to get a liquidity effect.
240 if (i==indxmsmdeqn(1))
241 H0td(indxmsmdeqn(3),indxmsmdeqn(4)) = hpmsmd(1)*sqrt(sg0bida(indxmsmdeqn(3))*sg0bida(indxmsmdeqn(4)));
242 H0td(indxmsmdeqn(4),indxmsmdeqn(3)) = hpmsmd(1)*sqrt(sg0bida(indxmsmdeqn(3))*sg0bida(indxmsmdeqn(4)));
243 elseif (i==indxmsmdeqn(2))
244 H0td(indxmsmdeqn(3),indxmsmdeqn(4)) = hpmsmd(2)*sqrt(sg0bida(indxmsmdeqn(3))*sg0bida(indxmsmdeqn(4)));
245 H0td(indxmsmdeqn(4),indxmsmdeqn(3)) = hpmsmd(2)*sqrt(sg0bida(indxmsmdeqn(3))*sg0bida(indxmsmdeqn(4)));
246 end
247 H0tdinv = inv(H0td);
248 %H0tdinv = diag(sg0bdinv);
249 %
250 H0tldcell_inv{i}=(Ui{i}'*kron(eye(nStates),H0tdinv/nStates))*Ui{i};
251
252
253 %*** For A+
254 if ~(lags==0) % For A1 to remain random walk properties
255 factor1=asymp(1:nvar,i);
256 sg1bd = sgpbida(1:nvar).*factor1;
257 sg1bdinv = 1./sg1bd;
258 %
259 Hptd(1:nvar,1:nvar)=diag(sg1bd);
260 Hptdinv(1:nvar,1:nvar)=diag(sg1bdinv);
261 if lags>1
262 factorpp=asymp(nvar+1:ncoef-1,i);
263 sgpp_cbd = sgppbd(1:ncoef-nvar-1) .* factorpp;
264 sgpp_cbdinv = 1./sgpp_cbd;
265 Hptd(nvar+1:ncoef-1,nvar+1:ncoef-1)=diag(sgpp_cbd);
266 Hptdinv(nvar+1:ncoef-1,nvar+1:ncoef-1)=diag(sgpp_cbdinv);
267 % condtional on A0i, H_plus_tilde
268 end
269 end
270 Hptldcell_inv{i}=(Vi{i}'*kron(eye(nStates),Hptdinv/nStates))*Vi{i};
271 %Hptdinv_3 = kron(eye(nStates),Hptdinv); % ?????
272 end
273
274