Debian Codegraph @

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

raw
1 function [Pi_bar,H0tldcell_inv,Hptldcell_inv] ...
2 = fn_rnrprior_tv(nvar,nStates,q_m,lags,xdgel,mu,Ui,Vi,nexo,asym0,asymp)
3 % Exports random Bayesian prior of Sims and Zha with linear restrictions applied, allowing for
4 % possibly with asymmetric prior.
5 % See Waggoner and Zha's Gibbs sampling paper and TVBVAR NOTE pp.50-61.
6 %
7 % nvar: number of endogenous variables
8 % nStates: Number of states.
9 % q_m: quarter or month
10 % lags: the maximum length of lag
11 % xdgel: the general matrix of the original data (no manipulation involved)
12 % with sample size including lags. Used only to get variances of residuals for
13 % the scaling purpose; NOT used for mu(5) and mu(6).
14 % mu: 6-by-1 vector of hyperparameters (the following numbers for Atlanta Fed's forecast), where
15 % mu(5) and mu(6) are NOT used here. See fn_dataxy.m for using mu(5) and mu(6).
16 % mu(1): overall tightness and also for A0; (0.57)
17 % mu(2): relative tightness for A+; (0.13)
18 % mu(3): relative tightness for the constant term; (0.1). NOTE: for other
19 % exogenous terms, the variance of each exogenous term must be taken into
20 % acount to eliminate the scaling factor.
21 % mu(4): tightness on lag decay; (1)
22 % mu(5): weight on nvar sums of coeffs dummy observations (unit roots); (5)
23 % mu(6): weight on single dummy initial observation including constant
24 % (cointegration, unit roots, and stationarity); (5)
25 % NOTE: for this function, mu(5) and mu(6) are not used.
26 % Ui: nvar-by-1 cell. In each cell, nvar-by-(qi+si) orthonormal basis for the null of the ith
27 % equation contemporaneous restriction matrix where qi is the number of free parameters
28 % within the state and si is the number of free parameters across the states.
29 % With this transformation, we have ai = Ui*bi or Ui'*ai = bi where ai is a vector
30 % of total original parameters and bi is a vector of free parameters. When no
31 % restrictions are imposed, we have Ui = I. There must be at least one free
32 % parameter left for the ith equation.
33 % Vi: nvar-by-1 cell. In each cell, k-by-(ri+ti) orthonormal basis for the null of the ith
34 % equation lagged restriction matrix where k is a total of exogenous variables and
35 % ri is the number of free parameters within the state and ti is the number of free
36 % parameters across the states. With this transformation, we have fi = Vi*gi
37 % or Vi'*fi = gi where fi is a vector of total original parameters and gi is a
38 % vector of free parameters. The ith equation is in the order of [nvar for 1st lag,
39 % ..., nvar for last lag, const].
40 % nexo: number of exogenous variables (if not specified, nexo=1 (constant) by default).
41 % The constant term is always put to the last of all endogenous and exogenous variables.
42 % asym0: nvar-by-nvar asymmetric prior on A0. Column -- equation.
43 % If ones(nvar,nvar), symmetric prior; if not, relative (asymmetric) tightness on A0.
44 % asymp: ncoef-1-by-nvar asymmetric prior on A+ bar constant. Column -- equation.
45 % If ones(ncoef-1,nvar), symmetric prior; if not, relative (asymmetric) tightness on A+.
46 % --------------------
47 % Pi_bar: ncoef-by-nvar matrix for the ith equation under random walk. Same for all equations
48 % H0tldcell_inv: cell(nvar,1). The ith cell represents the ith equation, where the dim is
49 % (qi+si)-by-(qi+si). The inverse of H0tld on p.60.
50 % Hptldcell_inv: cell(nvar,1). The ith cell represents the ith equation, where the dim is
51 % (ri+ti)-by-(ri+ti).The inverse of Hptld on p.60.
52 %
53 % Tao Zha, February 2000. Revised, September 2000, 2001.
54 % See fn_dataxy.m for using mu(5) and mu(6).
55 %
56 % Copyright (C) 1997-2012 Tao Zha
57 %
58 % This free software: you can redistribute it and/or modify
59 % it under the terms of the GNU General Public License as published by
60 % the Free Software Foundation, either version 3 of the License, or
61 % (at your option) any later version.
62 %
63 % It is distributed in the hope that it will be useful,
64 % but WITHOUT ANY WARRANTY; without even the implied warranty of
65 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
66 % GNU General Public License for more details.
67 %
68 % If you did not received a copy of the GNU General Public License
69 % with this software, see <http://www.gnu.org/licenses/>.
70 %
71
72
73 if nargin==8, nexo=1; end % <<>>1
74 ncoef = nvar*lags+nexo; % Number of coefficients in *each* equation for each state, RHS coefficients only.
75 ncoefsts = nStates*ncoef; % Number of coefficients in *each* equation in all states, RHS coefficients only.
76
77 H0tldcell_inv=cell(nvar,1); % inv(H0tilde) for different equations under asymmetric prior.
78 Hptldcell_inv=cell(nvar,1); % inv(H+tilde) for different equations under asymmetric prior.
79
80 %*** Constructing Pi_bar for the ith equation under the random walk assumption
81 Pi_bar = zeros(ncoef,nvar); % same for all equations
82 Pi_bar(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
156 %=================================================
157 % Computing the (prior) covariance matrix for the posterior of A0, no data yet
158 %=================================================
159 %
160 %
161 % ** set up the conditional prior variance sg0bi and sgpbi.
162 sg0bida = mu(1)^2*sg0bid; % ith equation
163 sgpbida = mu(1)^2*mu(2)^2*sgpbid;
164 sgpbida(ncoef-nexo+1:ncoef) = mu(1)^2*mu(3)^2;
165 %<<>> No scaling adjustment has been made for exogenous terms other than constant
166 sgppbd = sgpbida(nvar+1:ncoef); % corresponding to A++, in a Sims-Zha paper
167
168 Hptd = zeros(ncoef);
169 Hptdi=Hptd;
170 Hptd(ncoef,ncoef)=sgppbd(ncoef-nvar);
171 Hptdinv(ncoef,ncoef)=1./sgppbd(ncoef-nvar);
172 % condtional on A0i, H_plus_tilde
173
174
175 if nargin<10 % <<>>1 Default is no asymmetric information
176 asym0 = ones(nvar,nvar); % if not ones, then we have relative (asymmetric) tightness
177 asymp = ones(ncoef-1,nvar); % for A+. Column -- equation
178 end
179
180 %**** Asymmetric Information
181 %asym0 = ones(nvar,nvar); % if not ones, then we have relative (asymmetric) tightness
182 %asymp = ones(ncoef-1,nvar); % pp: plus without constant. Column -- equation
183 %>>>>>> B: asymmetric prior variance for asymp <<<<<<<<
184 %
185 %for i = 1:lags
186 % rowif = (i-1)*nvar+1;
187 % rowil = i*nvar;
188 % idmatw0 = 0.5; % weight assigned to idmat0 in the formation of asymp
189 % if (i==1)
190 % asymp(rowif:rowil,:)=(1-idmatw0)*ones(nvar)+idmatw0*idmat0; % first lag
191 % % note: idmat1 is already transposed. Column -- equation
192 % else
193 % %asymp(rowif:rowil,1:nvar) = (1-idmatw0)*ones(nvar)+idmatw0*idmat0;
194 % % <<<<<<< toggle +
195 % % Note: already transposed, since idmat0 is transposed.
196 % % Meaning: column implies equation
197 % asymp(rowif:rowil,1:nvar) = ones(nvar);
198 % % >>>>>>> toggle -
199 % end
200 %end
201 %
202 %>>>>>> E: asymmetric prior variance for asymp <<<<<<<<
203
204
205 %=================================================
206 % Computing the final covariance matrix (S1,...,Sm) for the prior of A0,
207 % and final Gb=(G1,...,Gm) for A+ if asymmetric prior or for
208 % B if symmetric prior for A+
209 %=================================================
210 %
211 for i = 1:nvar
212 %------------------------------
213 % Introduce prior information on which variables "belong" in various equations.
214 % In this first trial, we just introduce this information here, in a model-specific way.
215 % Eventually this info has to be passed parametricly. In our first shot, we just damp down
216 % all coefficients except those on the diagonal.
217
218 %*** For A0
219 factor0=asym0(:,i);
220 sg0bd = sg0bida.*factor0; % Note, this only works for the prior variance Sg(i)
221 % of a0(i) being diagonal. If the prior variance Sg(i) is not
222 % diagonal, we have to work on inv(Sg(i)) or sg0bdinv directly.
223 sg0bdinv = 1./sg0bd;
224 % * unconditional variance on A0+
225 H0td = diag(sg0bd); % unconditional
226 H0tdinv = diag(sg0bdinv);
227 %
228 H0tldcell_inv{i}=(Ui{i}'*kron(eye(nStates),H0tdinv/nStates))*Ui{i};
229
230
231 %*** For A+
232 if ~(lags==0) % For A1 to remain random walk properties
233 factor1=asymp(1:nvar,i);
234 sg1bd = sgpbida(1:nvar).*factor1;
235 sg1bdinv = 1./sg1bd;
236 %
237 Hptd(1:nvar,1:nvar)=diag(sg1bd);
238 Hptdinv(1:nvar,1:nvar)=diag(sg1bdinv);
239 if lags>1
240 factorpp=asymp(nvar+1:ncoef-1,i);
241 sgpp_cbd = sgppbd(1:ncoef-nvar-1) .* factorpp;
242 sgpp_cbdinv = 1./sgpp_cbd;
243 Hptd(nvar+1:ncoef-1,nvar+1:ncoef-1)=diag(sgpp_cbd);
244 Hptdinv(nvar+1:ncoef-1,nvar+1:ncoef-1)=diag(sgpp_cbdinv);
245 % condtional on A0i, H_plus_tilde
246 end
247 end
248 Hptldcell_inv{i}=(Vi{i}'*kron(eye(nStates),Hptdinv/nStates))*Vi{i};
249 %Hptdinv_3 = kron(eye(nStates),Hptdinv); % ?????
250 end
251
252