7017
|
1 ## Copyright (C) 1995, 1996, 1997, 2005, 2006, 2007 Kurt Hornik |
5410
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
5410
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
5410
|
18 |
|
19 ## -*- texinfo -*- |
5411
|
20 ## @deftypefn {Function File} {} binoinv (@var{x}, @var{n}, @var{p}) |
5410
|
21 ## For each element of @var{x}, compute the quantile at @var{x} of the |
|
22 ## binomial distribution with parameters @var{n} and @var{p}. |
|
23 ## @end deftypefn |
|
24 |
5428
|
25 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
5410
|
26 ## Description: Quantile function of the binomial distribution |
|
27 |
5411
|
28 function inv = binoinv (x, n, p) |
5410
|
29 |
|
30 if (nargin != 3) |
6046
|
31 print_usage (); |
5410
|
32 endif |
|
33 |
|
34 if (!isscalar (n) || !isscalar (p)) |
|
35 [retval, x, n, p] = common_size (x, n, p); |
|
36 if (retval > 0) |
5411
|
37 error ("binoinv: x, n and p must be of common size or scalars"); |
5410
|
38 endif |
|
39 endif |
|
40 |
|
41 sz = size (x); |
|
42 inv = zeros (sz); |
|
43 |
|
44 k = find (!(x >= 0) | !(x <= 1) | !(n >= 0) | (n != round (n)) |
10549
|
45 | !(p >= 0) | !(p <= 1)); |
5410
|
46 if (any (k)) |
|
47 inv(k) = NaN; |
|
48 endif |
|
49 |
|
50 k = find ((x >= 0) & (x <= 1) & (n >= 0) & (n == round (n)) |
10549
|
51 & (p >= 0) & (p <= 1)); |
5410
|
52 if (any (k)) |
|
53 if (isscalar (n) && isscalar (p)) |
5411
|
54 cdf = binopdf (0, n, p) * ones (size(k)); |
5410
|
55 while (any (inv(k) < n)) |
10549
|
56 m = find (cdf < x(k)); |
|
57 if (any (m)) |
5410
|
58 inv(k(m)) = inv(k(m)) + 1; |
5411
|
59 cdf(m) = cdf(m) + binopdf (inv(k(m)), n, p); |
10549
|
60 else |
5410
|
61 break; |
10549
|
62 endif |
5410
|
63 endwhile |
|
64 else |
5482
|
65 cdf = binopdf (0, n(k), p(k)); |
5410
|
66 while (any (inv(k) < n(k))) |
10549
|
67 m = find (cdf < x(k)); |
|
68 if (any (m)) |
5410
|
69 inv(k(m)) = inv(k(m)) + 1; |
5411
|
70 cdf(m) = cdf(m) + binopdf (inv(k(m)), n(k(m)), p(k(m))); |
10549
|
71 else |
5410
|
72 break; |
10549
|
73 endif |
5410
|
74 endwhile |
|
75 endif |
|
76 endif |
|
77 |
|
78 endfunction |