7017
|
1 ## Copyright (C) 1995, 1996, 1997, 2007 Kurt Hornik |
6349
|
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} {} nbinpdf (@var{x}, @var{n}, @var{p}) |
|
21 ## For each element of @var{x}, compute the probability density function |
|
22 ## (PDF) at @var{x} of the Pascal (negative binomial) distribution with |
|
23 ## parameters @var{n} and @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: PDF of the Pascal (negative binomial) distribution |
|
32 |
|
33 function pdf = nbinpdf (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 ("nbinpdf: x, n and p must be of common size or scalar"); |
|
43 endif |
|
44 endif |
|
45 |
|
46 pdf = zeros (size (x)); |
|
47 |
|
48 k = find (isnan (x) | (n < 1) | (n == Inf) | (n != round (n)) |
|
49 | (p < 0) | (p > 1)); |
|
50 if (any (k)) |
|
51 pdf(k) = NaN; |
|
52 endif |
|
53 |
|
54 ## Just for the fun of it ... |
|
55 k = find ((x == Inf) & (n > 0) & (n < Inf) & (n == round (n)) |
|
56 & (p == 0)); |
|
57 if (any (k)) |
|
58 pdf(k) = 1; |
|
59 endif |
|
60 |
|
61 k = find ((x >= 0) & (x < Inf) & (x == round (x)) & (n > 0) |
|
62 & (n < Inf) & (n == round (n)) & (p > 0) & (p <= 1)); |
|
63 if (any (k)) |
|
64 if (isscalar (n) && isscalar (p)) |
|
65 pdf(k) = bincoeff (-n, x(k)) .* (p ^ n) .* ((p - 1) .^ x(k)); |
|
66 else |
|
67 pdf(k) = bincoeff (-n(k), x(k)) .* (p(k) .^ n(k)) .* ((p(k) - 1) .^ x(k)); |
|
68 endif |
|
69 endif |
|
70 |
|
71 endfunction |