]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/nbincdf.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / nbincdf.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} {} nbincdf (@var{x}, @var{n}, @var{p})
22 ## For each element of @var{x}, compute the cumulative distribution function
23 ## (CDF) at @var{x} of the negative binomial distribution with
24 ## 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: CDF of the Pascal (negative binomial) distribution
36
37 function cdf = nbincdf (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 ("nbincdf: 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 ("nbincdf: X, N, and P must not be complex");
52   endif
53
54   if (isa (x, "single") || isa (n, "single") || isa (p, "single"))
55     cdf = zeros (size (x), "single");
56   else
57     cdf = zeros (size (x));
58   endif
59
60   k = (isnan (x) | isnan (n) | (n < 1) | (n == Inf) 
61        | (p < 0) | (p > 1) | isnan (p));
62   cdf(k) = NaN;
63
64   k = (x == Inf) & (n > 0) & (n < Inf) & (p >= 0) & (p <= 1);
65   cdf(k) = 1;
66
67   k = ((x >= 0) & (x < Inf) & (x == fix (x))
68        & (n > 0) & (n < Inf) & (p > 0) & (p <= 1));
69   if (isscalar (n) && isscalar (p))
70     cdf(k) = 1 - betainc (1-p, x(k)+1, n);
71   else
72     cdf(k) = 1 - betainc (1-p(k), x(k)+1, n(k));
73   endif
74
75 endfunction
76
77
78 %!shared x,y
79 %! x = [-1 0 1 2 Inf];
80 %! y = [0 1/2 3/4 7/8 1];
81 %!assert(nbincdf (x, ones(1,5), 0.5*ones(1,5)), y);
82 %!assert(nbincdf (x, 1, 0.5*ones(1,5)), y);
83 %!assert(nbincdf (x, ones(1,5), 0.5), y);
84 %!assert(nbincdf ([x(1:3) 0 x(5)], [0 1 NaN 1.5 Inf], 0.5), [NaN 1/2 NaN nbinpdf(0,1.5,0.5) NaN], eps);
85 %!assert(nbincdf (x, 1, 0.5*[-1 NaN 4 1 1]), [NaN NaN NaN y(4:5)]);
86 %!assert(nbincdf ([x(1:2) NaN x(4:5)], 1, 0.5), [y(1:2) NaN y(4:5)]);
87
88 %% Test class of input preserved
89 %!assert(nbincdf ([x, NaN], 1, 0.5), [y, NaN]);
90 %!assert(nbincdf (single([x, NaN]), 1, 0.5), single([y, NaN]));
91 %!assert(nbincdf ([x, NaN], single(1), 0.5), single([y, NaN]));
92 %!assert(nbincdf ([x, NaN], 1, single(0.5)), single([y, NaN]));
93
94 %% Test input validation
95 %!error nbincdf ()
96 %!error nbincdf (1)
97 %!error nbincdf (1,2)
98 %!error nbincdf (1,2,3,4)
99 %!error nbincdf (ones(3),ones(2),ones(2))
100 %!error nbincdf (ones(2),ones(3),ones(2))
101 %!error nbincdf (ones(2),ones(2),ones(3))
102 %!error nbincdf (i, 2, 2)
103 %!error nbincdf (2, i, 2)
104 %!error nbincdf (2, 2, i)
105