Debian Codegraph @

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

raw
1 function of = pmddf234(x,idmat0,idmat1,fss,nvar,ncoef,phi, ...
2 y,A0b,sg0bid,sgpbid)
3 %function of = pmddf232(x,idmat,fss,nvar,ncoef,phi, ...
4 % y,A0b,sg0bid,sgpbid)
5 % Leeper-Sims-Zha BVAR setup
6 % general program to setup A0 matrix and compute the likelihood
7 % requires
8 % x (parameter vector)
9 % a0indx (matrix indicating the free parameters in A0)
10 % fss (forecast sample size)
11 % nvar (number of variables)
12 % ncoef (number of coefficients in a single equation in A+)
13 % phi (r.h.s. observations in the system for A+)
14 % y (l.h.s. observations for A0)
15 % A0b (initial prior mean on A0)
16 % sg0bid (diagonal of Sigma0_bar, unweighted prior vcv on the parameters in i-th equation in A0)
17 % sgpbid (diagonal of Sigma+_bar, unweighted prior vcv on the parameters in i-th equation in A+)
18 %
19 % Revisions by CAS 9/27/96: If we transmit idmat, rather than a0indx, we can scale the elements of
20 % idmat so that it carries information about relative prior variances.
21 %
22 % Revsions by TZ, 10/12/96: efficiency improvement by streamlining the previous code
23 % according to the general setup in Sims and Zha "Bayesian Methods for ...".
24 %
25 % Copyright (C) 1997-2012 Christopher A. Sims and Tao Zha
26 %
27 % This free software: you can redistribute it and/or modify
28 % it under the terms of the GNU General Public License as published by
29 % the Free Software Foundation, either version 3 of the License, or
30 % (at your option) any later version.
31 %
32 % It is distributed in the hope that it will be useful,
33 % but WITHOUT ANY WARRANTY; without even the implied warranty of
34 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 % GNU General Public License for more details.
36 %
37 % If you did not received a copy of the GNU General Public License
38 % with this software, see <http://www.gnu.org/licenses/>.
39 %
40
41 global vaya0 vaha0 vaxah vahad vbiga GlbAph FRESHFUNCTION
42 % FRESHFUNCTION added 8/20/90 by CAS. It signals the pmddg23 whether the global variables
43 % it uses have been refreshed by a new pmddf231 call since the last gradient call.
44 a0indx=find(idmat0);
45 % pre-allocate GlbAph to be zeros(nvar,lags*nvar+1=ncoef)
46
47 nhp = 0; % <<>> 4 hyperparameters
48 na0p = length(a0indx); % <<>> number of A0 parameters
49 nfp = na0p+nhp;
50
51 % *** initializing
52 vaya0 = zeros(na0p,1);
53 vaha0 = zeros(na0p,1);
54 vaxah = zeros(na0p,1);
55 vahad = zeros(na0p,1);
56 vbiga = zeros(na0p,1);
57
58
59 A0h = zeros(nvar,nvar);
60 A0h(a0indx) = x(1:na0p);
61 %A0h = reshape(a0,nvar,nvar); CAS 9/24/96. The reshape is not necessary if a0 starts as nvar x nvar
62 % restrictions on A0
63
64 [A0hl,A0hu] = lu(A0h);
65
66 % ** hyperparameters
67 %%mu = x(na0p+1:nfp);
68 mu = ones(5,1);
69 mu(1) = 1;
70 %mu(1) = 2;
71 mu(2) = 0.3;
72 mu(3) = 25;
73 %mu(4)=1;
74 mu(4)=1;
75 mu(5) =1;
76 %mu(5) = 40;
77 % results from ...\dummy1\foreh\pmdd6.out
78 %
79 % mu(1): overall tightness and also for A0;
80 % mu(2): relative tightness for A+;
81 % mu(3): relative tightness for the constant term;
82 % mu(4): weight on single dummy initial observation including constant;
83 % mu(5): weight on nvar sums of coeffs dummy observations.
84
85 % ** weight prior dummy observations
86 %phi(1:nvar,:) = (mu(5)^2/mu(1)^2)*phi(1:nvar,:);
87 %y(1:nvar,:) = (mu(5)^2/mu(1)^2)*y(1:nvar,:);
88 %phi(ndobs,:) = mu
89 % modified by CAS 8/6/96. The dummy obs weights are naturally in std units, not var units.
90 %%phi(1:nvar,:) = mu(5)*phi(1:nvar,:);
91 %%y(1:nvar,:) = mu(5)*y(1:nvar,:);
92 %%phi(nvar+1,:) = mu(4)*phi(nvar+1,:);
93 %%y(nvar+1,:) = mu(4)*y(nvar+1,:);
94
95 % ** set up the conditional prior variance sg0bi and sgpbi.
96 %%sg0bida = mu(1)^2*sg0bid;
97 sgpbida = mu(1)^2*mu(2)^2*sgpbid;
98 sgpbida(ncoef) = mu(1)^2*mu(3)^2;
99 sgppbd = sgpbida(nvar+1:ncoef); % corresponding to A++, in an SZ paper
100 sgppb = diag(sgppbd);
101
102 %
103 % ** some common terms
104 [u d v]=svd(phi,0); %trial
105 % xtx = phi'*phi; %trial
106 vd=v.*(ones(size(v,2),1)*diag(d)'); %trial
107 xtx=vd*vd';
108 % M = eye(fss) - phi*(xtx\phi'); % not used except in line below %trial
109 yu = y'*u; %trial
110 cxyxx=yu*yu'; %trial
111 ymy=y'*y-cxyxx; %trial
112 %ymy = y'*M*y;
113 %cxy = phi'*y; %trial
114 cxy = vd*yu'; %trial
115 %cxpy = xtx\cxy; %not used further except in line below
116 %cxyxx = cxy'*cxpy;
117
118 % ** estimate A+_hat conditional on A0, ncoef*nvar, but not using full prior
119 %GlbAph = cxpy*A0h;
120
121
122 %%%%%%%
123 %%% build the objective function below
124 %%%%%%%
125 %
126 t1f = diag(abs(A0hu));
127 t1f = log(t1f);
128 % * without the prior |A0|^k
129 tt1 = (-1.0)*fss*sum(t1f);
130
131 tt3 = 0;
132 tt4 = 0;
133
134 disp(sprintf('Starting loop (of): %g',toc))
135
136 stril = 0;
137 Hp0p = zeros(ncoef);
138 % initializing Htd_+0*inv(Htd_00)*Htd_0+: conditional prior covariance
139 for i = 1:nvar
140 stri = find(idmat0(:,i)); %CAS change 9/24/96, to avoid slim chance of an exact zero in x vector
141 %at some point during an optimization.
142 strm = length(stri);
143
144 %%A0hb = A0h(stri,i)-A0b(stri,i);
145
146 % ** set up the conditional prior variance sg0bi and sgpbi.
147 % sg0bd = zeros(stri,1); Commented out by CAS, 9/24/96. Looks like this meant to have
148 % strm where it has stri, and in any case it is "overwritten" below.
149 %------------------------------
150 % Introduce prior information on which variables "belong" in various equations.
151 % In this first trial, we just introduce this information here, in a model-specific way.
152 % Eventually this info has to be passed parametricly. In our first shot, we just damp down
153 % all coefficients except those on the diagonal.
154 %%factor0=idmat0(:,i);
155 %%sg0bd = sg0bida(stri).*factor0(stri);
156 %%sg0b = diag(sg0bd);
157 %
158 factor1=idmat1(:,i);
159 sg1bd = sgpbida(1:nvar).*factor1;
160 sg1b = diag(sg1bd);
161
162 % ** set up the unconditional prior variance on A0i and A+i
163 XX = zeros(nvar,strm);
164 XX(stri,:) = eye(strm); % XX in Gelman, etel, on p479
165 %
166 % * final conditional prior variance on A+i give that on A0i, and
167 % * unconditional variance on A0+
168 %%H0td = sg0b; % unconditional
169 % H_~: td: tilde for ~
170 % ** inverse and chol decomp
171 %%H0tdi = inv(H0td);
172
173 %
174 Hptd = zeros(ncoef);
175 Hptd(1:nvar,1:nvar)=sg1b;
176 Hptd(nvar+1:ncoef,nvar+1:ncoef)=sgppb;
177 % condtional on A0i, H_plus_tilde
178
179 % ** inverse
180 Hptdi = inv(Hptd);
181 %%chhh=Hptdi*Hp0;
182 chhh = zeros(ncoef,strm); % <<>> the term not used for "of"\
183 chhh(1:nvar,:) = Hptdi(1:nvar,1:nvar)*XX;
184 chhh1 = XX'*chhh(1:nvar,:);
185
186 % common term
187 xxhp = xtx+Hptdi;
188
189 % ** final conditional prior mean on A+i
190 alptd = zeros(ncoef,1);
191 alptd(1:nvar) = XX*A0h(stri,i);
192 % conditional on A0i, alpha-plus-tilde
193
194 % * alpha_plus_* for the ith equation, ncoef*1
195 % *alps = xxhp\(phi'*y*A0h(:,i)+HptdDinalptd);
196 %%HptdDinalptd=Hptdi*alptd;
197 HptdDinalptd=zeros(ncoef,1);
198 HptdDinalptd(1:nvar) = Hptdi(1:nvar,1:nvar)*alptd(1:nvar);
199 xaha = cxy*A0h(:,i)+HptdDinalptd; %CAS 8/7/96. Replaced phi'*y with cxy
200 alpst = xaha'/xxhp;
201 GlbAph(i,:)=alpst;
202 % alpst': transpose of alps.
203
204 % ** 3rd bid term in i-th equation,
205 % ** with all other 3rd big terms prior to i-th
206 A0hy = A0h(:,i)'*ymy;
207 %%A0hbH = A0hb'*H0tdi;
208 %%tt3 = tt3 + A0hy*A0h(:,i) + A0hbH*A0hb;
209 tt3 = tt3 + A0hy*A0h(:,i);
210 % ** 4th bid term in i-th equation,
211 % ** with all other 4th big terms prior to i-th
212 A0hcxyxx = A0h(:,i)'*cxyxx;
213 %%atdHatd = alptd'*HptdDinalptd; % efficiency improvement
214 atdHatd = alptd(1:nvar)'*HptdDinalptd(1:nvar);
215 tt4 = tt4 + A0hcxyxx*A0h(:,i)+atdHatd-alpst*xaha;
216
217 %%%%
218 % *** passing the gradient to "pmdg6.m" (analytical gradient)
219 %%%%
220 % ** d1aya0 = d(alpha0'*Y'MY*alpha0)/d(a0??)
221 daya0 = 2.0*A0hy;
222 daya0 = daya0(:);
223 vaya0(stril+1:stril+strm) = daya0(stri);
224
225 % ** daha0 = d((alpha0-alpha0_tilde)'*inv(H_0_tilde)*
226 % ** (alpha0-alpha0_tilde))/d(a0??)
227 %%daha0 = 2.0*A0hbH;
228 %%vaha0(stril+1:stril+strm) = daha0(:);
229
230 % ** daxah = d(alpha_+^'*X'X*alpha_+^)/d(a0??)
231 % ** daxah = d(alpha_+^'*X'X*alpha_+^)/d(a0??)
232 %% daxah = 2*(Aph(:,i)'*xtx) * cxpy(:,stri);
233 %% vaxah(stril+1:stril+strm) = daxah';
234 daxah = 2*A0hcxyxx;
235 daxah = daxah(:);
236 vaxah(stril+1:stril+strm) = daxah(stri);
237
238
239 % ** dahad = d(alpha_+~'*inv(H_+~)*alpha_+~)/d(a0??)
240 dahad = 2*A0h(stri,i)'*chhh1;
241 % <<>> multiplications not used in "of"
242 vahad(stril+1:stril+strm) = dahad(:);
243
244
245 % ** dbiga = d((X'X*alpha_+^+inv(H_+~)*alpha_+*)'*a_+_*)/d(a0??)
246 dbiga = 2*alpst*(cxy(:,stri)+chhh);
247 % <<>> multiplications not used in "of"
248 vbiga(stril+1:stril+strm) = dbiga';
249
250 stril = stril + strm;
251 end
252
253 %disp(sprintf('Loop %d end: %g', i, toc))
254 disp(sprintf('Loop end (of): %g', toc))
255
256 %e_tfat = toc
257
258 of = tt1 + 0.5*tt3 + 0.5*tt4;
259 FRESHFUNCTION=1;