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} {} 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}.
27 ## Author: TT <Teresa.Twaroch@ci.tuwien.ac.at>
28 ## Description: PDF of the Gamma distribution
30 function pdf = gampdf (x, a, b)
36 if (!isscalar (a) || !isscalar (b))
37 [retval, x, a, b] = common_size (x, a, b);
39 error ("gampdf: X, A, and B must be of common size or scalars");
43 if (iscomplex (x) || iscomplex (a) || iscomplex (b))
44 error ("gampdf: X, A, and B must not be complex");
47 if (isa (x, "single") || isa (a, "single") || isa (b, "single"))
48 pdf = zeros (size (x), "single");
50 pdf = zeros (size (x));
53 k = !(a > 0) | !(b > 0) | isnan (x);
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);
61 pdf(k) = (x(k) .^ (a(k) - 1)) ...
62 .* exp (- x(k) ./ b(k)) ./ gamma (a(k)) ./ (b(k) .^ a(k));
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));
70 pdf(k) = exp (- a(k) .* log (b(k)) + (a(k)-1) .* log (x(k))
71 - x(k) ./ b(k) - gammaln (a(k)));
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]);
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]));
92 %% Test input validation
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)