1 function M=moment(i,p,opt,DIM)
2 % MOMENT estimates the p-th moment
4 % M = moment(x, p [,opt] [,DIM])
5 % M = moment(H, p [,opt])
6 % calculates p-th central moment from data x in dimension DIM
10 % opt 'ac': absolute 'a' and/or central ('c') moment
11 % DEFAULT: '' raw moments are estimated
15 % default or []: first DIMENSION, with more than 1 element
18 % - can deal with NaN's (missing values)
19 % - dimension argument
20 % - compatible to Matlab and Octave
22 % see also: STD, VAR, SKEWNESS, KURTOSIS, STATISTIC,
25 % http://mathworld.wolfram.com/Moment.html
27 % $Id: moment.m 8223 2011-04-20 09:16:06Z schloegl $
28 % Copyright (C) 2000-2002,2010 by Alois Schloegl <alois.schloegl@gmail.com>
29 % This functions is part of the NaN-toolbox
30 % http://pub.ist.ac.at/~schloegl/matlab/NaN/
32 % This program is free software; you can redistribute it and/or modify
33 % it under the terms of the GNU General Public License as published by
34 % the Free Software Foundation; either version 3 of the License, or
35 % (at your option) any later version.
37 % This program is distributed in the hope that it will be useful,
38 % but WITHOUT ANY WARRANTY; without even the implied warranty of
39 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 % GNU General Public License for more details.
42 % You should have received a copy of the GNU General Public License
43 % along with this program; If not, see <http://www.gnu.org/licenses/>.
53 fprintf('Error MOMENT: invalid number of arguments\n');
58 fprintf('Error MOMENT: invalid model order p=%f\n',p);
62 if isnumeric(opt) || ~isnumeric(DIM),
71 DIM = find(size(i)>1,1);
72 if isempty(DIM), DIM=1; end;
77 if isfield(i,'HISTOGRAM'),
78 sz = size(i.H)./size(i.X);
81 N = sumskipnan(i.H,1); % N
82 N = max(N-1,0); % for unbiased estimation
83 S = sumskipnan(i.H.*X,1); % sum
84 X = X - repmat(S./N, size(X)./size(S)); % remove mean
89 [M,n] = sumskipnan(X.^p.*i.H,1);
91 warning('invalid datatype')
95 [S,N] = sumskipnan(i,DIM); % gemerate N and SUM
96 N = max(N-1,0); % for unbiased estimation
97 i = i - repmat(S./N, size(i)./size(S)); % remove mean
102 [M,n] = sumskipnan(i.^p,DIM);
105 if isnan(N), N=n; end;