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 / skewness.m < prev    next >
Text File  |  1999-04-29  |  2KB  |  60 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: skewness (x)
  21. ##
  22. ## If x is a vector of length N, return the skewness
  23. ##
  24. ##   skewness (x) = N^(-1) std(x)^(-3) SUM_i (x(i)-mean(x))^3
  25. ##
  26. ## of x.
  27. ##
  28. ## If x is a matrix, return a row vector containing the skewness for each
  29. ## column.
  30.  
  31. ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at>
  32. ## Created: 29 July 1994
  33. ## Adapted-By: jwe
  34.  
  35. function retval = skewness (x)
  36.  
  37.   if (nargin != 1)
  38.     usage ("skewness (x)");
  39.   endif
  40.  
  41.   if (is_vec (x))
  42.     x = x - mean (x);
  43.     if (! any (x))
  44.       retval = 0;
  45.     else
  46.       retval = sum (x .^ 3) / (length (x) * std (x) ^ 3);
  47.     endif
  48.   elseif (is_mat (x))
  49.     [nr, nc] = size (x);
  50.     x = x - ones (nr, 1) * mean (x);
  51.     retval = zeros (1, nc);
  52.     s      = std (x);
  53.     ind    = find (s > 0);
  54.     retval (ind) = sum (x (:, ind) .^ 3) ./ (nr * s (ind) .^ 3);
  55.   else
  56.     error ("skewness: x has to be a matrix or a vector.");
  57.   endif
  58.  
  59. endfunction
  60.