Mercurial > octave-nkf
annotate scripts/statistics/distributions/binoinv.m @ 13949:0c15fece33ad
doc: Clarify binomial and gamma distribution paramters
* binocdf.m, binoinv.m, binopdf.m, binornd.m: Clarify that n is the
number of trials and p is the probability of success.
* gamcdf.m, gaminv.m, gaminv.m, gampdf.m, gamrnd.m: Clarify that a is
the shape parameter and b is the scale parameter.
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Sun, 27 Nov 2011 09:01:37 -0500 |
parents | 19b9f17d22af |
children | 72c96de7a403 |
rev | line source |
---|---|
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
1 ## Copyright (C) 2011 Rik Wehbring |
11523 | 2 ## Copyright (C) 1995-2011 Kurt Hornik |
5410 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
5410 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
5410 | 19 |
20 ## -*- texinfo -*- | |
5411 | 21 ## @deftypefn {Function File} {} binoinv (@var{x}, @var{n}, @var{p}) |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
22 ## For each element of @var{x}, compute the quantile (the inverse of |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
23 ## the CDF) at @var{x} of the binomial distribution with parameters |
13949
0c15fece33ad
doc: Clarify binomial and gamma distribution paramters
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13171
diff
changeset
|
24 ## @var{n} and @var{p}, where @var{n} is the number of trials and |
0c15fece33ad
doc: Clarify binomial and gamma distribution paramters
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13171
diff
changeset
|
25 ## @var{p} is the probability of success. |
5410 | 26 ## @end deftypefn |
27 | |
5428 | 28 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
5410 | 29 ## Description: Quantile function of the binomial distribution |
30 | |
5411 | 31 function inv = binoinv (x, n, p) |
5410 | 32 |
33 if (nargin != 3) | |
6046 | 34 print_usage (); |
5410 | 35 endif |
36 | |
37 if (!isscalar (n) || !isscalar (p)) | |
38 [retval, x, n, p] = common_size (x, n, p); | |
39 if (retval > 0) | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
40 error ("binoinv: X, N, and P must be of common size or scalars"); |
5410 | 41 endif |
42 endif | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
43 |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
44 if (iscomplex (x) || iscomplex (n) || iscomplex (p)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
45 error ("binoinv: X, N, and P must not be complex"); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
46 endif |
5410 | 47 |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
48 if (isa (x, "single") || isa (n, "single") || isa (p, "single")); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
49 inv = zeros (size (x), "single"); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
50 else |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
51 inv = zeros (size (x)); |
5410 | 52 endif |
53 | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
54 k = (!(x >= 0) | !(x <= 1) | !(n >= 0) | (n != fix (n)) | |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
55 !(p >= 0) | !(p <= 1)); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
56 inv(k) = NaN; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
57 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
58 k = find ((x >= 0) & (x <= 1) & (n >= 0) & (n == fix (n) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
59 & (p >= 0) & (p <= 1))); |
5410 | 60 if (any (k)) |
61 if (isscalar (n) && isscalar (p)) | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
62 cdf = binopdf (0, n, p) * ones (size (k)); |
5410 | 63 while (any (inv(k) < n)) |
10549 | 64 m = find (cdf < x(k)); |
65 if (any (m)) | |
5410 | 66 inv(k(m)) = inv(k(m)) + 1; |
5411 | 67 cdf(m) = cdf(m) + binopdf (inv(k(m)), n, p); |
10549 | 68 else |
5410 | 69 break; |
10549 | 70 endif |
5410 | 71 endwhile |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
72 else |
5482 | 73 cdf = binopdf (0, n(k), p(k)); |
5410 | 74 while (any (inv(k) < n(k))) |
10549 | 75 m = find (cdf < x(k)); |
76 if (any (m)) | |
5410 | 77 inv(k(m)) = inv(k(m)) + 1; |
5411 | 78 cdf(m) = cdf(m) + binopdf (inv(k(m)), n(k(m)), p(k(m))); |
10549 | 79 else |
5410 | 80 break; |
10549 | 81 endif |
5410 | 82 endwhile |
83 endif | |
84 endif | |
85 | |
86 endfunction | |
13171
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
87 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
88 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
89 %!shared x |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
90 %! x = [-1 0 0.5 1 2]; |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
91 %!assert(binoinv (x, 2*ones(1,5), 0.5*ones(1,5)), [NaN 0 1 2 NaN]); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
92 %!assert(binoinv (x, 2, 0.5*ones(1,5)), [NaN 0 1 2 NaN]); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
93 %!assert(binoinv (x, 2*ones(1,5), 0.5), [NaN 0 1 2 NaN]); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
94 %!assert(binoinv (x, 2*[0 -1 NaN 1.1 1], 0.5), [NaN NaN NaN NaN NaN]); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
95 %!assert(binoinv (x, 2, 0.5*[0 -1 NaN 3 1]), [NaN NaN NaN NaN NaN]); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
96 %!assert(binoinv ([x(1:2) NaN x(4:5)], 2, 0.5), [NaN 0 NaN 2 NaN]); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
97 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
98 %% Test class of input preserved |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
99 %!assert(binoinv ([x, NaN], 2, 0.5), [NaN 0 1 2 NaN NaN]); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
100 %!assert(binoinv (single([x, NaN]), 2, 0.5), single([NaN 0 1 2 NaN NaN])); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
101 %!assert(binoinv ([x, NaN], single(2), 0.5), single([NaN 0 1 2 NaN NaN])); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
102 %!assert(binoinv ([x, NaN], 2, single(0.5)), single([NaN 0 1 2 NaN NaN])); |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
103 |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
104 %% Test input validation |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
105 %!error binoinv () |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
106 %!error binoinv (1) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
107 %!error binoinv (1,2) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
108 %!error binoinv (1,2,3,4) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
109 %!error binoinv (ones(3),ones(2),ones(2)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
110 %!error binoinv (ones(2),ones(3),ones(2)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
111 %!error binoinv (ones(2),ones(2),ones(3)) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
112 %!error binoinv (i, 2, 2) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
113 %!error binoinv (2, i, 2) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
114 %!error binoinv (2, 2, i) |
19b9f17d22af
Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
115 |