]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/nbinpdf.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / nbinpdf.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} {} 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}.
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: PDF of the Pascal (negative binomial) distribution
36
37 function pdf = nbinpdf (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 ("nbinpdf: 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 ("nbinpdf: X, N, and P must not be complex");
52   endif
53
54   if (isa (x, "single") || isa (n, "single") || isa (p, "single"))
55     pdf = NaN (size (x), "single");
56   else
57     pdf = NaN (size (x));
58   endif
59
60   ok = (x < Inf) & (x == fix (x)) & (n > 0) & (n < Inf) & (p >= 0) & (p <= 1);
61
62   k = (x < 0) & ok;
63   pdf(k) = 0;
64
65   k = (x >= 0) & ok;
66   if (isscalar (n) && isscalar (p))
67     pdf(k) = bincoeff (-n, x(k)) .* (p ^ n) .* ((p - 1) .^ x(k));
68   else
69     pdf(k) = bincoeff (-n(k), x(k)) .* (p(k) .^ n(k)) .* ((p(k) - 1) .^ x(k));
70   endif
71   
72
73 endfunction
74
75
76 %!shared x,y
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]);
85
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]));
90
91 %% Test input validation
92 %!error nbinpdf ()
93 %!error nbinpdf (1)
94 %!error nbinpdf (1,2)
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)
102