]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/betapdf.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / betapdf.m
1 ## Copyright (C) 2012 Rik Wehbring
2 ## Copyright (C) 1995-2012 Kurt Hornik
3 ## Copyright (C) 2010 Christos Dimitrakakis
4 ##
5 ## This file is part of Octave.
6 ##
7 ## Octave is free software; you can redistribute it and/or modify it
8 ## under the terms of the GNU General Public License as published by
9 ## the Free Software Foundation; either version 3 of the License, or (at
10 ## your option) any later version.
11 ##
12 ## Octave is distributed in the hope that it will be useful, but
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 ## General Public License for more details.
16 ##
17 ## You should have received a copy of the GNU General Public License
18 ## along with Octave; see the file COPYING.  If not, see
19 ## <http://www.gnu.org/licenses/>.
20
21 ## -*- texinfo -*-
22 ## @deftypefn {Function File} {} betapdf (@var{x}, @var{a}, @var{b})
23 ## For each element of @var{x}, compute the probability density function (PDF)
24 ## at @var{x} of the Beta distribution with parameters @var{a} and @var{b}.
25 ## @end deftypefn
26
27 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>, CD <christos.dimitrakakis@gmail.com>
28 ## Description: PDF of the Beta distribution
29
30 function pdf = betapdf (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 ("betapdf: 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 ("betapdf: 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) & (x < 1) & (a > 0) & (b > 0) & ((a != 1) | (b != 1));
57   if (isscalar (a) && isscalar (b))
58     pdf(k) = exp ((a - 1) * log (x(k))
59                   + (b - 1) * log (1 - x(k))
60                   + lgamma (a + b) - lgamma (a) - lgamma (b));
61   else
62     pdf(k) = exp ((a(k) - 1) .* log (x(k))
63                   + (b(k) - 1) .* log (1 - x(k))
64                   + lgamma (a(k) + b(k)) - lgamma (a(k)) - lgamma (b(k)));
65   endif
66
67   ## Most important special cases when the density is finite.
68   k = (x == 0) & (a == 1) & (b > 0) & (b != 1);
69   if (isscalar (a) && isscalar (b))
70     pdf(k) = exp (lgamma (a + b) - lgamma (a) - lgamma (b));
71   else
72     pdf(k) = exp (lgamma (a(k) + b(k)) - lgamma (a(k)) - lgamma (b(k)));
73   endif
74
75   k = (x == 1) & (b == 1) & (a > 0) & (a != 1);
76   if (isscalar (a) && isscalar (b))
77     pdf(k) = exp (lgamma (a + b) - lgamma (a) - lgamma (b));
78   else
79     pdf(k) = exp (lgamma (a(k) + b(k)) - lgamma (a(k)) - lgamma (b(k)));
80   endif
81
82   k = (x >= 0) & (x <= 1) & (a == 1) & (b == 1);
83   pdf(k) = 1;
84
85   ## Other special case when the density at the boundary is infinite.
86   k = (x == 0) & (a < 1);
87   pdf(k) = Inf;
88
89   k = (x == 1) & (b < 1);
90   pdf(k) = Inf;
91
92 endfunction
93
94
95 %!shared x,y
96 %! x = [-1 0 0.5 1 2];
97 %! y = [0 2 1 0 0];
98 %!assert(betapdf (x, ones(1,5), 2*ones(1,5)), y);
99 %!assert(betapdf (x, 1, 2*ones(1,5)), y);
100 %!assert(betapdf (x, ones(1,5), 2), y);
101 %!assert(betapdf (x, [0 NaN 1 1 1], 2), [NaN NaN y(3:5)]);
102 %!assert(betapdf (x, 1, 2*[0 NaN 1 1 1]), [NaN NaN y(3:5)]);
103 %!assert(betapdf ([x, NaN], 1, 2), [y, NaN]);
104
105 %% Test class of input preserved
106 %!assert(betapdf (single([x, NaN]), 1, 2), single([y, NaN]));
107 %!assert(betapdf ([x, NaN], single(1), 2), single([y, NaN]));
108 %!assert(betapdf ([x, NaN], 1, single(2)), single([y, NaN]));
109
110 %% Beta (1/2,1/2) == arcsine distribution
111 %!test
112 %! x = rand (10,1);
113 %! y = 1./(pi * sqrt (x.*(1-x)));
114 %! assert(betapdf (x, 1/2, 1/2), y, 50*eps);
115
116 %% Test large input values to betapdf
117 %!assert (betapdf(0.5, 1000, 1000), 35.678, 1e-3)
118
119 %% Test input validation
120 %!error betapdf ()
121 %!error betapdf (1)
122 %!error betapdf (1,2)
123 %!error betapdf (1,2,3,4)
124 %!error betapdf (ones(3),ones(2),ones(2))
125 %!error betapdf (ones(2),ones(3),ones(2))
126 %!error betapdf (ones(2),ones(2),ones(3))
127 %!error betapdf (i, 2, 2)
128 %!error betapdf (2, i, 2)
129 %!error betapdf (2, 2, i)
130