annotate scripts/statistics/distributions/betapdf.m @ 20644:4e307c55a2b5

Use isempty () rather than any () for faster code in inverse statistical distributions. betainv.m, binoinv.m, gaminv.m, poissinv.m: Use '! isempty (k)' rather than 'any (k)' for faster code.
author Rik <rik@octave.org>
date Sun, 11 Oct 2015 21:09:41 -0700
parents 9fc020886ae9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14138
72c96de7a403 maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents: 13171
diff changeset
1 ## Copyright (C) 2012 Rik Wehbring
19731
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 19309
diff changeset
2 ## Copyright (C) 1995-2015 Kurt Hornik
11269
628b03328765 improve range of betapdf function
Christos Dimitrakakis <christos.dimitrakakis@gmail.com>
parents: 10549
diff changeset
3 ## Copyright (C) 2010 Christos Dimitrakakis
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
4 ##
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
5 ## This file is part of Octave.
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
6 ##
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
7 ## Octave is free software; you can redistribute it and/or modify it
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
8 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
9 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
10 ## your option) any later version.
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
11 ##
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
12 ## Octave is distributed in the hope that it will be useful, but
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
13 ## WITHOUT ANY WARRANTY; without even the implied warranty of
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
15 ## General Public License for more details.
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
16 ##
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
17 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
18 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
19 ## <http://www.gnu.org/licenses/>.
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
20
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
21 ## -*- texinfo -*-
5411
bee21f388110 [project @ 2005-07-13 17:53:44 by jwe]
jwe
parents: 5410
diff changeset
22 ## @deftypefn {Function File} {} betapdf (@var{x}, @var{a}, @var{b})
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
23 ## For each element of @var{x}, compute the probability density function (PDF)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
24 ## at @var{x} of the Beta distribution with parameters @var{a} and @var{b}.
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
25 ## @end deftypefn
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
26
11269
628b03328765 improve range of betapdf function
Christos Dimitrakakis <christos.dimitrakakis@gmail.com>
parents: 10549
diff changeset
27 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>, CD <christos.dimitrakakis@gmail.com>
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
28 ## Description: PDF of the Beta distribution
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
29
5411
bee21f388110 [project @ 2005-07-13 17:53:44 by jwe]
jwe
parents: 5410
diff changeset
30 function pdf = betapdf (x, a, b)
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
31
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
32 if (nargin != 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5428
diff changeset
33 print_usage ();
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
34 endif
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
35
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
36 if (! isscalar (a) || ! isscalar (b))
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
37 [retval, x, a, b] = common_size (x, a, b);
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
38 if (retval > 0)
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
39 error ("betapdf: X, A, and B must be of common size or scalars");
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
40 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
41 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
42
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
43 if (iscomplex (x) || iscomplex (a) || iscomplex (b))
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
44 error ("betapdf: X, A, and B must not be complex");
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
45 endif
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
46
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
47 if (isa (x, "single") || isa (a, "single") || isa (b, "single"));
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
48 pdf = zeros (size (x), "single");
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
49 else
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
50 pdf = zeros (size (x));
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
51 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
52
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
53 k = !(a > 0) | !(b > 0) | isnan (x);
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
54 pdf(k) = NaN;
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
55
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
56 k = (x > 0) & (x < 1) & (a > 0) & (b > 0) & ((a != 1) | (b != 1));
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
57 if (isscalar (a) && isscalar (b))
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
58 pdf(k) = exp ((a - 1) * log (x(k))
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
59 + (b - 1) * log (1 - x(k))
19309
653ed3a6ba83 Use gammaln in preference to lgamma in core Octave code.
Rik <rik@octave.org>
parents: 17744
diff changeset
60 + gammaln (a + b) - gammaln (a) - gammaln (b));
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
61 else
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
62 pdf(k) = exp ((a(k) - 1) .* log (x(k))
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
63 + (b(k) - 1) .* log (1 - x(k))
19309
653ed3a6ba83 Use gammaln in preference to lgamma in core Octave code.
Rik <rik@octave.org>
parents: 17744
diff changeset
64 + gammaln (a(k) + b(k)) - gammaln (a(k)) - gammaln (b(k)));
11269
628b03328765 improve range of betapdf function
Christos Dimitrakakis <christos.dimitrakakis@gmail.com>
parents: 10549
diff changeset
65 endif
628b03328765 improve range of betapdf function
Christos Dimitrakakis <christos.dimitrakakis@gmail.com>
parents: 10549
diff changeset
66
628b03328765 improve range of betapdf function
Christos Dimitrakakis <christos.dimitrakakis@gmail.com>
parents: 10549
diff changeset
67 ## Most important special cases when the density is finite.
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
68 k = (x == 0) & (a == 1) & (b > 0) & (b != 1);
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
69 if (isscalar (a) && isscalar (b))
19309
653ed3a6ba83 Use gammaln in preference to lgamma in core Octave code.
Rik <rik@octave.org>
parents: 17744
diff changeset
70 pdf(k) = exp (gammaln (a + b) - gammaln (a) - gammaln (b));
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
71 else
19309
653ed3a6ba83 Use gammaln in preference to lgamma in core Octave code.
Rik <rik@octave.org>
parents: 17744
diff changeset
72 pdf(k) = exp (gammaln (a(k) + b(k)) - gammaln (a(k)) - gammaln (b(k)));
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
73 endif
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
74
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
75 k = (x == 1) & (b == 1) & (a > 0) & (a != 1);
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
76 if (isscalar (a) && isscalar (b))
19309
653ed3a6ba83 Use gammaln in preference to lgamma in core Octave code.
Rik <rik@octave.org>
parents: 17744
diff changeset
77 pdf(k) = exp (gammaln (a + b) - gammaln (a) - gammaln (b));
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
78 else
19309
653ed3a6ba83 Use gammaln in preference to lgamma in core Octave code.
Rik <rik@octave.org>
parents: 17744
diff changeset
79 pdf(k) = exp (gammaln (a(k) + b(k)) - gammaln (a(k)) - gammaln (b(k)));
11269
628b03328765 improve range of betapdf function
Christos Dimitrakakis <christos.dimitrakakis@gmail.com>
parents: 10549
diff changeset
80 endif
628b03328765 improve range of betapdf function
Christos Dimitrakakis <christos.dimitrakakis@gmail.com>
parents: 10549
diff changeset
81
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
82 k = (x >= 0) & (x <= 1) & (a == 1) & (b == 1);
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
83 pdf(k) = 1;
11269
628b03328765 improve range of betapdf function
Christos Dimitrakakis <christos.dimitrakakis@gmail.com>
parents: 10549
diff changeset
84
628b03328765 improve range of betapdf function
Christos Dimitrakakis <christos.dimitrakakis@gmail.com>
parents: 10549
diff changeset
85 ## Other special case when the density at the boundary is infinite.
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
86 k = (x == 0) & (a < 1);
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
87 pdf(k) = Inf;
11269
628b03328765 improve range of betapdf function
Christos Dimitrakakis <christos.dimitrakakis@gmail.com>
parents: 10549
diff changeset
88
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
89 k = (x == 1) & (b < 1);
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
90 pdf(k) = Inf;
11269
628b03328765 improve range of betapdf function
Christos Dimitrakakis <christos.dimitrakakis@gmail.com>
parents: 10549
diff changeset
91
5410
56e066f5efc1 [project @ 2005-07-13 17:43:35 by jwe]
jwe
parents:
diff changeset
92 endfunction
11355
34c5cd5a17ec betapdf.m: Add test for large values of alpha and beta.
Rik <octave@nomad.inbox5.com>
parents: 11269
diff changeset
93
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
94
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
95 %!shared x,y
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
96 %! x = [-1 0 0.5 1 2];
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
97 %! y = [0 2 1 0 0];
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
98 %!assert (betapdf (x, ones (1,5), 2*ones (1,5)), y)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
99 %!assert (betapdf (x, 1, 2*ones (1,5)), y)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
100 %!assert (betapdf (x, ones (1,5), 2), y)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
101 %!assert (betapdf (x, [0 NaN 1 1 1], 2), [NaN NaN y(3:5)])
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
102 %!assert (betapdf (x, 1, 2*[0 NaN 1 1 1]), [NaN NaN y(3:5)])
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
103 %!assert (betapdf ([x, NaN], 1, 2), [y, NaN])
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
104
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
105 ## Test class of input preserved
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
106 %!assert (betapdf (single ([x, NaN]), 1, 2), single ([y, NaN]))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
107 %!assert (betapdf ([x, NaN], single (1), 2), single ([y, NaN]))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
108 %!assert (betapdf ([x, NaN], 1, single (2)), single ([y, NaN]))
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
109
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
110 ## Beta (1/2,1/2) == arcsine distribution
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
111 %!test
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
112 %! x = rand (10,1);
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
113 %! y = 1./(pi * sqrt (x.*(1-x)));
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
114 %! assert (betapdf (x, 1/2, 1/2), y, 50*eps);
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
115
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
116 ## Test large input values to betapdf
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
117 %!assert (betapdf (0.5, 1000, 1000), 35.678, 1e-3)
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
118
19867
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19731
diff changeset
119 ## Test input validation
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
120 %!error betapdf ()
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
121 %!error betapdf (1)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
122 %!error betapdf (1,2)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
123 %!error betapdf (1,2,3,4)
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
124 %!error betapdf (ones (3), ones (2), ones (2))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
125 %!error betapdf (ones (2), ones (3), ones (2))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
126 %!error betapdf (ones (2), ones (2), ones (3))
13171
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
127 %!error betapdf (i, 2, 2)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
128 %!error betapdf (2, i, 2)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
129 %!error betapdf (2, 2, i)
19b9f17d22af Overhaul of statistical distribution functions
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
130