annotate PsiCLASS-1.0.2/samtools-0.1.19/bcftools/kfunc.c @ 0:903fc43d6227 draft default tip

Uploaded
author lsong10
date Fri, 26 Mar 2021 16:52:45 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1 #include <math.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4 /* Log gamma function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 * \log{\Gamma(z)}
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 * AS245, 2nd algorithm, http://lib.stat.cmu.edu/apstat/245
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 double kf_lgamma(double z)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 double x = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11 x += 0.1659470187408462e-06 / (z+7);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12 x += 0.9934937113930748e-05 / (z+6);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13 x -= 0.1385710331296526 / (z+5);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 x += 12.50734324009056 / (z+4);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15 x -= 176.6150291498386 / (z+3);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16 x += 771.3234287757674 / (z+2);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 x -= 1259.139216722289 / (z+1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 x += 676.5203681218835 / z;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 x += 0.9999999999995183;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 return log(x) - 5.58106146679532777 - z + (z-0.5) * log(z+6.5);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 /* complementary error function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 * \frac{2}{\sqrt{\pi}} \int_x^{\infty} e^{-t^2} dt
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25 * AS66, 2nd algorithm, http://lib.stat.cmu.edu/apstat/66
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 double kf_erfc(double x)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 const double p0 = 220.2068679123761;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 const double p1 = 221.2135961699311;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 const double p2 = 112.0792914978709;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 const double p3 = 33.912866078383;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 const double p4 = 6.37396220353165;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 const double p5 = .7003830644436881;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 const double p6 = .03526249659989109;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36 const double q0 = 440.4137358247522;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 const double q1 = 793.8265125199484;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 const double q2 = 637.3336333788311;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 const double q3 = 296.5642487796737;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 const double q4 = 86.78073220294608;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 const double q5 = 16.06417757920695;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 const double q6 = 1.755667163182642;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 const double q7 = .08838834764831844;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 double expntl, z, p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 z = fabs(x) * M_SQRT2;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 if (z > 37.) return x > 0.? 0. : 2.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 expntl = exp(z * z * - .5);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 if (z < 10. / M_SQRT2) // for small z
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 p = expntl * ((((((p6 * z + p5) * z + p4) * z + p3) * z + p2) * z + p1) * z + p0)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 / (((((((q7 * z + q6) * z + q5) * z + q4) * z + q3) * z + q2) * z + q1) * z + q0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 else p = expntl / 2.506628274631001 / (z + 1. / (z + 2. / (z + 3. / (z + 4. / (z + .65)))));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 return x > 0.? 2. * p : 2. * (1. - p);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 /* The following computes regularized incomplete gamma functions.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 * Formulas are taken from Wiki, with additional input from Numerical
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 * Recipes in C (for modified Lentz's algorithm) and AS245
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 * (http://lib.stat.cmu.edu/apstat/245).
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 *
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 * A good online calculator is available at:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 *
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 * http://www.danielsoper.com/statcalc/calc23.aspx
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63 *
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 * It calculates upper incomplete gamma function, which equals
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 * kf_gammaq(s,z)*tgamma(s).
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 #define KF_GAMMA_EPS 1e-14
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 #define KF_TINY 1e-290
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71 // regularized lower incomplete gamma function, by series expansion
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 static double _kf_gammap(double s, double z)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 double sum, x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 int k;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 for (k = 1, sum = x = 1.; k < 100; ++k) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 sum += (x *= z / (s + k));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 if (x / sum < KF_GAMMA_EPS) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 return exp(s * log(z) - z - kf_lgamma(s + 1.) + log(sum));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 // regularized upper incomplete gamma function, by continued fraction
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83 static double _kf_gammaq(double s, double z)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 int j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 double C, D, f;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 f = 1. + z - s; C = f; D = 0.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 // Modified Lentz's algorithm for computing continued fraction
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 // See Numerical Recipes in C, 2nd edition, section 5.2
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 for (j = 1; j < 100; ++j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 double a = j * (s - j), b = (j<<1) + 1 + z - s, d;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 D = b + a * D;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 if (D < KF_TINY) D = KF_TINY;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 C = b + a / C;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 if (C < KF_TINY) C = KF_TINY;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 D = 1. / D;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97 d = C * D;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98 f *= d;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 if (fabs(d - 1.) < KF_GAMMA_EPS) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 return exp(s * log(z) - z - kf_lgamma(s) - log(f));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 double kf_gammap(double s, double z)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 return z <= 1. || z < s? _kf_gammap(s, z) : 1. - _kf_gammaq(s, z);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 double kf_gammaq(double s, double z)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 return z <= 1. || z < s? 1. - _kf_gammap(s, z) : _kf_gammaq(s, z);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114 /* Regularized incomplete beta function. The method is taken from
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 * Numerical Recipe in C, 2nd edition, section 6.4. The following web
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116 * page calculates the incomplete beta function, which equals
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 * kf_betai(a,b,x) * gamma(a) * gamma(b) / gamma(a+b):
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 *
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 * http://www.danielsoper.com/statcalc/calc36.aspx
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 static double kf_betai_aux(double a, double b, double x)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 double C, D, f;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124 int j;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 if (x == 0.) return 0.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 if (x == 1.) return 1.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 f = 1.; C = f; D = 0.;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 // Modified Lentz's algorithm for computing continued fraction
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 for (j = 1; j < 200; ++j) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130 double aa, d;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 int m = j>>1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132 aa = (j&1)? -(a + m) * (a + b + m) * x / ((a + 2*m) * (a + 2*m + 1))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 : m * (b - m) * x / ((a + 2*m - 1) * (a + 2*m));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134 D = 1. + aa * D;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135 if (D < KF_TINY) D = KF_TINY;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 C = 1. + aa / C;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137 if (C < KF_TINY) C = KF_TINY;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 D = 1. / D;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 d = C * D;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 f *= d;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 if (fabs(d - 1.) < KF_GAMMA_EPS) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143 return exp(kf_lgamma(a+b) - kf_lgamma(a) - kf_lgamma(b) + a * log(x) + b * log(1.-x)) / a / f;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 double kf_betai(double a, double b, double x)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147 return x < (a + 1.) / (a + b + 2.)? kf_betai_aux(a, b, x) : 1. - kf_betai_aux(b, a, 1. - x);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 #ifdef KF_MAIN
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151 #include <stdio.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 int main(int argc, char *argv[])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154 double x = 5.5, y = 3;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 double a, b;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 printf("erfc(%lg): %lg, %lg\n", x, erfc(x), kf_erfc(x));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157 printf("upper-gamma(%lg,%lg): %lg\n", x, y, kf_gammaq(y, x)*tgamma(y));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158 a = 2; b = 2; x = 0.5;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159 printf("incomplete-beta(%lg,%lg,%lg): %lg\n", a, b, x, kf_betai(a, b, x) / exp(kf_lgamma(a+b) - kf_lgamma(a) - kf_lgamma(b)));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162 #endif