6349
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
|
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. |
6349
|
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/>. |
6349
|
18 |
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} nbininv (@var{x}, @var{n}, @var{p}) |
|
21 ## For each element of @var{x}, compute the quantile at @var{x} of the |
|
22 ## Pascal (negative binomial) distribution with parameters @var{n} and |
|
23 ## @var{p}. |
|
24 ## |
|
25 ## The number of failures in a Bernoulli experiment with success |
|
26 ## probability @var{p} before the @var{n}-th success follows this |
|
27 ## distribution. |
|
28 ## @end deftypefn |
|
29 |
|
30 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
|
31 ## Description: Quantile function of the Pascal distribution |
|
32 |
|
33 function inv = nbininv (x, n, p) |
|
34 |
|
35 if (nargin != 3) |
|
36 print_usage (); |
|
37 endif |
|
38 |
|
39 if (!isscalar(n) || !isscalar(p)) |
|
40 [retval, x, n, p] = common_size (x, n, p); |
|
41 if (retval > 0) |
|
42 error ("nbininv: x, n and p must be of common size or scalar"); |
|
43 endif |
|
44 endif |
|
45 |
|
46 inv = zeros (size (x)); |
|
47 |
|
48 k = find (isnan (x) | (x < 0) | (x > 1) | (n < 1) | (n == Inf) |
|
49 | (n != round (n)) | (p < 0) | (p > 1)); |
|
50 if (any (k)) |
|
51 inv(k) = NaN; |
|
52 endif |
|
53 |
|
54 k = find ((x == 1) & (n > 0) & (n < Inf) & (n == round (n)) |
|
55 & (p >= 0) & (p <= 1)); |
|
56 if (any (k)) |
|
57 inv(k) = Inf; |
|
58 endif |
|
59 |
|
60 k = find ((x >= 0) & (x < 1) & (n > 0) & (n < Inf) |
|
61 & (n == round (n)) & (p > 0) & (p <= 1)); |
|
62 if (any (k)) |
|
63 m = zeros (size (k)); |
|
64 x = x(k); |
|
65 if (isscalar (n) && isscalar (p)) |
|
66 s = p ^ n * ones (size(k)); |
|
67 while (1) |
|
68 l = find (s < x); |
|
69 if (any (l)) |
|
70 m(l) = m(l) + 1; |
|
71 s(l) = s(l) + nbinpdf (m(l), n, p); |
|
72 else |
|
73 break; |
|
74 endif |
|
75 endwhile |
|
76 else |
|
77 n = n(k); |
|
78 p = p(k); |
|
79 s = p .^ n; |
|
80 while (1) |
|
81 l = find (s < x); |
|
82 if (any (l)) |
|
83 m(l) = m(l) + 1; |
|
84 s(l) = s(l) + nbinpdf (m(l), n(l), p(l)); |
|
85 else |
|
86 break; |
|
87 endif |
|
88 endwhile |
|
89 endif |
|
90 inv(k) = m; |
|
91 endif |
|
92 |
|
93 endfunction |