3191
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
3426
|
2 ## |
3191
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2, or (at your option) |
|
6 ## any later version. |
3426
|
7 ## |
3191
|
8 ## This program is distributed in the hope that it will be useful, but |
|
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
11 ## General Public License for more details. |
|
12 ## |
3191
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this file. If not, write to the Free Software Foundation, |
|
15 ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
16 |
3456
|
17 ## -*- texinfo -*- |
|
18 ## @deftypefn {Function File} {} beta_inv (@var{x}, @var{a}, @var{b}) |
|
19 ## For each component of @var{x}, compute the quantile (the inverse of |
|
20 ## the CDF) at @var{x} of the Beta distribution with parameters @var{a} |
|
21 ## and @var{b}. |
|
22 ## @end deftypefn |
3426
|
23 |
3456
|
24 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
25 ## Description: Quantile function of the Beta distribution |
3191
|
26 |
|
27 function inv = beta_inv (x, a, b) |
3426
|
28 |
3191
|
29 if (nargin != 3) |
|
30 usage ("beta_inv (x, a, b)"); |
|
31 endif |
3426
|
32 |
3191
|
33 [retval, x, a, b] = common_size (x, a, b); |
|
34 if (retval > 0) |
3456
|
35 error ("beta_inv: x, a and b must be of common size or scalars"); |
3191
|
36 endif |
|
37 |
|
38 [r, c] = size (x); |
|
39 s = r * c; |
|
40 x = reshape (x, s, 1); |
|
41 a = reshape (a, s, 1); |
|
42 b = reshape (b, s, 1); |
|
43 inv = zeros (s, 1); |
3426
|
44 |
3191
|
45 k = find ((x < 0) | (x > 1) | !(a > 0) | !(b > 0) | isnan (x)); |
3456
|
46 if (any (k)) |
3191
|
47 inv (k) = NaN * ones (length (k), 1); |
|
48 endif |
3426
|
49 |
3191
|
50 k = find ((x == 1) & (a > 0) & (b > 0)); |
3456
|
51 if (any (k)) |
3191
|
52 inv (k) = ones (length (k), 1); |
|
53 endif |
3426
|
54 |
3191
|
55 k = find ((x > 0) & (x < 1) & (a > 0) & (b > 0)); |
3456
|
56 if (any (k)) |
3191
|
57 a = a (k); |
|
58 b = b (k); |
|
59 x = x (k); |
3656
|
60 y = a ./ (a + b); |
3191
|
61 l = find (y < eps); |
3456
|
62 if (any (l)) |
3191
|
63 y(l) = sqrt (eps) * ones (length (l), 1); |
|
64 endif |
|
65 l = find (y > 1 - eps); |
3456
|
66 if (any (l)) |
3191
|
67 y(l) = 1 - sqrt (eps) * ones (length (l), 1); |
|
68 endif |
|
69 |
|
70 y_old = y; |
3653
|
71 for i = 1 : 10000 |
3191
|
72 h = (beta_cdf (y_old, a, b) - x) ./ beta_pdf (y_old, a, b); |
|
73 y_new = y_old - h; |
|
74 ind = find (y_new <= eps); |
|
75 if (any (ind)) |
3426
|
76 y_new (ind) = y_old (ind) / 10; |
3191
|
77 endif |
|
78 ind = find (y_new >= 1 - eps); |
3456
|
79 if (any (ind)) |
3426
|
80 y_new (ind) = 1 - (1 - y_old (ind)) / 10; |
3191
|
81 endif |
|
82 h = y_old - y_new; |
|
83 if (max (abs (h)) < sqrt (eps)) |
3426
|
84 break; |
3191
|
85 endif |
|
86 y_old = y_new; |
|
87 endfor |
3426
|
88 |
3191
|
89 inv (k) = y_new; |
|
90 endif |
|
91 |
|
92 inv = reshape (inv, r, c); |
3426
|
93 |
3191
|
94 endfunction |