Mercurial > octave-nkf
comparison scripts/statistics/base/quantile.m @ 18204:b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
* prctile.m, quantile.m: Use first non-singleton dimension when DIM is
unspecified. Change documentation to match new behavior. Add %!tests
to validate change.
author | Rik <rik@octave.org> |
---|---|
date | Fri, 03 Jan 2014 11:58:21 -0800 |
parents | d63878346099 |
children | 0850b5212619 |
comparison
equal
deleted
inserted
replaced
18203:adbbacce8aaf | 18204:b29beed0e98d |
---|---|
30 ## the @var{p}(i)th quantiles of each column of @var{x}. | 30 ## the @var{p}(i)th quantiles of each column of @var{x}. |
31 ## | 31 ## |
32 ## If @var{p} is unspecified, return the quantiles for | 32 ## If @var{p} is unspecified, return the quantiles for |
33 ## @code{[0.00 0.25 0.50 0.75 1.00]}. | 33 ## @code{[0.00 0.25 0.50 0.75 1.00]}. |
34 ## The optional argument @var{dim} determines the dimension along which | 34 ## The optional argument @var{dim} determines the dimension along which |
35 ## the quantiles are calculated. If @var{dim} is omitted, and @var{x} is | 35 ## the quantiles are calculated. If @var{dim} is omitted it defaults to |
36 ## a vector or matrix, it defaults to 1 (column-wise quantiles). If | 36 ## the first non-singleton dimension. |
37 ## @var{x} is an N-D array, @var{dim} defaults to the first non-singleton | |
38 ## dimension. | |
39 ## | 37 ## |
40 ## The methods available to calculate sample quantiles are the nine methods | 38 ## The methods available to calculate sample quantiles are the nine methods |
41 ## used by R (@url{http://www.r-project.org/}). The default value is | 39 ## used by R (@url{http://www.r-project.org/}). The default value is |
42 ## @w{METHOD = 5}. | 40 ## @w{METHOD = 5}. |
43 ## | 41 ## |
106 ## @end deftypefn | 104 ## @end deftypefn |
107 | 105 |
108 ## Author: Ben Abbott <bpabbott@mac.com> | 106 ## Author: Ben Abbott <bpabbott@mac.com> |
109 ## Description: Matlab style quantile function of a discrete/continuous distribution | 107 ## Description: Matlab style quantile function of a discrete/continuous distribution |
110 | 108 |
111 function q = quantile (x, p = [], dim = 1, method = 5) | 109 function q = quantile (x, p = [], dim, method = 5) |
112 | 110 |
113 if (nargin < 1 || nargin > 4) | 111 if (nargin < 1 || nargin > 4) |
114 print_usage (); | 112 print_usage (); |
115 endif | 113 endif |
116 | 114 |
124 | 122 |
125 if (! (isnumeric (p) && isvector (p))) | 123 if (! (isnumeric (p) && isvector (p))) |
126 error ("quantile: P must be a numeric vector"); | 124 error ("quantile: P must be a numeric vector"); |
127 endif | 125 endif |
128 | 126 |
129 if (!(isscalar (dim) && dim == fix (dim)) | 127 if (nargin < 3) |
130 || !(1 <= dim && dim <= ndims (x))) | 128 ## Find the first non-singleton dimension. |
131 error ("quantile: DIM must be an integer and a valid dimension"); | 129 (dim = find (size (x) > 1, 1)) || (dim = 1); |
130 else | |
131 if (!(isscalar (dim) && dim == fix (dim)) | |
132 || !(1 <= dim && dim <= ndims (x))) | |
133 error ("quantile: DIM must be an integer and a valid dimension"); | |
134 endif | |
132 endif | 135 endif |
133 | 136 |
134 ## Set the permutation vector. | 137 ## Set the permutation vector. |
135 perm = 1:ndims (x); | 138 perm = 1:ndims (x); |
136 perm(1) = dim; | 139 perm(1) = dim; |
154 ## Permute the 1st index back to dim. | 157 ## Permute the 1st index back to dim. |
155 q = ipermute (q, perm); | 158 q = ipermute (q, perm); |
156 | 159 |
157 endfunction | 160 endfunction |
158 | 161 |
162 | |
163 %!test | |
164 %! p = 0.50; | |
165 %! q = quantile (1:4, p); | |
166 %! qa = 2.5; | |
167 %! assert (q, qa); | |
168 %! q = quantile (1:4, p, 1); | |
169 %! qa = [1, 2, 3, 4]; | |
170 %! assert (q, qa); | |
171 %! q = quantile (1:4, p, 2); | |
172 %! qa = 2.5; | |
173 %! assert (q, qa); | |
174 | |
175 %!test | |
176 %! p = [0.50 0.75]; | |
177 %! q = quantile (1:4, p); | |
178 %! qa = [2.5 3.5]; | |
179 %! assert (q, qa); | |
180 %! q = quantile (1:4, p, 1); | |
181 %! qa = [1, 2, 3, 4; 1, 2, 3, 4]; | |
182 %! assert (q, qa); | |
183 %! q = quantile (1:4, p, 2); | |
184 %! qa = [2.5 3.5]; | |
185 %! assert (q, qa); | |
159 | 186 |
160 %!test | 187 %!test |
161 %! p = 0.5; | 188 %! p = 0.5; |
162 %! x = sort (rand (11)); | 189 %! x = sort (rand (11)); |
163 %! q = quantile (x, p); | 190 %! q = quantile (x, p); |