Debian Codegraph @

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

raw
1 function A0gbs = gibbsvar(A0gbs,cT,vR,nvar,fss,kdf,Indxcol)
2 % A0gbs = gibbsvar(A0gbs,cT,vR,nvar,fss,kdf)
3 % One-step Gibbs sampler for structural VARs -- simultaneous equations approach
4 % Ref.: D.F. Waggoner and T. Zha: "Does Normalization Matter for Inference?"
5 % See Note Forecast (2) pp. 44-51
6 %
7 % A0gbs: the last draw of A0 matrix
8 % cT{i}: nvar-by-nvar where T'*T=Sbd{i} which is kind of covariance martrix
9 % divided by fss already
10 % vR{i}: nvar-by-q{i} -- orthonormral basis for T*R, which is obtained through
11 % single value decomposition of Q*inv(T). See gibbsglb.m
12 % nvar: rank of A0 or # of variables
13 % fss: effective sample size == nSample (T)-lags+# of dummy observations
14 % kdf: polynomial power in the Gamma or 1d Wishart distribution
15 % Indxcol: a vector of random columns this Gibbs draws.
16 % When this input is not supplied, the Gibbs draws all columns
17 %------------------
18 % A0bgs: new draw of A0 matrix in this Gibbs step
19 %
20 % NOTE: Added Indxcol on 2/13/00 so that previous programs may not be compatible.
21 %
22 %
23 % Copyright (C) 1997-2012 Daniel Waggoner and Tao Zha
24 %
25 % This free software: you can redistribute it and/or modify
26 % it under the terms of the GNU General Public License as published by
27 % the Free Software Foundation, either version 3 of the License, or
28 % (at your option) any later version.
29 %
30 % It is distributed in the hope that it will be useful,
31 % but WITHOUT ANY WARRANTY; without even the implied warranty of
32 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 % GNU General Public License for more details.
34 %
35 % If you did not received a copy of the GNU General Public License
36 % with this software, see <http://www.gnu.org/licenses/>.
37 %
38
39 if (nargin==6), Indxcol=[1:nvar]; end
40
41 %---------------- Local loop for Gibbs given last A0gbs ----------
42 %* uR{i}: nvar-by-q{i} -- orthonormal with first q(i)-1 vectors lies in the
43 % span(T*a(j)|j~=i)
44 %*** Constructing u(1),...,u(q{i}) at each Gibbs step
45 %
46 sw0 = zeros(nvar,1);
47 for k=Indxcol % given last A0gbs and general new A0bgs
48 X = cT{k}*A0gbs; % given the latest updated A0gbs
49 X(:,k) = 0; % want to find non-zero sw s.t., X'*sw=0
50 [jL,Ux] = lu(X');
51 jIx0 = min(find(abs(diag(Ux))<eps)); % if isempty(jIx0), then something is wrong here
52 %
53 sw0(jIx0+1:end) = 0;
54 sw0(jIx0) = 1;
55 jA = Ux(1:jIx0-1,1:jIx0-1);
56 jb = Ux(1:jIx0-1,jIx0);
57 jy = -jA\jb;
58 sw0(1:jIx0-1) = jy;
59 sw = sw0/sqrt(sum(sw0.^2));
60 %
61 lenk = length(vR{k}(1,:));
62 gkb = zeros(lenk,1); % greek beta's
63 uR = zeros(nvar,lenk);
64 sx = zeros(nvar,lenk);
65 sx(:,1) = vR{k}(:,1);
66 for ki = 1:lenk-1
67 wxv = [sw'*sx(:,ki);sw'*vR{k}(:,ki+1)]; % w'*x and w'*v(+1)
68 dwxv = sqrt(sum(wxv.^2));
69 if (dwxv<eps)
70 uR(:,ki)=sx(:,ki); sx(:,ki+1)=vR{k}(:,ki+1);
71 else
72 wxv = wxv/dwxv;
73 uR(:,ki) = wxv(1)*vR{k}(:,ki+1) - wxv(2)*sx(:,ki);
74 sx(:,ki+1) = wxv(2)*vR{k}(:,ki+1) + wxv(1)*sx(:,ki);
75 end
76 end
77 uR(:,lenk) = sx(:,lenk); % uR now constructed
78 %
79 %--------- Gibbs loop ----------
80 %*** draw independently beta's that combine uR to form a's (columns of A0)
81 jcon = sqrt(1/fss);
82 gkb(1:lenk-1) = jcon*randn(lenk-1,1);
83 %* gamma or 1-d Wishart draw
84 jnk = jcon*randn(kdf+1,1);
85 if rand(1)<0.5
86 gkb(lenk) = sqrt(jnk'*jnk);
87 else
88 gkb(lenk) = -sqrt(jnk'*jnk);
89 end
90 %
91 %*** form new a(i) - ith column of A0
92 A0gbs(:,k) = (cT{k}\uR)*gkb;
93 end