Mercurial > octave-nkf
annotate scripts/statistics/distributions/betainv.m @ 11523:fd0a3ac60b0e
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 14 Jan 2011 05:47:45 -0500 |
parents | 1740012184f9 |
children | c792872f8942 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 1995-2011 Kurt Hornik |
5410 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
5410 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5410 | 18 |
19 ## -*- texinfo -*- | |
5411 | 20 ## @deftypefn {Function File} {} betainv (@var{x}, @var{a}, @var{b}) |
5410 | 21 ## For each component of @var{x}, compute the quantile (the inverse of |
22 ## the CDF) at @var{x} of the Beta distribution with parameters @var{a} | |
23 ## and @var{b}. | |
24 ## @end deftypefn | |
25 | |
5428 | 26 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
5410 | 27 ## Description: Quantile function of the Beta distribution |
28 | |
5411 | 29 function inv = betainv (x, a, b) |
5410 | 30 |
31 if (nargin != 3) | |
6046 | 32 print_usage (); |
5410 | 33 endif |
34 | |
35 if (!isscalar (a) || !isscalar(b)) | |
36 [retval, x, a, b] = common_size (x, a, b); | |
37 if (retval > 0) | |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
38 error ("betainv: X, A and B must be of common size or scalars"); |
5410 | 39 endif |
40 endif | |
41 | |
42 sz = size (x); | |
43 inv = zeros (sz); | |
44 | |
45 k = find ((x < 0) | (x > 1) | !(a > 0) | !(b > 0) | isnan (x)); | |
46 if (any (k)) | |
47 inv (k) = NaN; | |
48 endif | |
49 | |
50 k = find ((x == 1) & (a > 0) & (b > 0)); | |
51 if (any (k)) | |
52 inv (k) = 1; | |
53 endif | |
54 | |
55 k = find ((x > 0) & (x < 1) & (a > 0) & (b > 0)); | |
56 if (any (k)) | |
57 if (!isscalar(a) || !isscalar(b)) | |
58 a = a (k); | |
59 b = b (k); | |
60 y = a ./ (a + b); | |
61 else | |
62 y = a / (a + b) * ones (size (k)); | |
63 endif | |
64 x = x (k); | |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
65 |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
66 if (isa (y, "single")) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
67 myeps = eps ("single"); |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
68 else |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
69 myeps = eps; |
5410 | 70 endif |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
71 |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
72 l = find (y < myeps); |
5410 | 73 if (any (l)) |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
74 y(l) = sqrt (myeps) * ones (length (l), 1); |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
75 endif |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
76 l = find (y > 1 - myeps); |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
77 if (any (l)) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
78 y(l) = 1 - sqrt (myeps) * ones (length (l), 1); |
5410 | 79 endif |
80 | |
81 y_old = y; | |
82 for i = 1 : 10000 | |
5411 | 83 h = (betacdf (y_old, a, b) - x) ./ betapdf (y_old, a, b); |
5410 | 84 y_new = y_old - h; |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
85 ind = find (y_new <= myeps); |
5410 | 86 if (any (ind)) |
87 y_new (ind) = y_old (ind) / 10; | |
88 endif | |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
89 ind = find (y_new >= 1 - myeps); |
5410 | 90 if (any (ind)) |
91 y_new (ind) = 1 - (1 - y_old (ind)) / 10; | |
92 endif | |
93 h = y_old - y_new; | |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
94 if (max (abs (h)) < sqrt (myeps)) |
5410 | 95 break; |
96 endif | |
97 y_old = y_new; | |
98 endfor | |
99 | |
100 inv (k) = y_new; | |
101 endif | |
102 | |
103 endfunction |