| 1 |
function [Bh,e,xtx,xty] = fn_ols(y,phi) |
| 2 |
% [Bh,e,xtx,xty] = fn_ols(y,phi) |
| 3 |
% ols: estimate a system of equations: Y(T*nvar) = XB + u, X: T*k, B: k*nvar. |
| 4 |
% |
| 5 |
% y: Y: T-by-nvar |
| 6 |
% phi: X; T-by-k; column: number of r.h.s. variables (including |
| 7 |
%------------ |
| 8 |
% Bh: the estimated B; column: nvar; row: number of r.h.s. variables. |
| 9 |
% e: estimated residual e = y -xBh, T*nvar |
| 10 |
% xtx: X'X: k-by-k |
| 11 |
% xty: X'Y: k-by-nvar |
| 12 |
% deterministic terms)% |
| 13 |
% See also sye.m and syed.m |
| 14 |
|
| 15 |
% |
| 16 |
% Copyright (C) 1997-2012 Tao Zha |
| 17 |
% |
| 18 |
% This free software: you can redistribute it and/or modify |
| 19 |
% it under the terms of the GNU General Public License as published by |
| 20 |
% the Free Software Foundation, either version 3 of the License, or |
| 21 |
% (at your option) any later version. |
| 22 |
% |
| 23 |
% It is distributed in the hope that it will be useful, |
| 24 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 25 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 26 |
% GNU General Public License for more details. |
| 27 |
% |
| 28 |
% If you did not received a copy of the GNU General Public License |
| 29 |
% with this software, see <http://www.gnu.org/licenses/>. |
| 30 |
% |
| 31 |
|
| 32 |
|
| 33 |
% ** setup of orders and lengths ** |
| 34 |
|
| 35 |
[u d v]=svd(phi,0); %trial |
| 36 |
%xtx = phi'*phi; % X'X, k*k (ncoe*ncoe) |
| 37 |
vd=v.*(ones(size(v,2),1)*diag(d)'); %trial |
| 38 |
dinv = 1./diag(d); % inv(diag(d)) |
| 39 |
vdinv=v.*(ones(size(v,2),1)*dinv'); %trial |
| 40 |
xtx=vd*vd'; |
| 41 |
xtxinv = vdinv*vdinv'; |
| 42 |
%xty = phi'*y; % X'Y |
| 43 |
uy = u'*y; %trial |
| 44 |
xty = vd*uy; %trial |
| 45 |
%Bh = xtx\xty; %inv(X'X)*(X'Y), k*m (ncoe*nvar). |
| 46 |
Bh = xtxinv*xty; |
| 47 |
%e = y - phi*Bh; % from Y = XB + U, e: (T-lags)*nvar |
| 48 |
e = y - u*uy; |