Debian Codegraph @

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

raw
1 function [A0gbs, Wcell] = fn_gibbsrvar(A0gbs,UT,nvar,fss,n0,Indxcol)
2 % [A0gbs, Wcell] = fn_gibbsrvar(A0gbs,UT,nvar,fss,n0,Indxcol)
3 % One-step Gibbs sampler for restricted VARs in the structural form (including over-identified cases).
4 % Reference: "A Gibbs sampler for structural VARs" by D.F. Waggoner and T. Zha, ``
5 % Journal of Economic Dynamics & Control (JEDC) 28 (2003) 349-366.
6 % See Note Forecast (2) pp. 44-51, 70-71, and Theorem 1 and Section 3.1 in the WZ JEDC paper.
7 %
8 % A0gbs: the last draw of A0 matrix
9 % UT: cell(nvar,1) -- U_i*T_i in the proof of Theorem 1 where
10 % (1) a_i = U_i*b_i with b_i being a vector of free parameters
11 % (2) T_i (q_i-by-q_i) is from T_i*T_i'= S_i = inv(H0inv{i}/T) where H0inv is the inverse of
12 % the covariance martrix NOT divided by fss and S_i is defined in (14) on p.355 of the WZ JEDC paper.
13 % nvar: rank of A0 or # of variables
14 % fss: effective sample size == nSample (T)-lags+# of dummy observations
15 % n0: nvar-by-1, ith element represents the number of free A0 parameters in ith equation
16 % Indxcol: a row vector indicating random columns this Gibbs draws.
17 % When this input is not supplied, the Gibbs draws all columns
18 %------------------
19 % A0bgs: nvar-by-nvar. New draw of A0 matrix in this Gibbs step
20 % Wcell: cell(nvar,1). In each cell, columns of Wcell{i} form an orthonormal basis w_1,...,w_qi in Section 3.1 in the WZ paper. Added 9/04.
21 %
22 % Written by Tao Zha, August 2000. Revised, September 2004.
23 %
24 %
25 % Copyright (C) 1997-2012 Daniel Waggoner and Tao Zha
26 %
27 % This free software: you can redistribute it and/or modify
28 % it under the terms of the GNU General Public License as published by
29 % the Free Software Foundation, either version 3 of the License, or
30 % (at your option) any later version.
31 %
32 % It is distributed in the hope that it will be useful,
33 % but WITHOUT ANY WARRANTY; without even the implied warranty of
34 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 % GNU General Public License for more details.
36 %
37 % If you did not received a copy of the GNU General Public License
38 % with this software, see <http://www.gnu.org/licenses/>.
39 %
40
41
42 if (nargin==5), Indxcol=[1:nvar]; end
43
44 %---------------- Local loop for Gibbs given last A0gbs ----------
45 Wcell = cell(length(Indxcol),1);
46 w = zeros(nvar,1);
47 for ki=Indxcol % given last A0gbs and generate new A0bgs
48 X = A0gbs; % WZ's Section 4.3
49 X(:,ki) = 0; % want to find non-zero sw s.t., X'*w=0
50
51
52 %*** Solving for w and getting an orthonormal basis. See Theorem 1 and also p.48 in Forecast II
53 [jL,Ux] = lu(X');
54 jIx0 = min(find(abs(diag(Ux))<eps)); % if isempty(jIx0), then something is wrong here
55 w(jIx0+1:end) = 0; % if jIx0+1>end, no effect on w0
56 w(jIx0) = 1;
57 jA = Ux(1:jIx0-1,1:jIx0-1);
58 jb = Ux(1:jIx0-1,jIx0);
59 jy = -jA\jb;
60 w(1:jIx0-1) = jy;
61 % Note: if jIx0=1 (which almost never happens for numerical stability reasons), no effect on w.
62
63 %*** Constructing orthonormal basis w_1, ..., w_qi at each Gibbs step
64 w0 = UT{ki}'*w;
65 w1 = w0/sqrt(sum(w0.^2));
66 [W,jnk] = qr(w1); % Columns of W form an orthonormal basis w_1,...,w_qi in Section 3.1 in the WZ paper
67
68 %*** Draw beta's according to Theorem 1 in the WZ JEDC paper.
69 gkbeta = zeros(n0(ki),1); % qi-by-1: greak beta's
70 jstd = sqrt(1/fss);
71 gkbeta(2:end) = jstd*randn(n0(ki)-1,1); % for beta_2, ..., beta_qi
72 %--- Unnormalized (i.e., not normalized) gamma or 1-d Wishart draw of beta_1 in Theorem 1 of the WZ JEDC paper.
73 jr = jstd*randn(fss+1,1);
74 if rand(1)<0.5
75 gkbeta(1) = sqrt(jr'*jr);
76 else
77 gkbeta(1) = -sqrt(jr'*jr);
78 end
79
80 %*** Getting a new ki_th column in A0
81 A0gbs(:,ki) = UT{ki}*(W*gkbeta); % n-by-1: U_i*T_i*W*beta;
82 Wcell{ki} = W; %q_i-by-1.
83 end