Debian Codegraph @

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

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