Debian Codegraph @

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

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