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} {} nbininv (@var{x}, @var{n}, @var{p})
22 ## For each element of @var{x}, compute the quantile (the inverse of
23 ## the CDF) at @var{x} of the negative binomial distribution
24 ## with 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: Quantile function of the Pascal distribution
37 function inv = nbininv (x, n, p)
43 if (!isscalar (n) || !isscalar (p))
44 [retval, x, n, p] = common_size (x, n, p);
46 error ("nbininv: X, N, and P must be of common size or scalars");
50 if (iscomplex (x) || iscomplex (n) || iscomplex (p))
51 error ("nbininv: X, N, and P must not be complex");
54 if (isa (x, "single") || isa (n, "single") || isa (p, "single"))
55 inv = zeros (size (x), "single");
57 inv = zeros (size (x));
60 k = (isnan (x) | (x < 0) | (x > 1) | isnan (n) | (n < 1) | (n == Inf)
61 | isnan (p) | (p < 0) | (p > 1));
64 k = (x == 1) & (n > 0) & (n < Inf) & (p >= 0) & (p <= 1);
67 k = find ((x >= 0) & (x < 1) & (n > 0) & (n < Inf)
68 & (p > 0) & (p <= 1));
71 if (isscalar (n) && isscalar (p))
72 s = p ^ n * ones (size (k));
77 s(l) = s(l) + nbinpdf (m(l), n, p);
90 s(l) = s(l) + nbinpdf (m(l), n(l), p(l));
102 %! x = [-1 0 3/4 1 2];
103 %!assert(nbininv (x, ones(1,5), 0.5*ones(1,5)), [NaN 0 1 Inf NaN]);
104 %!assert(nbininv (x, 1, 0.5*ones(1,5)), [NaN 0 1 Inf NaN]);
105 %!assert(nbininv (x, ones(1,5), 0.5), [NaN 0 1 Inf NaN]);
106 %!assert(nbininv (x, [1 0 NaN Inf 1], 0.5), [NaN NaN NaN NaN NaN]);
107 %!assert(nbininv (x, [1 0 1.5 Inf 1], 0.5), [NaN NaN 2 NaN NaN]);
108 %!assert(nbininv (x, 1, 0.5*[1 -Inf NaN Inf 1]), [NaN NaN NaN NaN NaN]);
109 %!assert(nbininv ([x(1:2) NaN x(4:5)], 1, 0.5), [NaN 0 NaN Inf NaN]);
111 %% Test class of input preserved
112 %!assert(nbininv ([x, NaN], 1, 0.5), [NaN 0 1 Inf NaN NaN]);
113 %!assert(nbininv (single([x, NaN]), 1, 0.5), single([NaN 0 1 Inf NaN NaN]));
114 %!assert(nbininv ([x, NaN], single(1), 0.5), single([NaN 0 1 Inf NaN NaN]));
115 %!assert(nbininv ([x, NaN], 1, single(0.5)), single([NaN 0 1 Inf NaN NaN]));
117 %% Test input validation
120 %!error nbininv (1,2)
121 %!error nbininv (1,2,3,4)
122 %!error nbininv (ones(3),ones(2),ones(2))
123 %!error nbininv (ones(2),ones(3),ones(2))
124 %!error nbininv (ones(2),ones(2),ones(3))
125 %!error nbininv (i, 2, 2)
126 %!error nbininv (2, i, 2)
127 %!error nbininv (2, 2, i)