Mercurial > octave-nkf
annotate scripts/statistics/distributions/gaminv.m @ 7795:df9519e9990c
Handle single precision eps values
author | David Bateman <dbateman@free.fr> |
---|---|
date | Mon, 12 May 2008 22:57:11 +0200 |
parents | a1dbe9d80eee |
children | eb63fbe60fab |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1995, 1996, 1997, 2005, 2006, 2007 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} {} gaminv (@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 Gamma distribution with parameters @var{a} | |
5642 | 23 ## and @var{b}. |
24 ## @seealso{gamma, gammaln, gammainc, gampdf, gamcdf, gamrnd} | |
5410 | 25 ## @end deftypefn |
26 | |
5428 | 27 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
5410 | 28 ## Description: Quantile function of the Gamma distribution |
29 | |
5411 | 30 function inv = gaminv (x, a, b) |
5410 | 31 |
32 if (nargin != 3) | |
6046 | 33 print_usage (); |
5410 | 34 endif |
35 | |
36 if (!isscalar (a) || !isscalar(b)) | |
37 [retval, x, a, b] = common_size (x, a, b); | |
38 if (retval > 0) | |
5411 | 39 error ("gaminv: x, a and b must be of common size or scalars"); |
5410 | 40 endif |
41 endif | |
42 | |
43 sz = size (x); | |
44 inv = zeros (sz); | |
45 | |
46 k = find ((x < 0) | (x > 1) | isnan (x) | !(a > 0) | !(b > 0)); | |
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) = Inf; | |
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); | |
6777 | 61 y = a .* b; |
5410 | 62 else |
6777 | 63 y = a * b * ones (size (k)); |
5410 | 64 endif |
65 x = x (k); | |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
66 |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
67 if (isa (x, "single")) |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
68 myeps = eps ("single"); |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
69 else |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
70 myeps = eps; |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
71 endif |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
72 |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
73 l = find (x < myeps); |
5410 | 74 if (any (l)) |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
75 y(l) = sqrt (myeps) * ones (length (l), 1); |
5410 | 76 endif |
77 | |
78 y_old = y; | |
79 for i = 1 : 100 | |
5411 | 80 h = (gamcdf (y_old, a, b) - x) ./ gampdf (y_old, a, b); |
5410 | 81 y_new = y_old - h; |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
82 ind = find (y_new <= myeps); |
5410 | 83 if (any (ind)) |
84 y_new (ind) = y_old (ind) / 10; | |
85 h = y_old - y_new; | |
86 endif | |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
87 if (max (abs (h)) < sqrt (myeps)) |
5410 | 88 break; |
89 endif | |
90 y_old = y_new; | |
91 endfor | |
92 | |
93 inv (k) = y_new; | |
94 endif | |
95 | |
96 endfunction |