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} {} binocdf (@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 binomial distribution with parameters @var{n} and
24 ## @var{p}, where @var{n} is the number of trials and @var{p} is the
25 ## probability of success.
28 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
29 ## Description: CDF of the binomial distribution
31 function cdf = binocdf (x, n, p)
37 if (!isscalar (n) || !isscalar (p))
38 [retval, x, n, p] = common_size (x, n, p);
40 error ("binocdf: X, N, and P must be of common size or scalars");
44 if (iscomplex (x) || iscomplex (n) || iscomplex (p))
45 error ("binocdf: X, N, and P must not be complex");
48 if (isa (x, "single") || isa (n, "single") || isa (p, "single"));
49 cdf = zeros (size (x), "single");
51 cdf = zeros (size (x));
54 k = isnan (x) | !(n >= 0) | (n != fix (n)) | !(p >= 0) | !(p <= 1);
57 k = (x >= n) & (n >= 0) & (n == fix (n) & (p >= 0) & (p <= 1));
60 k = (x >= 0) & (x < n) & (n == fix (n)) & (p >= 0) & (p <= 1);
62 if (isscalar (n) && isscalar (p))
63 cdf(k) = 1 - betainc (p, tmp + 1, n - tmp);
65 cdf(k) = 1 - betainc (p(k), tmp + 1, n(k) - tmp);
73 %! y = [0 1/4 3/4 1 1];
74 %!assert(binocdf (x, 2*ones(1,5), 0.5*ones(1,5)), y);
75 %!assert(binocdf (x, 2, 0.5*ones(1,5)), y);
76 %!assert(binocdf (x, 2*ones(1,5), 0.5), y);
77 %!assert(binocdf (x, 2*[0 -1 NaN 1.1 1], 0.5), [0 NaN NaN NaN 1]);
78 %!assert(binocdf (x, 2, 0.5*[0 -1 NaN 3 1]), [0 NaN NaN NaN 1]);
79 %!assert(binocdf ([x(1:2) NaN x(4:5)], 2, 0.5), [y(1:2) NaN y(4:5)]);
81 %% Test class of input preserved
82 %!assert(binocdf ([x, NaN], 2, 0.5), [y, NaN]);
83 %!assert(binocdf (single([x, NaN]), 2, 0.5), single([y, NaN]));
84 %!assert(binocdf ([x, NaN], single(2), 0.5), single([y, NaN]));
85 %!assert(binocdf ([x, NaN], 2, single(0.5)), single([y, NaN]));
87 %% Test input validation
91 %!error binocdf (1,2,3,4)
92 %!error binocdf (ones(3),ones(2),ones(2))
93 %!error binocdf (ones(2),ones(3),ones(2))
94 %!error binocdf (ones(2),ones(2),ones(3))
95 %!error binocdf (i, 2, 2)
96 %!error binocdf (2, i, 2)
97 %!error binocdf (2, 2, i)