Debian Codegraph @

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

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