Mercurial > octave
annotate scripts/specfun/betaincinv.m @ 29616:69b6b783a8ab stable
betaincinv.m: Correctly handle inputs very close to 1.0 (bug #60528)
* betaincinv.m (newtons_method): if any value of x is greater than zero,
replace with 1-eps() to make the value minimally less than 1. Add BIST test
for bug #60528.
author | Rik <rik@octave.org> |
---|---|
date | Wed, 05 May 2021 14:26:44 -0700 |
parents | 26ba91f0eea7 |
children | 69e701caf5f6 7f5bd197fea6 |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
29358
0a5b15007766
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27984
diff
changeset
|
3 ## Copyright (C) 2017-2021 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
7 ## |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
8 ## This file is part of Octave. |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
9 ## |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
11 ## under the terms of the GNU General Public License as published by |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
13 ## (at your option) any later version. |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
14 ## |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
15 ## Octave is distributed in the hope that it will be useful, but |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
18 ## GNU General Public License for more details. |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
19 ## |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
20 ## You should have received a copy of the GNU General Public License |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
21 ## along with Octave; see the file COPYING. If not, see |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
25 |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
26 ## -*- texinfo -*- |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
27 ## @deftypefn {} {} betaincinv (@var{y}, @var{a}, @var{b}) |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
28 ## @deftypefnx {} {} betaincinv (@var{y}, @var{a}, @var{b}, "lower") |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
29 ## @deftypefnx {} {} betaincinv (@var{y}, @var{a}, @var{b}, "upper") |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
30 ## Compute the inverse of the normalized incomplete beta function. |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
31 ## |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
32 ## The normalized incomplete beta function is defined as |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
33 ## @tex |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
34 ## $$ |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
35 ## I_x (a, b) = {1 \over {B(a,b)}} \displaystyle{\int_0^x t^{a-1} (1-t)^{b-1} dt} |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
36 ## $$ |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
37 ## @end tex |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
38 ## @ifnottex |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
39 ## |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
40 ## @example |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
41 ## @group |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
42 ## x |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
43 ## / |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
44 ## | |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
45 ## I_x (a, b) = | t^(a-1) (1-t)^(b-1) dt |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
46 ## | |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
47 ## / |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
48 ## 0 |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
49 ## @end group |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
50 ## @end example |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
51 ## |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
52 ## @end ifnottex |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
53 ## |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
54 ## If two inputs are scalar, then @code{betaincinv (@var{y}, @var{a}, @var{b})} |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
55 ## is returned for each of the other inputs. |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
56 ## |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
57 ## If two or more inputs are not scalar, the sizes of them must agree, and |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
58 ## @code{betaincinv} is applied element-by-element. |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
59 ## |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
60 ## The variable @var{y} must be in the interval [0,1], while @var{a} and |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
61 ## @var{b} must be real and strictly positive. |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
62 ## |
25579
07c2c42f457e
doc: Miscellaneous documentation fixes all over the manual (bug #54288).
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
63 ## By default, @var{tail} is @qcode{"lower"} and the inverse of the incomplete |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
64 ## beta function integrated from 0 to @var{x} is computed. If @var{tail} is |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
65 ## @qcode{"upper"} then the complementary function integrated from @var{x} to 1 |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
66 ## is inverted. |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
67 ## |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
68 ## The function is computed by standard Newton's method, by solving |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
69 ## @tex |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
70 ## $$ |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
71 ## y - I_x (a, b) = 0 |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
72 ## $$ |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
73 ## @end tex |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
74 ## @ifnottex |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
75 ## |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
76 ## @example |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
77 ## @var{y} - betainc (@var{x}, @var{a}, @var{b}) = 0 |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
78 ## @end example |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
79 ## |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
80 ## @end ifnottex |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
81 ## |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
82 ## @seealso{betainc, beta, betaln} |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
83 ## @end deftypefn |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
84 |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
85 function x = betaincinv (y, a, b, tail = "lower") |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
86 |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
87 if (nargin < 3 || nargin > 4) |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
88 print_usage (); |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
89 endif |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
90 |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
91 [err, y, a, b] = common_size (y, a, b); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
92 if (err > 0) |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
93 error ("betaincinv: Y, A, and B must be of common size or scalars"); |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
94 endif |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
95 |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
96 if (iscomplex (y) || iscomplex (a) || iscomplex (b)) |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
97 error ("betaincinv: all inputs must be real"); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
98 endif |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
99 |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
100 ## FIXME: Should there be isnumeric checking? Right now it accepts char |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
101 ## arrays, but then produces a weird error later on. |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
102 |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
103 ## Remember original shape of data, but convert to column vector for calcs. |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
104 orig_sz = size (y); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
105 y = y(:); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
106 a = a(:); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
107 b = b(:); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
108 |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
109 if (any ((y < 0) | (y > 1))) |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
110 error ("betaincinv: Y must be in the range [0, 1]"); |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
111 endif |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
112 |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
113 if (any (a <= 0)) |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
114 error ("betaincinv: A must be strictly positive"); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
115 endif |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
116 |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
117 if (any (b <= 0)) |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
118 error ("betaincinv: B must be strictly positive"); |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
119 endif |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
120 |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
121 ## If any of the arguments is single then the output should be as well. |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
122 if (strcmp (class (y), "single") || strcmp (class (a), "single") |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
123 || strcmp (class (b), "single")) |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
124 y = single (y); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
125 a = single (a); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
126 b = single (b); |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
127 endif |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
128 |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
129 ## Convert to floating point if necessary |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
130 if (isinteger (y)) |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
131 y = double (y); |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
132 endif |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
133 if (isinteger (a)) |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
134 a = double (a); |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
135 endif |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
136 if (isinteger (b)) |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
137 b = double (b); |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
138 endif |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
139 |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
140 ## Initialize output array |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
141 x = zeros (size (y), class (y)); |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
142 |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
143 ## Parameters for the Newton method |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
144 maxit = 20; |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
145 tol = eps (class (y)); |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
146 |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
147 if (strcmpi (tail, "lower")) |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
148 p = y; |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
149 q = 1 - y; |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
150 x(y == 0) = 0; |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
151 x(y == 1) = 1; |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
152 elseif (strcmpi (tail, "upper")) |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
153 p = 1 - y; |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
154 q = y; |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
155 x(y == 0) = 1; |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
156 x(y == 1) = 0; |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
157 else |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
158 error ("betaincinv: invalid value for TAIL") |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
159 endif |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
160 |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
161 ## Special values have been already computed. |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
162 todo = (y != 0) & (y != 1); |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
163 |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
164 ## We will invert the lower version for p < 0.5 and the upper otherwise. |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
165 i_low = (p < 0.5); |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
166 i_upp = (! i_low); |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
167 |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
168 idx = todo & i_low; |
26268
6dd232798997
maint: Remove useless ';' from end of for, if, while, etc. statements.
Rik <rik@octave.org>
parents:
25579
diff
changeset
|
169 if (any (idx)) |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
170 n = nnz (idx); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
171 ## Function and derivative of the lower version. |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
172 F = @(x, a, b, y) y - betainc (x, a, b); |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
173 JF = @(x, a, b) - real (exp ((a-1) .* log (x) + (b-1) .* log1p (-x) + ... |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
174 gammaln (a+b) - gammaln (a) - gammaln (b))); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
175 |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
176 ## Compute the initial guess with a bisection method of 10 steps. |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
177 x0 = bisection_method (F, zeros (n,1), ones (n,1), ... |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
178 a(i_low), b(i_low), p(i_low), 10); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
179 |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
180 ## Use Newton's method to iteratively find solution. |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
181 x(i_low) = newton_method (F, JF, x0, a(i_low), b(i_low), p(i_low), ... |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
182 tol, maxit); |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
183 endif |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
184 |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
185 idx = todo & i_upp; |
26268
6dd232798997
maint: Remove useless ';' from end of for, if, while, etc. statements.
Rik <rik@octave.org>
parents:
25579
diff
changeset
|
186 if (any (idx)) |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
187 n = nnz (idx); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
188 ## Function and derivative of the upper version. |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
189 F = @(x, a, b, y) y - betainc (x, a, b, "upper"); |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
190 JF = @(x, a, b) real (exp ((a-1) .* log (x) + (b-1) .* log1p (-x) + ... |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
191 gammaln (a+b) - gammaln (a) - gammaln (b))); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
192 |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
193 ## Compute the initial guess with a bisection method of 10 steps. |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
194 x0 = bisection_method (F, zeros (n,1), ones (n,1), ... |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
195 a(i_upp), b(i_upp), q(i_upp), 10); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
196 |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
197 ## Use Newton's method to iteratively find solution. |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
198 x(i_upp) = newton_method (F, JF, x0, a(i_upp), b(i_upp), q(i_upp), ... |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
199 tol, maxit); |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
200 endif |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
201 |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
202 ## Restore original shape |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
203 x = reshape (x, orig_sz); |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
204 |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
205 endfunction |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
206 |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
207 |
27984
b09432b20a84
maint: Remove special cases of old version control keywords in code base.
Rik <rik@octave.org>
parents:
27978
diff
changeset
|
208 ## subfunctions: Bisection and Newton Methods |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
209 function xc = bisection_method (F, xl, xr, a, b, y, maxit) |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
210 F_l = F (xl, a, b, y); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
211 F_r = F (xr, a, b, y); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
212 for it = 1:maxit |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
213 xc = (xl + xr) / 2; |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
214 F_c = F (xc, a, b, y); |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
215 flag_l = ((F_c .* F_r) < 0); |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
216 flag_r = ((F_c .* F_l) < 0); |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
217 flag_c = (F_c == 0); |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
218 xl(flag_l) = xc(flag_l); |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
219 xr(flag_r) = xc(flag_r); |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
220 xl(flag_c) = xr(flag_c) = xc(flag_c); |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
221 F_l(flag_l) = F_c(flag_l); |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
222 F_r(flag_r) = F_c(flag_r); |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
223 F_l(flag_c) = F_r(flag_c) = 0; |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
224 endfor |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
225 endfunction |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
226 |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
227 function x = newton_method (F, JF, x0, a, b, y, tol, maxit); |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
228 l = numel (y); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
229 res = -F (x0, a, b, y) ./ JF (x0, a, b); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
230 todo = (abs(res) >= tol * abs (x0)); |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
231 x = x0; |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
232 it = 0; |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
233 while (any (todo) && (it < maxit)) |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
234 it++; |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
235 x(todo) += res(todo); |
29616
69b6b783a8ab
betaincinv.m: Correctly handle inputs very close to 1.0 (bug #60528)
Rik <rik@octave.org>
parents:
29614
diff
changeset
|
236 ## Avoid intermediate values outside betainc() range of [0, 1], bug #60528 |
69b6b783a8ab
betaincinv.m: Correctly handle inputs very close to 1.0 (bug #60528)
Rik <rik@octave.org>
parents:
29614
diff
changeset
|
237 x(x(todo) < 0) = eps; |
69b6b783a8ab
betaincinv.m: Correctly handle inputs very close to 1.0 (bug #60528)
Rik <rik@octave.org>
parents:
29614
diff
changeset
|
238 x(x(todo) > 1) = 1-eps; |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
239 res(todo) = -F(x(todo), a(todo), b(todo), y(todo)) ... |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
240 ./ JF (x(todo), a(todo), b(todo)); |
29616
69b6b783a8ab
betaincinv.m: Correctly handle inputs very close to 1.0 (bug #60528)
Rik <rik@octave.org>
parents:
29614
diff
changeset
|
241 todo = (abs (res) >= tol * abs (x)); |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
242 endwhile |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
243 x += res; |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
244 endfunction |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
245 |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
246 |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
247 %!test |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
248 %! x = linspace (0.1, 0.9, 11); |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
249 %! a = [2, 3, 4]; |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
250 %! [x,a,b] = ndgrid (x,a,a); |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
251 %! xx = betaincinv (betainc (x, a, b), a, b); |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
252 %! assert (xx, x, 3e-15); |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
253 |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
254 %!test |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
255 %! x = linspace (0.1, 0.9, 11); |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
256 %! a = [2, 3, 4]; |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
257 %! [x,a,b] = ndgrid (x,a,a); |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
258 %! xx = betaincinv (betainc (x, a, b, "upper"), a, b, "upper"); |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
259 %! assert (xx, x, 3e-15); |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
260 |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
261 %!test |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
262 %! x = linspace (0.1, 0.9, 11); |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
263 %! a = [0.1:0.1:1]; |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
264 %! [x,a,b] = ndgrid (x,a,a); |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
265 %! xx = betaincinv (betainc (x, a, b), a, b); |
24996
f80e68529bbf
test: relax tolerances on specfun tests to pass on i386 (bug #53437)
Mike Miller <mtmiller@octave.org>
parents:
24927
diff
changeset
|
266 %! assert (xx, x, 5e-15); |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
267 |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
268 %!test |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
269 %! x = linspace (0.1, 0.9, 11); |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
270 %! a = [0.1:0.1:1]; |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
271 %! [x,a,b] = ndgrid (x,a,a); |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
272 %! xx = betaincinv (betainc (x, a, b, "upper"), a, b, "upper"); |
24996
f80e68529bbf
test: relax tolerances on specfun tests to pass on i386 (bug #53437)
Mike Miller <mtmiller@octave.org>
parents:
24927
diff
changeset
|
273 %! assert (xx, x, 5e-15); |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
274 |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
275 ## Test the conservation of the input class |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
276 %!assert (class (betaincinv (0.5, 1, 1)), "double") |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
277 %!assert (class (betaincinv (single (0.5), 1, 1)), "single") |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
278 %!assert (class (betaincinv (0.5, single (1), 1)), "single") |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
279 %!assert (class (betaincinv (int8 (0), 1, 1)), "double") |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
280 %!assert (class (betaincinv (0.5, int8 (1), 1)), "double") |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
281 %!assert (class (betaincinv (int8 (0), single (1), 1)), "single") |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
282 %!assert (class (betaincinv (single (0.5), int8 (1), 1)), "single") |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
283 |
29616
69b6b783a8ab
betaincinv.m: Correctly handle inputs very close to 1.0 (bug #60528)
Rik <rik@octave.org>
parents:
29614
diff
changeset
|
284 %!assert <*60528> (betaincinv (1e-6, 1, 3), 3.3333344444450657e-07, 5*eps) |
69b6b783a8ab
betaincinv.m: Correctly handle inputs very close to 1.0 (bug #60528)
Rik <rik@octave.org>
parents:
29614
diff
changeset
|
285 %!assert <*60528> (betaincinv (1-1e-6, 3, 1), 0.999999666666556, 5*eps) |
29614
26ba91f0eea7
betaincinv.m: Correctly handle small inputs (bug #60528)
Rik <rik@octave.org>
parents:
29358
diff
changeset
|
286 |
24907
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
287 ## Test input validation |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
288 %!error betaincinv () |
bd89440407aa
Incomplete beta function moved to a .m file, fixing accuracy and
Michele Ginesi <michele.ginesi@gmail.com>
parents:
diff
changeset
|
289 %!error betaincinv (1) |
24927
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
290 %!error betaincinv (1,2) |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
291 %!error betaincinv (1,2,3,4,5) |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
292 %!error <must be of common size or scalars> |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
293 %! betaincinv (ones (2,2), ones (1,2), 1); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
294 %!error <all inputs must be real> betaincinv (0.5i, 1, 2) |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
295 %!error <all inputs must be real> betaincinv (0, 1i, 1) |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
296 %!error <all inputs must be real> betaincinv (0, 1, 1i) |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
297 %!error <Y must be in the range \[0, 1\]> betaincinv (-0.1,1,1) |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
298 %!error <Y must be in the range \[0, 1\]> betaincinv (1.1,1,1) |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
299 %!error <Y must be in the range \[0, 1\]> |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
300 %! y = ones (1, 1, 2); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
301 %! y(1,1,2) = -1; |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
302 %! betaincinv (y,1,1); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
303 %!error <A must be strictly positive> betaincinv (0.5,0,1) |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
304 %!error <A must be strictly positive> |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
305 %! a = ones (1, 1, 2); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
306 %! a(1,1,2) = 0; |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
307 %! betaincinv (1,a,1); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
308 %!error <B must be strictly positive> betaincinv (0.5,1,0) |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
309 %!error <B must be strictly positive> |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
310 %! b = ones (1, 1, 2); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
311 %! b(1,1,2) = 0; |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
312 %! betaincinv (1,1,b); |
c280560d9c96
Overhaul special functions modified by GSOC2018 project.
Rik <rik@octave.org>
parents:
24907
diff
changeset
|
313 %!error <invalid value for TAIL> betaincinv (1,2,3, "foobar") |