12 x += 0.1659470187408462e-06 / (z+7);
13 x += 0.9934937113930748e-05 / (z+6);
14 x -= 0.1385710331296526 / (z+5);
15 x += 12.50734324009056 / (z+4);
16 x -= 176.6150291498386 / (z+3);
17 x += 771.3234287757674 / (z+2);
18 x -= 1259.139216722289 / (z+1);
19 x += 676.5203681218835 / z;
20 x += 0.9999999999995183;
21 return log(x) - 5.58106146679532777 - z + (z-0.5) * log(z+6.5);
30 const double p0 = 220.2068679123761;
31 const double p1 = 221.2135961699311;
32 const double p2 = 112.0792914978709;
33 const double p3 = 33.912866078383;
34 const double p4 = 6.37396220353165;
35 const double p5 = .7003830644436881;
36 const double p6 = .03526249659989109;
37 const double q0 = 440.4137358247522;
38 const double q1 = 793.8265125199484;
39 const double q2 = 637.3336333788311;
40 const double q3 = 296.5642487796737;
41 const double q4 = 86.78073220294608;
42 const double q5 = 16.06417757920695;
43 const double q6 = 1.755667163182642;
44 const double q7 = .08838834764831844;
46 z = fabs(x) * M_SQRT2;
47 if (z > 37.)
return x > 0.? 0. : 2.;
48 expntl = exp(z * z * - .5);
49 if (z < 10. / M_SQRT2)
50 p = expntl * ((((((p6 * z + p5) * z + p4) * z + p3) * z + p2) * z + p1) * z + p0)
51 / (((((((q7 * z + q6) * z + q5) * z + q4) * z + q3) * z + q2) * z + q1) * z + q0);
52 else p = expntl / 2.506628274631001 / (z + 1. / (z + 2. / (z + 3. / (z + 4. / (z + .65)))));
53 return x > 0.? 2. * p : 2. * (1. - p);
69 #define KF_GAMMA_EPS 1e-14
70 #define KF_TINY 1e-290
73 static double _kf_gammap(
double s,
double z)
77 for (k = 1, sum = x = 1.; k < 100; ++k) {
78 sum += (x *= z / (s + k));
81 return exp(s * log(z) - z -
kf_lgamma(s + 1.) + log(sum));
84 static double _kf_gammaq(
double s,
double z)
88 f = 1. + z - s; C = f; D = 0.;
91 for (j = 1; j < 100; ++j) {
92 double a = j * (s - j), b = (j<<1) + 1 + z - s, d;
102 return exp(s * log(z) - z -
kf_lgamma(s) - log(f));
107 return z <= 1. || z < s? _kf_gammap(s, z) : 1. - _kf_gammaq(s, z);
112 return z <= 1. || z < s? 1. - _kf_gammap(s, z) : _kf_gammaq(s, z);
122 static double kf_betai_aux(
double a,
double b,
double x)
126 if (x == 0.)
return 0.;
127 if (x == 1.)
return 1.;
128 f = 1.; C = f; D = 0.;
130 for (j = 1; j < 200; ++j) {
133 aa = (j&1)? -(a + m) * (a + b + m) * x / ((a + 2*m) * (a + 2*m + 1))
134 : m * (b - m) * x / ((a + 2*m - 1) * (a + 2*m));
148 return x < (a + 1.) / (a + b + 2.)? kf_betai_aux(a, b, x) : 1. - kf_betai_aux(b, a, 1. - x);
153 int main(
int argc,
char *argv[])
155 double x = 5.5, y = 3;
157 printf(
"erfc(%lg): %lg, %lg\n", x, erfc(x),
kf_erfc(x));
158 printf(
"upper-gamma(%lg,%lg): %lg\n", x, y,
kf_gammaq(y, x)*tgamma(y));
159 a = 2; b = 2; x = 0.5;
167 static double lbinom(
int n,
int k)
169 if (k == 0 || n == k)
return 0;
170 return lgamma(n+1) - lgamma(k+1) - lgamma(n-k+1);
179 static double hypergeo(
int n11,
int n1_,
int n_1,
int n)
181 return exp(lbinom(n1_, n11) + lbinom(n-n1_, n_1-n11) - lbinom(n, n_1));
190 static double hypergeo_acc(
int n11,
int n1_,
int n_1,
int n,
hgacc_t *aux)
192 if (n1_ || n_1 || n) {
193 aux->
n11 = n11; aux->
n1_ = n1_; aux->
n_1 = n_1; aux->
n = n;
195 if (n11%11 && n11 + aux->
n - aux->
n1_ - aux->
n_1) {
196 if (n11 == aux->
n11 + 1) {
197 aux->
p *= (double)(aux->
n1_ - aux->
n11) / n11
198 * (aux->
n_1 - aux->
n11) / (n11 + aux->
n - aux->
n1_ - aux->
n_1);
202 if (n11 == aux->
n11 - 1) {
203 aux->
p *= (double)aux->
n11 / (aux->
n1_ - n11)
204 * (aux->
n11 + aux->
n - aux->
n1_ - aux->
n_1) / (aux->
n_1 - n11);
211 aux->
p = hypergeo(aux->
n11, aux->
n1_, aux->
n_1, aux->
n);
215 double kt_fisher_exact(
int n11,
int n12,
int n21,
int n22,
double *_left,
double *_right,
double *two)
218 double p, q, left, right;
222 n1_ = n11 + n12; n_1 = n11 + n21; n = n11 + n12 + n21 + n22;
223 max = (n_1 < n1_) ? n_1 : n1_;
225 if (min < 0) min = 0;
226 *two = *_left = *_right = 1.;
227 if (min == max)
return 1.;
228 q = hypergeo_acc(n11, n1_, n_1, n, &aux);
230 p = hypergeo_acc(min, 0, 0, 0, &aux);
231 for (left = 0., i = min + 1; p < 0.99999999 * q && i<=
max; ++i)
232 left += p, p = hypergeo_acc(i, 0, 0, 0, &aux);
234 if (p < 1.00000001 * q) left += p;
237 p = hypergeo_acc(max, 0, 0, 0, &aux);
238 for (right = 0., j = max - 1; p < 0.99999999 * q && j>=0; --j)
239 right += p, p = hypergeo_acc(j, 0, 0, 0, &aux);
241 if (p < 1.00000001 * q) right += p;
245 if (*two > 1.) *two = 1.;
247 if (abs(i - n11) < abs(j - n11)) right = 1. - left + q;
248 else left = 1.0 - right + q;
249 *_left = left; *_right = right;