3200
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
3426
|
2 ## |
3922
|
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 |
3200
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
3200
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
13 ## General Public License for more details. |
|
14 ## |
3200
|
15 ## You should have received a copy of the GNU General Public License |
3922
|
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. |
3200
|
19 |
3453
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} moment (@var{x}, @var{p}, @var{opt}) |
|
22 ## If @var{x} is a vector, compute the @var{p}-th moment of @var{x}. |
3200
|
23 ## |
3453
|
24 ## If @var{x} is a matrix, return the row vector containing the |
|
25 ## @var{p}-th moment of each column. |
3200
|
26 ## |
|
27 ## With the optional string opt, the kind of moment to be computed can |
3453
|
28 ## be specified. If opt contains @code{"c"} or @code{"a"}, central |
|
29 ## and/or absolute moments are returned. For example, |
|
30 ## |
|
31 ## @example |
|
32 ## moment (x, 3, "ac") |
|
33 ## @end example |
|
34 ## |
|
35 ## @noindent |
|
36 ## computes the third central absolute moment of @var{x}. |
|
37 ## @end deftypefn |
3200
|
38 |
|
39 ## Can easily be made to work for continuous distributions (using quad) |
|
40 ## as well, but how does the general case work? |
3426
|
41 |
3456
|
42 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
43 ## Description: Compute moments |
3426
|
44 |
3200
|
45 function m = moment (x, p, opt) |
3426
|
46 |
3200
|
47 if ((nargin < 2) || (nargin > 3)) |
3456
|
48 usage ("moment (x, p, type"); |
3200
|
49 endif |
3426
|
50 |
3200
|
51 [nr, nc] = size (x); |
|
52 if (nr == 0 || nc == 0) |
3456
|
53 error ("moment: x must not be empty"); |
3200
|
54 elseif (nr == 1) |
|
55 x = reshape (x, nc, 1); |
|
56 nr = nc; |
|
57 endif |
3426
|
58 |
3200
|
59 if (nargin == 3) |
4452
|
60 tmp = warn_str_to_num; |
|
61 unwind_protect |
|
62 warn_str_to_num = 0; |
|
63 if any (opt == "c") |
|
64 x = x - ones (nr, 1) * sum (x) / nr; |
|
65 endif |
|
66 if any (opt == "a") |
|
67 x = abs (x); |
|
68 endif |
|
69 unwind_protect_cleanup |
|
70 warn_str_to_num = tmp; |
|
71 end_unwind_protect |
3200
|
72 endif |
3426
|
73 |
3200
|
74 m = sum(x .^ p) / nr; |
3426
|
75 |
3200
|
76 endfunction |