Debian Codegraph @

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

raw
1 function [Bh,e,xtx,phi,y] = syed(z,nn)
2 % syed: estimate a system of equations: [Bh,e,xtx,phi,y] = syed(z,nn)
3 % Y((T-lags)*nvar) = XB + u, X: (T-lags)*k, B: k*nvar.
4 % where z is the T*(nvar+ndt) raw data matrix (nvar of variables +
5 % ndt -- number of deterministic terms);
6 % nn is 5 inputs [auindx, ndt, nvar,lags,sample period (total)];
7 % auindx = 0 (no autoregressive) and 1 (autoregressive);
8 % total -- including lags, etc.
9 % Bh: the estimated B; column: nvar; row: [nvar for 1st lag, ...,
10 % nvar for last lag, deterministic terms (ndt)]
11 % e: estimated residual e = y -xBh, (T-lags)*nvar
12 % xtx: X'X
13 % phi: X; column: [nvar for 1st lag, ...,
14 % nvar for last lag, deterministic terms (ndt)]
15 % y: Y
16 %
17 % See also "sye.m".
18 %
19 % Copyright (C) 1997-2012 Tao Zha
20 %
21 % This free software: you can redistribute it and/or modify
22 % it under the terms of the GNU General Public License as published by
23 % the Free Software Foundation, either version 3 of the License, or
24 % (at your option) any later version.
25 %
26 % It is distributed in the hope that it will be useful,
27 % but WITHOUT ANY WARRANTY; without even the implied warranty of
28 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 % GNU General Public License for more details.
30 %
31 % If you did not received a copy of the GNU General Public License
32 % with this software, see <http://www.gnu.org/licenses/>.
33 %
34
35 % ** setup of orders and lengths **
36 auindx = nn(1);
37 ndt = nn(2); % # of deterministic terms including constant
38 nvar = nn(3); % # of endogenous variables
39 lags = nn(4);
40 sp = nn(5); % sample period
41
42 ess = sp-lags; % effective sample size
43 sb = lags+1; % sample beginning
44 sl = sp; % sample last period
45
46 % ** construct X for Y = X*B + U where phi = X **
47 x = z(:,1:nvar);
48 C = z(:,nvar+1:nvar+ndt); % C = [] when ndt=0
49 %
50 if auindx == 0
51 ncoe = ndt; % with deterministic terms
52 phi = zeros(sp,ncoe); % preallocating
53 y = x;
54 else
55 y = x(sb:sl,:);
56 ncoe = nvar*lags + ndt; % with deterministic terms
57 phi = zeros(ess,ncoe); % preallocating
58 for k=1:lags, phi(:,nvar*(k-1)+1:nvar*k) = x(sb-k:sl-k,:); end
59 end
60 %
61 if length(C) == 0
62 phi(:,ncoe-ndt+1:ncoe) = C;
63 else
64 phi(:,ncoe-ndt+1:ncoe) = C(1:sp,:); % perhaps, it should have been be C(sb:sp,:). 2/24/00
65 end
66 %
67 % row: T-lags; column: [nvar for 1st lag, ..., nvar for last lag,
68 % deterministic terms (ndt)]
69 % Thus, # of columns is nvar*lags+ndt = ncoe.
70 % ** estimate: B, XTX, residuals **
71 [u d v]=svd(phi,0); %trial
72 %xtx = phi'*phi; % X'X, k*k (ncoe*ncoe)
73 vd=v.*(ones(size(v,2),1)*diag(d)'); %trial
74 dinv = 1./diag(d); % inv(diag(d))
75 vdinv=v.*(ones(size(v,2),1)*dinv'); %trial
76 xtx=vd*vd';
77 xtxinv = vdinv*vdinv';
78 %xty = phi'*y; % X'Y
79 uy = u'*y; %trial
80 xty = vd*uy; %trial
81 %Bh = xtx\xty; %inv(X'X)*(X'Y), k*m (ncoe*nvar).
82 Bh = xtxinv*xty;
83 %e = y - phi*Bh; % from Y = XB + U, e: (T-lags)*nvar
84 e = y - u*uy;