]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/binopdf.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / binopdf.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} {} binopdf (@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 binomial distribution with parameters @var{n}
24 ## and @var{p}, where @var{n} is the number of trials and @var{p} is the
25 ## probability of success.
26 ## @end deftypefn
27
28 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
29 ## Description: PDF of the binomial distribution
30
31 function pdf = binopdf (x, n, p)
32
33   if (nargin != 3)
34     print_usage ();
35   endif
36
37   if (! isscalar (n) || ! isscalar (p))
38     [retval, x, n, p] = common_size (x, n, p);
39     if (retval > 0)
40       error ("binopdf: X, N, and P must be of common size or scalars");
41     endif
42   endif
43
44   if (iscomplex (x) || iscomplex (n) || iscomplex (p))
45     error ("binopdf: X, N, and P must not be complex");
46   endif
47
48   if (isa (x, "single") || isa (n, "single") || isa (p, "single"));
49     pdf = zeros (size (x), "single");
50   else
51     pdf = zeros (size (x));
52   endif
53
54   k = (x == fix (x)) & (n == fix (n)) & (n >= 0) & (p >= 0) & (p <= 1);
55
56   pdf(! k) = NaN;
57
58   k &= ((x >= 0) & (x <= n));
59   if (isscalar (n) && isscalar (p))
60     pdf(k) = exp (gammaln (n+1) - gammaln (x(k)+1) - gammaln (n-x(k)+1)
61                   + x(k)*log (p) + (n-x(k))*log (1-p));
62   else
63     pdf(k) = exp (gammaln (n(k)+1) - gammaln (x(k)+1) - gammaln (n(k)-x(k)+1)
64                   + x(k).*log (p(k)) + (n(k)-x(k)).*log (1-p(k)));
65   endif
66
67 endfunction
68
69
70 %!shared x,y,tol
71 %! if (ismac ())
72 %!   tol = eps ();
73 %! else
74 %!   tol = 0;
75 %! endif
76 %! x = [-1 0 1 2 3];
77 %! y = [0 1/4 1/2 1/4 0];
78 %!assert(binopdf (x, 2*ones(1,5), 0.5*ones(1,5)), y, tol);
79 %!assert(binopdf (x, 2, 0.5*ones(1,5)), y, tol);
80 %!assert(binopdf (x, 2*ones(1,5), 0.5), y, tol);
81 %!assert(binopdf (x, 2*[0 -1 NaN 1.1 1], 0.5), [0 NaN NaN NaN 0]);
82 %!assert(binopdf (x, 2, 0.5*[0 -1 NaN 3 1]), [0 NaN NaN NaN 0]);
83 %!assert(binopdf ([x, NaN], 2, 0.5), [y, NaN], tol);
84
85 %% Test class of input preserved
86 %!assert(binopdf (single([x, NaN]), 2, 0.5), single([y, NaN]));
87 %!assert(binopdf ([x, NaN], single(2), 0.5), single([y, NaN]));
88 %!assert(binopdf ([x, NaN], 2, single(0.5)), single([y, NaN]));
89
90 %% Test input validation
91 %!error binopdf ()
92 %!error binopdf (1)
93 %!error binopdf (1,2)
94 %!error binopdf (1,2,3,4)
95 %!error binopdf (ones(3),ones(2),ones(2))
96 %!error binopdf (ones(2),ones(3),ones(2))
97 %!error binopdf (ones(2),ones(2),ones(3))
98 %!error binopdf (i, 2, 2)
99 %!error binopdf (2, i, 2)
100 %!error binopdf (2, 2, i)
101