]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/base/iqr.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / base / iqr.m
1 ## Copyright (C) 1995-2012 Kurt Hornik
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {} iqr (@var{x})
21 ## @deftypefnx {Function File} {} iqr (@var{x}, @var{dim})
22 ## Return the interquartile range, i.e., the difference between the upper
23 ## and lower quartile of the input data.  If @var{x} is a matrix, do the
24 ## above for first non-singleton dimension of @var{x}.
25 ##
26 ## If the optional argument @var{dim} is given, operate along this dimension.
27 ##
28 ## As a measure of dispersion, the interquartile range is less affected by
29 ## outliers than either @code{range} or @code{std}.
30 ## @seealso{range, std}
31 ## @end deftypefn
32
33 ## Author KH <Kurt.Hornik@wu-wien.ac.at>
34 ## Description: Interquartile range
35
36 function y = iqr (x, dim)
37
38   if (nargin != 1 && nargin != 2)
39     print_usage ();
40   endif
41
42   if (! (isnumeric (x) || islogical (x)))
43     error ("iqr: X must be a numeric vector or matrix");
44   endif
45
46   nd = ndims (x);
47   sz = size (x);
48   nel = numel (x);
49   if (nargin != 2)
50     ## Find the first non-singleton dimension.
51     (dim = find (sz > 1, 1)) || (dim = 1);
52   else
53     if (!(isscalar (dim) && dim == fix (dim))
54         || !(1 <= dim && dim <= nd))
55       error ("iqr: DIM must be an integer and a valid dimension");
56     endif
57   endif
58
59   ## This code is a bit heavy, but is needed until empirical_inv
60   ## can take a matrix, rather than just a vector argument.
61   n = sz(dim);
62   sz(dim) = 1;
63   if (isa (x, 'single'))
64     y = zeros (sz, 'single');
65   else
66     y = zeros (sz);
67   endif
68   stride = prod (sz(1:dim-1));
69   for i = 1 : nel / n;
70     offset = i;
71     offset2 = 0;
72     while (offset > stride)
73       offset -= stride;
74       offset2++;
75     endwhile
76     offset += offset2 * stride * n;
77     rng = [0 : n-1] * stride + offset;
78
79     y(i) = diff (empirical_inv ([1/4, 3/4], x(rng)));
80   endfor
81
82 endfunction
83
84
85 %!assert (iqr (1:101), 50);
86 %!assert (iqr (single(1:101)), single(50));
87
88 %%!test
89 %%! x = [1:100];
90 %%! n = iqr (x, 0:10);
91 %%! assert (n, [repmat(100, 1, 10), 1]);
92
93 %!error iqr ();
94 %!error iqr (1, 2, 3);
95 %!error iqr (1);
96 %!error iqr (['A'; 'B']);
97 %!error iqr (1:10, 3);
98