Debian Codegraph @

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

raw
1 function [phi,y,fss,xtx,xty,yty,Bols] = xydata(xdgel,lags,mu)
2 % [phi,y,xtx,xty,yty,Bols,fss] = xydata(xdgel,lags,mu)
3 % Construct matrices X, Y, X'X, etc. so that y = X*B + U, where y is T-by-1, X T-by-ncoef,
4 % and B is ncoef-by-1.
5 %
6 % xdgel: the general matrix of the original data (no manipulation involved)
7 % with sample size including lags
8 % lags: the lag length in the AR(p) process
9 % mu: 2-by-1 vector of hyperparameters for dummy observations (FRA's parameter settings)
10 % mu(1): weight on nvar sums of coeffs dummy observations (unit roots); (5)
11 % mu(2): weight on single dummy initial observation including constant
12 % (cointegration, unit roots, and stationarity); (5)
13 %---------------
14 % phi: X: T-by-ncoef where ncoef=nvar*lags + constant and T=fss
15 % y: Y: T-by-nvar where T=fss
16 % fss: effective sample size (including dummies if mu is provided) exclusing all lags
17 % xtx: X'X (Data)
18 % xty: X'Y (Data)
19 % yty: Y'Y (Data)
20 % Bols: ncoef-by-1: OLS estimate of B
21 %
22 %function [Gb,Sbd,Bh,SpH,fss,ndobs,phi,y,nvar,ncoef,xxhpc,a0indx,na0p,...
23 % idmat0,idmatpp] = szasbvar(idfile,q_m,lags,xdgel,mu)
24 % Now version: [Gb,Sbd,Bh,SpH,fss,ndobs,phi,y,nvar,ncoef,xxhpc,a0indx,na0p,...
25 % idmat0,idmatpp] = szasbvar(idfile,q_m,lags,xdgel,mu)
26 %
27 % Estimating the Bayesian VAR of Sims and Zha with asymmetric prior (as)
28 %
29 % idfile: Identification filename with rows corresponding to equations.
30 % But all the output will be transposed to columns
31 % corresponding to equations.
32 % q_m: quarter or month
33 % lags: the maximum length of lag
34 % nhp: number of haperparameters
35 % xdgel: the general matrix of the original data (no manipulation involved)
36 % with sample size including lags
37 % mu: 6-by-1 vector of hyperparameters (the following numbers for Atlanta Fed's forecast)
38 % mu(1): overall tightness and also for A0; (0.57)
39 % mu(2): relative tightness for A+; (0.13)
40 % mu(3): relative tightness for the constant term; (0.1)
41 % mu(4): tightness on lag decay; (1)
42 % mu(5): weight on nvar sums of coeffs dummy observations (unit roots); (5)
43 % mu(6): weight on single dummy initial observation including constant
44 % (cointegration, unit roots, and stationarity); (5)
45 % --------------------
46 % --------------------
47 % Gb: cell(nvar,1). Each cell, postmultiplied by A0(:,i), is used to compute A+(:,i)
48 % where A+ is k-by-m, where k--ncnoef, m--nvar, if asymmetric prior
49 % Sbd: cell(nvar,1). Sbd=diag(S(1), ..., S(m)). Already divided by 'fss' for the
50 % posterior of a0 or A0(:) in Waggoner and Zha when one has asymmetric prior.
51 % Note,"bd" stands for block diagonal.
52 % Bh: reduced form B, k(mp+1)-by-m, column--equation. Bh=NaN if asymmetric prior.
53 % In the form of y=X*B+U where B=[B1|B2| ...|Bp|C]
54 % where Bi: m-by-m (i=1,..,p--lag length), C: 1-by-ml, constant.
55 % SpH: divided by T, the final S in the Waggoner-Zha exponential part of p(A0|y)
56 % SpH=NaN if asymmetric prior
57 % fss: in-sample-size (for forecasting). Including dummy observations
58 % ndobs: number of dummy observations, ndobs=nvar+1
59 % phi: X in the form of y = X*B+U. Row: nSmaple-lags+ndobs. Column: ncoef
60 % y: y in the form y = X*B+U. Including the dummy observations too,
61 % T-lags+ndobs-by-nvar.
62 % nvar: number of variables
63 % ncoef: number of coefficients in *each* equation. RHS coefficients only, nvar*lags+1
64 % xxhpc: chol(X'X+inv(H_p_tilde)): upper triangular but its transpose
65 % is lower triagular Choleski
66 % a0indx: the index number for free parameters in A0. Column meaning equation
67 % na0p: number of free parameters in A0
68 % idmat0: identification matrix for A0 with asymmetric prior; column -- equation.
69 % idmatpp: asymmetric prior variance for A+ without constant; column -- equation.
70 %
71 % Revisions by CAS 9/27/96: If we transmit idmat, rather than a0indx, we can scale
72 % the elements of idmat0 or idmatp so that it carries information about relative
73 % prior variances.
74 % Revsions by TZ, 10/13/96: Efficiency improvement by streamlining the previous
75 % code according to the general setup in Sims and Zha's IER and according
76 % to Zha's notes Forecast (0) (p.3) and Forecast (1) (p.9).
77 %
78 %
79 % NOTE: "nSample" is something I deleted as an input recently, so it may not
80 % be compatible with old programs, 10/15/98 by TAZ.
81 %
82 % NOTE2: I put "mu" as an input arg and delete "nhp" as an input arg, so it may
83 % not be compatible with old programs, 03/06/99 by TAZ
84 %
85 %
86 % Copyright (C) 1997-2012 Lutz Kilian and Tao Zha
87 %
88 % This free software: you can redistribute it and/or modify
89 % it under the terms of the GNU General Public License as published by
90 % the Free Software Foundation, either version 3 of the License, or
91 % (at your option) any later version.
92 %
93 % It is distributed in the hope that it will be useful,
94 % but WITHOUT ANY WARRANTY; without even the implied warranty of
95 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
96 % GNU General Public License for more details.
97 %
98 % If you did not received a copy of the GNU General Public License
99 % with this software, see <http://www.gnu.org/licenses/>.
100 %
101
102
103 [nSample,nvar]=size(xdgel); % sample size including lags and number of variables
104 ncoef=nvar*lags+1; % number of coefficients in *each* equation, RHS coefficients only,
105 % with only constant
106 ndobs=nvar+1; % number of dummy observations
107 sb = lags+1; % the beginning of original sample without dummies
108
109 if nargin==3 % activate the option of dummy observations
110 fss = nSample+ndobs-lags;
111 % Effective sample size including dummies but exclusing all lags
112
113 %
114 % **** nvar prior dummy observations with the sum of coefficients
115 % ** construct X for Y = X*B + U where phi = X: (T-lags)*k, Y: (T-lags)*nvar
116 % ** columns: k = # of [nvar for 1st lag, ..., nvar for last lag, const]
117 % ** Now, T=T+ndobs -- added with "ndobs" dummy observations
118 %
119 phi = zeros(fss,ncoef);
120 const = ones(fss,1);
121 const(1:nvar) = zeros(nvar,1);
122 % the first nvar periods: no or zero constant (designed for dummies)!
123 phi(:,ncoef) = const;
124 %
125 xdgelint = mean(xdgel(1:lags,:),1); % mean of the first lags initial conditions
126 %**** Dummies for phi
127 for k=1:nvar
128 for m=1:lags
129 phi(ndobs,nvar*(m-1)+k) = xdgelint(k);
130 phi(k,nvar*(m-1)+k) = xdgelint(k);
131 % <<>> multiply hyperparameter later
132 end
133 end
134 %* True data for phi
135 for k=1:lags
136 phi(ndobs+1:fss,nvar*(k-1)+1:nvar*k) = xdgel(sb-k:nSample-k,:);
137 % row: T-lags; column: [nvar for 1st lag, ..., nvar for last lag, const]
138 % Thus, # of columns is nvar*lags+1 = ncoef.
139 end
140 %
141 % ** Y with "ndobs" dummies added
142 y = zeros(fss,nvar);
143 %* Dummies for y
144 for k=1:nvar
145 y(ndobs,k) = xdgelint(k);
146 y(k,k) = xdgelint(k);
147 % multiply hyperparameter later
148 end
149 %* True data for y
150 y(ndobs+1:fss,:) = xdgel(sb:nSample,:);
151 %
152 %*** Dummies once again (finfal version)
153 phi(1:nvar,:) = 1*mu(1)*phi(1:nvar,:); % standard Sims and Zha prior
154 y(1:nvar,:) = mu(1)*y(1:nvar,:); % standard Sims and Zha prior
155 %
156 phi(nvar+1,:) = mu(2)*phi(nvar+1,:);
157 y(nvar+1,:) = mu(2)*y(nvar+1,:);
158 else % dummy observations
159 fss = nSample-lags;
160 % Effective sample size with no dummies but exclusing all lags
161
162 %
163 % **** nvar prior dummy observations with the sum of coefficients
164 % ** construct X for Y = X*B + U where phi = X: (T-lags)*k, Y: (T-lags)*nvar
165 % ** columns: k = # of [nvar for 1st lag, ..., nvar for last lag, const]
166 % ** Now, T=T+ndobs -- added with "ndobs" dummy observations
167 %
168 phi = zeros(fss,ncoef);
169 const = ones(fss,1);
170 phi(:,ncoef) = const;
171 %* True data for phi
172 for k=1:lags
173 phi(1:fss,nvar*(k-1)+1:nvar*k) = xdgel(sb-k:nSample-k,:);
174 % row: T-lags; column: [nvar for 1st lag, ..., nvar for last lag, const]
175 % Thus, # of columns is nvar*lags+1 = ncoef.
176 end
177 %
178 %* True data for y
179 y(1:fss,:) = xdgel(sb:nSample,:);
180 end
181 %*** data input for the posterior density
182 [xq,xr]=qr(phi,0);
183 xtx=xr'*xr; % X'X
184 xty=phi'*y; % X'Y
185 yty = y'*y; % Y'Y
186
187 Bols = xr\(xr'\xty); % inv(X'X)*X'Y
188