home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21eb.zip / octave / SCRIPTS.ZIP / scripts / statistics / base / moment.m < prev    next >
Text File  |  1997-02-19  |  2KB  |  62 lines

  1. ## Copyright (C) 1995, 1996, 1997  Kurt Hornik
  2. ## 
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 2, or (at your option)
  6. ## any later version.
  7. ## 
  8. ## This program is distributed in the hope that it will be useful, but
  9. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. ## General Public License for more details. 
  12. ## 
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this file.  If not, write to the Free Software Foundation,
  15. ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16.  
  17. ## usage:  moment (x, p [, opt])
  18. ##
  19. ## Computes the p-th moment of x if it is a vector;  if x is a matrix,
  20. ## return the row vector of the p-th moment of each column.
  21. ##
  22. ## With the optional string opt, the kind of moment to be computed can
  23. ## be specified.  If opt contains `c' or `a', central and/or absolute
  24. ## moments are returned.  I.e., `moment(x, 3, "ac")' computes the third
  25. ## central absolute moment of x.
  26.  
  27. ## Can easily be made to work for continuous distributions (using quad)
  28. ## as well, but how does the general case work?
  29.   
  30. ## Author:  KH <Kurt.Hornik@ci.tuwien.ac.at>
  31. ## Description:  Compute moments
  32.   
  33. function m = moment (x, p, opt)
  34.   
  35.   if ((nargin < 2) || (nargin > 3))
  36.     usage ("moment (x, p [, type]")
  37.   endif
  38.   
  39.   [nr, nc] = size (x);
  40.   if (nr == 0 || nc == 0)
  41.     error ("moment:  x must not be empty");
  42.   elseif (nr == 1)
  43.     x  = reshape (x, nc, 1);
  44.     nr = nc;
  45.   endif
  46.   
  47.   if (nargin == 3)
  48.     tmp = implicit_str_to_num_ok;
  49.     implicit_str_to_num_ok = "true";
  50.     if any (opt == "c")
  51.       x = x - ones (nr, 1) * sum (x) / nr;
  52.     endif
  53.     if any (opt == "a")
  54.       x = abs (x);
  55.     endif
  56.     implicit_str_to_num_ok = tmp;
  57.   endif
  58.   
  59.   m = sum(x .^ p) / nr;
  60.   
  61. endfunction
  62.