annotate scripts/statistics/base/moment.m @ 19630:0e1f5a750d00

maint: Periodic merge of gui-release to default.
author John W. Eaton <jwe@octave.org>
date Tue, 20 Jan 2015 10:24:46 -0500
parents 9ac2357f19bc 446c46af4b42
children 4197fc428c7d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17744
d63878346099 maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents: 17281
diff changeset
1 ## Copyright (C) 1995-2013 Kurt Hornik
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
2 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
3 ## This file is part of Octave.
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
4 ##
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
8 ## your option) any later version.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
9 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
13 ## General Public License for more details.
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
14 ##
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
17 ## <http://www.gnu.org/licenses/>.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
18
3453
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
19 ## -*- texinfo -*-
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
20 ## @deftypefn {Function File} {} moment (@var{x}, @var{p})
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
21 ## @deftypefnx {Function File} {} moment (@var{x}, @var{p}, @var{type})
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
22 ## @deftypefnx {Function File} {} moment (@var{x}, @var{p}, @var{dim})
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
23 ## @deftypefnx {Function File} {} moment (@var{x}, @var{p}, @var{type}, @var{dim})
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
24 ## @deftypefnx {Function File} {} moment (@var{x}, @var{p}, @var{dim}, @var{type})
17873
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
25 ## Compute the @var{p}-th central moment of the vector @var{x}.
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
26 ##
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
27 ## @tex
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
28 ## $$
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
29 ## {\sum_{i=1}^N (x_i - \bar{x})^p \over N}
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
30 ## $$
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
31 ## @end tex
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
32 ## @ifnottex
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
33 ##
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
34 ## @example
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
35 ## @group
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
36 ## 1/N SUM_i (x(i) - mean(x))^p
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
37 ## @end group
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
38 ## @end example
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
39 ##
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
40 ## @end ifnottex
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17909
diff changeset
41 ##
17873
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
42 ## If @var{x} is a matrix, return the row vector containing the @var{p}-th
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
43 ## central moment of each column.
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
44 ##
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
45 ## The optional string @var{type} specifies the type of moment to be computed.
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
46 ## Valid options are:
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
47 ##
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
48 ## @table @asis
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
49 ## @item @qcode{"c"}
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17909
diff changeset
50 ## Central Moment (default).
17873
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
51 ##
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
52 ## @item @qcode{"a"}
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
53 ## @itemx @qcode{"ac"}
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
54 ## Absolute Central Moment. The moment about the mean ignoring sign
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
55 ## defined as
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
56 ## @tex
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
57 ## $$
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
58 ## {\sum_{i=1}^N {\left| x_i - \bar{x} \right|}^p \over N}
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
59 ## $$
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
60 ## @end tex
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
61 ## @ifnottex
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
62 ##
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
63 ## @example
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
64 ## @group
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
65 ## 1/N SUM_i (abs (x(i) - mean(x)))^p
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
66 ## @end group
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
67 ## @end example
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
68 ##
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
69 ## @end ifnottex
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
70 ##
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
71 ## @item @qcode{"r"}
19627
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17909
diff changeset
72 ## Raw Moment. The moment about zero defined as
446c46af4b42 strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents: 17909
diff changeset
73 ##
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
74 ## @tex
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
75 ## $$
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
76 ## {\rm moment} (x) = { \sum_{i=1}^N {x_i}^p \over N }
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
77 ## $$
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
78 ## @end tex
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
79 ## @ifnottex
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
80 ##
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
81 ## @example
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
82 ## @group
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
83 ## moment (x) = 1/N SUM_i x(i)^p
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
84 ## @end group
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
85 ## @end example
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
86 ##
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
87 ## @end ifnottex
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
88 ##
17909
88c37cf66fb5 moment.m: Add @nospell macro around "ar" so spellcheck passes.
Rik <rik@octave.org>
parents: 17895
diff changeset
89 ## @item @nospell{@qcode{"ar"}}
17873
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
90 ## Absolute Raw Moment. The moment about zero ignoring sign defined as
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
91 ## @tex
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
92 ## $$
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
93 ## {\sum_{i=1}^N {\left| x_i \right|}^p \over N}
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
94 ## $$
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
95 ## @end tex
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
96 ## @ifnottex
3453
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
97 ##
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
98 ## @example
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
99 ## @group
14327
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
100 ## 1/N SUM_i ( abs (x(i)) )^p
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
101 ## @end group
3453
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
102 ## @end example
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
103 ##
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
104 ## @end ifnottex
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
105 ## @end table
18851
9ac2357f19bc doc: Replace "non-zero" with "nonzero" to match existing usage.
Rik <rik@octave.org>
parents: 17909
diff changeset
106 ##
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
107 ## If the optional argument @var{dim} is given, operate along this dimension.
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
108 ##
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
109 ## If both @var{type} and @var{dim} are given they may appear in any order.
12575
d0b799dafede Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents: 11595
diff changeset
110 ## @seealso{var, skewness, kurtosis}
3453
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
111 ## @end deftypefn
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
112
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
113 ## Can easily be made to work for continuous distributions (using quad)
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
114 ## as well, but how does the general case work?
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
115
5428
2a16423e4aa0 [project @ 2005-08-23 18:38:27 by jwe]
jwe
parents: 5307
diff changeset
116 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
3456
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3453
diff changeset
117 ## Description: Compute moments
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
118
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4452
diff changeset
119 function m = moment (x, p, opt1, opt2)
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
120
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
121 if (nargin < 2 || nargin > 4)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5568
diff changeset
122 print_usage ();
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
123 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
124
17873
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
125 if (! (isnumeric (x) || islogical (x)) || isempty (x))
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
126 error ("moment: X must be a non-empty numeric matrix or vector");
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
127 endif
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
128
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
129 if (! (isnumeric (p) && isscalar (p)))
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
130 error ("moment: P must be a numeric scalar");
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
131 endif
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
132
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
133 need_dim = false;
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4452
diff changeset
134
5090
c42c90543e36 [project @ 2004-12-03 04:32:11 by jwe]
jwe
parents: 4855
diff changeset
135 if (nargin == 2)
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
136 type = "";
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
137 need_dim = true;
5090
c42c90543e36 [project @ 2004-12-03 04:32:11 by jwe]
jwe
parents: 4855
diff changeset
138 elseif (nargin == 3)
5443
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5428
diff changeset
139 if (ischar (opt1))
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
140 type = opt1;
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
141 need_dim = true;
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4452
diff changeset
142 else
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4452
diff changeset
143 dim = opt1;
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
144 type = "";
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4452
diff changeset
145 endif
5090
c42c90543e36 [project @ 2004-12-03 04:32:11 by jwe]
jwe
parents: 4855
diff changeset
146 elseif (nargin == 4)
5443
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5428
diff changeset
147 if (ischar (opt1))
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
148 type = opt1;
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4452
diff changeset
149 dim = opt2;
5443
ec8c33dcd1bf [project @ 2005-09-08 01:40:57 by jwe]
jwe
parents: 5428
diff changeset
150 elseif (ischar (opt2))
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
151 type = opt2;
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4452
diff changeset
152 dim = opt1;
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4452
diff changeset
153 else
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
154 error ("moment: expecting TYPE to be a string");
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4452
diff changeset
155 endif
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4452
diff changeset
156 endif
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4452
diff changeset
157
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
158 nd = ndims (x);
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4452
diff changeset
159 sz = size (x);
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
160 if (need_dim)
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
161 ## Find the first non-singleton dimension.
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
162 (dim = find (sz > 1, 1)) || (dim = 1);
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
163 else
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
164 if (!(isscalar (dim) && dim == fix (dim)) ||
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
165 !(1 <= dim && dim <= nd))
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
166 error ("moment: DIM must be an integer and a valid dimension");
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
167 endif
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
168 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
169
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
170 n = sz(dim);
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
171
17873
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
172 if (! any (type == "r"))
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
173 x = center (x, dim);
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5443
diff changeset
174 endif
14552
86854d032a37 maint: miscellaneous style fixes for .m files
John W. Eaton <jwe@octave.org>
parents: 14363
diff changeset
175 if (any (type == "a"))
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5443
diff changeset
176 x = abs (x);
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5443
diff changeset
177 endif
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 4452
diff changeset
178
5568
e9cde940b271 [project @ 2005-12-08 02:28:22 by jwe]
jwe
parents: 5443
diff changeset
179 m = sum (x .^ p, dim) / n;
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
180
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
181 endfunction
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
182
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
183
17895
ed2ef5d96929 moment.m: Add eps tolerance to %!tests.
Rik <rik@octave.org>
parents: 17873
diff changeset
184 %!shared x
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
185 %! x = rand (10);
17895
ed2ef5d96929 moment.m: Add eps tolerance to %!tests.
Rik <rik@octave.org>
parents: 17873
diff changeset
186 %!assert (moment (x,1), mean (center (x)), eps)
ed2ef5d96929 moment.m: Add eps tolerance to %!tests.
Rik <rik@octave.org>
parents: 17873
diff changeset
187 %!assert (moment (x,2), meansq (center (x)), eps)
ed2ef5d96929 moment.m: Add eps tolerance to %!tests.
Rik <rik@octave.org>
parents: 17873
diff changeset
188 %!assert (moment (x,1,2), mean (center (x, 2), 2), eps)
ed2ef5d96929 moment.m: Add eps tolerance to %!tests.
Rik <rik@octave.org>
parents: 17873
diff changeset
189 %!assert (moment (x,1,"a"), mean (abs (center (x))), eps)
ed2ef5d96929 moment.m: Add eps tolerance to %!tests.
Rik <rik@octave.org>
parents: 17873
diff changeset
190 %!assert (moment (x,1,"r"), mean (x), eps)
ed2ef5d96929 moment.m: Add eps tolerance to %!tests.
Rik <rik@octave.org>
parents: 17873
diff changeset
191 %!assert (moment (x,1,"ar"), mean (abs (x)), eps)
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
192
17873
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
193 %!assert (moment (single ([1 2 3]), 1, "r"), single (2))
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
194
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
195 %% Test input validation
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
196 %!error moment ()
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
197 %!error moment (1)
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
198 %!error moment (1, 2, 3, 4, 5)
17873
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
199 %!error <X must be a non-empty numeric matrix> moment (['A'; 'B'], 2)
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
200 %!error <X must be a non-empty numeric matrix> moment (ones (2,0,3), 2)
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
201 %!error <P must be a numeric scalar> moment (1, true)
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
202 %!error <P must be a numeric scalar> moment (1, ones (2,2))
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
203 %!error <expecting TYPE to be a string> moment (1, 2, 3, 4)
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
204 %!error <DIM must be an integer and a valid dimension> moment (1, 2, ones (2,2))
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
205 %!error <DIM must be an integer and a valid dimension> moment (1, 2, 1.5)
58b39152b0f6 moment.m: Compute central, rather than raw, moment for ML compatibility (bug #36718).
Rik <rik@octave.org>
parents: 17744
diff changeset
206 %!error <DIM must be an integer and a valid dimension> moment (1, 2, 4)
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 7017
diff changeset
207