[3839] | 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 | using System;
|
---|
| 29 |
|
---|
| 30 | namespace alglib
|
---|
| 31 | {
|
---|
| 32 | public class fdistr
|
---|
| 33 | {
|
---|
| 34 | /*************************************************************************
|
---|
| 35 | F distribution
|
---|
| 36 |
|
---|
| 37 | Returns the area from zero to x under the F density
|
---|
| 38 | function (also known as Snedcor's density or the
|
---|
| 39 | variance ratio density). This is the density
|
---|
| 40 | of x = (u1/df1)/(u2/df2), where u1 and u2 are random
|
---|
| 41 | variables having Chi square distributions with df1
|
---|
| 42 | and df2 degrees of freedom, respectively.
|
---|
| 43 | The incomplete beta integral is used, according to the
|
---|
| 44 | formula
|
---|
| 45 |
|
---|
| 46 | P(x) = incbet( df1/2, df2/2, (df1*x/(df2 + df1*x) ).
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 | The arguments a and b are greater than zero, and x is
|
---|
| 50 | nonnegative.
|
---|
| 51 |
|
---|
| 52 | ACCURACY:
|
---|
| 53 |
|
---|
| 54 | Tested at random points (a,b,x).
|
---|
| 55 |
|
---|
| 56 | x a,b Relative error:
|
---|
| 57 | arithmetic domain domain # trials peak rms
|
---|
| 58 | IEEE 0,1 0,100 100000 9.8e-15 1.7e-15
|
---|
| 59 | IEEE 1,5 0,100 100000 6.5e-15 3.5e-16
|
---|
| 60 | IEEE 0,1 1,10000 100000 2.2e-11 3.3e-12
|
---|
| 61 | IEEE 1,5 1,10000 100000 1.1e-11 1.7e-13
|
---|
| 62 |
|
---|
| 63 | Cephes Math Library Release 2.8: June, 2000
|
---|
| 64 | Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
|
---|
| 65 | *************************************************************************/
|
---|
| 66 | public static double fdistribution(int a,
|
---|
| 67 | int b,
|
---|
| 68 | double x)
|
---|
| 69 | {
|
---|
| 70 | double result = 0;
|
---|
| 71 | double w = 0;
|
---|
| 72 |
|
---|
| 73 | System.Diagnostics.Debug.Assert(a>=1 & b>=1 & (double)(x)>=(double)(0), "Domain error in FDistribution");
|
---|
| 74 | w = a*x;
|
---|
| 75 | w = w/(b+w);
|
---|
| 76 | result = ibetaf.incompletebeta(0.5*a, 0.5*b, w);
|
---|
| 77 | return result;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 |
|
---|
| 81 | /*************************************************************************
|
---|
| 82 | Complemented F distribution
|
---|
| 83 |
|
---|
| 84 | Returns the area from x to infinity under the F density
|
---|
| 85 | function (also known as Snedcor's density or the
|
---|
| 86 | variance ratio density).
|
---|
| 87 |
|
---|
| 88 |
|
---|
| 89 | inf.
|
---|
| 90 | -
|
---|
| 91 | 1 | | a-1 b-1
|
---|
| 92 | 1-P(x) = ------ | t (1-t) dt
|
---|
| 93 | B(a,b) | |
|
---|
| 94 | -
|
---|
| 95 | x
|
---|
| 96 |
|
---|
| 97 |
|
---|
| 98 | The incomplete beta integral is used, according to the
|
---|
| 99 | formula
|
---|
| 100 |
|
---|
| 101 | P(x) = incbet( df2/2, df1/2, (df2/(df2 + df1*x) ).
|
---|
| 102 |
|
---|
| 103 |
|
---|
| 104 | ACCURACY:
|
---|
| 105 |
|
---|
| 106 | Tested at random points (a,b,x) in the indicated intervals.
|
---|
| 107 | x a,b Relative error:
|
---|
| 108 | arithmetic domain domain # trials peak rms
|
---|
| 109 | IEEE 0,1 1,100 100000 3.7e-14 5.9e-16
|
---|
| 110 | IEEE 1,5 1,100 100000 8.0e-15 1.6e-15
|
---|
| 111 | IEEE 0,1 1,10000 100000 1.8e-11 3.5e-13
|
---|
| 112 | IEEE 1,5 1,10000 100000 2.0e-11 3.0e-12
|
---|
| 113 |
|
---|
| 114 | Cephes Math Library Release 2.8: June, 2000
|
---|
| 115 | Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
|
---|
| 116 | *************************************************************************/
|
---|
| 117 | public static double fcdistribution(int a,
|
---|
| 118 | int b,
|
---|
| 119 | double x)
|
---|
| 120 | {
|
---|
| 121 | double result = 0;
|
---|
| 122 | double w = 0;
|
---|
| 123 |
|
---|
| 124 | System.Diagnostics.Debug.Assert(a>=1 & b>=1 & (double)(x)>=(double)(0), "Domain error in FCDistribution");
|
---|
| 125 | w = b/(b+a*x);
|
---|
| 126 | result = ibetaf.incompletebeta(0.5*b, 0.5*a, w);
|
---|
| 127 | return result;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 |
|
---|
| 131 | /*************************************************************************
|
---|
| 132 | Inverse of complemented F distribution
|
---|
| 133 |
|
---|
| 134 | Finds the F density argument x such that the integral
|
---|
| 135 | from x to infinity of the F density is equal to the
|
---|
| 136 | given probability p.
|
---|
| 137 |
|
---|
| 138 | This is accomplished using the inverse beta integral
|
---|
| 139 | function and the relations
|
---|
| 140 |
|
---|
| 141 | z = incbi( df2/2, df1/2, p )
|
---|
| 142 | x = df2 (1-z) / (df1 z).
|
---|
| 143 |
|
---|
| 144 | Note: the following relations hold for the inverse of
|
---|
| 145 | the uncomplemented F distribution:
|
---|
| 146 |
|
---|
| 147 | z = incbi( df1/2, df2/2, p )
|
---|
| 148 | x = df2 z / (df1 (1-z)).
|
---|
| 149 |
|
---|
| 150 | ACCURACY:
|
---|
| 151 |
|
---|
| 152 | Tested at random points (a,b,p).
|
---|
| 153 |
|
---|
| 154 | a,b Relative error:
|
---|
| 155 | arithmetic domain # trials peak rms
|
---|
| 156 | For p between .001 and 1:
|
---|
| 157 | IEEE 1,100 100000 8.3e-15 4.7e-16
|
---|
| 158 | IEEE 1,10000 100000 2.1e-11 1.4e-13
|
---|
| 159 | For p between 10^-6 and 10^-3:
|
---|
| 160 | IEEE 1,100 50000 1.3e-12 8.4e-15
|
---|
| 161 | IEEE 1,10000 50000 3.0e-12 4.8e-14
|
---|
| 162 |
|
---|
| 163 | Cephes Math Library Release 2.8: June, 2000
|
---|
| 164 | Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
|
---|
| 165 | *************************************************************************/
|
---|
| 166 | public static double invfdistribution(int a,
|
---|
| 167 | int b,
|
---|
| 168 | double y)
|
---|
| 169 | {
|
---|
| 170 | double result = 0;
|
---|
| 171 | double w = 0;
|
---|
| 172 |
|
---|
| 173 | System.Diagnostics.Debug.Assert(a>=1 & b>=1 & (double)(y)>(double)(0) & (double)(y)<=(double)(1), "Domain error in InvFDistribution");
|
---|
| 174 |
|
---|
| 175 | //
|
---|
| 176 | // Compute probability for x = 0.5
|
---|
| 177 | //
|
---|
| 178 | w = ibetaf.incompletebeta(0.5*b, 0.5*a, 0.5);
|
---|
| 179 |
|
---|
| 180 | //
|
---|
| 181 | // If that is greater than y, then the solution w < .5
|
---|
| 182 | // Otherwise, solve at 1-y to remove cancellation in (b - b*w)
|
---|
| 183 | //
|
---|
| 184 | if( (double)(w)>(double)(y) | (double)(y)<(double)(0.001) )
|
---|
| 185 | {
|
---|
| 186 | w = ibetaf.invincompletebeta(0.5*b, 0.5*a, y);
|
---|
| 187 | result = (b-b*w)/(a*w);
|
---|
| 188 | }
|
---|
| 189 | else
|
---|
| 190 | {
|
---|
| 191 | w = ibetaf.invincompletebeta(0.5*a, 0.5*b, 1.0-y);
|
---|
| 192 | result = b*w/(a*(1.0-w));
|
---|
| 193 | }
|
---|
| 194 | return result;
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 | }
|
---|