File: mad.m

package info (click to toggle)
octave 9.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 144,300 kB
  • sloc: cpp: 332,784; ansic: 77,239; fortran: 20,963; objc: 9,396; sh: 8,213; yacc: 4,925; lex: 4,389; perl: 1,544; java: 1,366; awk: 1,259; makefile: 648; xml: 189
file content (240 lines) | stat: -rw-r--r-- 8,959 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
########################################################################
##
## Copyright (C) 2017-2024 The Octave Project Developers
##
## See the file COPYRIGHT.md in the top-level directory of this
## distribution or <https://octave.org/copyright/>.
##
## This file is part of Octave.
##
## Octave is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <https://www.gnu.org/licenses/>.
##
########################################################################

## -*- texinfo -*-
## @deftypefn  {} {@var{m} =} mad (@var{x})
## @deftypefnx {} {@var{m} =} mad (@var{x}, @var{opt})
## @deftypefnx {} {@var{m} =} mad (@var{x}, @var{opt}, @var{dim})
## @deftypefnx {} {@var{m} =} mad (@var{x}, @var{opt}, @var{vecdim})
## @deftypefnx {} {@var{m} =} mad (@var{x}, @var{opt}, "all")
## Compute the mean or median absolute deviation (MAD) of the elements of
## @var{x}.
##
## The mean absolute deviation is defined as
##
## @example
## @var{mad} = mean (abs (@var{x} - mean (@var{x})))
## @end example
##
## The median absolute deviation is defined as
##
## @example
## @var{mad} = median (abs (@var{x} - median (@var{x})))
## @end example
##
## If @var{x} is a vector, compute @code{mad} for each element in @var{x}.  If
## @var{x} is an array the calculation is performed over the first
## non-singleton dimension.
##
## @code{mad} excludes NaN values from calculation similar to using the
## @code{omitnan} option in @code{var}, @code{mean}, and @code{median}.
##
## The optional argument @var{opt} determines whether mean or median absolute
## deviation is calculated.  The default is 0 which corresponds to mean
## absolute deviation; a value of 1 corresponds to median absolute deviation.
## Passing an empty input [] defaults to mean absolute deviation
## (@var{opt} = 0).
##
## The optional argument @var{dim} forces @code{mad} to operate along the
## specified dimension.  Specifying any singleton dimension in @var{x},
## including any dimension exceeding @code{ndims (@var{x})}, will result in
## an output of 0.
##
## Specifying the dimension as @var{vecdim}, a vector of non-repeating
## dimensions, will return the @code{mad} over the array slice defined by
## @var{vecdim}.  If @var{vecdim} indexes all dimensions of @var{x}, then it is
## equivalent to the option @qcode{"all"}.  Any dimension included in
## @var{vecdim} greater than @code{ndims (@var{x})} is ignored.
##
## Specifying the dimension as @qcode{"all"} will force @code{mad} to operate
## on all elements of @var{x}, and is equivalent to @code{mad (@var{x}(:))}.
##
## As a measure of dispersion, @code{mad} is less affected by outliers than
## @code{std}.
## @seealso{bounds, range, iqr, std, mean, median}
## @end deftypefn

function m = mad (x, opt = 0, dim)

  if (nargin < 1)
    print_usage ();
  endif

  if (! (isnumeric (x) || islogical (x)))
    error ("mad: X must be a numeric vector or matrix");
  endif

  if (isempty (opt))
    opt = 0;
  elseif (! isscalar (opt) || (opt != 0 && opt != 1))
    error ("mad: OPT must be 0 or 1");
  endif

  if (nargin < 3)
    ## Dim not provided

    ## First check for special empty case.
    if (isempty (x) && ndims (x) == 2 && size (x) == [0, 0])
      if (isa (x, "single"))
        m = NaN ("single");
      else
        m = NaN;
      endif
      return;
    endif

    ## Then find the first non-singleton dimension.
    (dim = find (size (x) != 1, 1)) || (dim = 1);
  endif

  if (opt == 0)
    fcn = @mean;
  else
    fcn = @median;
  endif

  m = fcn (abs (x - fcn (x, dim, "omitnan")), dim, "omitnan");

