Debian Codegraph @

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

raw
1 function A0gbs = fn_gibbsrvar(A0gbs,UT,nvar,fss,n0,Indxcol)
2 % A0gbs = fn_gibbsrvar(A0gbs,UT,nvar,fss,n0,Indxcol)
3 % One-step Gibbs sampler for restricted VARs in the structural form
4 % Ref.: D.F. Waggoner and T. Zha: ``A Gibbs sampler for structural VARs''
5 % See Note Forecast (2) pp. 44-51 and Theorem 1 and Section 3 in the WZ paper.
6 %
7 % A0gbs: the last draw of A0 matrix
8 % UT: cell(nvar,1) -- U_i*T_i in the proof of Theorem 1 where
9 % (1) a_i = U_i*b_i with b_i being a vector of free parameters
10 % (2) T_i (q_i-by-q_i) is from T_i*T_i'=H0inv{i}/T. Note that H0inv is the inverse of
11 % the covariance martrix NOT divided by fss. See Theorem 1.
12 % nvar: rank of A0 or # of variables
13 % fss: effective sample size == nSample (T)-lags+# of dummy observations
14 % n0: nvar-by-1, ith element represents the number of free A0 parameters in ith equation
15 % Indxcol: a row vector indicating 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 % Written by Tao Zha, August 2000
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==5), Indxcol=[1:nvar]; end
40
41 %---------------- Local loop for Gibbs given last A0gbs ----------
42 %
43 w = zeros(nvar,1);
44 for ki=Indxcol % given last A0gbs and generate new A0bgs
45 X = A0gbs; % WZ's Section 4.3
46 X(:,ki) = 0; % want to find non-zero sw s.t., X'*w=0
47
48
49 %*** Solving for w and getting an orthonormal basis. See Theorem 1 and also p.48 in Forecast II
50 [jL,Ux] = lu(X');
51 jIx0 = min(find(abs(diag(Ux))<eps)); % if isempty(jIx0), then something is wrong here
52 w(jIx0+1:end) = 0; % if jIx0+1>end, no effect on w0
53 w(jIx0) = 1;
54 jA = Ux(1:jIx0-1,1:jIx0-1);
55 jb = Ux(1:jIx0-1,jIx0);
56 jy = -jA\jb;
57 w(1:jIx0-1) = jy;
58 % Note: if jIx0=1 (which almost never happens for numerical stability reasons), no effect on w.
59
60 %*** Constructing orthonormal basis w_1, ..., w_qi at each Gibbs step
61 w0 = UT{ki}'*w;
62 w1 = w0/sqrt(sum(w0.^2));
63 [W,jnk] = qr(w1); % columns of W form an orthonormal basis w_1,...,w_qi in Section 4.2 in the WZ paper
64
65 %*** Draw beta's in Theorem 1
66 gkbeta = zeros(n0(ki),1); % qi-by-1: greak beta's
67 jstd = sqrt(1/fss);
68 gkbeta(2:end) = jstd*randn(n0(ki)-1,1); % for beta_2, ..., beta_qi
69 %--- Unnormalized (i.e., not normalized) gamma or 1-d Wishart draw of beta_1 in Theorem 1.
70 %* gamma or 1-d Wishart draw of beta_1
71 jr = jstd*randn(fss+1,1);
72 if rand(1)<0.5
73 gkbeta(1) = sqrt(jr'*jr);
74 else
75 gkbeta(1) = -sqrt(jr'*jr);
76 end
77
78 %*** Getting a new ki_th column in A0
79 A0gbs(:,ki) = UT{ki}*(W*gkbeta); % n-by-1
80 end