1 ## Copyright (C) 2012 Rik Wehbring
2 ## Copyright (C) 1995-2012 Kurt Hornik
4 ## This file is part of Octave.
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ## General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING. If not, see
18 ## <http://www.gnu.org/licenses/>.
21 ## @deftypefn {Function File} {} nbinpdf (@var{x}, @var{n}, @var{p})
22 ## For each element of @var{x}, compute the probability density function
23 ## (PDF) at @var{x} of the negative binomial distribution with
24 ## parameters @var{n} and @var{p}.
26 ## When @var{n} is integer this is the Pascal distribution. When
27 ## @var{n} is extended to real numbers this is the Polya distribution.
29 ## The number of failures in a Bernoulli experiment with success
30 ## probability @var{p} before the @var{n}-th success follows this
34 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
35 ## Description: PDF of the Pascal (negative binomial) distribution
37 function pdf = nbinpdf (x, n, p)
43 if (!isscalar (n) || !isscalar (p))
44 [retval, x, n, p] = common_size (x, n, p);
46 error ("nbinpdf: X, N, and P must be of common size or scalars");
50 if (iscomplex (x) || iscomplex (n) || iscomplex (p))
51 error ("nbinpdf: X, N, and P must not be complex");
54 if (isa (x, "single") || isa (n, "single") || isa (p, "single"))
55 pdf = NaN (size (x), "single");
60 ok = (x < Inf) & (x == fix (x)) & (n > 0) & (n < Inf) & (p >= 0) & (p <= 1);
66 if (isscalar (n) && isscalar (p))
67 pdf(k) = bincoeff (-n, x(k)) .* (p ^ n) .* ((p - 1) .^ x(k));
69 pdf(k) = bincoeff (-n(k), x(k)) .* (p(k) .^ n(k)) .* ((p(k) - 1) .^ x(k));
77 %! x = [-1 0 1 2 Inf];
78 %! y = [0 1/2 1/4 1/8 NaN];
79 %!assert(nbinpdf (x, ones(1,5), 0.5*ones(1,5)), y);
80 %!assert(nbinpdf (x, 1, 0.5*ones(1,5)), y);
81 %!assert(nbinpdf (x, ones(1,5), 0.5), y);
82 %!assert(nbinpdf (x, [0 1 NaN 1.5 Inf], 0.5), [NaN 1/2 NaN 1.875*0.5^1.5/4 NaN], eps);
83 %!assert(nbinpdf (x, 1, 0.5*[-1 NaN 4 1 1]), [NaN NaN NaN y(4:5)]);
84 %!assert(nbinpdf ([x, NaN], 1, 0.5), [y, NaN]);
86 %% Test class of input preserved
87 %!assert(nbinpdf (single([x, NaN]), 1, 0.5), single([y, NaN]));
88 %!assert(nbinpdf ([x, NaN], single(1), 0.5), single([y, NaN]));
89 %!assert(nbinpdf ([x, NaN], 1, single(0.5)), single([y, NaN]));
91 %% Test input validation
95 %!error nbinpdf (1,2,3,4)
96 %!error nbinpdf (ones(3),ones(2),ones(2))
97 %!error nbinpdf (ones(2),ones(3),ones(2))
98 %!error nbinpdf (ones(2),ones(2),ones(3))
99 %!error nbinpdf (i, 2, 2)
100 %!error nbinpdf (2, i, 2)
101 %!error nbinpdf (2, 2, i)