Debian Codegraph @

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

raw
1 function [Avhx,AvhxD,hAvhx,hAvhxD,cJump,hAvhh] = smtplis2(Avhx,AvhxD,hAvhx,hAvhxD,...
2 A0xhat,tdf,cJump,scf,H_sr,nfp,Sbd,fss,nvar,a0indx,hAvhh)
3 % [Avhx,AvhxD,hAvhx,hAvhxD,cJump,hAvhh] = smtplis2(Avhx,AvhxD,hAvhx,hAvhxD,...
4 % A0xhat,tdf,cJump,scf,H_sr,nfp,Sbd,fss,nvar,a0indx,hAvhh)
5 % Straight Metropolis Algorithm for identified VARs with 2 parallel sequences
6 %
7 % Avhx: previous draw of parameter x in 1st (kept) sequence
8 % AvhxD: previous draw of parameter x in 2nd (discarded) sequence
9 % hAvhx: lh value of previous draw in 1st (kept) sequence
10 % hAvhxD: lh vlaue of previous draw in 2nd (discarded) sequence
11 % A0xhat: ML estimate of A0
12 % tdf: degrees of freedom of the jump t-distribution
13 % cJump: old count for times of jumping
14 % scf: scale down factor for stand. dev. -- square root of covariance matrix H
15 % H_sr: square root of covariance matrix H
16 % nfp: number of free parameters
17 % Sbd: S in block diagonal covariance matrix in asymmetric prior case
18 % fss: effective sample size
19 % nvar: number of variables
20 % a0indx: index of locations of free elements in A0
21 % hAvhh: highest point of lh
22 %--------------
23 % Avhx: new draw of parameter x in 1st (kept) sequence
24 % AvhxD: new draw of parameter x in 2nd (discarded) sequence
25 % hAvhx: lh value of new draw in 1st (kept) sequence
26 % AvhxD: lh vlaue of new draw in 2nd (discarded) sequence
27 % cJump: new count for times of jumping
28 % hAvhh: highest point of lh
29 %
30 % November 1998 by Tao Zha
31 %
32 % Copyright (C) 1997-2012 Tao Zha
33 %
34 % This free software: you can redistribute it and/or modify
35 % it under the terms of the GNU General Public License as published by
36 % the Free Software Foundation, either version 3 of the License, or
37 % (at your option) any later version.
38 %
39 % It is distributed in the hope that it will be useful,
40 % but WITHOUT ANY WARRANTY; without even the implied warranty of
41 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 % GNU General Public License for more details.
43 %
44 % If you did not received a copy of the GNU General Public License
45 % with this software, see <http://www.gnu.org/licenses/>.
46 %
47
48 if ~(fix(tdf)-tdf==0)
49 warning('tdf in msstart.m must be integer for drawing chi^2 from normal')
50 disp('press ctrl-c to abort')
51 pause
52 end
53
54 for k=1:2 % parallel sequences; 2nd to be discarded
55 %** draw free elements Avh in A0 and hyperparameters from t-dist
56 %gsg = gamrnd(ga,gb); % G-sigma from Gamma draw
57 %Avhz = (1/sqrt(gsg))*(scf*H_sr*randn(nfp,1));
58 % scf is used to control the acceptance ratio -- countJump
59 Avhz1 = scf*H_sr*randn(nfp,1); % normal draws
60 %Avhz1 = 2*randn(nfp,1); % normal draws
61 csq=randn(tdf,1);
62 csq=sum(csq .* csq);
63 Avhz = Avhz1/sqrt(csq/tdf);
64
65 if (k==1)
66 Avhy = Avhx + Avhz; % random walk chain -- Metropolis
67 else
68 Avhy = AvhxD + Avhz; % random walk chain -- Metropolis
69 end
70
71 % ** actual density, having taken log
72 hAvhy = a0asfun(Avhy,Sbd,fss,nvar,a0indx);
73 hAvhy = -hAvhy; % converted to logLH
74
75 if (k==1)
76 mphxy = exp(hAvhy-hAvhx);
77 else
78 mphxy = exp(hAvhy-hAvhxD);
79 end
80
81 %** draw u from uniform(0,1)
82 u = rand(1);
83 Jump = min([mphxy 1]);
84 if u <= Jump
85 %** Normalization: get the point that is nearest to axhat or A0xhat
86 Atem=zeros(nvar);
87 Atem(a0indx) = Avhy;
88 Adiff = (Atem - A0xhat).^2;
89 % distance that may be far from axhat or A0xhat
90 Adiffn = (-Atem - A0xhat).^2;
91 % distance by chaning the sign of Atem
92 cAdiff = sum(Adiff); % each column summed up
93 cAdiffn = sum(Adiffn); % each column summed up
94 cAindx = find(cAdiffn<cAdiff); % index for shorter distance
95 Atemn = Atem;
96 Atemn(:,cAindx) = -Atem(:,cAindx);
97 % find the shortest or nearest distance
98 %** get the value of logPoster or logLH
99 Avhy = Atemn(a0indx);
100 %
101
102 if (k==1)
103 Avhx = Avhy;
104 hAvhx = hAvhy;
105 if hAvhy > hAvhh
106 hAvhh = hAvhy;
107 Avhh = Avhy;
108 end
109 cJump = cJump+1;
110 else
111 AvhxD = Avhy;
112 hAvhxD = hAvhy;
113 end
114 end
115 end