Debian Codegraph @

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

raw
1 function [xtx,xty,yty,fss,phi,y,ncoef,xr,Bh,e] = dataxy(nvar,lags,z,mu,indxDummy,nexo)
2 % [xtx,xty,yty,fss,phi,y,ncoef,xr,Bh,e] = dataxy(nvar,lags,z,mu,indxDummy,nexo)
3 %
4 % Export arranged data matrices for future estimation for DM models.
5 % See Wagonner and Zha's Gibbs sampling paper.
6 %
7 % nvar: number of endogenous variables.
8 % lags: the maximum length of lag
9 % nhp: number of haperparameters
10 % z: T*(nvar+(nexo-1)) matrix of raw or original data (no manipulation involved)
11 % with sample size including lags.
12 % mu: 6-by-1 vector of hyperparameters (the following numbers for Atlanta Fed's forecast)
13 % mu(1): overall tightness and also for A0; (0.57)
14 % mu(2): relative tightness for A+; (0.13)
15 % mu(3): relative tightness for the constant term; (0.1)
16 % mu(4): tightness on lag decay; (1)
17 % mu(5): weight on nvar sums of coeffs dummy observations (unit roots); (5)
18 % mu(6): weight on single dummy initial observation including constant
19 % (cointegration, unit roots, and stationarity); (5)
20 % Here, only mu(5) and mu(6) are used for dummy observations
21 % indxDummy = 1; % 1: add dummy observations to the data; 0: no dummy added.
22 % nexo: number of exogenous variables. Besides constant terms, we have nexo-1 exogenous variables.
23 % -------------------
24 % xtx: X'X: k-by-k where k=ncoef
25 % xty: X'Y: k-by-nvar
26 % yty: Y'Y: nvar-by-nvar
27 % fss: T: sample size excluding lags. With dummyies, fss=nSample-lags+ndobs.
28 % phi: X; T-by-k; column: [nvar for 1st lag, ..., nvar for last lag, const]
29 % y: Y: T-by-nvar where T=fss
30 % ncoef: number of coefficients in *each* equation. RHS coefficients only, nvar*lags+ncoef
31 % xr: the economy size (ncoef-by-ncoef) in qr(phi) so that xr=chol(X'*X) or xr'*xr=X'*X
32 % Bh: ncoef-by-nvar estimated reduced-form parameter; column: nvar;
33 % row: ncoef=[nvar for 1st lag, ..., nvar for last lag, exogenous variables, const]
34 % e: estimated residual e = y -x*Bh, T-by-nvar
35 %
36 % Tao Zha, February 2000
37 %
38 % Copyright (C) 1997-2012 Tao Zha
39 %
40 % This free software: you can redistribute it and/or modify
41 % it under the terms of the GNU General Public License as published by
42 % the Free Software Foundation, either version 3 of the License, or
43 % (at your option) any later version.
44 %
45 % It is distributed in the hope that it will be useful,
46 % but WITHOUT ANY WARRANTY; without even the implied warranty of
47 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 % GNU General Public License for more details.
49 %
50 % If you did not received a copy of the GNU General Public License
51 % with this software, see <http://www.gnu.org/licenses/>.
52 %
53
54
55 if nargin == 5
56 nexo=1; % default for constant term
57 elseif nexo<1
58 error('We need at least one exogenous term so nexo must >= 1')
59 end
60
61 %*** original sample dimension without dummy prior
62 nSample = size(z,1); % the sample size (including lags, of course)
63 sb = lags+1; % original beginning without dummies
64 ncoef = nvar*lags+nexo; % number of coefficients in *each* equation, RHS coefficients only.
65
66 if indxDummy % prior dummy prior
67
68 %*** expanded sample dimension by dummy prior
69 ndobs=nvar+1; % number of dummy observations
70 fss = nSample+ndobs-lags;
71
72 %
73 % **** nvar prior dummy observations with the sum of coefficients
74 % ** construct X for Y = X*B + U where phi = X: (T-lags)*k, Y: (T-lags)*nvar
75 % ** columns: k = # of [nvar for 1st lag, ..., nvar for last lag, exo var, const]
76 % ** Now, T=T+ndobs -- added with "ndobs" dummy observations
77 %
78 phi = zeros(fss,ncoef);
79 %* constant term
80 const = ones(fss,1);
81 const(1:nvar) = zeros(nvar,1);
82 phi(:,ncoef) = const; % the first nvar periods: no or zero constant!
83 %* other exogenous (than) constant term
84 phi(ndobs+1:end,ncoef-nexo+1:ncoef-1) = z(lags+1:end,nvar+1:nvar+nexo-1);
85 exox = zeros(ndobs,nexo);
86 phi(1:ndobs,ncoef-nexo+1:ncoef-1) = exox(:,1:nexo-1);
87 % this = [] when nexo=1 (no other exogenous than constant)
88
89 xdgel = z(:,1:nvar); % endogenous variable matrix
90 xdgelint = mean(xdgel(1:lags,:),1); % mean of the first lags initial conditions
91 %* Dummies
92 for k=1:nvar
93 for m=1:lags
94 phi(ndobs,nvar*(m-1)+k) = xdgelint(k);
95 phi(k,nvar*(m-1)+k) = xdgelint(k);
96 % <<>> multiply hyperparameter later
97 end
98 end
99 %* True data
100 for k=1:lags
101 phi(ndobs+1:fss,nvar*(k-1)+1:nvar*k) = xdgel(sb-k:nSample-k,:);
102 % row: T-lags; column: [nvar for 1st lag, ..., nvar for last lag, exo var, const]
103 % Thus, # of columns is nvar*lags+nexo = ncoef.
104 end
105 %
106 % ** Y with "ndobs" dummies added
107 y = zeros(fss,nvar);
108 %* Dummies
109 for k=1:nvar
110 y(ndobs,k) = xdgelint(k);
111 y(k,k) = xdgelint(k);
112 % multiply hyperparameter later
113 end
114 %* True data
115 y(ndobs+1:fss,:) = xdgel(sb:nSample,:);
116
117 phi(1:nvar,:) = 1*mu(5)*phi(1:nvar,:); % standard Sims and Zha prior
118 y(1:nvar,:) = mu(5)*y(1:nvar,:); % standard Sims and Zha prior
119 phi(nvar+1,:) = mu(6)*phi(nvar+1,:);
120 y(nvar+1,:) = mu(6)*y(nvar+1,:);
121
122 [xq,xr]=qr(phi,0);
123 xtx=xr'*xr;
124 xty=phi'*y;
125 [yq,yr]=qr(y,0);
126 yty=yr'*yr;
127 Bh = xr\(xr'\xty); % xtx\xty where inv(X'X)*(X'Y)
128 e=y-phi*Bh;
129 else
130 fss = nSample-lags;
131 %
132 % ** construct X for Y = X*B + U where phi = X: (T-lags)*k, Y: (T-lags)*nvar
133 % ** columns: k = # of [nvar for 1st lag, ..., nvar for last lag, exo var, const]
134 %
135 phi = zeros(fss,ncoef);
136 %* constant term
137 const = ones(fss,1);
138 phi(:,ncoef) = const; % the first nvar periods: no or zero constant!
139 %* other exogenous (than) constant term
140 phi(:,ncoef-nexo+1:ncoef-1) = z(lags+1:end,nvar+1:nvar+nexo-1);
141 % this = [] when nexo=1 (no other exogenous than constant)
142
143 xdgel = z(:,1:nvar); % endogenous variable matrix
144 %* True data
145 for k=1:lags
146 phi(:,nvar*(k-1)+1:nvar*k) = xdgel(sb-k:nSample-k,:);
147 % row: T-lags; column: [nvar for 1st lag, ..., nvar for last lag, exo var, const]
148 % Thus, # of columns is nvar*lags+nexo = ncoef.
149 end
150 %
151 y = xdgel(sb:nSample,:);
152
153 [xq,xr]=qr(phi,0);
154 xtx=xr'*xr;
155 xty=phi'*y;
156 [yq,yr]=qr(y,0);
157 yty=yr'*yr;
158 Bh = xr\(xr'\xty); % xtx\xty where inv(X'X)*(X'Y)
159 e=y-phi*Bh;
160 end