forked from c-zhou/yahs
-
Notifications
You must be signed in to change notification settings - Fork 4
/
binomlite.c
2338 lines (2019 loc) · 70.8 KB
/
binomlite.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************************
* MIT License *
* *
* Copyright (c) 2021 Chenxi Zhou <[email protected]> *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy *
* of this software and associated documentation files (the "Software"), to deal *
* in the Software without restriction, including without limitation the rights *
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
* SOFTWARE. *
*********************************************************************************/
/********************************** Revision History *****************************
* *
* 07/12/21 - Chenxi Zhou: Created *
* a lite version of the qbinom function (quantile function of *
* the binomial distribution) modified from R math core library *
* *
*********************************************************************************/
#include <math.h>
#include <float.h>
#include <limits.h>
#define fmax2(x,y) (((x) >= (y)) ? (x) : (y))
#define fmin2(x,y) (((x) <= (y)) ? (x) : (y))
#undef R_Log1_Exp
#define R_Log1_Exp(x) ((x) > -M_LN2 ? log(-rexpm1(x)) : log1p(-exp(x)))
#define ML_NEGINF ((-1.0) / 0.0)
# define R_FINITE(x) isfinite(x)
#define R_D__0 (log_p ? ML_NEGINF : 0.) /* 0 */
#define R_D__1 (log_p ? 0. : 1.) /* 1 */
#define R_DT_0 (lower_tail ? R_D__0 : R_D__1) /* 0 */
#define R_DT_1 (lower_tail ? R_D__1 : R_D__0) /* 1 */
#define R_D_Lval(p) (lower_tail ? (p) : (0.5 - (p) + 0.5)) /* p */
#define R_D_Cval(p) (lower_tail ? (0.5 - (p) + 0.5) : (p)) /* 1 - p */
/*#define R_DT_qIv(p) R_D_Lval(R_D_qIv(p)) * p in qF ! */
#define R_DT_qIv(p) (log_p ? (lower_tail ? exp(p) : - expm1(p)) \
: R_D_Lval(p))
/*#define R_DT_CIv(p) R_D_Cval(R_D_qIv(p)) * 1 - p in qF */
#define R_DT_CIv(p) (log_p ? (lower_tail ? -expm1(p) : exp(p)) \
: R_D_Cval(p))
#define R_D_exp(x) (log_p ? (x) : exp(x)) /* exp(x) */
#ifndef M_LN_SQRT_2PI
#define M_LN_SQRT_2PI 0.918938533204672741780329736406 /* log(sqrt(2*pi))
== log(2*pi)/2 */
#endif
#ifndef M_SQRT_PI
#define M_SQRT_PI 1.772453850905516027298167483341 /* sqrt(pi) */
#endif
static double bfrac(double, double, double, double, double, double, int log_p);
static void bgrat(double, double, double, double, double *, double, int *, int log_w);
static double grat_r(double a, double x, double r, double eps);
static double apser(double, double, double, double);
static double bpser(double, double, double, double, int log_p);
static double basym(double, double, double, double, int log_p);
static double fpser(double, double, double, double, int log_p);
static double bup(double, double, double, double, int, double, int give_log);
static double exparg(int);
static double psi(double);
static double gam1(double);
static double gamln1(double);
static double betaln(double, double);
static double algdiv(double, double);
static double brcmp1(int, double, double, double, double, int give_log);
static double brcomp(double, double, double, double, int log_p);
static double rlog1(double);
static double bcorr(double, double);
static double gamln(double);
static double alnrel(double);
static double esum(int, double, int give_log);
static double erf__(double);
static double rexpm1(double);
static double erfc1(int, double);
static double gsumln(double, double);
static double logspace_add (double, double);
/*
* Compute the log of a sum from logs of terms, i.e.,
*
* log (exp (logx) + exp (logy))
*
* without causing overflows and without throwing away large handfuls
* of accuracy.
*/
double logspace_add (double logx, double logy)
{
return fmax2 (logx, logy) + log1p (exp (-fabs (logx - logy)));
}
double fpser(double a, double b, double x, double eps, int log_p)
{
/* ----------------------------------------------------------------------- *
* EVALUATION OF I (A,B)
* X
* FOR B < MIN(EPS, EPS*A) AND X <= 0.5
* ----------------------------------------------------------------------- */
double ans, c, s, t, an, tol;
/* SET ans := x^a : */
if (log_p) {
ans = a * log(x);
} else if (a > eps * 0.001) {
t = a * log(x);
if (t < exparg(1)) { /* exp(t) would underflow */
return 0.;
}
ans = exp(t);
} else {
ans = 1.;
}
/* NOTE THAT 1/B(A,B) = B */
if (log_p)
ans += log(b) - log(a);
else
ans *= b / a;
tol = eps / a;
an = a + 1.;
t = x;
s = t / an;
do {
an += 1.;
t = x * t;
c = t / an;
s += c;
} while (fabs(c) > tol);
if (log_p)
ans += log1p(a * s);
else
ans *= a * s + 1.;
return ans;
} /* fpser */
static double apser(double a, double b, double x, double eps)
{
/* -----------------------------------------------------------------------
* apser() yields the incomplete beta ratio I_{1-x}(b,a) for
* a <= min(eps,eps*b), b*x <= 1, and x <= 0.5, i.e., a is very small.
* Use only if above inequalities are satisfied.
* ----------------------------------------------------------------------- */
static double const g = .577215664901533;
double tol, c, j, s, t, aj;
double bx = b * x;
t = x - bx;
if (b * eps <= 0.02)
c = log(x) + psi(b) + g + t;
else // b > 2e13 : psi(b) ~= log(b)
c = log(bx) + g + t;
tol = eps * 5. * fabs(c);
j = 1.;
s = 0.;
do {
j += 1.;
t *= x - bx / j;
aj = t / j;
s += aj;
} while (fabs(aj) > tol);
return -a * (c + s);
} /* apser */
static double bpser(double a, double b, double x, double eps, int log_p)
{
/* -----------------------------------------------------------------------
* Power SERies expansion for evaluating I_x(a,b) when
* b <= 1 or b*x <= 0.7. eps is the tolerance used.
* NB: if log_p is 1, also use it if (b < 40 & lambda > 650)
* ----------------------------------------------------------------------- */
int i, m;
double ans, c, t, u, z, a0, b0, apb;
if (x == 0.) {
return R_D__0;
}
/* ----------------------------------------------------------------------- */
/* compute the factor x^a/(a*Beta(a,b)) */
/* ----------------------------------------------------------------------- */
a0 = fmin2(a,b);
if (a0 >= 1.) { /* ------ 1 <= a0 <= b0 ------ */
z = a * log(x) - betaln(a, b);
ans = log_p ? z - log(a) : exp(z) / a;
} else {
b0 = fmax2(a,b);
if (b0 < 8.) {
if (b0 <= 1.) { /* ------ a0 < 1 and b0 <= 1 ------ */
if(log_p) {
ans = a * log(x);
} else {
ans = pow(x, a);
if (ans == 0.) /* once underflow, always underflow .. */
return ans;
}
apb = a + b;
if (apb > 1.) {
u = a + b - 1.;
z = (gam1(u) + 1.) / apb;
} else {
z = gam1(apb) + 1.;
}
c = (gam1(a) + 1.) * (gam1(b) + 1.) / z;
if(log_p) /* FIXME ? -- improve quite a bit for c ~= 1 */
ans += log(c * (b / apb));
else
ans *= c * (b / apb);
} else { /* ------ a0 < 1 < b0 < 8 ------ */
u = gamln1(a0);
m = (int)(b0 - 1.);
if (m >= 1) {
c = 1.;
for (i = 1; i <= m; ++i) {
b0 += -1.;
c *= b0 / (a0 + b0);
}
u += log(c);
}
z = a * log(x) - u;
b0 += -1.; // => b0 in (0, 7)
apb = a0 + b0;
if (apb > 1.) {
u = a0 + b0 - 1.;
t = (gam1(u) + 1.) / apb;
} else {
t = gam1(apb) + 1.;
}
if(log_p) /* FIXME? potential for improving log(t) */
ans = z + log(a0 / a) + log1p(gam1(b0)) - log(t);
else
ans = exp(z) * (a0 / a) * (gam1(b0) + 1.) / t;
}
} else { /* ------ a0 < 1 < 8 <= b0 ------ */
u = gamln1(a0) + algdiv(a0, b0);
z = a * log(x) - u;
if(log_p)
ans = z + log(a0 / a);
else
ans = a0 / a * exp(z);
}
}
if (ans == R_D__0 || (!log_p && a <= eps * 0.1)) {
return ans;
}
/* ----------------------------------------------------------------------- */
/* COMPUTE THE SERIES */
/* ----------------------------------------------------------------------- */
double tol = eps / a,
n = 0.,
sum = 0., w;
c = 1.;
do { // sum is alternating as long as n < b (<==> 1 - b/n < 0)
n += 1.;
c *= (0.5 - b / n + 0.5) * x;
w = c / (a + n);
sum += w;
} while (n < 1e7 && fabs(w) > tol);
if(log_p) {
if (a*sum > -1.)
ans += log1p(a * sum);
else
ans = ML_NEGINF;
} else if (a*sum > -1.) {
ans *= (a * sum + 1.);
} else { // underflow to
ans = 0.;
}
return ans;
} /* bpser */
static double bup(double a, double b, double x, double y, int n, double eps, int give_log)
{
/* ----------------------------------------------------------------------- */
/* EVALUATION OF I_x(A,B) - I_x(A+N,B) WHERE N IS A POSITIVE INT. */
/* EPS IS THE TOLERANCE USED. */
/* ----------------------------------------------------------------------- */
double ret_val;
int i, k, mu;
double d, l;
// Obtain the scaling factor exp(-mu) and exp(mu)*(x^a * y^b / beta(a,b))/a
double apb = a + b,
ap1 = a + 1.;
if (n > 1 && a >= 1. && apb >= ap1 * 1.1) {
mu = (int)fabs(exparg(1));
k = (int) exparg(0);
if (mu > k)
mu = k;
d = exp(-(double) mu);
} else {
mu = 0;
d = 1.;
}
/* L10: */
ret_val = give_log? brcmp1(mu, a, b, x, y, 1) - log(a)
: brcmp1(mu, a, b, x, y, 0) / a;
if (n == 1 ||
(give_log && ret_val == ML_NEGINF) || (!give_log && ret_val == 0.))
return ret_val;
int nm1 = n - 1;
double w = d;
/* LET K BE THE INDEX OF THE MAXIMUM TERM */
k = 0;
if (b > 1.) {
if (y > 1e-4) {
double r = (b - 1.) * x / y - a;
if (r >= 1.)
k = (r < nm1) ? (int) r : nm1;
} else {
k = nm1;
}
// ADD THE INCREASING TERMS OF THE SERIES - if k > 0
/* L30: */
for (i = 0; i < k; ++i) {
l = (double) i;
d *= (apb + l) / (ap1 + l) * x;
w += d;
}
}
// L40: ADD THE REMAINING TERMS OF THE SERIES
for (i = k; i < nm1; ++i) {
l = (double) i;
d *= (apb + l) / (ap1 + l) * x;
w += d;
if (d <= eps * w) /* relativ convergence (eps) */
break;
}
// L50: TERMINATE THE PROCEDURE
if(give_log) {
ret_val += log(w);
} else {
ret_val *= w;
}
return ret_val;
} /* bup */
static double bfrac(double a, double b, double x, double y, double lambda, double eps, int log_p)
{
/* -----------------------------------------------------------------------
Continued fraction expansion for I_x(a,b) when a, b > 1.
It is assumed that lambda = (a + b)*y - b.
-----------------------------------------------------------------------*/
double c, e, n, p, r, s, t, w, c0, c1, r0, an, bn, yp1, anp1, bnp1, beta, alpha, brc;
brc = brcomp(a, b, x, y, log_p);
if (!log_p && brc == 0.) {
return 0.;
}
c = lambda + 1.;
c0 = b / a;
c1 = 1. / a + 1.;
yp1 = y + 1.;
n = 0.;
p = 1.;
s = a + 1.;
an = 0.;
bn = 1.;
anp1 = 1.;
bnp1 = c / c1;
r = c1 / c;
/* CONTINUED FRACTION CALCULATION */
do {
n += 1.;
t = n / a;
w = n * (b - n) * x;
e = a / s;
alpha = p * (p + c0) * e * e * (w * x);
e = (t + 1.) / (c1 + t + t);
beta = n + w / s + e * (c + n * yp1);
p = t + 1.;
s += 2.;
/* update an, bn, anp1, and bnp1 */
t = alpha * an + beta * anp1; an = anp1; anp1 = t;
t = alpha * bn + beta * bnp1; bn = bnp1; bnp1 = t;
r0 = r;
r = anp1 / bnp1;
if (fabs(r - r0) <= eps * r)
break;
/* rescale an, bn, anp1, and bnp1 */
an /= bnp1;
bn /= bnp1;
anp1 = r;
bnp1 = 1.;
} while (n < 10000);// arbitrary; had '1' --> infinite loop for lambda = Inf
return (log_p ? brc + log(r) : brc * r);
} /* bfrac */
static double brcomp(double a, double b, double x, double y, int log_p)
{
/* -----------------------------------------------------------------------
* Evaluation of x^a * y^b / Beta(a,b)
* ----------------------------------------------------------------------- */
static double const__ = .398942280401433; /* == 1/sqrt(2*pi); */
/* R has M_1_SQRT_2PI , and M_LN_SQRT_2PI = ln(sqrt(2*pi)) = 0.918938.. */
int i, n;
double c, e, u, v, z, a0, b0, apb;
if (x == 0. || y == 0.) {
return R_D__0;
}
a0 = fmin2(a, b);
if (a0 < 8.) {
double lnx, lny;
if (x <= .375) {
lnx = log(x);
lny = alnrel(-x);
}
else {
if (y > .375) {
lnx = log(x);
lny = log(y);
} else {
lnx = alnrel(-y);
lny = log(y);
}
}
z = a * lnx + b * lny;
if (a0 >= 1.) {
z -= betaln(a, b);
return R_D_exp(z);
}
/* ----------------------------------------------------------------------- */
/* PROCEDURE FOR a < 1 OR b < 1 */
/* ----------------------------------------------------------------------- */
b0 = fmax2(a, b);
if (b0 >= 8.) { /* L80: */
u = gamln1(a0) + algdiv(a0, b0);
return (log_p ? log(a0) + (z - u) : a0 * exp(z - u));
}
/* else : */
if (b0 <= 1.) { /* algorithm for max(a,b) = b0 <= 1 */
double e_z = R_D_exp(z);
if (!log_p && e_z == 0.) /* exp() underflow */
return 0.;
apb = a + b;
if (apb > 1.) {
u = a + b - 1.;
z = (gam1(u) + 1.) / apb;
} else {
z = gam1(apb) + 1.;
}
c = (gam1(a) + 1.) * (gam1(b) + 1.) / z;
/* FIXME? log(a0*c)= log(a0)+ log(c) and that is improvable */
return (log_p
? e_z + log(a0 * c) - log1p(a0/b0)
: e_z * (a0 * c) / (a0 / b0 + 1.));
}
/* else : ALGORITHM FOR 1 < b0 < 8 */
u = gamln1(a0);
n = (int)(b0 - 1.);
if (n >= 1) {
c = 1.;
for (i = 1; i <= n; ++i) {
b0 += -1.;
c *= b0 / (a0 + b0);
}
u = log(c) + u;
}
z -= u;
b0 += -1.;
apb = a0 + b0;
double t;
if (apb > 1.) {
u = a0 + b0 - 1.;
t = (gam1(u) + 1.) / apb;
} else {
t = gam1(apb) + 1.;
}
return (log_p
? log(a0) + z + log1p(gam1(b0)) - log(t)
: a0 * exp(z) * (gam1(b0) + 1.) / t);
} else {
/* ----------------------------------------------------------------------- */
/* PROCEDURE FOR A >= 8 AND B >= 8 */
/* ----------------------------------------------------------------------- */
double h, x0, y0, lambda;
if (a <= b) {
h = a / b;
x0 = h / (h + 1.);
y0 = 1. / (h + 1.);
lambda = a - (a + b) * x;
} else {
h = b / a;
x0 = 1. / (h + 1.);
y0 = h / (h + 1.);
lambda = (a + b) * y - b;
}
e = -lambda / a;
if (fabs(e) > .6)
u = e - log(x / x0);
else
u = rlog1(e);
e = lambda / b;
if (fabs(e) <= .6)
v = rlog1(e);
else
v = e - log(y / y0);
z = log_p ? -(a * u + b * v) : exp(-(a * u + b * v));
return(log_p
? -M_LN_SQRT_2PI + .5*log(b * x0) + z - bcorr(a,b)
: const__ * sqrt(b * x0) * z * exp(-bcorr(a, b)));
}
} /* brcomp */
// called only once from bup(), as r = brcmp1(mu, a, b, x, y, 0) / a;
// -----
static double brcmp1(int mu, double a, double b, double x, double y, int give_log)
{
/* -----------------------------------------------------------------------
* Evaluation of exp(mu) * x^a * y^b / beta(a,b)
* ----------------------------------------------------------------------- */
static double const__ = .398942280401433; /* == 1/sqrt(2*pi); */
/* R has M_1_SQRT_2PI */
/* Local variables */
double c, t, u, v, z, a0, b0, apb;
a0 = fmin2(a,b);
if (a0 < 8.) {
double lnx, lny;
if (x <= .375) {
lnx = log(x);
lny = alnrel(-x);
} else if (y > .375) {
// L11:
lnx = log(x);
lny = log(y);
} else {
lnx = alnrel(-y);
lny = log(y);
}
// L20:
z = a * lnx + b * lny;
if (a0 >= 1.) {
z -= betaln(a, b);
return esum(mu, z, give_log);
}
// else :
/* ----------------------------------------------------------------------- */
/* PROCEDURE FOR A < 1 OR B < 1 */
/* ----------------------------------------------------------------------- */
// L30:
b0 = fmax2(a,b);
if (b0 >= 8.) {
/* L80: ALGORITHM FOR b0 >= 8 */
u = gamln1(a0) + algdiv(a0, b0);
return give_log
? log(a0) + esum(mu, z - u, 1)
: a0 * esum(mu, z - u, 0);
} else if (b0 <= 1.) {
// a0 < 1, b0 <= 1
double ans = esum(mu, z, give_log);
if (ans == (give_log ? ML_NEGINF : 0.))
return ans;
apb = a + b;
if (apb > 1.) {
// L40:
u = a + b - 1.;
z = (gam1(u) + 1.) / apb;
} else {
z = gam1(apb) + 1.;
}
// L50:
c = give_log
? log1p(gam1(a)) + log1p(gam1(b)) - log(z)
: (gam1(a) + 1.) * (gam1(b) + 1.) / z;
return give_log
? ans + log(a0) + c - log1p(a0 / b0)
: ans * (a0 * c) / (a0 / b0 + 1.);
}
// else: algorithm for a0 < 1 < b0 < 8
// L60:
u = gamln1(a0);
int i, n = (int)(b0 - 1.);
if (n >= 1) {
c = 1.;
for (i = 1; i <= n; ++i) {
b0 += -1.;
c *= b0 / (a0 + b0);
/* L61: */
}
u += log(c); // TODO?: log(c) = log( prod(...) ) = sum( log(...) )
}
// L70:
z -= u;
b0 += -1.;
apb = a0 + b0;
if (apb > 1.) {
// L71:
t = (gam1(apb - 1.) + 1.) / apb;
} else {
t = gam1(apb) + 1.;
}
// L72:
return give_log
? log(a0)+ esum(mu, z, 1) + log1p(gam1(b0)) - log(t) // TODO? log(t) = log1p(..)
: a0 * esum(mu, z, 0) * (gam1(b0) + 1.) / t;
} else {
/* ----------------------------------------------------------------------- */
/* PROCEDURE FOR A >= 8 AND B >= 8 */
/* ----------------------------------------------------------------------- */
// L100:
double h, x0, y0, lambda;
if (a > b) {
// L101:
h = b / a;
x0 = 1. / (h + 1.);// => lx0 := log(x0) = 0 - log1p(h)
y0 = h / (h + 1.);
lambda = (a + b) * y - b;
} else {
h = a / b;
x0 = h / (h + 1.); // => lx0 := log(x0) = - log1p(1/h)
y0 = 1. / (h + 1.);
lambda = a - (a + b) * x;
}
double lx0 = -log1p(b/a); // in both cases
// L110:
double e = -lambda / a;
if (fabs(e) > 0.6) {
// L111:
u = e - log(x / x0);
} else {
u = rlog1(e);
}
// L120:
e = lambda / b;
if (fabs(e) > 0.6) {
// L121:
v = e - log(y / y0);
} else {
v = rlog1(e);
}
// L130:
z = esum(mu, -(a * u + b * v), give_log);
return give_log
? log(const__)+ (log(b) + lx0)/2. + z - bcorr(a, b)
: const__ * sqrt(b * x0) * z * exp(-bcorr(a, b));
}
} /* brcmp1 */
static void bgrat(double a, double b, double x, double y, double *w,
double eps, int *ierr, int log_w)
{
/* -----------------------------------------------------------------------
* Asymptotic Expansion for I_x(a,b) when a is larger than b.
* Compute w := w + I_x(a,b)
* It is assumed a >= 15 and b <= 1.
* eps is the tolerance used.
* ierr is a variable that reports the status of the results.
*
* if(log_w), *w itself must be in log-space;
* compute w := w + I_x(a,b) but return *w = log(w):
* *w := log(exp(*w) + I_x(a,b)) = logspace_add(*w, log( I_x(a,b) ))
* ----------------------------------------------------------------------- */
#define n_terms_bgrat 30
double c[n_terms_bgrat], d[n_terms_bgrat];
double bm1 = b - 0.5 - 0.5,
nu = a + bm1 * 0.5, /* nu = a + (b-1)/2 =: T, in (9.1) of
* Didonato & Morris(1992), p.362 */
lnx = (y > 0.375) ? log(x) : alnrel(-y),
z = -nu * lnx; // z =: u in (9.1) of D.&M.(1992)
if (b * z == 0.) { // should not happen, but does, e.g.,
// for pbeta(1e-320, 1e-5, 0.5) i.e., _subnormal_ x,
// Warning ... bgrat(a=20.5, b=1e-05, x=1, y=9.99989e-321): ..
/* L_Error: THE EXPANSION CANNOT BE COMPUTED */
*ierr = 1; return;
}
/* COMPUTATION OF THE EXPANSION */
double
/* r1 = b * (gam1(b) + 1.) * exp(b * log(z)),// = b/gamma(b+1) z^b = z^b / gamma(b)
* set r := exp(-z) * z^b / gamma(b) ;
* gam1(b) = 1/gamma(b+1) - 1 , b in [-1/2, 3/2] */
// exp(a*lnx) underflows for large (a * lnx); e.g. large a ==> using log_r := log(r):
// r = r1 * exp(a * lnx) * exp(bm1 * 0.5 * lnx);
// log(r)=log(b) + log1p(gam1(b)) + b * log(z) + (a * lnx) + (bm1 * 0.5 * lnx),
log_r = log(b) + log1p(gam1(b)) + b * log(z) + nu * lnx,
// FIXME work with log_u = log(u) also when log_p=0 (??)
// u is 'factored out' from the expansion {and multiplied back, at the end}:
log_u = log_r - (algdiv(b, a) + b * log(nu)),// algdiv(b,a) = log(gamma(a)/gamma(a+b))
/* u = (log_p) ? log_r - u : exp(log_r-u); // =: M in (9.2) of {reference above} */
/* u = algdiv(b, a) + b * log(nu);// algdiv(b,a) = log(gamma(a)/gamma(a+b)) */
// u = (log_p) ? log_u : exp(log_u); // =: M in (9.2) of {reference above}
u = exp(log_u);
if (log_u == ML_NEGINF) {
/* L_Error: THE EXPANSION CANNOT BE COMPUTED */ *ierr = 2; return;
}
int u_0 = (u == 0.); // underflow --> do work with log(u) == log_u !
double l = // := *w/u .. but with care: such that it also works when u underflows to 0:
log_w
? ((*w == ML_NEGINF) ? 0. : exp( *w - log_u))
: ((*w == 0.) ? 0. : exp(log(*w) - log_u));
double
q_r = grat_r(b, z, log_r, eps), // = q/r of former grat1(b,z, r, &p, &q)
v = 0.25 / (nu * nu),
t2 = lnx * 0.25 * lnx,
j = q_r,
sum = j,
t = 1., cn = 1., n2 = 0.;
int n;
for (n = 1; n <= n_terms_bgrat; ++n) {
double bp2n = b + n2;
j = (bp2n * (bp2n + 1.) * j + (z + bp2n + 1.) * t) * v;
n2 += 2.;
t *= t2;
cn /= n2 * (n2 + 1.);
int nm1 = n - 1;
c[nm1] = cn;
double s = 0.;
if (n > 1) {
double coef = b - n;
int i;
for (i = 1; i <= nm1; ++i) {
s += coef * c[i - 1] * d[nm1 - i];
coef += b;
}
}
d[nm1] = bm1 * cn + s / n;
double dj = d[nm1] * j;
sum += dj;
if (sum <= 0.) {
/* L_Error: THE EXPANSION CANNOT BE COMPUTED */ *ierr = 3; return;
}
if (fabs(dj) <= eps * (sum + l)) {
*ierr = 0;
break;
} else if(n == n_terms_bgrat) { // never? ; please notify R-core if seen:
*ierr = 4;
}
} // for(n .. n_terms..)
/* ADD THE RESULTS TO W */
if(log_w) // *w is in log space already:
*w = logspace_add(*w, log_u + log(sum));
else
*w += (u_0 ? exp(log_u + log(sum)) : u * sum);
return;
} /* bgrat */
// called only from bgrat() , as q_r = grat_r(b, z, log_r, eps) :
static double grat_r(double a, double x, double log_r, double eps)
{
/* -----------------------------------------------------------------------
* Scaled complement of incomplete gamma ratio function
* grat_r(a,x,r) := Q(a,x) / r
* where
* Q(a,x) = pgamma(x,a, lower.tail=0)
* and r = e^(-x)* x^a / Gamma(a) == exp(log_r)
*
* It is assumed that a <= 1. eps is the tolerance to be used.
* ----------------------------------------------------------------------- */
if (a * x == 0.) { /* L130: */
if (x <= a) {
/* L100: */ return exp(-log_r);
} else {
/* L110:*/ return 0.;
}
}
else if (a == 0.5) { // e.g. when called from pt()
/* L120: */
if (x < 0.25) {
double p = erf__(sqrt(x));
return (0.5 - p + 0.5)*exp(-log_r);
} else { // 2013-02-27: improvement for "large" x: direct computation of q/r:
double sx = sqrt(x),
q_r = erfc1(1, sx)/sx * M_SQRT_PI;
return q_r;
}
} else if (x < 1.1) { /* L10: Taylor series for P(a,x)/x^a */
double an = 3.,
c = x,
sum = x / (a + 3.),
tol = eps * 0.1 / (a + 1.), t;
do {
an += 1.;
c *= -(x / an);
t = c / (a + an);
sum += t;
} while (fabs(t) > tol);
double j = a * x * ((sum/6. - 0.5/(a + 2.)) * x + 1./(a + 1.)),
z = a * log(x),
h = gam1(a),
g = h + 1.;
if ((x >= 0.25 && (a < x / 2.59)) || (z > -0.13394)) {
// L40:
double l = rexpm1(z),
q = ((l + 0.5 + 0.5) * j - l) * g - h;
if (q <= 0.) {
/* L110:*/ return 0.;
} else {
return q * exp(-log_r);
}
} else {
double p = exp(z) * g * (0.5 - j + 0.5);
return /* q/r = */ (0.5 - p + 0.5) * exp(-log_r);
}
} else {
/* L50: ---- (x >= 1.1) ---- Continued Fraction Expansion */
double a2n_1 = 1.,
a2n = 1.,
b2n_1 = x,
b2n = x + (1. - a),
c = 1., am0, an0;
do {
a2n_1 = x * a2n + c * a2n_1;
b2n_1 = x * b2n + c * b2n_1;
am0 = a2n_1 / b2n_1;
c += 1.;
double c_a = c - a;
a2n = a2n_1 + c_a * a2n;
b2n = b2n_1 + c_a * b2n;
an0 = a2n / b2n;
} while (fabs(an0 - am0) >= eps * an0);
return /* q/r = (r * an0)/r = */ an0;
}
} /* grat_r */
static double basym(double a, double b, double lambda, double eps, int log_p)
{
/* ----------------------------------------------------------------------- */
/* ASYMPTOTIC EXPANSION FOR I_x(A,B) FOR LARGE A AND B. */
/* LAMBDA = (A + B)*Y - B AND EPS IS THE TOLERANCE USED. */
/* IT IS ASSUMED THAT LAMBDA IS NONNEGATIVE AND THAT */
/* A AND B ARE GREATER THAN OR EQUAL TO 15. */
/* ----------------------------------------------------------------------- */
/* ------------------------ */
/* ****** NUM IS THE MAXIMUM VALUE THAT N CAN TAKE IN THE DO LOOP */
/* ENDING AT STATEMENT 50. IT IS REQUIRED THAT NUM BE EVEN. */
#define num_IT 20
/* THE ARRAYS A0, B0, C, D HAVE DIMENSION NUM + 1. */
static double const e0 = 1.12837916709551;/* e0 == 2/sqrt(pi) */
static double const e1 = .353553390593274;/* e1 == 2^(-3/2) */
static double const ln_e0 = 0.120782237635245; /* == ln(e0) */
double a0[num_IT + 1], b0[num_IT + 1], c[num_IT + 1], d[num_IT + 1];
double f = a * rlog1(-lambda/a) + b * rlog1(lambda/b), t;
if(log_p)
t = -f;
else {
t = exp(-f);
if (t == 0.) {
return 0; /* once underflow, always underflow .. */
}
}
double z0 = sqrt(f),
z = z0 / e1 * 0.5,
z2 = f + f,
h, r0, r1, w0;
if (a < b) {
h = a / b;
r0 = 1. / (h + 1.);
r1 = (b - a) / b;
w0 = 1. / sqrt(a * (h + 1.));
} else {
h = b / a;
r0 = 1. / (h + 1.);
r1 = (b - a) / a;
w0 = 1. / sqrt(b * (h + 1.));
}
a0[0] = r1 * .66666666666666663;
c[0] = a0[0] * -0.5;
d[0] = -c[0];
double j0 = 0.5 / e0 * erfc1(1, z0),
j1 = e1,
sum = j0 + d[0] * w0 * j1;
double s = 1.,
h2 = h * h,
hn = 1.,
w = w0,
znm1 = z,
zn = z2;
int n;
for (n = 2; n <= num_IT; n += 2) {
hn *= h2;
a0[n - 1] = r0 * 2. * (h * hn + 1.) / (n + 2.);
int np1 = n + 1;
s += hn;
a0[np1 - 1] = r1 * 2. * s / (n + 3.);
int i, j, m;
for (i = n; i <= np1; ++i) {
double r = (i + 1.) * -0.5;
b0[0] = r * a0[0];
for (m = 2; m <= i; ++m) {
double bsum = 0.;
for (j = 1; j <= m-1; ++j) {
int mmj = m - j;
bsum += (j * r - mmj) * a0[j - 1] * b0[mmj - 1];
}
b0[m - 1] = r * a0[m - 1] + bsum / m;
}
c[i - 1] = b0[i - 1] / (i + 1.);
double dsum = 0.;