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} {} betainv (@var{x}, @var{a}, @var{b})
22 ## For each element of @var{x}, compute the quantile (the inverse of
23 ## the CDF) at @var{x} of the Beta distribution with parameters @var{a}
27 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
28 ## Description: Quantile function of the Beta distribution
30 function inv = betainv (x, a, b)
36 if (!isscalar (a) || !isscalar (b))
37 [retval, x, a, b] = common_size (x, a, b);
39 error ("betainv: X, A, and B must be of common size or scalars");
43 if (iscomplex (x) || iscomplex (a) || iscomplex (b))
44 error ("betainv: X, A, and B must not be complex");
47 if (isa (x, "single") || isa (a, "single") || isa (b, "single"))
48 inv = zeros (size (x), "single");
50 inv = zeros (size (x));
53 k = (x < 0) | (x > 1) | !(a > 0) | !(b > 0) | isnan (x);
56 k = (x == 1) & (a > 0) & (b > 0);
59 k = find ((x > 0) & (x < 1) & (a > 0) & (b > 0));
61 if (!isscalar (a) || !isscalar (b))
66 y = a / (a + b) * ones (size (k));
70 if (isa (y, "single"))
71 myeps = eps ("single");
78 y(l) = sqrt (myeps) * ones (length (l), 1);
80 l = find (y > 1 - myeps);
82 y(l) = 1 - sqrt (myeps) * ones (length (l), 1);
87 h = (betacdf (y_old, a, b) - x) ./ betapdf (y_old, a, b);
89 ind = find (y_new <= myeps);
91 y_new (ind) = y_old (ind) / 10;
93 ind = find (y_new >= 1 - myeps);
95 y_new (ind) = 1 - (1 - y_old (ind)) / 10;
98 if (max (abs (h)) < sqrt (myeps))
111 %! x = [-1 0 0.75 1 2];
112 %!assert(betainv (x, ones(1,5), 2*ones(1,5)), [NaN 0 0.5 1 NaN]);
113 %!assert(betainv (x, 1, 2*ones(1,5)), [NaN 0 0.5 1 NaN]);
114 %!assert(betainv (x, ones(1,5), 2), [NaN 0 0.5 1 NaN]);
115 %!assert(betainv (x, [1 0 NaN 1 1], 2), [NaN NaN NaN 1 NaN]);
116 %!assert(betainv (x, 1, 2*[1 0 NaN 1 1]), [NaN NaN NaN 1 NaN]);
117 %!assert(betainv ([x(1:2) NaN x(4:5)], 1, 2), [NaN 0 NaN 1 NaN]);
119 %% Test class of input preserved
120 %!assert(betainv ([x, NaN], 1, 2), [NaN 0 0.5 1 NaN NaN]);
121 %!assert(betainv (single([x, NaN]), 1, 2), single([NaN 0 0.5 1 NaN NaN]));
122 %!assert(betainv ([x, NaN], single(1), 2), single([NaN 0 0.5 1 NaN NaN]));
123 %!assert(betainv ([x, NaN], 1, single(2)), single([NaN 0 0.5 1 NaN NaN]));
125 %% Test input validation
128 %!error betainv (1,2)
129 %!error betainv (1,2,3,4)
130 %!error betainv (ones(3),ones(2),ones(2))
131 %!error betainv (ones(2),ones(3),ones(2))
132 %!error betainv (ones(2),ones(2),ones(3))
133 %!error betainv (i, 2, 2)
134 %!error betainv (2, i, 2)
135 %!error betainv (2, 2, i)