Debian Codegraph @

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

raw
1 function [xdraw,timeminutes,nswitch] = a0onlysim(xinput)
2 % [xdraw,timeminutes,nswitch] = a0onlysim(xinput)
3 % Export and plot the simulated pdfs of draws of only selected parameters;
4 % Print and save Gelman's measures of B and W for all parameters;
5 % Ref: Waggoner and Zha "Does Normalization Matter for Inference"
6 % See note Forecast (2)
7 %
8 % xinput{1}: nfp -- total number of free parameters
9 % xinput{2}: nvar -- number of variables
10 % xinput{3}: xhat -- ML estimate of free parameters in A0
11 % xinput{4}: hess1 -- Hessian of -logLH
12 % xinput{5}:Indxv -- index for selected variables of interest; normall first 2 are of our interest
13 % to select variables, always check idmat0 to make sure.
14 % When Indxv=[], xdraw is empty as well. When IndxGgraph=1, it plots
15 % (1) pdf of 1st v for every buffer, (2) scattered plot of 1st and 2nd for every buffer,
16 % (3) pdf of 1st v for all sequences; (4) scattered plot of 3rd and 4th for all sequences
17 % (5) scattered plot of 1st and 2nd for al sequences.
18 % xinput{6}: IndxGraph - 1: plot graphs; 0: no graphs
19 % xinput{7}: idmat0 -- Index for non-zero elements in A0 with column to equation
20 % xinput{8}: nstarts -- # of starting points in Gibbs sampling
21 % xinput{9}: ndraws1 -- # of 1st loop to be discarded
22 % xinput{10}: ndraws2 -- # of 2nd loop for saving A0 draws
23 % xinput{11}: imndraws=nstarts*ndraws2
24 % xinput{12}: a0indx -- index number for non-zero elements in A0
25 % xinput{13}: tdf -- degrees of freedom for t-distribution
26 % xinput{14}: nbuffer -- interval for printing, plotting, and saving
27 % xinput{15}: Sbd -- nvar-by-nvar S{1}, ..., S{n} -- kind of covariance matrix for each simultaneous equation
28 % Already divided by "fss."
29 % xinput{16}: nSample -- the original sample size including lags
30 % xinput{17}: IndxNmlr -- index for which normalization rule to choose
31 % xinput{18}: IndxGibbs -- index for WZ Gibbs; 1; Gibbs; 0: Metropolis
32 % xinput{19}: scf -- reduction scale factor for Metropolis jumping kernel
33 % xinput{20}: H_sr -- square root of the inverse of the covariance matrix
34 % for free elements in A0 (nfp-by-nfp)
35 % xinput{21}: fss -- effective sample size == nSample-lags+# of dummy observations
36 %------------------
37 % xdraw: nstarts*ndraws2-by-length(Indxv) matrix of draws;
38 % empty if Indxv=[]
39 % timeminutes: minutes used for this simulation
40 %
41 % Written by Tao Zha 1999
42 %
43 % Copyright (C) 1997-2012 Tao Zha
44 %
45 % This free software: you can redistribute it and/or modify
46 % it under the terms of the GNU General Public License as published by
47 % the Free Software Foundation, either version 3 of the License, or
48 % (at your option) any later version.
49 %
50 % It is distributed in the hope that it will be useful,
51 % but WITHOUT ANY WARRANTY; without even the implied warranty of
52 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
53 % GNU General Public License for more details.
54 %
55 % If you did not received a copy of the GNU General Public License
56 % with this software, see <http://www.gnu.org/licenses/>.
57 %
58
59 nfp=xinput{1}; nvar=xinput{2}; xhat=xinput{3}; hess=xinput{4}; Indxv=xinput{5};
60 IndxGraph=xinput{6}; idmat0=xinput{7}; nstarts=xinput{8}; ndraws1=xinput{9}; ndraws2=xinput{10};
61 imndraws=xinput{11}; a0indx=xinput{12}; tdf=xinput{13}; nbuffer=xinput{14}; Sbd=xinput{15};
62 nSample = xinput{16}; IndxNmlr=xinput{17}; IndxGibbs=xinput{18}; scf=xinput{19}; H_sr=xinput{20};
63 fss=xinput{21};
64
65 if IndxGraph>length(Indxv)
66 disp(' ')
67 warning('when IndxGraph=1, Indxv must have at least 1 elements')
68 disp('Press ctrl-c to abort')
69 pause
70 end
71
72 Avhx_bs = zeros(nfp,1);
73 Avhx_bm = zeros(nfp,1);
74 Avhx_bj = zeros(nfp,1);
75 Avhx_cj = zeros(nfp,1);
76 Avhx_cm = zeros(nfp,1);
77 A0_h = zeros(nvar);
78 A0gbs = A0_h; % drawn A0 in Gibbs sampling
79 Avhxm = zeros(nfp,1);
80 Avhxs = Avhxm;
81 A0xhat = zeros(nvar);
82 A0xhat(a0indx) = xhat;
83 % A0hatw = zeros(nvar^2,nbuffer);
84
85 countJump = zeros(nstarts,1);
86
87
88 %===================================
89 % Here begins with the big loop
90 %===================================
91 H1 = chol(hess); % upper triangular so that H1' is a lower triangular decomp
92 baseW = H_sr; %inv(H1); %H_sr; % covariance matrix without scaling
93 nswitch=0; %<<>> total number of sign switches
94 A0inxhat = inv(A0xhat); % inverse of ML estimate
95 a0indx0 = find(idmat0==0); % index for all zero's in A0;
96 if isempty(Indxv)
97 xdraw=[];
98 else
99 xdraw=zeros(imndraws,length(Indxv)); %<<>> draws of selected variables
100 end
101
102
103 [cT,vR,kdf] = gibbsglb(Sbd,idmat0,nvar,fss);
104
105 tic
106 for starts = 1:nstarts
107 starts
108 if starts == 1
109 A0gbs(a0indx) = xhat; % from "load ..."
110 if ~IndxGibbs % Metropolist
111 Avhx = xhat;
112 hAvhx = a0asfun(Avhx,Sbd,fss,nvar,a0indx);
113 hAvhx = -hAvhx; % converted to logLH
114 end
115 else
116 Avhx = baseW*randn(nfp,1); %H_sr*randn(nfp,1); % D: discarded sequence
117 csq=randn(tdf,1);
118 csq=sum(csq .* csq);
119 Avhx = xhat+Avhx/sqrt(csq/tdf);
120 %** Normalization by the choice of IndxNmlr
121 A0gbs(a0indx) = Avhx;
122 if ~IndxNmlr(5)
123 [A0gbs,jnk] = nmlzvar(A0gbs,A0xhat,A0inxhat,IndxNmlr,nswitch,[]);
124 else
125 A0ingbs = inv(A0gbs);
126 [A0gbs,jnk,jnk1] = nmlzvar(A0gbs,A0xhat,A0inxhat,IndxNmlr,nswitch,A0ingbs);
127 end
128 %
129 if ~IndxGibbs % Metropolist
130 Avhx = A0gbs(a0indx);
131 hAvhx = a0asfun(Avhx,Sbd,fss,nvar,a0indx);
132 hAvhx = -hAvhx; % converted to logLH
133 end
134 end
135 %
136 Avhxmm = zeros(nfp,1);
137 Avhxss = zeros(nfp,1);
138 cJump = 0;
139
140 for draws = 1:ndraws1
141 if IndxGibbs
142 A0gbs = gibbsvar(A0gbs,cT,vR,nvar,fss,kdf);
143 else % Metropolis
144 [Avhx,hAvhx,cJump] = smtplis(Avhx,hAvhx,tdf,cJump,scf,...
145 baseW,nfp,Sbd,fss,nvar,a0indx);
146 end
147 end
148
149 wdraws=(starts-1)*ndraws2+0;
150 for draws = 1:ndraws2
151 drawsc = (starts-1)*ndraws2+draws;
152 if IndxGibbs
153 A0gbs = gibbsvar(A0gbs,cT,vR,nvar,fss,kdf);
154 A0gbs(a0indx0) = 0; % set all zeros in A0gbs clean to avoid possible cumulative round-off errors
155 else % Metropolis
156 [Avhx,hAvhx,cJump] = smtplis(Avhx,hAvhx,tdf,cJump,scf,...
157 baseW,nfp,Sbd,fss,nvar,a0indx);
158 A0gbs(a0indx) = Avhx;
159 end
160
161 %*** call normalization so that A0_h is normalized
162 if ~IndxNmlr(5)
163 [A0_h,nswitch] = nmlzvar(A0gbs,A0xhat,A0inxhat,IndxNmlr,nswitch,[]);
164 else
165 A0ingbs = inv(A0gbs);
166 [A0_h,nswitch,A0_hin] = nmlzvar(A0gbs,A0xhat,A0inxhat,IndxNmlr,nswitch,A0ingbs);
167 end
168 Avhx_norm = A0_h(a0indx);
169
170 Avhxm = Avhxm + Avhx_norm; % 1st step to overall mean of parameter
171 Avhxs = Avhxs + Avhx_norm.^2; % 1st step to overall 2nd moment of parameter
172
173 %* compute the mean and 2nd moment
174 %** Getting average of variances W and variance of means B/n -- B_n
175 %* see Gelman, p.331, my Shock(0), 12-13, and my Forecast (2), 28-31
176 if (nstarts>1)
177 Avhxmm = Avhxmm + Avhx_norm; % n*(phi_.j)
178 Avhxss = Avhxss + Avhx_norm.^2; % 1st step to (phi_ij) for fixed j
179 end
180
181 if ~isempty(Indxv)
182 xdraw((starts-1)*ndraws2+draws,:) = Avhx_norm(Indxv)'; %<<>>
183 end
184 % A0hatw(:,drawsc-wdraws) = A0_h(:);
185 if ~mod(draws,nbuffer)
186 starts
187 draws
188 wdraws=drawsc
189 % fwriteid = fopen('outA0.bin','a');
190 % count = fwrite(fwriteid,A0hatw,'double');
191 % status = fclose('all');
192 if IndxGraph
193 %** 1-d pdf plot
194 % figure(2)
195 % histpdfg(xdraw((starts-1)*ndraws2+(1:draws),1),50,[],[],[]);
196 % pause(1)
197
198 %** 2-d scatterplot
199 figure(3)
200 plot(xdraw((starts-1)*ndraws2+(1:draws),1),...
201 xdraw((starts-1)*ndraws2+(1:draws),2),'.');
202 drawnow
203 end
204 end
205 end
206 %
207 if ~IndxGibbs
208 countJump(starts,1) = cJump;
209 end
210 %
211 %** Getting average of variances W and variance of means B/n -- B_n
212 %** see Gelman, p.331, my Shock(0), pp.12-13, and my Forecast (2), pp.28-31
213 if (nstarts>1)
214 Avhx_aj = Avhxmm/ndraws2; % (phi_.j)
215 Avhx_bs = Avhx_bs + Avhx_aj.^2; % 1st step to 2nd moment of (phi_.j)
216 Avhx_bm = Avhx_bm + Avhx_aj; % 1st step to (phi_..)
217 %
218 Avhx_bj = Avhxss/ndraws2; % 2nd moment of (phi_ij) for fixed j
219 Avhx_cj = Avhx_bj - Avhx_aj.^2; % ((n-1)/n)*S^2_j
220 Avhx_cm = Avhx_cm + Avhx_cj; % sum of ((n-1)/n)*S^2_j across j
221 end
222 end
223 timend = toc
224
225 if ~IndxGibbs
226 countJump = countJump/ndraws2
227 end
228
229 Avhxm = Avhxm/(imndraws);
230 Avhxs = Avhxs/(imndraws);
231 Avhxv = Avhxs - Avhxm.^2;
232 Avhxs = sqrt(Avhxv); % stardard deviation
233 A0hm = zeros(nvar);
234 A0hm(a0indx) = Avhxm % mean
235 A0hv = zeros(nvar);
236 A0hv(a0indx) = Avhxv; % varaince matrix
237 A0hs = zeros(nvar);
238 A0hs(a0indx) = Avhxs; % standar deviation
239
240 %**** Getting Within-Sequence W and Between-Sequence B_n
241 % see Gelman, p.331, my Shock(0), pp.12-13, and my Forecast (2), pp.28-31
242 if (nstarts>1)
243 AvhxW = (ndraws2/(ndraws2-1))*Avhx_cm/nstarts;
244 % W: average of j within-sequence variances
245 AvhxB_n = (nstarts/(nstarts-1)) * ( Avhx_bs/nstarts - (Avhx_bm/nstarts).^2 );
246 % B/n: variance of J within-sequence means
247 AvhxB = ndraws2*AvhxB_n; % B
248 %
249 B_W1 = AvhxB ./ AvhxW;
250 B1 = AvhxB;
251 W1 = AvhxW;
252 GR1 = sqrt((ndraws2-1)/ndraws2 + B_W1/ndraws2);
253 % measure of Gelman reduction; need not be 1 to be accurate,
254 % contrary to what Gelman claims
255 save outB_W B_W1 B1 W1 GR1 nstarts ndraws2 imndraws timend Avhxs ...
256 A0xhat A0hm A0hs A0hv IndxGibbs countJump nswitch
257
258 titstr = ['J ' num2str(nstarts) ' n1 ' num2str(ndraws1) ...
259 ' n2 ' num2str(ndraws2) ' timend minutes ' num2str(timend/60)];
260 disp(' ')
261 disp(titstr)
262 disp('B/W sqrt(B) sqrt(W) Std(A0) GR')
263 format short g
264 [B_W1 sqrt(B1) sqrt(W1) Avhxs GR1]
265 else
266 save outB_W nstarts ndraws2 imndraws timend Avhxs ...
267 A0xhat A0hm A0hs A0hv IndxGibbs countJump nswitch
268 end
269
270 disp(' ')
271 disp('nswitch nswitch/imndraws -- # of sign switches')
272 [nswitch nswitch/imndraws]
273 disp(' ')
274 disp('timend/60 minutes')
275 timeminutes = timend/60
276 nswitch=nswitch/imndraws;
277
278 if IndxGraph
279 %
280 % figure(4)
281 % histpdfg(xdraw(:,1),50,[],[],[]);
282 % figure(5)
283 % plot(xdraw(:,3),xdraw(:,4),'.');
284 % figure(6)
285 % plot(xdraw(:,1),xdraw(:,2),'.');
286 end