]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/gamcdf.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / gamcdf.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} {} gamcdf (@var{x}, @var{a}, @var{b})
22 ## For each element of @var{x}, compute the cumulative distribution
23 ## function (CDF) at @var{x} of the Gamma distribution with shape
24 ## parameter @var{a} and scale @var{b}.
25 ## @end deftypefn
26
27 ## Author: TT <Teresa.Twaroch@ci.tuwien.ac.at>
28 ## Description: CDF of the Gamma distribution
29
30 function cdf = gamcdf (x, a, b)
31
32   if (nargin != 3)
33     print_usage ();
34   endif
35
36   if (!isscalar (a) || !isscalar (b))
37     [retval, x, a, b] = common_size (x, a, b);
38     if (retval > 0)
39       error ("gamcdf: X, A, and B must be of common size or scalars");
40     endif
41   endif
42
43   if (iscomplex (x) || iscomplex (a) || iscomplex (b))
44     error ("gamcdf: X, A, and B must not be complex");
45   endif
46
47   if (isa (x, "single") || isa (a, "single") || isa (b, "single"))
48     cdf = zeros (size (x), "single");
49   else
50     cdf = zeros (size (x));
51   endif
52
53   k = isnan (x) | !(a > 0) | !(a < Inf) | !(b > 0) | !(b < Inf);
54   cdf(k) = NaN;
55
56   k = (x > 0) & (a > 0) & (a < Inf) & (b > 0) & (b < Inf);
57   if (isscalar (a) && isscalar (b))
58     cdf(k) = gammainc (x(k) / b, a);
59   else
60     cdf(k) = gammainc (x(k) ./ b(k), a(k));
61   endif
62
63 endfunction
64
65
66 %!shared x,y
67 %! x = [-1 0 0.5 1 2 Inf];
68 %! y = [0, gammainc(x(2:end), 1)];
69 %!assert(gamcdf (x, ones(1,6), ones(1,6)), y);
70 %!assert(gamcdf (x, 1, ones(1,6)), y);
71 %!assert(gamcdf (x, ones(1,6), 1), y);
72 %!assert(gamcdf (x, [0 -Inf NaN Inf 1 1], 1), [NaN NaN NaN NaN y(5:6)]);
73 %!assert(gamcdf (x, 1, [0 -Inf NaN Inf 1 1]), [NaN NaN NaN NaN y(5:6)]);
74 %!assert(gamcdf ([x(1:2) NaN x(4:6)], 1, 1), [y(1:2) NaN y(4:6)]);
75
76 %% Test class of input preserved
77 %!assert(gamcdf ([x, NaN], 1, 1), [y, NaN]);
78 %!assert(gamcdf (single([x, NaN]), 1, 1), single([y, NaN]), eps("single"));
79
80 %% Test input validation
81 %!error gamcdf ()
82 %!error gamcdf (1)
83 %!error gamcdf (1,2)
84 %!error gamcdf (1,2,3,4)
85 %!error gamcdf (ones(3),ones(2),ones(2))
86 %!error gamcdf (ones(2),ones(3),ones(2))
87 %!error gamcdf (ones(2),ones(2),ones(3))
88 %!error gamcdf (i, 2, 2)
89 %!error gamcdf (2, i, 2)
90 %!error gamcdf (2, 2, i)
91