1 function R = mad(i,DIM)
2 % MAD estimates the Mean Absolute deviation
3 % (note that according to [1,2] this is the mean deviation;
4 % not the mean absolute deviation)
7 % calculates the mean deviation of x in dimension DIM
12 % default or []: first DIMENSION, with more than 1 element
15 % - can deal with NaN's (missing values)
16 % - dimension argument
17 % - compatible to Matlab and Octave
19 % see also: SUMSKIPNAN, VAR, STD,
22 % [1] http://mathworld.wolfram.com/MeanDeviation.html
23 % [2] L. Sachs, "Applied Statistics: A Handbook of Techniques", Springer-Verlag, 1984, page 253.
25 % [3] http://mathworld.wolfram.com/MeanAbsoluteDeviation.html
26 % [4] Kenney, J. F. and Keeping, E. S. "Mean Absolute Deviation." ยง6.4 in Mathematics of Statistics, Pt. 1, 3rd ed. Princeton, NJ: Van Nostrand, pp. 76-77 1962.
28 % $Id: mad.m 8223 2011-04-20 09:16:06Z schloegl $
29 % Copyright (C) 2000-2002,2010 by Alois Schloegl <alois.schloegl@gmail.com>
30 % This is part of the NaN-toolbox. For more details see
31 % http://pub.ist.ac.at/~schloegl/matlab/NaN/
33 % This program is free software; you can redistribute it and/or modify
34 % it under the terms of the GNU General Public License as published by
35 % the Free Software Foundation; either version 2 of the License, or
36 % (at your option) any later version.
38 % This program is distributed in the hope that it will be useful,
39 % but WITHOUT ANY WARRANTY; without even the implied warranty of
40 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 % GNU General Public License for more details.
43 % You should have received a copy of the GNU General Public License
44 % along with this program; If not, see <http://www.gnu.org/licenses/>.
47 DIM = find(size(i)>1,1);
48 if isempty(DIM), DIM=1; end;
52 [S,N] = sumskipnan(i,DIM); % sum
53 i = i - repmat(S./N,size(i)./size(S)); % remove mean
54 [S,N] = sumskipnan(abs(i),DIM); %
56 %if flag_implicit_unbiased_estim; %% ------- unbiased estimates -----------
57 n1 = max(N-1,0); % in case of n=0 and n=1, the (biased) variance, STD and STE are INF