Debian Codegraph @

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

raw
1 function [of,GlbAphh] = pmddwf23(x,a0indx,fss,nvar,ncoef,phi, ...
2 y,xd0,ys1,A0b,sg0bid,sgpbid)
3 %function of = pmddf23(x,a0indx,fss,nvar,ncoef,phi, ...
4 % y,xd0,ys1,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 % xd0 (X(0) for dummy observations y*(1))
16 % ys1 (dummy initial observations y*(1))
17 % A0b (initial prior mean on A0)
18 % sg0bid (diagonal of Sigma0_bar, unweighted prior vcv on the parameters in i-th equation in A0)
19 % sgpbid (diagonal of Sigma+_bar, unweighted prior vcv on the parameters in i-th equation in A+)
20 %
21 % Copyright (C) 1997-2012 Tao Zha
22 %
23 % This free software: you can redistribute it and/or modify
24 % it under the terms of the GNU General Public License as published by
25 % the Free Software Foundation, either version 3 of the License, or
26 % (at your option) any later version.
27 %
28 % It is distributed in the hope that it will be useful,
29 % but WITHOUT ANY WARRANTY; without even the implied warranty of
30 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 % GNU General Public License for more details.
32 %
33 % If you did not received a copy of the GNU General Public License
34 % with this software, see <http://www.gnu.org/licenses/>.
35 %
36
37 global vaya0 vaha0 vaxah vahad vbiga GlbAph
38 % pre-allocate GlbAph to be zeros(nvar,lags*nvar+1=ncoef)
39
40 GlbAphh=zeros(ncoef,nvar);
41
42
43 nhp = 0; % <<>> 4 hyperparameters
44 na0p = length(a0indx); % <<>> number of A0 parameters
45 nfp = na0p+nhp;
46
47 a0 = zeros(nvar^2,1);
48 a0(a0indx) = x(1:na0p);
49 A0h = reshape(a0,nvar,nvar);
50 % restrictions on A0
51
52 [A0hl,A0hu] = lu(A0h);
53
54 % ** hyperparameters
55 %%mu = x(na0p+1:nfp);
56 mu = ones(5,1);
57 mu(1) = 1;
58 mu(2) = 0.2;
59 mu(3) = 20;
60 mu(5) = 1;
61 % results from ...\dummy1\foreh\pmdd6.out
62 %
63 % mu(1): overall tightness and also for A0;
64 % mu(2): relative tightness for A+;
65 % mu(3): relative tightness for the constant term;
66 % mu(4): relative tightness for dummy initial observations;
67 % mu(5): relative tightness for prior dummy observations.
68
69 % ** weight prior dummy observations
70 %phi(1:nvar,:) = (mu(5)^2/mu(1)^2)*phi(1:nvar,:);
71 %y(1:nvar,:) = (mu(5)^2/mu(1)^2)*y(1:nvar,:);
72 % modified by CAS 8/6/96. The dummy obs weights are naturally in std units, not var units.
73 phi(1:nvar,:) = mu(5)*phi(1:nvar,:);
74 y(1:nvar,:) = mu(5)*y(1:nvar,:);
75
76 % ** set up the conditional prior variance sg0bi and sgpbi.
77 sg0bida = mu(1)^2*sg0bid;
78 sgpbida = mu(1)^2*mu(2)^2*sgpbid;
79 sgpbida(ncoef) = mu(1)^2*mu(3)^2;
80 sg0bi = diag(sg0bida);
81 sgpbi = diag(sgpbida);
82
83 %
84 % ** some common terms
85 [u d v]=svd(phi,0); %trial
86 % xtx = phi'*phi; %trial
87 vd=v.*(ones(size(v,2),1)*diag(d)'); %trial
88 xtx=vd*vd';
89 % M = eye(fss) - phi*(xtx\phi'); % not used except in line below %trial
90 yu = y'*u; %trial
91 cxyxx=yu*yu'; %trial
92 ymy=y'*y-cxyxx; %trial
93 %ymy = y'*M*y;
94 %cxy = phi'*y; %trial
95 cxy = vd*yu'; %trial
96 %cxpy = xtx\cxy; %not used further except in line below
97 %cxyxx = cxy'*cxpy;
98
99 % ** estimate A+_hat conditional on A0, ncoef*nvar, but not using full prior
100 %GlbAph = cxpy*A0h;
101
102
103 %%%%%%%
104 %%% build the objective function below
105 %%%%%%%
106 %
107 t1f = diag(abs(A0hu));
108 t1f = log(t1f);
109 tt1 = (-1.0)*fss*sum(t1f);
110
111 tt3 = 0;
112 tt4 = 0;
113
114 stril = 0;
115 Hp0p = zeros(ncoef);
116 % initializing Htd_+0*inv(Htd_00)*Htd_0+: conditional prior covariance
117 for i = 1:nvar
118 stri = find(A0h(:,i));
119 strm = length(stri);
120
121 A0hb = A0h(stri,i)-A0b(stri,i);
122 Hp0 = zeros(ncoef,strm);
123 % initializing Htd_+0*inv(Htd_00)*Htd_0
124
125 % ** set up the conditional prior variance sg0bi and sgpbi.
126 sg0bd = zeros(stri,1);
127 sg0bd = sg0bida(stri);
128 sg0b = diag(sg0bd);
129
130 % ** set up the unconditional prior variance on A0i and A+i
131 XX = zeros(nvar,strm);
132 XX(stri,:) = eye(strm); % XX in Gelman, etel, on p479
133 %
134 Hb = zeros(strm+ncoef); % including A0i and A+i
135 Hb(1:strm,1:strm) = sg0b;
136 Hb(1:strm,strm+1:strm+nvar) = sg0b*XX';
137 Hb(strm+1:strm+nvar,1:strm) = XX*sg0b;
138 Hb(strm+1:strm+nvar,strm+1:strm+nvar) = XX*sg0b*XX'+sgpbi(1:nvar,1:nvar);
139 Hb(strm+nvar+1:strm+ncoef,strm+nvar+1:strm+ncoef) = ...
140 sgpbi(nvar+1:ncoef,nvar+1:ncoef);
141
142 % ** set up the final prior variance on A0i and A+i, i =1,..,6 separately.
143 % ** using dummy initial observations
144 %zs1 = [ys1(:,stri) -xd0]; % z*1 for the 1st equation
145
146 % * final unconditional prior variance on A0i and A+i
147 %Hbzs1=Hb*zs1';
148 %Htd = Hb - Hbzs1*((zs1*Hbzs1+mu(1)^2*mu(4)^2*eye(nvar))\Hbzs1');
149 % CAS mod 8/7/96: Hope is that dropping above two lines in favor of the one below
150 % removes double-counting of dummy observations and a possible scale sensitivity.
151 Htd = Hb;
152 % H_~: td: tilde for ~
153
154 % * final conditional prior variance on A+i give that on A0i, and
155 % * unconditional variance on A0+
156 H0td = Htd(1:strm,1:strm); % unconditional
157 % ** inverse and chol decomp
158 H0tdi = inv(H0td);
159
160 %
161 Hptd10 = Htd(strm+1:strm+nvar,1:strm);
162 % top matrix of nvar in Htd_+0
163 Hptd100 = Hptd10*H0tdi;
164 Hp0(1:nvar,:) = Hptd100;
165 Hptd101 = Hptd100*Hptd10';
166 Hp0p(1:nvar,1:nvar) = Hptd101;
167 Hptd = Htd(strm+1:strm+ncoef,strm+1:strm+ncoef) - Hp0p;
168 % condtional on A0i, H_plus_tilde
169
170 % ** inverse
171 Hptdi = inv(Hptd);
172
173 % common term
174 xxhp = xtx+Hptdi;
175 Vxxhp = chol(xxhp);
176 % inverse of Vxxhp is the upper triangular decomp
177 % of inv(Vxxhp) (covariance of alpst)
178
179 % ** final conditional prior mean on A+i
180 alptd = zeros(ncoef,1);
181 alptd(1:nvar) = XX*A0b(stri,i);
182 %alptd = alptd + Hptdf*(H0tdi*A0hb); CAS efficiency improvement 8/8/96
183 alptd = alptd + Hp0*A0hb;
184 % conditional on A0i, alpha-plus-tilde
185
186 % * alpha_plus_* for the ith equation, ncoef*1
187 % *alps = xxhp\(phi'*y*A0h(:,i)+HptdDinalptd);
188 HptdDinalptd=Hptdi*alptd;
189 xaha = cxy*A0h(:,i)+HptdDinalptd; %CAS 8/7/96. Replaced phi'*y with cxy
190 alpst = xaha'/xxhp;
191 GlbAph(i,:)=alpst;
192 % alpst': transpose of alps.
193 GlbAphh(:,i)=alpst' + Vxxhp\randn(length(alpst),1);
194 % posterior alpha_plus_*
195
196
197 % ** 3rd bid term in i-th equation,
198 % ** with all other 3rd big terms prior to i-th
199 A0hy = A0h(:,i)'*ymy;
200 A0hbH = A0hb'*H0tdi;
201 tt3 = tt3 + A0hy*A0h(:,i) + A0hbH*A0hb;
202
203 % ** 4th bid term in i-th equation,
204 % ** with all other 4th big terms prior to i-th
205 A0hcxyxx = A0h(:,i)'*cxyxx;
206 atdHatd = alptd'*HptdDinalptd;
207 tt4 = tt4 + A0hcxyxx*A0h(:,i)+atdHatd-alpst*xaha;
208
209 stril = stril + strm;
210 end
211
212 of = tt1 + 0.5*tt3 + 0.5*tt4;
213