1## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2003, 2004, 2005,
2## 2006, 2007 Kurt Hornik
4## This file is part of Octave.
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.
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.
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/>.
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}.
24## If @var{x} is a matrix, return the row vector containing the
25## @var{p}-th moment of each column.
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,
36## computes the third central absolute moment of @var{x}.
38## If the optional argument @var{dim} is supplied, work along dimension
42## Can easily be made to work for continuous distributions (using quad)
43## as well, but how does the general case work?
45## Author: KH <Kurt.Hornik@wu-wien.ac.at>
46## Description: Compute moments
48function m = moment (x, p, opt1, opt2)
50 if ((nargin < 2) || (nargin > 4))
71 elseif (ischar (opt2))
75 error ("moment: expecting opt to be a string");
82 t = find (size (x) != 1);
94 error ("moment: x must not be empty");
98 rng = ones (1, length (sz));
100 x = x - repmat (sum (x, dim), rng) / n;
106 m = sum (x .^ p, dim) / n;