annotate scripts/statistics/base/__quantile__.m @ 11894:af0adfbd3d16 release-3-0-x

workaround missing roundb
author Jaroslav Hajek <highegg@gmail.com>
date Mon, 08 Dec 2008 07:47:35 +0100
parents 221d555a5b91
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11889
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
1 ## Copyright (C) 2008 Ben Abbott, Jaroslav Hajek
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
2 ##
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
3 ## This file is part of Octave.
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
4 ##
221d555a5b91 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
221d555a5b91 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
221d555a5b91 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
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
8 ## your option) any later version.
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
9 ##
221d555a5b91 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
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
221d555a5b91 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
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
13 ## General Public License for more details.
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
14 ##
221d555a5b91 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
221d555a5b91 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
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
18
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
19 ## -*- texinfo -*-
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
20 ## @deftypefn {Function File} {@var{q} =} __quantile__ (@var{x}, @var{p})
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
21 ## @deftypefnx {Function File} {@var{q} =} __quantile__ (@var{x}, @var{p}, @var{method})
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
22 ## For the cumulative probability values in @var{p}, compute the
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
23 ## quantiles, @var{q} (the inverse of the cdf), for the sample, @var{x}.
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
24 ##
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
25 ## The optional input, @var{method}, refers to nine methods available in R
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
26 ## (http://www.r-project.org/). The default is @var{method} = 7. For more
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
27 ## detail, see `help quantile'.
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
28 ## @seealso{prctile, quantile, statistics}
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
29 ## @end deftypefn
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
30
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
31 ## Author: Ben Abbott <bpabbott@mac.com>
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
32 ## Vectorized version: Jaroslav Hajek <highegg@gmail.com>
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
33 ## Description: Quantile function of a empirical samples
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
34
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
35 function inv = __quantile__ (x, p, method = 5)
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
36
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
37 if (nargin < 2 || nargin > 3)
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
38 print_usage ();
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
39 endif
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
40
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
41 if (! ismatrix (x))
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
42 error ("quantile: x must be a matrix");
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
43 endif
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
44
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
45 ## Save length and set shape of quantiles.
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
46 n = numel (p);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
47 p = p(:);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
48
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
49 ## Save length and set shape of samples.
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
50 ## TODO: does sort guarantee that NaN's come at the end?
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
51 x = sort (x);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
52 m = sum (! isnan (x));
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
53 mx = size (x, 1);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
54 nx = size (x, 2);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
55
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
56 ## Initialize output values.
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
57 inv = Inf*(-(p < 0) + (p > 1));
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
58 inv = repmat (inv, 1, nx);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
59
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
60 ## Do the work.
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
61 if (any(k = find((p >= 0) & (p <= 1))))
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
62 n = length (k);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
63 p = p (k);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
64 ## Special case.
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
65 if (mx == 1)
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
66 inv(k,:) = repmat (x, n, 1);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
67 return
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
68 endif
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
69
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
70 ## The column-distribution indices.
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
71 pcd = kron (ones (n, 1), mx*(0:nx-1));
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
72 mm = kron (ones (n, 1), m);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
73 switch method
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
74 case {1, 2, 3}
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
75 switch method
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
76 case 1
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
77 p = max (ceil (kron (p, m)), 1);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
78 inv(k,:) = x(p + pcd);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
79
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
80 case 2
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
81 p = kron (p, m);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
82 p_lr = max (ceil (p), 1);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
83 p_rl = min (floor (p + 1), mm);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
84 inv(k,:) = (x(p_lr + pcd) + x(p_rl + pcd))/2;
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
85
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
86 case 3
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
87 ## Used by SAS, method PCTLDEF=2.
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
88 ## http://support.sas.com/onlinedoc/913/getDoc/en/statug.hlp/stdize_sect14.htm
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
89 t = max (kron (p, m), 1);
11894
af0adfbd3d16 workaround missing roundb
Jaroslav Hajek <highegg@gmail.com>
parents: 11889
diff changeset
90 # t = roundb (t);
af0adfbd3d16 workaround missing roundb
Jaroslav Hajek <highegg@gmail.com>
parents: 11889
diff changeset
91 # roundb unsupported in 3.0.x, so supply a hack
af0adfbd3d16 workaround missing roundb
Jaroslav Hajek <highegg@gmail.com>
parents: 11889
diff changeset
92 t1 = round (t);
af0adfbd3d16 workaround missing roundb
Jaroslav Hajek <highegg@gmail.com>
parents: 11889
diff changeset
93 t1m = (abs (t - t1) == 0.5) & (mod (t1, 2) == 1);
af0adfbd3d16 workaround missing roundb
Jaroslav Hajek <highegg@gmail.com>
parents: 11889
diff changeset
94 t1 (t1m) += 2 * (t-t1)(t1m);
af0adfbd3d16 workaround missing roundb
Jaroslav Hajek <highegg@gmail.com>
parents: 11889
diff changeset
95 t = t1;
11889
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
96 inv(k,:) = x(t + pcd);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
97 endswitch
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
98
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
99 otherwise
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
100 switch method
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
101 case 4
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
102 p = kron (p, m);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
103
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
104 case 5
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
105 ## Used by Matlab.
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
106 p = kron (p, m) + 0.5;
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
107
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
108 case 6
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
109 ## Used by Minitab and SPSS.
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
110 p = kron (p, m+1);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
111
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
112 case 7
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
113 ## Used by S and R.
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
114 p = kron (p, m-1) + 1;
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
115
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
116 case 8
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
117 ## Median unbiased .
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
118 p = kron (p, m+1/3) + 1/3;
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
119
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
120 case 9
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
121 ## Approximately unbiased respecting order statistics.
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
122 p = kron (p, m+0.25) + 0.375;
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
123
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
124 otherwise
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
125 error ("quantile: Unknown method, '%d'",method)
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
126 endswitch
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
127
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
128 ## Duplicate single values.
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
129 imm1 = mm == 1;
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
130 x(2,imm1) = x(1,imm1);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
131
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
132 ## Interval indices.
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
133 pi = max (min (floor (p), mm-1), 1);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
134 pr = max (min (p - pi, 1), 0);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
135 pi += pcd;
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
136 inv(k,:) = (1-pr) .* x(pi) + pr .* x(pi+1);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
137 endswitch
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
138 endif
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
139
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
140 endfunction
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
141
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
142 %!test
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
143 %! p = 0.5;
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
144 %! x = sort (rand (11));
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
145 %! q = __quantile__ (x, p);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
146 %! assert (q, x(6,:))
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
147
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
148 %!test
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
149 %! p = [0.00, 0.25, 0.50, 0.75, 1.00];
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
150 %! x = [1; 2; 3; 4];
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
151 %! a = [1.0000 1.0000 2.0000 3.0000 4.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
152 %! 1.0000 1.5000 2.5000 3.5000 4.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
153 %! 1.0000 1.0000 2.0000 3.0000 4.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
154 %! 1.0000 1.0000 2.0000 3.0000 4.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
155 %! 1.0000 1.5000 2.5000 3.5000 4.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
156 %! 1.0000 1.2500 2.5000 3.7500 4.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
157 %! 1.0000 1.7500 2.5000 3.2500 4.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
158 %! 1.0000 1.4167 2.5000 3.5833 4.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
159 %! 1.0000 1.4375 2.5000 3.5625 4.0000];
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
160 %! for m = (1:9)
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
161 %! q = __quantile__ (x, p, m).';
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
162 %! assert (q, a(m,:), 0.0001)
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
163 %! endfor
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
164
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
165 %!test
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
166 %! p = [0.00, 0.25, 0.50, 0.75, 1.00];
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
167 %! x = [1; 2; 3; 4; 5];
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
168 %! a = [1.0000 2.0000 3.0000 4.0000 5.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
169 %! 1.0000 2.0000 3.0000 4.0000 5.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
170 %! 1.0000 1.0000 2.0000 4.0000 5.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
171 %! 1.0000 1.2500 2.5000 3.7500 5.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
172 %! 1.0000 1.7500 3.0000 4.2500 5.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
173 %! 1.0000 1.5000 3.0000 4.5000 5.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
174 %! 1.0000 2.0000 3.0000 4.0000 5.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
175 %! 1.0000 1.6667 3.0000 4.3333 5.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
176 %! 1.0000 1.6875 3.0000 4.3125 5.0000];
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
177 %! for m = (1:9)
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
178 %! q = __quantile__ (x, p, m).';
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
179 %! assert (q, a(m,:), 0.0001)
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
180 %! endfor
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
181
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
182 %!test
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
183 %! p = [0.00, 0.25, 0.50, 0.75, 1.00];
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
184 %! x = [1; 2; 5; 9];
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
185 %! a = [1.0000 1.0000 2.0000 5.0000 9.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
186 %! 1.0000 1.5000 3.5000 7.0000 9.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
187 %! 1.0000 1.0000 2.0000 5.0000 9.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
188 %! 1.0000 1.0000 2.0000 5.0000 9.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
189 %! 1.0000 1.5000 3.5000 7.0000 9.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
190 %! 1.0000 1.2500 3.5000 8.0000 9.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
191 %! 1.0000 1.7500 3.5000 6.0000 9.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
192 %! 1.0000 1.4167 3.5000 7.3333 9.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
193 %! 1.0000 1.4375 3.5000 7.2500 9.0000];
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
194 %! for m = (1:9)
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
195 %! q = __quantile__ (x, p, m).';
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
196 %! assert (q, a(m,:), 0.0001)
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
197 %! endfor
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
198
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
199 %!test
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
200 %! p = [0.00, 0.25, 0.50, 0.75, 1.00];
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
201 %! x = [1; 2; 5; 9; 11];
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
202 %! a = [1.0000 2.0000 5.0000 9.0000 11.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
203 %! 1.0000 2.0000 5.0000 9.0000 11.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
204 %! 1.0000 1.0000 2.0000 9.0000 11.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
205 %! 1.0000 1.2500 3.5000 8.0000 11.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
206 %! 1.0000 1.7500 5.0000 9.5000 11.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
207 %! 1.0000 1.5000 5.0000 10.0000 11.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
208 %! 1.0000 2.0000 5.0000 9.0000 11.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
209 %! 1.0000 1.6667 5.0000 9.6667 11.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
210 %! 1.0000 1.6875 5.0000 9.6250 11.0000];
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
211 %! for m = (1:9)
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
212 %! q = __quantile__ (x, p, m).';
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
213 %! assert (q, a(m,:), 0.0001)
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
214 %! endfor
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
215
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
216 %!test
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
217 %! p = [0.00, 0.25, 0.50, 0.75, 1.00];
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
218 %! x = [16; 11; 15; 12; 15; 8; 11; 12; 6; 10];
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
219 %! a = [6.0000 10.0000 11.0000 15.0000 16.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
220 %! 6.0000 10.0000 11.5000 15.0000 16.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
221 %! 6.0000 8.0000 11.0000 15.0000 16.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
222 %! 6.0000 9.0000 11.0000 13.5000 16.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
223 %! 6.0000 10.0000 11.5000 15.0000 16.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
224 %! 6.0000 9.5000 11.5000 15.0000 16.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
225 %! 6.0000 10.2500 11.5000 14.2500 16.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
226 %! 6.0000 9.8333 11.5000 15.0000 16.0000
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
227 %! 6.0000 9.8750 11.5000 15.0000 16.0000];
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
228 %! for m = (1:9)
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
229 %! q = __quantile__ (x, p, m).';
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
230 %! assert (q, a(m,:), 0.0001)
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
231 %! endfor
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
232
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
233 %!test
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
234 %! p = [0.00, 0.25, 0.50, 0.75, 1.00];
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
235 %! x = [-0.58851; 0.40048; 0.49527; -2.551500; -0.52057; ...
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
236 %! -0.17841; 0.057322; -0.62523; 0.042906; 0.12337];
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
237 %! a = [-2.551474 -0.588505 -0.178409 0.123366 0.495271
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
238 %! -2.551474 -0.588505 -0.067751 0.123366 0.495271
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
239 %! -2.551474 -0.625231 -0.178409 0.123366 0.495271
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
240 %! -2.551474 -0.606868 -0.178409 0.090344 0.495271
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
241 %! -2.551474 -0.588505 -0.067751 0.123366 0.495271
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
242 %! -2.551474 -0.597687 -0.067751 0.192645 0.495271
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
243 %! -2.551474 -0.571522 -0.067751 0.106855 0.495271
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
244 %! -2.551474 -0.591566 -0.067751 0.146459 0.495271
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
245 %! -2.551474 -0.590801 -0.067751 0.140686 0.495271];
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
246 %! for m = (1:9)
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
247 %! q = __quantile__ (x, p, m).';
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
248 %! assert (q, a(m,:), 0.0001)
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
249 %! endfor
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
250
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
251 %!test
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
252 %! p = 0.5;
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
253 %! x = [0.112600, 0.114800, 0.052100, 0.236400, 0.139300
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
254 %! 0.171800, 0.727300, 0.204100, 0.453100, 0.158500
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
255 %! 0.279500, 0.797800, 0.329600, 0.556700, 0.730700
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
256 %! 0.428800, 0.875300, 0.647700, 0.628700, 0.816500
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
257 %! 0.933100, 0.931200, 0.963500, 0.779600, 0.846100];
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
258 %! tol = 0.00001;
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
259 %! x(5,5) = NaN;
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
260 %! assert (__quantile__ (x, p), [0.27950, 0.79780, 0.32960, 0.55670, 0.44460], tol);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
261 %! x(1,1) = NaN;
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
262 %! assert (__quantile__ (x, p), [0.35415, 0.79780, 0.32960, 0.55670, 0.44460], tol);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
263 %! x(3,3) = NaN;
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
264 %! assert (__quantile__ (x, p), [0.35415, 0.79780, 0.42590, 0.55670, 0.44460], tol);
221d555a5b91 Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents:
diff changeset
265