Mercurial > octave-antonio
annotate scripts/statistics/base/iqr.m @ 11587:c792872f8942
all script files: untabify and strip trailing whitespace
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 20 Jan 2011 17:35:29 -0500 |
parents | fd0a3ac60b0e |
children | 6b2f14af2360 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 1995-2011 Kurt Hornik |
3426 | 2 ## |
3922 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
3426 | 9 ## |
3922 | 10 ## Octave is distributed in the hope that it will be useful, but |
3200 | 11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 13 ## General Public License for more details. |
14 ## | |
3200 | 15 ## You should have received a copy of the GNU General Public License |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
3200 | 18 |
3453 | 19 ## -*- texinfo -*- |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
20 ## @deftypefn {Function File} {} iqr (@var{x}) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
21 ## @deftypefnx {Function File} {} iqr (@var{x}, @var{dim}) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
22 ## Return the interquartile range, i.e., the difference between the upper |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
23 ## and lower quartile of the input data. If @var{x} is a matrix, do the |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
24 ## above for first non-singleton dimension of @var{x}. |
3200 | 25 ## |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
26 ## If the optional argument @var{dim} is given, operate along this dimension. |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
27 ## |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
28 ## As a measure of dispersion, the interquartile range is less affected by |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
29 ## outliers than either @code{range} or @code{std}. |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
30 ## @seealso{range, std} |
3453 | 31 ## @end deftypefn |
3426 | 32 |
5428 | 33 ## Author KH <Kurt.Hornik@wu-wien.ac.at> |
3456 | 34 ## Description: Interquartile range |
3200 | 35 |
4885 | 36 function y = iqr (x, dim) |
3426 | 37 |
4885 | 38 if (nargin != 1 && nargin != 2) |
6046 | 39 print_usage (); |
3200 | 40 endif |
41 | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
42 if (!(ismatrix (x) && isnumeric (x)) || isscalar(x)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
43 error ("iqr: X must be a numeric vector or matrix"); |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
44 endif |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
45 |
4885 | 46 nd = ndims (x); |
47 sz = size (x); | |
48 nel = numel (x); | |
49 if (nargin != 2) | |
4886 | 50 ## Find the first non-singleton dimension. |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
51 dim = find (sz > 1, 1); |
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
52 if (isempty (dim)) |
4885 | 53 dim = 1; |
54 endif | |
55 else | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
56 if (!(isscalar (dim) && dim == fix (dim)) |
11149
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10669
diff
changeset
|
57 || !(1 <= dim && dim <= nd)) |
10669
cab3b148d4e4
Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
58 error ("iqr: DIM must be an integer and a valid dimension"); |
4885 | 59 endif |
4681 | 60 endif |
4631 | 61 |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
62 ## This code is a bit heavy, but is needed until empirical_inv |
4885 | 63 ## takes other than vector arguments. |
4886 | 64 c = sz(dim); |
65 sz(dim) = 1; | |
4885 | 66 y = zeros (sz); |
4886 | 67 stride = prod (sz(1:dim-1)); |
4885 | 68 for i = 1 : nel / c; |
69 offset = i; | |
70 offset2 = 0; | |
71 while (offset > stride) | |
72 offset -= stride; | |
73 offset2++; | |
74 endwhile | |
75 offset += offset2 * stride * c; | |
76 rng = [0 : c-1] * stride + offset; | |
4631 | 77 |
4885 | 78 y (i) = empirical_inv (3/4, x(rng)) - empirical_inv (1/4, x(rng)); |
4681 | 79 endfor |
3426 | 80 |
3200 | 81 endfunction |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
82 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
83 %!assert (iqr (1:101), 50); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
84 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
85 %%!test |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
86 %%! x = [1:100]; |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
87 %%! n = iqr (x, 0:10); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
88 %%! assert (n, [repmat(100, 1, 10), 1]); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
89 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
90 %!error iqr (); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
91 %!error iqr (1, 2, 3); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
92 %!error iqr (1); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
93 %!error iqr ([true, true]); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
94 %!error iqr (1:10, 3); |