Debian Codegraph @

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

raw
1 function [Pi,H0multi,Hpmulti,H0invmulti,Hpinvmulti] ...
2 = fn_rnrprior(nvar,q_m,lags,xdgel,mu,nexo,asym0,asymp)
3 % [Pi,H0multi,Hpmulti,H0invmulti,Hpinvmulti] ...
4 % = fn_rnrprior(nvar,q_m,lags,xdgel,mu,nexo,asym0,asymp)
5 % Exporting random Bayesian prior of Sims and Zha with asymmetric rior (but no linear restrictions)
6 % See Waggoner and Zha's Gibbs sampling paper
7 %
8 % nvar: number of endogenous variables
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 subdirectory, mu(5) and mu(6) are not used.
26 % asym0: nvar-by-nvar asymmetric prior on A0. Column -- equation.
27 % If ones(nvar,nvar), symmetric prior; if not, relative (asymmetric) tightness on A0.
28 % asymp: ncoef-1-by-nvar asymmetric prior on A+ bar constant. Column -- equation.
29 % If ones(ncoef-1,nvar), symmetric prior; if not, relative (asymmetric) tightness on A+.
30 % nexo: number of exogenous variables (if not specified, nexo=1 (constant) by default).
31 % The constant term is always put to the last of all endogenous and exogenous variables.
32 % --------------------
33 % Pi: ncoef-by-nvar matrix for the ith equation under random walk. Same for all equations
34 % H0multi: nvar-by-nvar-by-nvar; H0 for different equations under asymmetric prior
35 % Hpmulti: ncoef-by-ncoef-by-nvar; H+ for different equations under asymmetric prior
36 % H0invmulti: nvar-by-nvar-by-nvar; inv(H0) for different equations under asymmetric prior
37 % Hpinvmulti: ncoef-by-ncoef-by-nvar; inv(H+) for different equations under asymmetric prior
38 %
39 % Tao Zha, February 2000. Revised, September 2000.
40 % See fn_dataxy.m for using mu(5) and mu(6).
41 %
42 % Copyright (C) 1997-2012 Tao Zha
43 %
44 % This free software: you can redistribute it and/or modify
45 % it under the terms of the GNU General Public License as published by
46 % the Free Software Foundation, either version 3 of the License, or
47 % (at your option) any later version.
48 %
49 % It is distributed in the hope that it will be useful,
50 % but WITHOUT ANY WARRANTY; without even the implied warranty of
51 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52 % GNU General Public License for more details.
53 %
54 % If you did not received a copy of the GNU General Public License
55 % with this software, see <http://www.gnu.org/licenses/>.
56 %
57
58
59 if nargin==5, nexo=1; end
60 ncoef = nvar*lags+nexo; % number of coefficients in *each* equation, RHS coefficients only.
61
62 H0multi=zeros(nvar,nvar,nvar); % H0 for different equations under asymmetric prior
63 Hpmulti=zeros(ncoef,ncoef,nvar); % H+ for different equations under asymmetric prior
64 H0invmulti=zeros(nvar,nvar,nvar); % inv(H0) for different equations under asymmetric prior
65 Hpinvmulti=zeros(ncoef,ncoef,nvar); % inv(H+) for different equations under asymmetric prior
66
67 %*** Constructing Pi for the ith equation under the random walk assumption
68 Pi = zeros(ncoef,nvar); % same for all equations
69 Pi(1:nvar,1:nvar) = eye(nvar); % random walk
70
71 %
72 %@@@ Prepared for Bayesian prior
73 %
74 %
75 % ** monthly lag decay in order to match quarterly decay: a*exp(bl) where
76 % ** l is the monthly lag. Suppose quarterly decay is 1/x where x=1,2,3,4.
77 % ** Let the decay of l1 (a*exp(b*l1)) match that of x1 (say, beginning: 1/1)
78 % ** and the decay of l2 (a*exp(b*l2)) match that of x2 (say, end: 1/5),
79 % ** we can solve for a and b which are
80 % ** b = (log_x1-log_x2)/(l1-l2), and a = x1*exp(-b*l1).
81 if q_m==12
82 l1 = 1; % 1st month == 1st quarter
83 xx1 = 1; % 1st quarter
84 l2 = lags; % last month
85 xx2 = 1/((ceil(lags/3))^mu(4)); % last quarter
86 %xx2 = 1/6; % last quarter
87 % 3rd quarter: i.e., we intend to let decay of the 6th month match
88 % that of the 3rd quarter, so that the 6th month decays a little
89 % faster than the second quarter which is 1/2.
90 if lags==1
91 b = 0;
92 else
93 b = (log(xx1)-log(xx2))/(l1-l2);
94 end
95 a = xx1*exp(-b*l1);
96 end
97
98
99
100 %
101 % *** specify the prior for each equation separately, SZ method,
102 % ** get the residuals from univariate regressions.
103 %
104 sgh = zeros(nvar,1); % square root
105 sgsh = sgh; % square
106 nSample=size(xdgel,1); % sample size-lags
107 yu = xdgel;
108 C = ones(nSample,1);
109 for k=1:nvar
110 [Bk,ek,junk1,junk2,junk3,junk4] = sye([yu(:,k) C],lags);
111 clear Bk junk1 junk2 junk3 junk4;
112 sgsh(k) = ek'*ek/(nSample-lags);
113 sgh(k) = sqrt(sgsh(k));
114 end
115 % ** prior variance for A0(:,1), same for all equations!!!
116 sg0bid = zeros(nvar,1); % Sigma0_bar diagonal only for the ith equation
117 for j=1:nvar
118 sg0bid(j) = 1/sgsh(j); % sgsh = sigmai^2
119 end
120 % ** prior variance for lagged and exogeous variables, same for all equations
121 sgpbid = zeros(ncoef,1); % Sigma_plus_bar, diagonal, for the ith equation
122 for i = 1:lags
123 if (q_m==12)
124 lagdecay = a*exp(b*i*mu(4));
125 end
126 %
127 for j = 1:nvar
128 if (q_m==12)
129 % exponential decay to match quarterly decay
130 sgpbid((i-1)*nvar+j) = lagdecay^2/sgsh(j); % ith equation
131 elseif (q_m==4)
132 sgpbid((i-1)*nvar+j) = (1/i^mu(4))^2/sgsh(j); % ith equation
133 else
134 error('Incompatibility with lags, check the possible errors!!!')
135 %warning('Incompatibility with lags, check the possible errors!!!')
136 %return
137 end
138 end
139 end
140 %
141
142
143 %=================================================
144 % Computing the (prior) covariance matrix for the posterior of A0, no data yet
145 %=================================================
146 %
147 %
148 % ** set up the conditional prior variance sg0bi and sgpbi.
149 sg0bida = mu(1)^2*sg0bid; % ith equation
150 sgpbida = mu(1)^2*mu(2)^2*sgpbid;
151 sgpbida(ncoef-nexo+1:ncoef) = mu(1)^2*mu(3)^2;
152 %<<>> No scaling adjustment has been made for exogenous terms other than constant
153 sgppbd = sgpbida(nvar+1:ncoef); % corresponding to A++, in a Sims-Zha paper
154
155 Hptd = zeros(ncoef);
156 Hptdi=Hptd;
157 Hptd(ncoef,ncoef)=sgppbd(ncoef-nvar);
158 Hptdinv(ncoef,ncoef)=1./sgppbd(ncoef-nvar);
159 % condtional on A0i, H_plus_tilde
160
161
162 if nargin<7 % the default is no asymmetric information
163 asym0 = ones(nvar,nvar); % if not ones, then we have relative (asymmetric) tightness
164 asymp = ones(ncoef-1,nvar); % for A+. Column -- equation
165 end
166
167 %**** Asymmetric Information
168 %asym0 = ones(nvar,nvar); % if not ones, then we have relative (asymmetric) tightness
169 %asymp = ones(ncoef-1,nvar); % pp: plus without constant. Column -- equation
170 %>>>>>> B: asymmetric prior variance for asymp <<<<<<<<
171 %
172 %for i = 1:lags
173 % rowif = (i-1)*nvar+1;
174 % rowil = i*nvar;
175 % idmatw0 = 0.5; % weight assigned to idmat0 in the formation of asymp
176 % if (i==1)
177 % asymp(rowif:rowil,:)=(1-idmatw0)*ones(nvar)+idmatw0*idmat0; % first lag
178 % % note: idmat1 is already transposed. Column -- equation
179 % else
180 % %asymp(rowif:rowil,1:nvar) = (1-idmatw0)*ones(nvar)+idmatw0*idmat0;
181 % % <<<<<<< toggle +
182 % % Note: already transposed, since idmat0 is transposed.
183 % % Meaning: column implies equation
184 % asymp(rowif:rowil,1:nvar) = ones(nvar);
185 % % >>>>>>> toggle -
186 % end
187 %end
188 %
189 %>>>>>> E: asymmetric prior variance for asymp <<<<<<<<
190
191
192 %=================================================
193 % Computing the final covariance matrix (S1,...,Sm) for the prior of A0,
194 % and final Gb=(G1,...,Gm) for A+ if asymmetric prior or for
195 % B if symmetric prior for A+
196 %=================================================
197 %
198 for i = 1:nvar
199 %------------------------------
200 % Introduce prior information on which variables "belong" in various equations.
201 % In this first trial, we just introduce this information here, in a model-specific way.
202 % Eventually this info has to be passed parametricly. In our first shot, we just damp down
203 % all coefficients except those on the diagonal.
204
205 %*** For A0
206 factor0=asym0(:,i);
207 sg0bd = sg0bida.*factor0; % Note, this only works for the prior variance Sg(i)
208 % of a0(i) being diagonal. If the prior variance Sg(i) is not
209 % diagonal, we have to work on inv(Sg(i)) or sg0bdinv directly.
210 sg0bdinv = 1./sg0bd;
211 % * unconditional variance on A0+
212 H0td = diag(sg0bd); % unconditional
213 H0tdinv = diag(sg0bdinv);
214 %
215 H0multi(:,:,i)=H0td;
216 H0invmulti(:,:,i)=H0tdinv;
217
218
219 %*** For A+
220 if ~(lags==0) % For A1 to remain random walk properties
221 factor1=asymp(1:nvar,i);
222 sg1bd = sgpbida(1:nvar).*factor1;
223 sg1bdinv = 1./sg1bd;
224 %
225 Hptd(1:nvar,1:nvar)=diag(sg1bd);
226 Hptdinv(1:nvar,1:nvar)=diag(sg1bdinv);
227 if lags>1
228 factorpp=asymp(nvar+1:ncoef-1,i);
229 sgpp_cbd = sgppbd(1:ncoef-nvar-1) .* factorpp;
230 sgpp_cbdinv = 1./sgpp_cbd;
231 Hptd(nvar+1:ncoef-1,nvar+1:ncoef-1)=diag(sgpp_cbd);
232 Hptdinv(nvar+1:ncoef-1,nvar+1:ncoef-1)=diag(sgpp_cbdinv);
233 % condtional on A0i, H_plus_tilde
234 end
235 end
236 Hpmulti(:,:,i)=Hptd;
237 Hpinvmulti(:,:,i)=Hptdinv;
238 end
239
240