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 -*- |
|
21 ## @deftypefn {Function File} {} beta_inv (@var{x}, @var{a}, @var{b}) |
|
22 ## For each component of @var{x}, compute the quantile (the inverse of |
|
23 ## the CDF) at @var{x} of the Beta distribution with parameters @var{a} |
|
24 ## and @var{b}. |
|
25 ## @end deftypefn |
|
26 |
|
27 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
28 ## Description: Quantile function of the Beta distribution |
|
29 |
|
30 function inv = beta_inv (x, a, b) |
|
31 |
|
32 if (nargin != 3) |
|
33 usage ("beta_inv (x, a, b)"); |
|
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 ("beta_inv: x, a and b must be of common size or scalars"); |
|
40 endif |
|
41 endif |
|
42 |
|
43 sz = size (x); |
|
44 inv = zeros (sz); |
|
45 |
|
46 k = find ((x < 0) | (x > 1) | !(a > 0) | !(b > 0) | isnan (x)); |
|
47 if (any (k)) |
|
48 inv (k) = NaN; |
|
49 endif |
|
50 |
|
51 k = find ((x == 1) & (a > 0) & (b > 0)); |
|
52 if (any (k)) |
|
53 inv (k) = 1; |
|
54 endif |
|
55 |
|
56 k = find ((x > 0) & (x < 1) & (a > 0) & (b > 0)); |
|
57 if (any (k)) |
|
58 if (!isscalar(a) || !isscalar(b)) |
|
59 a = a (k); |
|
60 b = b (k); |
|
61 y = a ./ (a + b); |
|
62 else |
|
63 y = a / (a + b) * ones (size (k)); |
|
64 endif |
|
65 x = x (k); |
|
66 l = find (y < eps); |
|
67 if (any (l)) |
|
68 y(l) = sqrt (eps) * ones (length (l), 1); |
|
69 endif |
|
70 l = find (y > 1 - eps); |
|
71 if (any (l)) |
|
72 y(l) = 1 - sqrt (eps) * ones (length (l), 1); |
|
73 endif |
|
74 |
|
75 y_old = y; |
|
76 for i = 1 : 10000 |
|
77 h = (beta_cdf (y_old, a, b) - x) ./ beta_pdf (y_old, a, b); |
|
78 y_new = y_old - h; |
|
79 ind = find (y_new <= eps); |
|
80 if (any (ind)) |
|
81 y_new (ind) = y_old (ind) / 10; |
|
82 endif |
|
83 ind = find (y_new >= 1 - eps); |
|
84 if (any (ind)) |
|
85 y_new (ind) = 1 - (1 - y_old (ind)) / 10; |
|
86 endif |
|
87 h = y_old - y_new; |
|
88 if (max (abs (h)) < sqrt (eps)) |
|
89 break; |
|
90 endif |
|
91 y_old = y_new; |
|
92 endfor |
|
93 |
|
94 inv (k) = y_new; |
|
95 endif |
|
96 |
|
97 endfunction |