endfunction


%!assert (mad (123), 0)
%!assert (mad (Inf), NaN)
%!assert (mad ([3, Inf]),Inf)
%!assert (mad ([0, 0, 1, 2, 100]), 31.76)
%!assert (mad (single ([0, 0, 1, 2, 100])), single (31.76))
%!assert (mad ([0, 0, 1, 2, 100]'), 31.76)
%!assert (mad ([0, 0, 1, 2, 100], 1), 1)
%!assert (mad (single ([0, 0, 1, 2, 100]), 1), single (1))
%!assert (mad ([0, 0, 1, 2, 100]', 1), 1)
%!assert (mad (magic (4)), [4, 4, 4, 4])
%!assert (mad (magic (4), [], 2), [6; 2; 2; 6])
%!assert (mad (magic (4), 1), [2.5, 3.5, 3.5, 2.5])
%!assert (mad (magic (4), 1, 2), [5.5; 1.5; 1.5; 5.5])
%!assert (mad (magic (4), 0, 3), zeros (4))
%!assert (mad (magic (4), 1, 3), zeros (4))
%!assert (mad (cat (3, magic (4), magic (4))), 4 * ones (1, 4, 2))

## Test all and vecdim options
%!assert (mad (magic (4), 0, "all"), 4)
%!assert (mad (magic (4), 1, "all"), 4)
%!assert (mad (magic (4), 0, [1 2]), 4)
%!assert (mad (magic (4), 0, [1 3]), mad (magic (4), 0, 1))
%!assert (mad (magic (4), 0, [1 2 3]), 4)
%!assert (mad (magic (4), 1, [1 2]), 4)
%!assert (mad (magic (4), 1, [1 3]), mad (magic (4), 1, 1))
%!assert (mad (magic (4), 1, [1 2 3]), 4)
%!assert (mad (magic (4), 0, [3 4 99]), zeros (4))
%!assert (mad (magic (4), 1, [3 4 99]), zeros (4))

## Verify ignoring NaN values unless all NaN
%!assert (mad (NaN), NaN)
%!assert (mad (NaN (2)), NaN (1, 2))
%!assert (mad ([1, 2; 3, NaN]), [1, 0])
%!assert (mad ([1, 2; 3, NaN], [], 1), [1, 0])
%!assert (mad ([1, 2; 3, NaN], [], 2), [0.5; 0], eps)
%!assert (mad ([1, NaN; 3, NaN], [], 1), [1, NaN])
%!assert (mad ([1, NaN; 3, NaN], [], 2), [0; 0])

## Verify compatible empty handling
%!assert (mad ([]), NaN)
%!assert (mad ([], 0, 1), NaN (1,0))
%!assert (mad ([], 0, 2), NaN (0,1))
%!assert (mad ([], 0, 3), NaN (0,0))
%!assert (mad (single ([])), NaN ('single'))
%!assert (mad (ones (0, 1)), NaN)
%!assert (mad (ones (0, 1), 0, 1), NaN (1, 1))
%!assert (mad (ones (0, 1), 0, 2), NaN (0, 1))
%!assert (mad (ones (0, 1), 0, 3), NaN (0, 1))
%!assert (mad (ones (1, 0)), NaN)
%!assert (mad (ones (1, 0), 0, 1), NaN (1, 0))
%!assert (mad (ones (1, 0), 0, 2), NaN (1, 1))
%!assert (mad (ones (1, 0), 0, 3), NaN (1, 0))
%!assert (mad (ones (0, 0, 0)), NaN (1, 0, 0))
%!assert (mad (ones (1, 0, 0)), NaN (1, 1, 0))
%!assert (mad (ones (0, 1, 0)), NaN (1, 1, 0))
%!assert (mad (ones (0, 0, 0)), NaN (1, 0, 0))
%!assert (mad (ones (0, 1, 0), 0, 1), NaN (1, 1, 0))
%!assert (mad (ones (0, 1, 0), 0, 2), NaN (0, 1, 0))
%!assert (mad (ones (0, 1, 0), 0, 3), NaN (0, 1, 1))
%!assert (mad (ones (0, 1, 0), 0, 4), NaN (0, 1, 0))
%!assert (mad (ones (0, 2, 1, 0)), ones (1, 2, 1, 0))
%!assert (mad (ones (2, 0, 1, 0)), ones (1, 0, 1, 0))

## Test Inf handling
%!assert (mad ([3, 4, Inf]), Inf)
%!assert (mad ([Inf, 3, 4]), Inf)
%!assert (mad ([3, 4, Inf], 0), Inf)
%!assert (mad ([3, 4, Inf], 0, 1), [0, 0, NaN])
%!assert (mad ([3, 4, Inf], 0, 2), Inf)
%!assert (mad ([3, 4, Inf], 0, 3), [0, 0, NaN])
%!assert (mad ([3, 4, Inf]', 0), Inf)
%!assert (mad ([3, 4, Inf]', 0, 1), Inf)
%!assert (mad ([3, 4, Inf]', 0, 2), [0; 0; NaN])
%!assert (mad ([3, 4, Inf]', 0, 3), [0; 0; NaN])

%!assert (mad ([Inf, 3, 4], 1), 1)
%!assert (mad ([3, 4, Inf], 1), 1)
%!assert (mad ([3, 4, Inf], 1, 1), [0, 0, NaN])
%!assert (mad ([3, 4, Inf], 1, 2), 1)
%!assert (mad ([3, 4, Inf], 1, 3), [0, 0, NaN])
%!assert (mad ([3, 4, Inf]', 1), 1)
%!assert (mad ([3, 4, Inf]', 1, 1), 1)
%!assert (mad ([3, 4, Inf]', 1, 2), [0; 0; NaN])
%!assert (mad ([3, 4, Inf]', 1, 3), [0; 0; NaN])

%!assert (mad ([3, Inf, Inf], 1), Inf)
%!assert (mad ([3, 4, 5, Inf], 1), 1)
%!assert (mad ([3, 4, Inf, Inf], 1), Inf)
%!assert (mad ([3, Inf, Inf, Inf], 1), Inf)

%!assert <*65405> (mad ([-Inf, Inf]), NaN)
%!assert <*65405> (mad ([-Inf, Inf], 0), NaN)
%!assert <*65405> (mad ([-Inf, Inf], 1), NaN)
%!assert <*65405> (mad ([-Inf, Inf]', 0), NaN)
%!assert <*65405> (mad ([-Inf, Inf]', 1), NaN)
%!assert <*65405> (mad ([-Inf, Inf]', 0, 1), NaN)
%!assert <*65405> (mad ([-Inf, Inf]', 0, 2), [NaN; NaN])
%!assert <*65405> (mad ([-Inf, Inf]', 0, 3), [NaN; NaN])
%!assert <*65405> (mad ([-Inf, Inf]', 1, 1), NaN)
%!assert <*65405> (mad ([-Inf, Inf]', 1, 2), [NaN; NaN])
%!assert <*65405> (mad ([-Inf, Inf]', 1, 3), [NaN; NaN])
%!assert <*65405> (mad (Inf (2), 0), [NaN, NaN])
%!assert <*65405> (mad (Inf (2), 1), [NaN, NaN])
%!assert <*65405> (mad (Inf (2), 0, 1), [NaN, NaN])
%!assert <*65405> (mad (Inf (2), 0, 2), [NaN; NaN])
%!assert <*65405> (mad (Inf (2), 0, 3), NaN (2))
%!assert <*65405> (mad (Inf (2), 1, 1), [NaN, NaN])
%!assert <*65405> (mad (Inf (2), 1, 2), [NaN; NaN])
%!assert <*65405> (mad (Inf (2), 1, 3), NaN (2))

## Test input case insensitivity
%!assert (mad ([1 2 3], 0, "aLL"), 2/3, eps)
%!assert (mad ([1 2 3], 1, "aLL"), 1)

## Test input validation
%!error <Invalid call> mad ()
%!error <X must be a numeric> mad (['A'; 'B'])
%!error <OPT must be 0 or 1> mad (1, 2)