5410
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
5411
|
21 ## @deftypefn {Function File} {} gampdf (@var{x}, @var{a}, @var{b}) |
5410
|
22 ## For each element of @var{x}, return the probability density function |
|
23 ## (PDF) at @var{x} of the Gamma distribution with parameters @var{a} |
|
24 ## and @var{b}. |
5642
|
25 ## @seealso{gamma, gammaln, gammainc, gamcdf, gaminv, gamrnd} |
5410
|
26 ## @end deftypefn |
|
27 |
|
28 ## Author: TT <Teresa.Twaroch@ci.tuwien.ac.at> |
|
29 ## Description: PDF of the Gamma distribution |
|
30 |
5411
|
31 function pdf = gampdf (x, a, b) |
5410
|
32 |
|
33 if (nargin != 3) |
5411
|
34 usage ("gampdf (x, a, b)"); |
5410
|
35 endif |
|
36 |
|
37 if (!isscalar (a) || !isscalar(b)) |
|
38 [retval, x, a, b] = common_size (x, a, b); |
|
39 if (retval > 0) |
5411
|
40 error ("gampdf: x, a and b must be of common size or scalars"); |
5410
|
41 endif |
|
42 endif |
|
43 |
|
44 sz = size(x); |
|
45 pdf = zeros (sz); |
|
46 |
|
47 k = find (!(a > 0) | !(b > 0) | isnan (x)); |
|
48 if (any (k)) |
|
49 pdf (k) = NaN; |
|
50 endif |
|
51 |
|
52 k = find ((x > 0) & (a > 0) & (a <= 1) & (b > 0)); |
|
53 if (any (k)) |
|
54 if (isscalar(a) && isscalar(b)) |
|
55 pdf(k) = ((b .^ a) .* (x(k) .^ (a - 1)) |
|
56 .* exp(-b .* x(k)) ./ gamma (a)); |
|
57 else |
|
58 pdf(k) = ((b(k) .^ a(k)) .* (x(k) .^ (a(k) - 1)) |
|
59 .* exp(-b(k) .* x(k)) ./ gamma (a(k))); |
|
60 endif |
|
61 endif |
|
62 |
|
63 k = find ((x > 0) & (a > 1) & (b > 0)); |
|
64 if (any (k)) |
|
65 if (isscalar(a) && isscalar(b)) |
|
66 pdf(k) = exp (a .* log (b) + (a-1) .* log (x(k)) |
|
67 - b .* x(k) - gammaln (a)); |
|
68 else |
|
69 pdf(k) = exp (a(k) .* log (b(k)) + (a(k)-1) .* log (x(k)) |
|
70 - b(k) .* x(k) - gammaln (a(k))); |
|
71 endif |
|
72 endif |
|
73 |
|
74 endfunction |