]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/nbininv.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / nbininv.m
1 ## Copyright (C) 2012 Rik Wehbring
2 ## Copyright (C) 1995-2012 Kurt Hornik
3 ##
4 ## This file is part of Octave.
5 ##
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.
10 ##
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.
15 ##
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/>.
19
20 ## -*- texinfo -*-
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}.
25 ##
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.
28 ## 
29 ## The number of failures in a Bernoulli experiment with success
30 ## probability @var{p} before the @var{n}-th success follows this
31 ## distribution.
32 ## @end deftypefn
33
34 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
35 ## Description: Quantile function of the Pascal distribution
36
37 function inv = nbininv (x, n, p)
38
39   if (nargin != 3)
40     print_usage ();
41   endif
42
43   if (!isscalar (n) || !isscalar (p))
44     [retval, x, n, p] = common_size (x, n, p);
45     if (retval > 0)
46       error ("nbininv: X, N, and P must be of common size or scalars");
47     endif
48   endif
49
50   if (iscomplex (x) || iscomplex (n) || iscomplex (p))
51     error ("nbininv: X, N, and P must not be complex");
52   endif
53
54   if (isa (x, "single") || isa (n, "single") || isa (p, "single"))
55     inv = zeros (size (x), "single");
56   else
57     inv = zeros (size (x));
58   endif
59
60   k = (isnan (x) | (x < 0) | (x > 1) | isnan (n) | (n < 1) | (n == Inf)
61        | isnan (p) | (p < 0) | (p > 1));
62   inv(k) = NaN;
63
64   k = (x == 1) & (n > 0) & (n < Inf) & (p >= 0) & (p <= 1);
65   inv(k) = Inf;
66
67   k = find ((x >= 0) & (x < 1) & (n > 0) & (n < Inf)
68             & (p > 0) & (p <= 1));
69   m = zeros (size (k));
70   x = x(k);
71   if (isscalar (n) && isscalar (p))
72     s = p ^ n * ones (size (k));
73     while (1)
74       l = find (s < x);
75       if (any (l))
76         m(l) = m(l) + 1;
77         s(l) = s(l) + nbinpdf (m(l), n, p);
78       else
79         break;
80       endif
81     endwhile
82   else
83     n = n(k);
84     p = p(k);
85     s = p .^ n;
86     while (1)
87       l = find (s < x);
88       if (any (l))
89         m(l) = m(l) + 1;
90         s(l) = s(l) + nbinpdf (m(l), n(l), p(l));
91       else
92         break;
93       endif
94     endwhile
95   endif
96   inv(k) = m;
97
98 endfunction
99
100
101 %!shared x
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]);
110
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]));
116
117 %% Test input validation
118 %!error nbininv ()
119 %!error nbininv (1)
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)
128