changelog shortlog tags changeset files revisions annotate raw

scripts/statistics/base/prctile.m

changeset 10289: 4b124317dc38
parent:1bf0ce0930be
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (43 minutes ago)
permissions: -rw-r--r--
description: base_properties::set_children: account for hidden children
1## Copyright (C) 2008, 2009 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{y} =} prctile (@var{x}, @var{p})
21## @deftypefnx {Function File} {@var{q} =} prctile (@var{x}, @var{p}, @var{dim})
22## For a sample @var{x}, compute the quantiles, @var{y}, corresponding
23## to the cumulative probability values, P, in percent. All non-numeric
24## values (NaNs) of X are ignored.
25##
26## If @var{x} is a matrix, compute the percentiles for each column and
27## return them in a matrix, such that the i-th row of @var{y} contains the
28## @var{p}(i)th percentiles of each column of @var{x}.
29##
30## The optional argument @var{dim} determines the dimension along which
31## the percentiles are calculated. If @var{dim} is omitted, and @var{x} is
32## a vector or matrix, it defaults to 1 (column wise quantiles). In the
33## instance that @var{x} is a N-d array, @var{dim} defaults to the first
34## dimension whose size greater than unity.
35##
36## @end deftypefn
37
38## Author: Ben Abbott <bpabbott@mac.com>
39## Description: Matlab style prctile function.
40
41function q = prctile (x, p, dim)
42
43 if (nargin < 1 || nargin > 3)
44 print_usage ();
45 endif
46
47 if (nargin < 2)
48 p = 100*[0.00 0.25, 0.50, 0.75, 1.00];
49 endif
50
51 if (nargin < 3)
52 if (ndims (x) == 2)
53 ## If a matrix or vector, use the 1st dimension.
54 dim = 1;
55 else
56 ## If an N-d array, use the firt dimension with a length > 1.
57 dim = find (size(v) != 1);
58 if (isempty (dim))
59 dim = 1;
60 endif
61 endif
62 endif
63
64 ## Convert from percent.
65 p = 0.01 * p;
66
67 ## The 5th method is compatible with Matlab.
68 method = 5;
69
70 ## Call the quantile function
71 q = quantile (x, p, dim, method);
72
73endfunction
74
75%!test
76%! pct = 50;
77%! q = prctile (1:4, pct, 1);
78%! qa = [1, 2, 3, 4];
79%! assert (q, qa);
80%! q = prctile (1:4, pct, 2);
81%! qa = 2.5000;
82%! assert (q, qa);
83
84%!test
85%! pct = 50;
86%! x = [0.1126, 0.1148, 0.0521, 0.2364, 0.1393
87%! 0.1718, 0.7273, 0.2041, 0.4531, 0.1585
88%! 0.2795, 0.7978, 0.3296, 0.5567, 0.7307
89%! 0.4288, 0.8753, 0.6477, 0.6287, 0.8165
90%! 0.9331, 0.9312, 0.9635, 0.7796, 0.8461];
91%! tol = 0.0001;
92%! q = prctile (x, pct, 1);
93%! qa = [0.2795, 0.7978, 0.3296, 0.5567, 0.7307];
94%! assert (q, qa, tol);
95%! q = prctile (x, pct, 2);
96%! qa = [0.1148; 0.2041; 0.5567; 0.6477; 0.9312];
97%! assert (q, qa, tol);
98
99%!test
100%! pct = 50;
101%! tol = 0.0001;
102%! x = [0.1126, 0.1148, 0.0521, 0.2364, 0.1393
103%! 0.1718, 0.7273, 0.2041, 0.4531, 0.1585
104%! 0.2795, 0.7978, 0.3296, 0.5567, 0.7307
105%! 0.4288, 0.8753, 0.6477, 0.6287, 0.8165
106%! 0.9331, 0.9312, 0.9635, 0.7796, 0.8461];
107%! x(5,5) = Inf;
108%! q = prctile (x, pct, 1);
109%! qa = [0.2795, 0.7978, 0.3296, 0.5567, 0.7307];
110%! assert (q, qa, tol);
111%! x(5,5) = -Inf;
112%! q = prctile (x, pct, 1);
113%! qa = [0.2795, 0.7978, 0.3296, 0.5567, 0.1585];
114%! assert (q, qa, tol);
115%! x(1,1) = Inf;
116%! q = prctile (x, pct, 1);
117%! qa = [0.4288, 0.7978, 0.3296, 0.5567, 0.1585];
118%! assert (q, qa, tol);
119
120%!test
121%! pct = 50;
122%! tol = 0.0001;
123%! x = [0.1126, 0.1148, 0.0521, 0.2364, 0.1393
124%! 0.1718, 0.7273, 0.2041, 0.4531, 0.1585
125%! 0.2795, 0.7978, 0.3296, 0.5567, 0.7307
126%! 0.4288, 0.8753, 0.6477, 0.6287, 0.8165
127%! 0.9331, 0.9312, 0.9635, 0.7796, 0.8461];
128%! x(3,3) = Inf;
129%! q = prctile (x, pct, 1);
130%! qa = [0.2795, 0.7978, 0.6477, 0.5567, 0.7307];
131%! assert (q, qa, tol);
132%! q = prctile (x, pct, 2);
133%! qa = [0.1148; 0.2041; 0.7307; 0.6477; 0.9312];
134%! assert (q, qa, tol);
135
136%!test
137%! pct = 50;
138%! tol = 0.0001;
139%! x = [0.1126, 0.1148, 0.0521, 0.2364, 0.1393
140%! 0.1718, 0.7273, 0.2041, 0.4531, 0.1585
141%! 0.2795, 0.7978, 0.3296, 0.5567, 0.7307
142%! 0.4288, 0.8753, 0.6477, 0.6287, 0.8165
143%! 0.9331, 0.9312, 0.9635, 0.7796, 0.8461];
144%! x(5,5) = NaN;
145%! q = prctile (x, pct, 2);
146%! qa = [0.1148; 0.2041; 0.5567; 0.6477; 0.9322];
147%! assert (q, qa, tol);
148%! x(1,1) = NaN;
149%! q = prctile (x, pct, 2);
150%! qa = [0.1270; 0.2041; 0.5567; 0.6477; 0.9322];
151%! assert (q, qa, tol);
152%! x(3,3) = NaN;
153%! q = prctile (x, pct, 2);
154%! qa = [0.1270; 0.2041; 0.6437; 0.6477; 0.9322];
155%! assert (q, qa, tol);
156