1 | /*************************************************************************
|
---|
2 | Cephes Math Library Release 2.8: June, 2000
|
---|
3 | Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
|
---|
4 |
|
---|
5 | Contributors:
|
---|
6 | * Sergey Bochkanov (ALGLIB project). Translation from C to
|
---|
7 | pseudocode.
|
---|
8 |
|
---|
9 | See subroutines comments for additional copyrights.
|
---|
10 |
|
---|
11 | >>> SOURCE LICENSE >>>
|
---|
12 | This program is free software; you can redistribute it and/or modify
|
---|
13 | it under the terms of the GNU General Public License as published by
|
---|
14 | the Free Software Foundation (www.fsf.org); either version 2 of the
|
---|
15 | License, or (at your option) any later version.
|
---|
16 |
|
---|
17 | This program is distributed in the hope that it will be useful,
|
---|
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
20 | GNU General Public License for more details.
|
---|
21 |
|
---|
22 | A copy of the GNU General Public License is available at
|
---|
23 | http://www.fsf.org/licensing/licenses
|
---|
24 |
|
---|
25 | >>> END OF LICENSE >>>
|
---|
26 | *************************************************************************/
|
---|
27 |
|
---|
28 |
|
---|
29 | namespace alglib {
|
---|
30 | public class fdistr {
|
---|
31 | /*************************************************************************
|
---|
32 | F distribution
|
---|
33 |
|
---|
34 | Returns the area from zero to x under the F density
|
---|
35 | function (also known as Snedcor's density or the
|
---|
36 | variance ratio density). This is the density
|
---|
37 | of x = (u1/df1)/(u2/df2), where u1 and u2 are random
|
---|
38 | variables having Chi square distributions with df1
|
---|
39 | and df2 degrees of freedom, respectively.
|
---|
40 | The incomplete beta integral is used, according to the
|
---|
41 | formula
|
---|
42 |
|
---|
43 | P(x) = incbet( df1/2, df2/2, (df1*x/(df2 + df1*x) ).
|
---|
44 |
|
---|
45 |
|
---|
46 | The arguments a and b are greater than zero, and x is
|
---|
47 | nonnegative.
|
---|
48 |
|
---|
49 | ACCURACY:
|
---|
50 |
|
---|
51 | Tested at random points (a,b,x).
|
---|
52 |
|
---|
53 | x a,b Relative error:
|
---|
54 | arithmetic domain domain # trials peak rms
|
---|
55 | IEEE 0,1 0,100 100000 9.8e-15 1.7e-15
|
---|
56 | IEEE 1,5 0,100 100000 6.5e-15 3.5e-16
|
---|
57 | IEEE 0,1 1,10000 100000 2.2e-11 3.3e-12
|
---|
58 | IEEE 1,5 1,10000 100000 1.1e-11 1.7e-13
|
---|
59 |
|
---|
60 | Cephes Math Library Release 2.8: June, 2000
|
---|
61 | Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
|
---|
62 | *************************************************************************/
|
---|
63 | public static double fdistribution(int a,
|
---|
64 | int b,
|
---|
65 | double x) {
|
---|
66 | double result = 0;
|
---|
67 | double w = 0;
|
---|
68 |
|
---|
69 | System.Diagnostics.Debug.Assert(a >= 1 & b >= 1 & (double)(x) >= (double)(0), "Domain error in FDistribution");
|
---|
70 | w = a * x;
|
---|
71 | w = w / (b + w);
|
---|
72 | result = ibetaf.incompletebeta(0.5 * a, 0.5 * b, w);
|
---|
73 | return result;
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | /*************************************************************************
|
---|
78 | Complemented F distribution
|
---|
79 |
|
---|
80 | Returns the area from x to infinity under the F density
|
---|
81 | function (also known as Snedcor's density or the
|
---|
82 | variance ratio density).
|
---|
83 |
|
---|
84 |
|
---|
85 | inf.
|
---|
86 | -
|
---|
87 | 1 | | a-1 b-1
|
---|
88 | 1-P(x) = ------ | t (1-t) dt
|
---|
89 | B(a,b) | |
|
---|
90 | -
|
---|
91 | x
|
---|
92 |
|
---|
93 |
|
---|
94 | The incomplete beta integral is used, according to the
|
---|
95 | formula
|
---|
96 |
|
---|
97 | P(x) = incbet( df2/2, df1/2, (df2/(df2 + df1*x) ).
|
---|
98 |
|
---|
99 |
|
---|
100 | ACCURACY:
|
---|
101 |
|
---|
102 | Tested at random points (a,b,x) in the indicated intervals.
|
---|
103 | x a,b Relative error:
|
---|
104 | arithmetic domain domain # trials peak rms
|
---|
105 | IEEE 0,1 1,100 100000 3.7e-14 5.9e-16
|
---|
106 | IEEE 1,5 1,100 100000 8.0e-15 1.6e-15
|
---|
107 | IEEE 0,1 1,10000 100000 1.8e-11 3.5e-13
|
---|
108 | IEEE 1,5 1,10000 100000 2.0e-11 3.0e-12
|
---|
109 |
|
---|
110 | Cephes Math Library Release 2.8: June, 2000
|
---|
111 | Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
|
---|
112 | *************************************************************************/
|
---|
113 | public static double fcdistribution(int a,
|
---|
114 | int b,
|
---|
115 | double x) {
|
---|
116 | double result = 0;
|
---|
117 | double w = 0;
|
---|
118 |
|
---|
119 | System.Diagnostics.Debug.Assert(a >= 1 & b >= 1 & (double)(x) >= (double)(0), "Domain error in FCDistribution");
|
---|
120 | w = b / (b + a * x);
|
---|
121 | result = ibetaf.incompletebeta(0.5 * b, 0.5 * a, w);
|
---|
122 | return result;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | /*************************************************************************
|
---|
127 | Inverse of complemented F distribution
|
---|
128 |
|
---|
129 | Finds the F density argument x such that the integral
|
---|
130 | from x to infinity of the F density is equal to the
|
---|
131 | given probability p.
|
---|
132 |
|
---|
133 | This is accomplished using the inverse beta integral
|
---|
134 | function and the relations
|
---|
135 |
|
---|
136 | z = incbi( df2/2, df1/2, p )
|
---|
137 | x = df2 (1-z) / (df1 z).
|
---|
138 |
|
---|
139 | Note: the following relations hold for the inverse of
|
---|
140 | the uncomplemented F distribution:
|
---|
141 |
|
---|
142 | z = incbi( df1/2, df2/2, p )
|
---|
143 | x = df2 z / (df1 (1-z)).
|
---|
144 |
|
---|
145 | ACCURACY:
|
---|
146 |
|
---|
147 | Tested at random points (a,b,p).
|
---|
148 |
|
---|
149 | a,b Relative error:
|
---|
150 | arithmetic domain # trials peak rms
|
---|
151 | For p between .001 and 1:
|
---|
152 | IEEE 1,100 100000 8.3e-15 4.7e-16
|
---|
153 | IEEE 1,10000 100000 2.1e-11 1.4e-13
|
---|
154 | For p between 10^-6 and 10^-3:
|
---|
155 | IEEE 1,100 50000 1.3e-12 8.4e-15
|
---|
156 | IEEE 1,10000 50000 3.0e-12 4.8e-14
|
---|
157 |
|
---|
158 | Cephes Math Library Release 2.8: June, 2000
|
---|
159 | Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
|
---|
160 | *************************************************************************/
|
---|
161 | public static double invfdistribution(int a,
|
---|
162 | int b,
|
---|
163 | double y) {
|
---|
164 | double result = 0;
|
---|
165 | double w = 0;
|
---|
166 |
|
---|
167 | System.Diagnostics.Debug.Assert(a >= 1 & b >= 1 & (double)(y) > (double)(0) & (double)(y) <= (double)(1), "Domain error in InvFDistribution");
|
---|
168 |
|
---|
169 | //
|
---|
170 | // Compute probability for x = 0.5
|
---|
171 | //
|
---|
172 | w = ibetaf.incompletebeta(0.5 * b, 0.5 * a, 0.5);
|
---|
173 |
|
---|
174 | //
|
---|
175 | // If that is greater than y, then the solution w < .5
|
---|
176 | // Otherwise, solve at 1-y to remove cancellation in (b - b*w)
|
---|
177 | //
|
---|
178 | if ((double)(w) > (double)(y) | (double)(y) < (double)(0.001)) {
|
---|
179 | w = ibetaf.invincompletebeta(0.5 * b, 0.5 * a, y);
|
---|
180 | result = (b - b * w) / (a * w);
|
---|
181 | } else {
|
---|
182 | w = ibetaf.invincompletebeta(0.5 * a, 0.5 * b, 1.0 - y);
|
---|
183 | result = b * w / (a * (1.0 - w));
|
---|
184 | }
|
---|
185 | return result;
|
---|
186 | }
|
---|
187 | }
|
---|
188 | }
|
---|