]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/base/prctile.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / base / prctile.m
1 ## Copyright (C) 2008-2012 Ben Abbott
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} {@var{q} =} prctile (@var{x})
21 ## @deftypefnx {Function File} {@var{q} =} prctile (@var{x}, @var{p})
22 ## @deftypefnx {Function File} {@var{q} =} prctile (@var{x}, @var{p}, @var{dim})
23 ## For a sample @var{x}, compute the quantiles, @var{q}, corresponding
24 ## to the cumulative probability values, @var{p}, in percent.  All non-numeric
25 ## values (NaNs) of @var{x} are ignored.
26 ##
27 ## If @var{x} is a matrix, compute the percentiles for each column and
28 ## return them in a matrix, such that the i-th row of @var{y} contains the
29 ## @var{p}(i)th percentiles of each column of @var{x}.
30 ##
31 ## If @var{p} is unspecified, return the quantiles for @code{[0 25 50 75 100]}.
32 ## The optional argument @var{dim} determines the dimension along which
33 ## the percentiles are calculated.  If @var{dim} is omitted, and @var{x} is
34 ## a vector or matrix, it defaults to 1 (column-wise quantiles).  When
35 ## @var{x} is an N-D array, @var{dim} defaults to the first non-singleton
36 ## dimension.
37 ## @seealso{quantile}
38 ## @end deftypefn
39
40 ## Author: Ben Abbott <bpabbott@mac.com>
41 ## Description: Matlab style prctile function.
42
43 function q = prctile (x, p = [], dim)
44
45   if (nargin < 1 || nargin > 3)
46     print_usage ();
47   endif
48
49   if (! (isnumeric (x) || islogical (x)))
50     error ("prctile: X must be a numeric vector or matrix");
51   endif
52
53   if (isempty (p))
54     p = [0, 25, 50, 75, 100];
55   endif
56
57   if (! (isnumeric (p) && isvector (p)))
58     error ("prctile: P must be a numeric vector");
59   endif
60
61   nd = ndims (x);
62   if (nargin == 2)
63     if (nd == 2)
64       ## If a matrix or vector, always use 1st dimension.
65       dim = 1;
66     else
67       ## If an N-d array, find the first non-singleton dimension.
68       (dim = find (sz > 1, 1)) || (dim = 1);
69     endif
70   else
71     if (!(isscalar (dim) && dim == fix (dim))
72         || !(1 <= dim && dim <= nd))
73       error ("prctile: DIM must be an integer and a valid dimension");
74     endif
75   endif
76
77   ## Convert from percent to decimal.
78   p /= 100;
79
80   q = quantile (x, p, dim);
81
82 endfunction
83
84
85 %!test
86 %! pct = 50;
87 %! q = prctile (1:4, pct, 1);
88 %! qa = [1, 2, 3, 4];
89 %! assert (q, qa);
90 %! q = prctile (1:4, pct, 2);
91 %! qa = 2.5000;
92 %! assert (q, qa);
93
94 %!test
95 %! pct = 50;
96 %! x = [0.1126, 0.1148, 0.0521, 0.2364, 0.1393
97 %!      0.1718, 0.7273, 0.2041, 0.4531, 0.1585
98 %!      0.2795, 0.7978, 0.3296, 0.5567, 0.7307
99 %!      0.4288, 0.8753, 0.6477, 0.6287, 0.8165
100 %!      0.9331, 0.9312, 0.9635, 0.7796, 0.8461];
101 %! tol = 0.0001;
102 %! q = prctile (x, pct, 1);
103 %! qa = [0.2795, 0.7978, 0.3296, 0.5567, 0.7307];
104 %! assert (q, qa, tol);
105 %! q = prctile (x, pct, 2);
106 %! qa = [0.1148; 0.2041; 0.5567; 0.6477; 0.9312];
107 %! assert (q, qa, tol);
108
109 %!test
110 %! pct = 50;
111 %! tol = 0.0001;
112 %! x = [0.1126, 0.1148, 0.0521, 0.2364, 0.1393
113 %!      0.1718, 0.7273, 0.2041, 0.4531, 0.1585
114 %!      0.2795, 0.7978, 0.3296, 0.5567, 0.7307
115 %!      0.4288, 0.8753, 0.6477, 0.6287, 0.8165
116 %!      0.9331, 0.9312, 0.9635, 0.7796, 0.8461];
117 %! x(5,5) = Inf;
118 %! q = prctile (x, pct, 1);
119 %! qa = [0.2795, 0.7978, 0.3296, 0.5567, 0.7307];
120 %! assert (q, qa, tol);
121 %! x(5,5) = -Inf;
122 %! q = prctile (x, pct, 1);
123 %! qa = [0.2795, 0.7978, 0.3296, 0.5567, 0.1585];
124 %! assert (q, qa, tol);
125 %! x(1,1) = Inf;
126 %! q = prctile (x, pct, 1);
127 %! qa = [0.4288, 0.7978, 0.3296, 0.5567, 0.1585];
128 %! assert (q, qa, tol);
129
130 %!test
131 %! pct = 50;
132 %! tol = 0.0001;
133 %! x = [0.1126, 0.1148, 0.0521, 0.2364, 0.1393
134 %!      0.1718, 0.7273, 0.2041, 0.4531, 0.1585
135 %!      0.2795, 0.7978, 0.3296, 0.5567, 0.7307
136 %!      0.4288, 0.8753, 0.6477, 0.6287, 0.8165
137 %!      0.9331, 0.9312, 0.9635, 0.7796, 0.8461];
138 %! x(3,3) = Inf;
139 %! q = prctile (x, pct, 1);
140 %! qa = [0.2795, 0.7978, 0.6477, 0.5567, 0.7307];
141 %! assert (q, qa, tol);
142 %! q = prctile (x, pct, 2);
143 %! qa = [0.1148; 0.2041; 0.7307; 0.6477; 0.9312];
144 %! assert (q, qa, tol);
145
146 %!test
147 %! pct = 50;
148 %! tol = 0.0001;
149 %! x = [0.1126, 0.1148, 0.0521, 0.2364, 0.1393
150 %!      0.1718, 0.7273, 0.2041, 0.4531, 0.1585
151 %!      0.2795, 0.7978, 0.3296, 0.5567, 0.7307
152 %!      0.4288, 0.8753, 0.6477, 0.6287, 0.8165
153 %!      0.9331, 0.9312, 0.9635, 0.7796, 0.8461];
154 %! x(5,5) = NaN;
155 %! q = prctile (x, pct, 2);
156 %! qa = [0.1148; 0.2041; 0.5567; 0.6477; 0.9322];
157 %! assert (q, qa, tol);
158 %! x(1,1) = NaN;
159 %! q = prctile (x, pct, 2);
160 %! qa = [0.1270; 0.2041; 0.5567; 0.6477; 0.9322];
161 %! assert (q, qa, tol);
162 %! x(3,3) = NaN;
163 %! q = prctile (x, pct, 2);
164 %! qa = [0.1270; 0.2041; 0.6437; 0.6477; 0.9322];
165 %! assert (q, qa, tol);
166
167 %% Test input validation
168 %!error prctile ()
169 %!error prctile (1, 2, 3, 4)
170 %!error prctile (['A'; 'B'], 10)
171 %!error prctile (1:10, [true, false])
172 %!error prctile (1:10, ones (2,2))
173 %!error prctile (1, 1, 1.5)
174 %!error prctile (1, 1, 0)
175 %!error prctile (1, 1, 3)