changelog shortlog tags changeset files revisions annotate raw

scripts/statistics/base/moment.m

changeset 10289: 4b124317dc38
parent:93c65f2a5668
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (71 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2003, 2004, 2005,
2## 2006, 2007 Kurt Hornik
3##
4## This file is part of Octave.
5##
6## Octave is free software; you can redistribute it and/or modify it
7## under the terms of the GNU General Public License as published by
8## the Free Software Foundation; either version 3 of the License, or (at
9## your option) any later version.
10##
11## Octave is distributed in the hope that it will be useful, but
12## WITHOUT ANY WARRANTY; without even the implied warranty of
13## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14## General Public License for more details.
15##
16## You should have received a copy of the GNU General Public License
17## along with Octave; see the file COPYING. If not, see
18## <http://www.gnu.org/licenses/>.
19
20## -*- texinfo -*-
21## @deftypefn {Function File} {} moment (@var{x}, @var{p}, @var{opt}, @var{dim})
22## If @var{x} is a vector, compute the @var{p}-th moment of @var{x}.
23##
24## If @var{x} is a matrix, return the row vector containing the
25## @var{p}-th moment of each column.
26##
27## With the optional string opt, the kind of moment to be computed can
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##
38## If the optional argument @var{dim} is supplied, work along dimension
39## @var{dim}.
40## @end deftypefn
41
42## Can easily be made to work for continuous distributions (using quad)
43## as well, but how does the general case work?
44
45## Author: KH <Kurt.Hornik@wu-wien.ac.at>
46## Description: Compute moments
47
48function m = moment (x, p, opt1, opt2)
49
50 if ((nargin < 2) || (nargin > 4))
51 print_usage ();
52 endif
53
54 need_dim = 0;
55
56 if (nargin == 2)
57 opt = "";
58 need_dim = 1;
59 elseif (nargin == 3)
60 if (ischar (opt1))
61 opt = opt1;
62 need_dim = 1;
63 else
64 dim = opt1;
65 opt = "";
66 endif
67 elseif (nargin == 4)
68 if (ischar (opt1))
69 opt = opt1;
70 dim = opt2;
71 elseif (ischar (opt2))
72 opt = opt2;
73 dim = opt1;
74 else
75 error ("moment: expecting opt to be a string");
76 endif
77 else
78 print_usage ();
79 endif
80
81 if (need_dim)
82 t = find (size (x) != 1);
83 if (isempty (t))
84 dim = 1;
85 else
86 dim = t(1);
87 endif
88 endif
89
90 sz = size (x);
91 n = sz (dim);
92
93 if (numel (x) < 1)
94 error ("moment: x must not be empty");
95 endif
96
97 if any (opt == "c")
98 rng = ones (1, length (sz));
99 rng(dim) = sz(dim);
100 x = x - repmat (sum (x, dim), rng) / n;
101 endif
102 if any (opt == "a")
103 x = abs (x);
104 endif
105
106 m = sum (x .^ p, dim) / n;
107
108endfunction