home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts.fat / stat / base / ols.m < prev    next >
Text File  |  1999-04-29  |  2KB  |  71 lines

  1. ## Copyright (C) 1996, 1997 John W. Eaton
  2. ##
  3. ## This file is part of Octave.
  4. ##
  5. ## Octave is free software; you can redistribute it and/or modify it
  6. ## under the terms of the GNU General Public License as published by
  7. ## the Free Software Foundation; either version 2, or (at your option)
  8. ## any later version.
  9. ##
  10. ## Octave is distributed in the hope that it will be useful, but
  11. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. ## General Public License for more details.
  14. ##
  15. ## You should have received a copy of the GNU General Public License
  16. ## along with Octave; see the file COPYING.  If not, write to the Free
  17. ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  18. ## 02111-1307, USA.
  19.  
  20. ## usage: [BETA, SIGMA [, R]] = ols (Y, X)
  21. ##
  22. ## Ordinary Least Squares (OLS) estimation for the multivariate model
  23. ##
  24. ##     Y = X*B + E,  mean(E) = 0,  cov(vec(E)) = kron(S,I)
  25. ##
  26. ## with Y ... T x p     As usual, each row of Y and X is an observation
  27. ##      X ... T x k     and each column a variable.
  28. ##      B ... k x p
  29. ##      E ... T x p.
  30. ##
  31. ## BETA is the OLS estimator for B, i.e.
  32. ##
  33. ##   BETA = pinv(X)*Y,
  34. ##
  35. ## where pinv(X) denotes the pseudoinverse of X.
  36. ## SIGMA is the OLS estimator for the matrix S, i.e.
  37. ##
  38. ##   SIGMA = (Y - X*BETA)'*(Y - X*BETA) / (T - rank(X)).
  39. ##
  40. ## R = Y - X*BETA is the matrix of OLS residuals.
  41.  
  42. ## Author: Teresa Twaroch <twaroch@ci.tuwien.ac.at>
  43. ## Created: May 1993
  44. ## Adapted-By: jwe
  45.  
  46. function [BETA, SIGMA, R] = ols (Y, X)
  47.  
  48.   if (nargin != 2)
  49.     error("usage : [BETA, SIGMA [, R]] = ols (Y, X)");
  50.   endif
  51.  
  52.   [nr, nc] = size (X);
  53.   [ry, cy] = size (Y);
  54.   if (nr != ry)
  55.     error ("ols: incorrect matrix dimensions");
  56.   endif
  57.  
  58.   Z = X' * X;
  59.   r = rank (Z);
  60.  
  61.   if (r == nc)
  62.     BETA = inv (Z) * X' * Y;
  63.   else
  64.     BETA = pinv (X) * Y;
  65.   endif
  66.  
  67.   R = Y - X * BETA;
  68.   SIGMA = R' * R / (nr - r);
  69.  
  70. endfunction
  71.