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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
5410
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
5410
|
18 |
|
19 ## -*- texinfo -*- |
5411
|
20 ## @deftypefn {Function File} {} gampdf (@var{x}, @var{a}, @var{b}) |
5410
|
21 ## For each element of @var{x}, return the probability density function |
|
22 ## (PDF) at @var{x} of the Gamma distribution with parameters @var{a} |
|
23 ## and @var{b}. |
5642
|
24 ## @seealso{gamma, gammaln, gammainc, gamcdf, gaminv, gamrnd} |
5410
|
25 ## @end deftypefn |
|
26 |
|
27 ## Author: TT <Teresa.Twaroch@ci.tuwien.ac.at> |
|
28 ## Description: PDF of the Gamma distribution |
|
29 |
5411
|
30 function pdf = gampdf (x, a, b) |
5410
|
31 |
|
32 if (nargin != 3) |
6046
|
33 print_usage (); |
5410
|
34 endif |
|
35 |
|
36 if (!isscalar (a) || !isscalar(b)) |
|
37 [retval, x, a, b] = common_size (x, a, b); |
|
38 if (retval > 0) |
5411
|
39 error ("gampdf: x, a and b must be of common size or scalars"); |
5410
|
40 endif |
|
41 endif |
|
42 |
|
43 sz = size(x); |
|
44 pdf = zeros (sz); |
|
45 |
|
46 k = find (!(a > 0) | !(b > 0) | isnan (x)); |
|
47 if (any (k)) |
|
48 pdf (k) = NaN; |
|
49 endif |
|
50 |
|
51 k = find ((x > 0) & (a > 0) & (a <= 1) & (b > 0)); |
|
52 if (any (k)) |
|
53 if (isscalar(a) && isscalar(b)) |
6777
|
54 pdf(k) = (x(k) .^ (a - 1)) ... |
|
55 .* exp(- x(k) ./ b) ./ gamma (a) ./ (b .^ a); |
5410
|
56 else |
6777
|
57 pdf(k) = (x(k) .^ (a(k) - 1)) ... |
|
58 .* exp(- x(k) ./ b(k)) ./ gamma (a(k)) ./ (b(k) .^ a(k)); |
5410
|
59 endif |
|
60 endif |
|
61 |
|
62 k = find ((x > 0) & (a > 1) & (b > 0)); |
|
63 if (any (k)) |
|
64 if (isscalar(a) && isscalar(b)) |
6777
|
65 pdf(k) = exp (- a .* log (b) + (a-1) .* log (x(k)) |
|
66 - x(k) ./ b - gammaln (a)); |
5410
|
67 else |
6777
|
68 pdf(k) = exp (- a(k) .* log (b(k)) + (a(k)-1) .* log (x(k)) |
|
69 - x(k) ./ b(k) - gammaln (a(k))); |
5410
|
70 endif |
|
71 endif |
|
72 |
|
73 endfunction |