]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/binocdf.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / binocdf.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} {} 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.
26 ## @end deftypefn
27
28 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
29 ## Description: CDF of the binomial distribution
30
31 function cdf = binocdf (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 ("binocdf: 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 ("binocdf: X, N, and P must not be complex");
46   endif
47
48   if (isa (x, "single") || isa (n, "single") || isa (p, "single"));
49     cdf = zeros (size (x), "single");
50   else
51     cdf = zeros (size (x));
52   endif
53
54   k = isnan (x) | !(n >= 0) | (n != fix (n)) | !(p >= 0) | !(p <= 1);
55   cdf(k) = NaN;
56
57   k = (x >= n) & (n >= 0) & (n == fix (n) & (p >= 0) & (p <= 1));
58   cdf(k) = 1;
59
60   k = (x >= 0) & (x < n) & (n == fix (n)) & (p >= 0) & (p <= 1);
61   tmp = floor (x(k));
62   if (isscalar (n) && isscalar (p))
63     cdf(k) = 1 - betainc (p, tmp + 1, n - tmp);
64   else
65     cdf(k) = 1 - betainc (p(k), tmp + 1, n(k) - tmp);
66   endif
67
68 endfunction
69
70
71 %!shared x,y
72 %! x = [-1 0 1 2 3];
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)]);
80
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]));
86
87 %% Test input validation
88 %!error binocdf ()
89 %!error binocdf (1)
90 %!error binocdf (1,2)
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)
98