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