Debian Codegraph @

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

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