Debian Codegraph @

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

raw
1 function [Bh,e,xtx,xty,phi,y,ncoe,xr] = sye(z,lags)
2 % Now [Bh,e,xtx,xty,phi,y,ncoe,xr] = sye(z,lags)
3 % Old: [Bh,e,xtx,xty,phi,y,ncoe,Sigu,xtxinv] = sye(z,lags)
4 %
5 % Estimate a system of equations in the form of y(T*nvar) = XB + u,
6 % X--phi: T*k, B: k*nvar; where T=sp-lags, k=ncoe,
7 %
8 % z: (T+lags)-by-(nvar+1) raw data matrix (nvar of variables + constant).
9 % lags: number of lags
10 %--------------------
11 % Bh: k-by-nvar estimated reduced-form parameter; column: nvar;
12 % row: k=ncoe=[nvar for 1st lag, ..., nvar for last lag, const]
13 % e: estimated residual e = y -xBh, T-by-nvar
14 % xtx: X'X: k-by-k
15 % xty: X'Y: k-by-nvar
16 % phi: X; T-by-k; column: [nvar for 1st lag, ..., nvar for last lag, const]
17 % y: Y: T-by-nvar
18 % ncoe: number of coeffcients per equation: nvar*lags + 1
19 % xr: the economy size (k-by-k) in qr(phi) so that xr=chol(X'*X)
20 % Sigu: e'*e: nvar-by-nvar. Note, not divided (undivided) by "nobs"
21 % xtxinv: inv(X'X): k-by-k
22 %
23 % See also syed.m (allowing for more predetermined terms) which has not
24 % been yet updated as "sye.m".
25 %
26 % Note, "lags" is something I changed recently, so it may not be compatible
27 % with old programs, 10/15/98 by TAZ.
28 %
29 % Revised, 5/2/99. Replaced outputs Sigu and xtxinv with xr so that previous
30 % programs may be incompatible.
31
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 % ** setup of orders and lengths **
50 [sp,nvar] = size(z); % sp: sample period T include lags
51 nvar = nvar-1; % -1: takes out the counting of constant
52
53 ess = sp-lags; % effective sample size
54 sb = lags+1; % sample beginning
55 sl = sp; % sample last period
56 ncoe = nvar*lags + 1; % with constant
57
58 % ** construct X for Y = X*B + U where phi = X **
59 x = z(:,1:nvar);
60 C = z(:,nvar+1);
61 phi = zeros(ess,ncoe);
62 phi(:,ncoe) = C(1:ess);
63 for k=1:lags, phi(:,nvar*(k-1)+1:nvar*k) = x(sb-k:sl-k,:); end
64 % row: T-lags; column: [nvar for 1st lag, ..., nvar for last lag, const]
65 % Thus, # of columns is nvar*lags+1 = ncoef.
66 % ** estimate: B, XTX, residuals **
67 y = x(sb:sl,:);
68 %
69 %**** The following, though stable, is too slow *****
70 % [u d v]=svd(phi,0); %trial
71 % %xtx = phi'*phi; % X'X, k*k (ncoe*ncoe)
72 % vd=v.*(ones(size(v,2),1)*diag(d)'); %trial
73 % dinv = 1./diag(d); % inv(diag(d))
74 % vdinv=v.*(ones(size(v,2),1)*dinv'); %trial
75 % xtx=vd*vd';
76 % xtxinv = vdinv*vdinv';
77 % %xty = phi'*y; % X'Y
78 % uy = u'*y; %trial
79 % xty = vd*uy; %trial
80 % %Bh = xtx\xty; %inv(X'X)*(X'Y), k*m (ncoe*nvar).
81 % Bh = xtxinv*xty;
82 % %e = y - phi*Bh; % from Y = XB + U, e: (T-lags)*nvar
83 % e = y - u*uy;
84 %**** The following, though stable, is too slow *****
85
86 %===== (Fast but perhaps less accurate) alternative to the above =========
87 [xq,xr]=qr(phi,0);
88 xtx=xr'*xr;
89 xty=phi'*y;
90 Bh = xr\(xr'\xty);
91 e=y-phi*Bh;
92 %===== (Fast but perhaps less accurate) alternative to the above =========
93
94
95 %* not numerically stable way of computing e'*e
96 %Sigu = y'*y-xty'*Bh;
97 %Sigu = y'*(eye(ess)-phi*xtxinv*phi')*y; % probablly better way, commented out
98 % by TZ, 2/28/98. See following
99 %Sigu = y'*(eye(ess)-u*u')*y; % I think this is the best, TZ, 2/28/98
100 % Note, u*u'=x*inv(x'x)*x.