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