]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/gampdf.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / gampdf.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} {} gampdf (@var{x}, @var{a}, @var{b})
22 ## For each element of @var{x}, return the probability density function
23 ## (PDF) at @var{x} of the Gamma distribution with shape parameter
24 ## @var{a} and scale @var{b}.
25 ## @end deftypefn
26
27 ## Author: TT <Teresa.Twaroch@ci.tuwien.ac.at>
28 ## Description: PDF of the Gamma distribution
29
30 function pdf = gampdf (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 ("gampdf: 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 ("gampdf: X, A, and B must not be complex");
45   endif
46
47   if (isa (x, "single") || isa (a, "single") || isa (b, "single"))
48     pdf = zeros (size (x), "single");
49   else
50     pdf = zeros (size (x));
51   endif
52
53   k = !(a > 0) | !(b > 0) | isnan (x);
54   pdf(k) = NaN;
55
56   k = (x >= 0) & (a > 0) & (a <= 1) & (b > 0);
57   if (isscalar (a) && isscalar (b))
58     pdf(k) = (x(k) .^ (a - 1)) ...
59               .* exp (- x(k) / b) / gamma (a) / (b ^ a);
60   else
61     pdf(k) = (x(k) .^ (a(k) - 1)) ...
62               .* exp (- x(k) ./ b(k)) ./ gamma (a(k)) ./ (b(k) .^ a(k));
63   endif
64
65   k = (x >= 0) & (a > 1) & (b > 0);
66   if (isscalar (a) && isscalar (b))
67     pdf(k) = exp (- a * log (b) + (a-1) * log (x(k))
68                   - x(k) / b - gammaln (a));
69   else
70     pdf(k) = exp (- a(k) .* log (b(k)) + (a(k)-1) .* log (x(k))
71                   - x(k) ./ b(k) - gammaln (a(k)));
72   endif
73
74 endfunction
75
76
77 %!shared x,y
78 %! x = [-1 0 0.5 1 Inf];
79 %! y = [0 exp(-x(2:end))];
80 %!assert(gampdf (x, ones(1,5), ones(1,5)), y);
81 %!assert(gampdf (x, 1, ones(1,5)), y);
82 %!assert(gampdf (x, ones(1,5), 1), y);
83 %!assert(gampdf (x, [0 -Inf NaN Inf 1], 1), [NaN NaN NaN NaN y(5)]);
84 %!assert(gampdf (x, 1, [0 -Inf NaN Inf 1]), [NaN NaN NaN 0 y(5)]);
85 %!assert(gampdf ([x, NaN], 1, 1), [y, NaN]);
86
87 %% Test class of input preserved
88 %!assert(gampdf (single([x, NaN]), 1, 1), single([y, NaN]));
89 %!assert(gampdf ([x, NaN], single(1), 1), single([y, NaN]));
90 %!assert(gampdf ([x, NaN], 1, single(1)), single([y, NaN]));
91
92 %% Test input validation
93 %!error gampdf ()
94 %!error gampdf (1)
95 %!error gampdf (1,2)
96 %!error gampdf (1,2,3,4)
97 %!error gampdf (ones(3),ones(2),ones(2))
98 %!error gampdf (ones(2),ones(3),ones(2))
99 %!error gampdf (ones(2),ones(2),ones(3))
100 %!error gampdf (i, 2, 2)
101 %!error gampdf (2, i, 2)
102 %!error gampdf (2, 2, i)
103