3200
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
3426
|
2 ## |
3200
|
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. |
3426
|
7 ## |
3200
|
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 |
3426
|
11 ## General Public License for more details. |
|
12 ## |
3200
|
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 |
3453
|
17 ## -*- texinfo -*- |
|
18 ## @deftypefn {Function File} {} moment (@var{x}, @var{p}, @var{opt}) |
|
19 ## If @var{x} is a vector, compute the @var{p}-th moment of @var{x}. |
3200
|
20 ## |
3453
|
21 ## If @var{x} is a matrix, return the row vector containing the |
|
22 ## @var{p}-th moment of each column. |
3200
|
23 ## |
|
24 ## With the optional string opt, the kind of moment to be computed can |
3453
|
25 ## be specified. If opt contains @code{"c"} or @code{"a"}, central |
|
26 ## and/or absolute moments are returned. For example, |
|
27 ## |
|
28 ## @example |
|
29 ## moment (x, 3, "ac") |
|
30 ## @end example |
|
31 ## |
|
32 ## @noindent |
|
33 ## computes the third central absolute moment of @var{x}. |
|
34 ## @end deftypefn |
3200
|
35 |
|
36 ## Can easily be made to work for continuous distributions (using quad) |
|
37 ## as well, but how does the general case work? |
3426
|
38 |
3200
|
39 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
40 ## Description: Compute moments |
3426
|
41 |
3200
|
42 function m = moment (x, p, opt) |
3426
|
43 |
3200
|
44 if ((nargin < 2) || (nargin > 3)) |
|
45 usage ("moment (x, p [, type]") |
|
46 endif |
3426
|
47 |
3200
|
48 [nr, nc] = size (x); |
|
49 if (nr == 0 || nc == 0) |
|
50 error ("moment: x must not be empty"); |
|
51 elseif (nr == 1) |
|
52 x = reshape (x, nc, 1); |
|
53 nr = nc; |
|
54 endif |
3426
|
55 |
3200
|
56 if (nargin == 3) |
|
57 tmp = implicit_str_to_num_ok; |
|
58 implicit_str_to_num_ok = "true"; |
|
59 if any (opt == "c") |
|
60 x = x - ones (nr, 1) * sum (x) / nr; |
|
61 endif |
|
62 if any (opt == "a") |
|
63 x = abs (x); |
|
64 endif |
|
65 implicit_str_to_num_ok = tmp; |
|
66 endif |
3426
|
67 |
3200
|
68 m = sum(x .^ p) / nr; |
3426
|
69 |
3200
|
70 endfunction |