Mercurial > octave-nkf
annotate 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 |
rev | line source |
---|---|
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
16792
diff
changeset
|
1 ## Copyright (C) 2008-2013 Ben Abbott and Jaroslav Hajek |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
2 ## |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
3 ## This file is part of Octave. |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
4 ## |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
5 ## Octave is free software; you can redistribute it and/or modify it |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
6 ## under the terms of the GNU General Public License as published by |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
8 ## your option) any later version. |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
9 ## |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
10 ## Octave is distributed in the hope that it will be useful, but |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
13 ## General Public License for more details. |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
14 ## |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
15 ## You should have received a copy of the GNU General Public License |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
16 ## along with Octave; see the file COPYING. If not, see |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
17 ## <http://www.gnu.org/licenses/>. |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
18 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
19 ## -*- texinfo -*- |
14740
c827d74a643b
doc: Add additional calling form of quantile() to docstring.
Rik <octave@nomad.inbox5.com>
parents:
14373
diff
changeset
|
20 ## @deftypefn {Function File} {@var{q} =} quantile (@var{x}) |
c827d74a643b
doc: Add additional calling form of quantile() to docstring.
Rik <octave@nomad.inbox5.com>
parents:
14373
diff
changeset
|
21 ## @deftypefnx {Function File} {@var{q} =} quantile (@var{x}, @var{p}) |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
22 ## @deftypefnx {Function File} {@var{q} =} quantile (@var{x}, @var{p}, @var{dim}) |
8507 | 23 ## @deftypefnx {Function File} {@var{q} =} quantile (@var{x}, @var{p}, @var{dim}, @var{method}) |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
24 ## For a sample, @var{x}, calculate the quantiles, @var{q}, corresponding to |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
25 ## the cumulative probability values in @var{p}. All non-numeric values (NaNs) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11131
diff
changeset
|
26 ## of @var{x} are ignored. |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
27 ## |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
28 ## If @var{x} is a matrix, compute the quantiles for each column and |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
29 ## return them in a matrix, such that the i-th row of @var{q} contains |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
30 ## the @var{p}(i)th quantiles of each column of @var{x}. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
31 ## |
14740
c827d74a643b
doc: Add additional calling form of quantile() to docstring.
Rik <octave@nomad.inbox5.com>
parents:
14373
diff
changeset
|
32 ## If @var{p} is unspecified, return the quantiles for |
c827d74a643b
doc: Add additional calling form of quantile() to docstring.
Rik <octave@nomad.inbox5.com>
parents:
14373
diff
changeset
|
33 ## @code{[0.00 0.25 0.50 0.75 1.00]}. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
34 ## The optional argument @var{dim} determines the dimension along which |
18204
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
35 ## the quantiles are calculated. If @var{dim} is omitted it defaults to |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
36 ## the first non-singleton dimension. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
37 ## |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
38 ## The methods available to calculate sample quantiles are the nine methods |
16792
b157ba28f123
doc: Use Texinfo @url command consistently throughout documentation.
Rik <rik@octave.org>
parents:
16766
diff
changeset
|
39 ## used by R (@url{http://www.r-project.org/}). The default value is |
b157ba28f123
doc: Use Texinfo @url command consistently throughout documentation.
Rik <rik@octave.org>
parents:
16766
diff
changeset
|
40 ## @w{METHOD = 5}. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
41 ## |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
42 ## Discontinuous sample quantile methods 1, 2, and 3 |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
43 ## |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
44 ## @enumerate 1 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
45 ## @item Method 1: Inverse of empirical distribution function. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
46 ## |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
47 ## @item Method 2: Similar to method 1 but with averaging at discontinuities. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
48 ## |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
49 ## @item Method 3: SAS definition: nearest even order statistic. |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
50 ## @end enumerate |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
51 ## |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
52 ## Continuous sample quantile methods 4 through 9, where p(k) is the linear |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
53 ## interpolation function respecting each methods' representative cdf. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
54 ## |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
55 ## @enumerate 4 |
14373
d00900b3dc4b
doc: Use two spaces at start of sentence.
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
56 ## @item Method 4: p(k) = k / n. That is, linear interpolation of the |
11120
a44f979a35ce
style fixes for some .m files
John W. Eaton <jwe@octave.org>
parents:
10821
diff
changeset
|
57 ## empirical cdf. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
58 ## |
14373
d00900b3dc4b
doc: Use two spaces at start of sentence.
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
59 ## @item Method 5: p(k) = (k - 0.5) / n. That is a piecewise linear function |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
60 ## where the knots are the values midway through the steps of the empirical |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
61 ## cdf. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
62 ## |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
63 ## @item Method 6: p(k) = k / (n + 1). |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
64 ## |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
65 ## @item Method 7: p(k) = (k - 1) / (n - 1). |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
66 ## |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
67 ## @item Method 8: p(k) = (k - 1/3) / (n + 1/3). The resulting quantile |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
68 ## estimates are approximately median-unbiased regardless of the distribution |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11131
diff
changeset
|
69 ## of @var{x}. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
70 ## |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
71 ## @item Method 9: p(k) = (k - 3/8) / (n + 1/4). The resulting quantile |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
72 ## estimates are approximately unbiased for the expected order statistics if |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11131
diff
changeset
|
73 ## @var{x} is normally distributed. |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
74 ## @end enumerate |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
75 ## |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
76 ## Hyndman and Fan (1996) recommend method 8. Maxima, S, and R |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
77 ## (versions prior to 2.0.0) use 7 as their default. Minitab and SPSS |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
78 ## use method 6. @sc{matlab} uses method 5. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
79 ## |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
80 ## References: |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
81 ## |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
82 ## @itemize @bullet |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
83 ## @item Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New |
9069
634274aaa183
Cleanup documentation for stats.texi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
84 ## S Language. Wadsworth & Brooks/Cole. |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
85 ## |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
86 ## @item Hyndman, R. J. and Fan, Y. (1996) Sample quantiles in |
9069
634274aaa183
Cleanup documentation for stats.texi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
87 ## statistical packages, American Statistician, 50, 361--365. |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
88 ## |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
89 ## @item R: A Language and Environment for Statistical Computing; |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
90 ## @url{http://cran.r-project.org/doc/manuals/fullrefman.pdf}. |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
91 ## @end itemize |
12569
6ef23b4a3402
Add quantile, prctile functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
12480
diff
changeset
|
92 ## |
6ef23b4a3402
Add quantile, prctile functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
12480
diff
changeset
|
93 ## Examples: |
13929
9cae456085c2
Grammarcheck of documentation before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
12974
diff
changeset
|
94 ## @c Set example in small font to prevent overfull line |
12569
6ef23b4a3402
Add quantile, prctile functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
12480
diff
changeset
|
95 ## |
12974
6590446c2498
doc: Correct Texinfo overfull hbox instances.
Rik <octave@nomad.inbox5.com>
parents:
12656
diff
changeset
|
96 ## @smallexample |
12569
6ef23b4a3402
Add quantile, prctile functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
12480
diff
changeset
|
97 ## @group |
12974
6590446c2498
doc: Correct Texinfo overfull hbox instances.
Rik <octave@nomad.inbox5.com>
parents:
12656
diff
changeset
|
98 ## x = randi (1000, [10, 1]); # Create empirical data in range 1-1000 |
6590446c2498
doc: Correct Texinfo overfull hbox instances.
Rik <octave@nomad.inbox5.com>
parents:
12656
diff
changeset
|
99 ## q = quantile (x, [0, 1]); # Return minimum, maximum of distribution |
6590446c2498
doc: Correct Texinfo overfull hbox instances.
Rik <octave@nomad.inbox5.com>
parents:
12656
diff
changeset
|
100 ## q = quantile (x, [0.25 0.5 0.75]); # Return quartiles of distribution |
12569
6ef23b4a3402
Add quantile, prctile functions to documentation.
Rik <octave@nomad.inbox5.com>
parents:
12480
diff
changeset
|
101 ## @end group |
12974
6590446c2498
doc: Correct Texinfo overfull hbox instances.
Rik <octave@nomad.inbox5.com>
parents:
12656
diff
changeset
|
102 ## @end smallexample |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11131
diff
changeset
|
103 ## @seealso{prctile} |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
104 ## @end deftypefn |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
105 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
106 ## Author: Ben Abbott <bpabbott@mac.com> |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
107 ## Description: Matlab style quantile function of a discrete/continuous distribution |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
108 |
18204
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
109 function q = quantile (x, p = [], dim, method = 5) |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
110 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
111 if (nargin < 1 || nargin > 4) |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
112 print_usage (); |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
113 endif |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
114 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
115 if (! (isnumeric (x) || islogical (x))) |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
116 error ("quantile: X must be a numeric vector or matrix"); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
117 endif |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
118 |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
119 if (isempty (p)) |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
120 p = [0.00 0.25, 0.50, 0.75, 1.00]; |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
121 endif |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
122 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
123 if (! (isnumeric (p) && isvector (p))) |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
124 error ("quantile: P must be a numeric vector"); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
125 endif |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
126 |
18204
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
127 if (nargin < 3) |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
128 ## Find the first non-singleton dimension. |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
129 (dim = find (size (x) > 1, 1)) || (dim = 1); |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
130 else |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
131 if (!(isscalar (dim) && dim == fix (dim)) |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
132 || !(1 <= dim && dim <= ndims (x))) |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
133 error ("quantile: DIM must be an integer and a valid dimension"); |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
134 endif |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
135 endif |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
136 |
8506 | 137 ## Set the permutation vector. |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14740
diff
changeset
|
138 perm = 1:ndims (x); |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
139 perm(1) = dim; |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
140 perm(dim) = 1; |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
141 |
8506 | 142 ## Permute dim to the 1st index. |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
143 x = permute (x, perm); |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
144 |
8506 | 145 ## Save the size of the permuted x N-d array. |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
146 sx = size (x); |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
147 |
8506 | 148 ## Reshape to a 2-d array. |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
149 x = reshape (x, [sx(1), prod(sx(2:end))]); |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
150 |
8506 | 151 ## Calculate the quantiles. |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
152 q = __quantile__ (x, p, method); |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
153 |
8506 | 154 ## Return the shape to the original N-d array. |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
155 q = reshape (q, [numel(p), sx(2:end)]); |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
156 |
8506 | 157 ## Permute the 1st index back to dim. |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
158 q = ipermute (q, perm); |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
159 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
160 endfunction |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
161 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
162 |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
163 %!test |
18204
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
164 %! p = 0.50; |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
165 %! q = quantile (1:4, p); |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
166 %! qa = 2.5; |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
167 %! assert (q, qa); |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
168 %! q = quantile (1:4, p, 1); |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
169 %! qa = [1, 2, 3, 4]; |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
170 %! assert (q, qa); |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
171 %! q = quantile (1:4, p, 2); |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
172 %! qa = 2.5; |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
173 %! assert (q, qa); |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
174 |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
175 %!test |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
176 %! p = [0.50 0.75]; |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
177 %! q = quantile (1:4, p); |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
178 %! qa = [2.5 3.5]; |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
179 %! assert (q, qa); |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
180 %! q = quantile (1:4, p, 1); |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
181 %! qa = [1, 2, 3, 4; 1, 2, 3, 4]; |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
182 %! assert (q, qa); |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
183 %! q = quantile (1:4, p, 2); |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
184 %! qa = [2.5 3.5]; |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
185 %! assert (q, qa); |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
186 |
b29beed0e98d
Use first non-singleton dimension for prctile, quantile (bug #40736).
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
187 %!test |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
188 %! p = 0.5; |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
189 %! x = sort (rand (11)); |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
190 %! q = quantile (x, p); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
191 %! assert (q, x(6,:)); |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
192 %! x = x.'; |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
193 %! q = quantile (x, p, 2); |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
194 %! assert (q, x(:,6)); |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
195 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
196 %!test |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
197 %! p = [0.00, 0.25, 0.50, 0.75, 1.00]; |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
198 %! x = [1; 2; 3; 4]; |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
199 %! a = [1.0000 1.0000 2.0000 3.0000 4.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
200 %! 1.0000 1.5000 2.5000 3.5000 4.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
201 %! 1.0000 1.0000 2.0000 3.0000 4.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
202 %! 1.0000 1.0000 2.0000 3.0000 4.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
203 %! 1.0000 1.5000 2.5000 3.5000 4.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
204 %! 1.0000 1.2500 2.5000 3.7500 4.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
205 %! 1.0000 1.7500 2.5000 3.2500 4.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
206 %! 1.0000 1.4167 2.5000 3.5833 4.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
207 %! 1.0000 1.4375 2.5000 3.5625 4.0000]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
208 %! for m = 1:9 |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
209 %! q = quantile (x, p, 1, m).'; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
210 %! assert (q, a(m,:), 0.0001); |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
211 %! endfor |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
212 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
213 %!test |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
214 %! p = [0.00, 0.25, 0.50, 0.75, 1.00]; |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
215 %! x = [1; 2; 3; 4; 5]; |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
216 %! a = [1.0000 2.0000 3.0000 4.0000 5.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
217 %! 1.0000 2.0000 3.0000 4.0000 5.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
218 %! 1.0000 1.0000 2.0000 4.0000 5.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
219 %! 1.0000 1.2500 2.5000 3.7500 5.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
220 %! 1.0000 1.7500 3.0000 4.2500 5.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
221 %! 1.0000 1.5000 3.0000 4.5000 5.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
222 %! 1.0000 2.0000 3.0000 4.0000 5.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
223 %! 1.0000 1.6667 3.0000 4.3333 5.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
224 %! 1.0000 1.6875 3.0000 4.3125 5.0000]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
225 %! for m = 1:9 |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
226 %! q = quantile (x, p, 1, m).'; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
227 %! assert (q, a(m,:), 0.0001); |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
228 %! endfor |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
229 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
230 %!test |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
231 %! p = [0.00, 0.25, 0.50, 0.75, 1.00]; |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
232 %! x = [1; 2; 5; 9]; |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
233 %! a = [1.0000 1.0000 2.0000 5.0000 9.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
234 %! 1.0000 1.5000 3.5000 7.0000 9.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
235 %! 1.0000 1.0000 2.0000 5.0000 9.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
236 %! 1.0000 1.0000 2.0000 5.0000 9.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
237 %! 1.0000 1.5000 3.5000 7.0000 9.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
238 %! 1.0000 1.2500 3.5000 8.0000 9.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
239 %! 1.0000 1.7500 3.5000 6.0000 9.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
240 %! 1.0000 1.4167 3.5000 7.3333 9.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
241 %! 1.0000 1.4375 3.5000 7.2500 9.0000]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
242 %! for m = 1:9 |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
243 %! q = quantile (x, p, 1, m).'; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
244 %! assert (q, a(m,:), 0.0001); |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
245 %! endfor |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
246 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
247 %!test |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
248 %! p = [0.00, 0.25, 0.50, 0.75, 1.00]; |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
249 %! x = [1; 2; 5; 9; 11]; |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
250 %! a = [1.0000 2.0000 5.0000 9.0000 11.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
251 %! 1.0000 2.0000 5.0000 9.0000 11.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
252 %! 1.0000 1.0000 2.0000 9.0000 11.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
253 %! 1.0000 1.2500 3.5000 8.0000 11.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
254 %! 1.0000 1.7500 5.0000 9.5000 11.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
255 %! 1.0000 1.5000 5.0000 10.0000 11.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
256 %! 1.0000 2.0000 5.0000 9.0000 11.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
257 %! 1.0000 1.6667 5.0000 9.6667 11.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
258 %! 1.0000 1.6875 5.0000 9.6250 11.0000]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
259 %! for m = 1:9 |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
260 %! q = quantile (x, p, 1, m).'; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
261 %! assert (q, a(m,:), 0.0001); |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
262 %! endfor |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
263 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
264 %!test |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
265 %! p = [0.00, 0.25, 0.50, 0.75, 1.00]; |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
266 %! x = [16; 11; 15; 12; 15; 8; 11; 12; 6; 10]; |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
267 %! a = [6.0000 10.0000 11.0000 15.0000 16.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
268 %! 6.0000 10.0000 11.5000 15.0000 16.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
269 %! 6.0000 8.0000 11.0000 15.0000 16.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
270 %! 6.0000 9.0000 11.0000 13.5000 16.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
271 %! 6.0000 10.0000 11.5000 15.0000 16.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
272 %! 6.0000 9.5000 11.5000 15.0000 16.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
273 %! 6.0000 10.2500 11.5000 14.2500 16.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
274 %! 6.0000 9.8333 11.5000 15.0000 16.0000 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
275 %! 6.0000 9.8750 11.5000 15.0000 16.0000]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
276 %! for m = 1:9 |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
277 %! q = quantile (x, p, 1, m).'; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
278 %! assert (q, a(m,:), 0.0001); |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
279 %! endfor |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
280 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
281 %!test |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
282 %! p = [0.00, 0.25, 0.50, 0.75, 1.00]; |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
283 %! x = [-0.58851; 0.40048; 0.49527; -2.551500; -0.52057; ... |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
284 %! -0.17841; 0.057322; -0.62523; 0.042906; 0.12337]; |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
285 %! a = [-2.551474 -0.588505 -0.178409 0.123366 0.495271 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
286 %! -2.551474 -0.588505 -0.067751 0.123366 0.495271 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
287 %! -2.551474 -0.625231 -0.178409 0.123366 0.495271 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
288 %! -2.551474 -0.606868 -0.178409 0.090344 0.495271 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
289 %! -2.551474 -0.588505 -0.067751 0.123366 0.495271 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
290 %! -2.551474 -0.597687 -0.067751 0.192645 0.495271 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
291 %! -2.551474 -0.571522 -0.067751 0.106855 0.495271 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
292 %! -2.551474 -0.591566 -0.067751 0.146459 0.495271 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
293 %! -2.551474 -0.590801 -0.067751 0.140686 0.495271]; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
294 %! for m = 1:9 |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
295 %! q = quantile (x, p, 1, m).'; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
296 %! assert (q, a(m,:), 0.0001); |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
297 %! endfor |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
298 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
299 %!test |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
300 %! p = 0.5; |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
301 %! x = [0.112600, 0.114800, 0.052100, 0.236400, 0.139300 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
302 %! 0.171800, 0.727300, 0.204100, 0.453100, 0.158500 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
303 %! 0.279500, 0.797800, 0.329600, 0.556700, 0.730700 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
304 %! 0.428800, 0.875300, 0.647700, 0.628700, 0.816500 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
305 %! 0.933100, 0.931200, 0.963500, 0.779600, 0.846100]; |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
306 %! tol = 0.00001; |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
307 %! x(5,5) = NaN; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
308 %! assert (quantile (x, p, 1), [0.27950, 0.79780, 0.32960, 0.55670, 0.44460], tol); |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
309 %! x(1,1) = NaN; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
310 %! assert (quantile (x, p, 1), [0.35415, 0.79780, 0.32960, 0.55670, 0.44460], tol); |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
311 %! x(3,3) = NaN; |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
312 %! assert (quantile (x, p, 1), [0.35415, 0.79780, 0.42590, 0.55670, 0.44460], tol); |
7643
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
313 |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
314 %!test |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
315 %! sx = [2, 3, 4]; |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
316 %! x = rand (sx); |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
317 %! dim = 2; |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
318 %! p = 0.5; |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
319 %! yobs = quantile (x, p, dim); |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
320 %! yexp = median (x, dim); |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
321 %! assert (yobs, yexp); |
0220da981c2a
Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff
changeset
|
322 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11131
diff
changeset
|
323 %% Test input validation |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11131
diff
changeset
|
324 %!error quantile () |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11131
diff
changeset
|
325 %!error quantile (1, 2, 3, 4, 5) |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
326 %!error quantile (['A'; 'B'], 10) |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
327 %!error quantile (1:10, [true, false]) |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
328 %!error quantile (1:10, ones (2,2)) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11131
diff
changeset
|
329 %!error quantile (1, 1, 1.5) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11131
diff
changeset
|
330 %!error quantile (1, 1, 0) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11131
diff
changeset
|
331 %!error quantile (1, 1, 3) |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
332 %!error quantile ((1:5)', 0.5, 1, 0) |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
333 %!error quantile ((1:5)', 0.5, 1, 10) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11131
diff
changeset
|
334 |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
335 ## For the cumulative probability values in @var{p}, compute the |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
336 ## quantiles, @var{q} (the inverse of the cdf), for the sample, @var{x}. |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
337 ## |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
338 ## The optional input, @var{method}, refers to nine methods available in R |
16766
7268845c0a1e
avoid backquote in error messages, some uses in doc strings
John W. Eaton <jwe@octave.org>
parents:
14868
diff
changeset
|
339 ## (http://www.r-project.org/). The default is @var{method} = 7. |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
340 ## @seealso{prctile, quantile, statistics} |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
341 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
342 ## Author: Ben Abbott <bpabbott@mac.com> |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
343 ## Vectorized version: Jaroslav Hajek <highegg@gmail.com> |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11131
diff
changeset
|
344 ## Description: Quantile function of empirical samples |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
345 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
346 function inv = __quantile__ (x, p, method = 5) |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
347 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
348 if (nargin < 2 || nargin > 3) |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
349 print_usage (); |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
350 endif |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
351 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
352 if (isinteger (x) || islogical (x)) |
12585
4e32a1bb9096
mode.m, quantile.m: Return output of same class as input.
Rik <octave@nomad.inbox5.com>
parents:
12569
diff
changeset
|
353 x = double (x); |
4e32a1bb9096
mode.m, quantile.m: Return output of same class as input.
Rik <octave@nomad.inbox5.com>
parents:
12569
diff
changeset
|
354 endif |
4e32a1bb9096
mode.m, quantile.m: Return output of same class as input.
Rik <octave@nomad.inbox5.com>
parents:
12569
diff
changeset
|
355 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
356 ## set shape of quantiles to column vector. |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
357 p = p(:); |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
358 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
359 ## Save length and set shape of samples. |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
360 ## FIXME: does sort guarantee that NaN's come at the end? |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
361 x = sort (x); |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
362 m = sum (! isnan (x)); |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
363 [xr, xc] = size (x); |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
364 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
365 ## Initialize output values. |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
366 inv = Inf (class (x)) * (-(p < 0) + (p > 1)); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
367 inv = repmat (inv, 1, xc); |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
368 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
369 ## Do the work. |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
370 if (any (k = find ((p >= 0) & (p <= 1)))) |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
371 n = length (k); |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
372 p = p(k); |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
373 ## Special case of 1 row. |
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
374 if (xr == 1) |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
375 inv(k,:) = repmat (x, n, 1); |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
376 return; |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
377 endif |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
378 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
379 ## The column-distribution indices. |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
380 pcd = kron (ones (n, 1), xr*(0:xc-1)); |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
381 mm = kron (ones (n, 1), m); |
11120
a44f979a35ce
style fixes for some .m files
John W. Eaton <jwe@octave.org>
parents:
10821
diff
changeset
|
382 switch (method) |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
383 case {1, 2, 3} |
11120
a44f979a35ce
style fixes for some .m files
John W. Eaton <jwe@octave.org>
parents:
10821
diff
changeset
|
384 switch (method) |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
385 case 1 |
10549 | 386 p = max (ceil (kron (p, m)), 1); |
387 inv(k,:) = x(p + pcd); | |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
388 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
389 case 2 |
10549 | 390 p = kron (p, m); |
391 p_lr = max (ceil (p), 1); | |
392 p_rl = min (floor (p + 1), mm); | |
393 inv(k,:) = (x(p_lr + pcd) + x(p_rl + pcd))/2; | |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
394 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
395 case 3 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
396 ## Used by SAS, method PCTLDEF=2. |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
397 ## http://support.sas.com/onlinedoc/913/getDoc/en/statug.hlp/stdize_sect14.htm |
10549 | 398 t = max (kron (p, m), 1); |
399 t = roundb (t); | |
400 inv(k,:) = x(t + pcd); | |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
401 endswitch |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
402 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
403 otherwise |
11120
a44f979a35ce
style fixes for some .m files
John W. Eaton <jwe@octave.org>
parents:
10821
diff
changeset
|
404 switch (method) |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
405 case 4 |
10549 | 406 p = kron (p, m); |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
407 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
408 case 5 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
409 ## Used by Matlab. |
10549 | 410 p = kron (p, m) + 0.5; |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
411 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
412 case 6 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
413 ## Used by Minitab and SPSS. |
10549 | 414 p = kron (p, m+1); |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
415 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
416 case 7 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
417 ## Used by S and R. |
10549 | 418 p = kron (p, m-1) + 1; |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
419 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
420 case 8 |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
421 ## Median unbiased. |
10549 | 422 p = kron (p, m+1/3) + 1/3; |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
423 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
424 case 9 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
425 ## Approximately unbiased respecting order statistics. |
10549 | 426 p = kron (p, m+0.25) + 0.375; |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
427 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
428 otherwise |
12480
139f993936af
Uppercase variables in script error strings.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
429 error ("quantile: Unknown METHOD, '%d'", method); |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
430 endswitch |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
431 |
10549 | 432 ## Duplicate single values. |
12656
6b2f14af2360
Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents:
12653
diff
changeset
|
433 imm1 = (mm == 1); |
10549 | 434 x(2,imm1) = x(1,imm1); |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
435 |
10549 | 436 ## Interval indices. |
437 pi = max (min (floor (p), mm-1), 1); | |
438 pr = max (min (p - pi, 1), 0); | |
439 pi += pcd; | |
440 inv(k,:) = (1-pr) .* x(pi) + pr .* x(pi+1); | |
9899
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
441 endswitch |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
442 endif |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
443 |
9f25290a35e8
more private function and subfunction changes
John W. Eaton <jwe@octave.org>
parents:
9069
diff
changeset
|
444 endfunction |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
11131
diff
changeset
|
445 |