Debian Codegraph @

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

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