function [Bh,e,xtx,xty] = fn_ols(y,phi) % [Bh,e,xtx,xty] = fn_ols(y,phi) % ols: estimate a system of equations: Y(T*nvar) = XB + u, X: T*k, B: k*nvar. % % y: Y: T-by-nvar % phi: X; T-by-k; column: number of r.h.s. variables (including %------------ % Bh: the estimated B; column: nvar; row: number of r.h.s. variables. % e: estimated residual e = y -xBh, T*nvar % xtx: X'X: k-by-k % xty: X'Y: k-by-nvar % deterministic terms)% % See also sye.m and syed.m % ** setup of orders and lengths ** [u d v]=svd(phi,0); %trial %xtx = phi'*phi; % X'X, k*k (ncoe*ncoe) vd=v.*(ones(size(v,2),1)*diag(d)'); %trial dinv = 1./diag(d); % inv(diag(d)) vdinv=v.*(ones(size(v,2),1)*dinv'); %trial xtx=vd*vd'; xtxinv = vdinv*vdinv'; %xty = phi'*y; % X'Y uy = u'*y; %trial xty = vd*uy; %trial %Bh = xtx\xty; %inv(X'X)*(X'Y), k*m (ncoe*nvar). Bh = xtxinv*xty; %e = y - phi*Bh; % from Y = XB + U, e: (T-lags)*nvar e = y - u*uy;