/
toms708.c
2313 lines (1955 loc) · 57.1 KB
/
toms708.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
/* Based on C translation of ACM TOMS 708
Please do not change this, e.g. to use R's versions of the
ancillary routines, without investigating the error analysis as we
do need very high relative accuracy. This version has about
14 digits accuracy.
*/
#undef min
#define min(a,b) ((a < b)?a:b)
#undef max
#define max(a,b) ((a > b)?a:b)
#include "nmath.h"
#include "dpq.h"
/* after config.h to avoid warning on Solaris */
#include <limits.h>
/* <math.h> is included by above, with suitable defines in glibc systems
to make log1p and expm1 declared */
/**----------- DEBUGGING -------------
*
* make CFLAGS='-DDEBUG_bratio ...'
*MM: (cd `R-devel RHOME`/src/nmath ; gcc -std=gnu99 -I. -I../../src/include -I../../../R/src/include -I/usr/local/include -DHAVE_CONFIG_H -DDEBUG_bratio -g -c ../../../R/src/nmath/toms708.c -o toms708.o; cd ../..; make R)
*/
#ifdef DEBUG_bratio
# include <R_ext/PrtUtil.h>
#endif
/* MM added R_D_LExp, so redefine here in terms of rexpm1 */
#undef R_Log1_Exp
#define R_Log1_Exp(x) ((x) > -M_LN2 ? log(-rexpm1(x)) : log1p(-exp(x)))
static double bfrac(double, double, double, double, double, double, int log_p);
static void bgrat(double, double, double, double, double *, double, int *);
static void grat1(double, double, double, double *, double *, double);
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);
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);
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);
static double erf__(double);
static double rexpm1(double);
static double erfc1(int, double);
static double gsumln(double, double);
/* ALGORITHM 708, COLLECTED ALGORITHMS FROM ACM.
* This work published in Transactions On Mathematical Software,
* vol. 18, no. 3, September 1992, pp. 360-373z.
*/
/* Changes by R Core Team :
* add log_p and work towards gaining precision in that case
*/
void attribute_hidden
bratio(double a, double b, double x, double y, double *w, double *w1,
int *ierr, int log_p)
{
/* -----------------------------------------------------------------------
* Evaluation of the Incomplete Beta function I_x(a,b)
* --------------------
* It is assumed that a and b are nonnegative, and that x <= 1
* and y = 1 - x. Bratio assigns w and w1 the values
* w = I_x(a,b)
* w1 = 1 - I_x(a,b)
* ierr is a variable that reports the status of the results.
* If no input errors are detected then ierr is set to 0 and
* w and w1 are computed. otherwise, if an error is detected,
* then w and w1 are assigned the value 0 and ierr is set to
* one of the following values ...
* ierr = 1 if a or b is negative
* ierr = 2 if a = b = 0
* ierr = 3 if x < 0 or x > 1
* ierr = 4 if y < 0 or y > 1
* ierr = 5 if x + y != 1
* ierr = 6 if x = a = 0
* ierr = 7 if y = b = 0
* ierr = 8 "error" in bgrat()
* --------------------
* Written by Alfred H. Morris, Jr.
* Naval Surface Warfare Center
* Dahlgren, Virginia
* Revised ... Nov 1991
* ----------------------------------------------------------------------- */
Rboolean do_swap;
int n, ierr1 = 0;
double z, a0, b0, x0, y0, eps, lambda;
/* eps is a machine dependent constant: the smallest
* floating point number for which 1.0 + eps > 1.0 */
eps = 2.0 * Rf_d1mach(3); /* == DBL_EPSILON (in R, Rmath) */
/* ----------------------------------------------------------------------- */
*w = R_D__0;
*w1 = R_D__0;
if (a < 0.0 || b < 0.0) { *ierr = 1; return; }
if (a == 0.0 && b == 0.0) { *ierr = 2; return; }
if (x < 0.0 || x > 1.0) { *ierr = 3; return; }
if (y < 0.0 || y > 1.0) { *ierr = 4; return; }
/* check that 'y == 1 - x' : */
z = x + y - 0.5 - 0.5;
if (fabs(z) > eps * 3.0) { *ierr = 5; return; }
#ifdef DEBUG_bratio
REprintf("bratio(a=%g, b=%g, x=%9g, y=%9g, .., log_p=%d): ", a,b,x,y, log_p);
#endif
*ierr = 0;
if (x == 0.0) goto L200;
if (y == 0.0) goto L210;
if (a == 0.0) goto L211;
if (b == 0.0) goto L201;
eps = max(eps, 1e-15);
if (max(a,b) < eps * .001) { /* procedure for a and b < 0.001 * eps */
/* L230: */
if(log_p) {
z = log(a + b);
*w = log(b) - z;
*w1 = log(a) - z;
} else {
*w = b / (a + b);
*w1 = a / (a + b);
}
return;
}
#define SET_0_noswap \
a0 = a; x0 = x; \
b0 = b; y0 = y;
#define SET_0_swap \
a0 = b; x0 = y; \
b0 = a; y0 = x;
if (min(a,b) <= 1.) { /*------------------------ a <= 1 or b <= 1 ---- */
do_swap = (x > 0.5);
if (do_swap) {
SET_0_swap;
} else {
SET_0_noswap;
}
/* now have x0 <= 1/2 <= y0 (still x0+y0 == 1) */
#ifdef DEBUG_bratio
REprintf(" min(a,b) <= 1 : do_swap = %d; ", do_swap);
#endif
if (b0 < min(eps, eps * a0)) { /* L80: */
*w = fpser(a0, b0, x0, eps, log_p);
*w1 = log_p ? R_Log1_Exp(*w) : 0.5 - *w + 0.5;
#ifdef DEBUG_bratio
REprintf(" b0 small -> w := fpser(*) = %15g\n", *w);
#endif
goto L_end_after_log;
}
if (a0 < min(eps, eps * b0) && b0 * x0 <= 1.0) { /* L90: */
*w1 = apser(a0, b0, x0, eps);
#ifdef DEBUG_bratio
REprintf(" a0 small -> w1 := apser(*) = %15g\n", *w1);
#endif
goto L_end_from_w1;
}
if (max(a0,b0) > 1.0) { /* L20: min(a,b) <= 1 < max(a,b) */
#ifdef DEBUG_bratio
REprintf(" L20: min(a,b) <= 1 < max(a,b); ");
#endif
if (b0 <= 1.0) goto L100;
if (x0 >= 0.29) /* was 0.3, PR#13786 */ goto L110;
if (x0 < 0.1) {
if (pow(x0*b0, a0) <= 0.7) {
goto L100;
}
}
if (b0 > 15.0) {
*w1 = 0.;
goto L131;
}
} else { /* a, b <= 1 */
#ifdef DEBUG_bratio
REprintf(" both a,b <= 1; ");
#endif
if (a0 >= min(0.2, b0)) goto L100;
if (pow(x0, a0) <= 0.9) goto L100;
if (x0 >= 0.3) goto L110;
}
n = 20; /* goto L130; */
*w1 = bup(b0, a0, y0, x0, n, eps);
#ifdef DEBUG_bratio
REprintf(" ... n=20 and *w1 := bup(*) = %15g; ");
#endif
b0 += n;
L131:
bgrat(b0, a0, y0, x0, w1, 15*eps, &ierr1);
#ifdef DEBUG_bratio
REprintf(" L131: bgrat(*, w1) ==> w1 = %15g\n", *w1);
#endif
goto L_end_from_w1;
}
else { /* L30: -------------------- both a, b > 1 {a0 > 1 & b0 > 1} ---*/
if (a > b)
lambda = (a + b) * y - b;
else
lambda = a - (a + b) * x;
do_swap = (lambda < 0.0);
if (do_swap) {
lambda = -lambda;
SET_0_swap;
} else {
SET_0_noswap;
}
#ifdef DEBUG_bratio
REprintf(" L30: both a, b > 1; |lambda| = %#g, do_swap = %d\n",
lambda, do_swap);
#endif
if (b0 < 40.0) {
#ifdef DEBUG_bratio
REprintf(" b0 < 40; ");
#endif
if (b0 * x0 <= 0.7
|| (log_p && lambda > 650.)) /* << added 2010-03-18 */
goto L100;
else
goto L140;
}
else if (a0 > b0) { /* ---- a0 > b0 >= 40 ---- */
#ifdef DEBUG_bratio
REprintf(" a0 > b0 >= 40; ");
#endif
if (b0 <= 100.0 || lambda > b0 * 0.03)
goto L120;
} else if (a0 <= 100.0) {
#ifdef DEBUG_bratio
REprintf(" a0 <= 100; a0 <= b0 >= 40; ");
#endif
goto L120;
}
else if (lambda > a0 * 0.03) {
#ifdef DEBUG_bratio
REprintf(" b0 >= a0 > 100; lambda > a0 * 0.03 ");
#endif
goto L120;
}
/* else if none of the above L180: */
*w = basym(a0, b0, lambda, eps * 100.0, log_p);
*w1 = log_p ? R_Log1_Exp(*w) : 0.5 - *w + 0.5;
#ifdef DEBUG_bratio
REprintf(" b0 >= a0 > 100; lambda <= a0 * 0.03: *w := basym(*) = %15g\n",
*w);
#endif
goto L_end_after_log;
} /* else: a, b > 1 */
/* EVALUATION OF THE APPROPRIATE ALGORITHM */
L100:
*w = bpser(a0, b0, x0, eps, log_p);
*w1 = log_p ? R_Log1_Exp(*w) : 0.5 - *w + 0.5;
#ifdef DEBUG_bratio
REprintf(" L100: *w := bpser(*) = %15g\n", *w);
#endif
goto L_end_after_log;
L110:
*w1 = bpser(b0, a0, y0, eps, log_p);
*w = log_p ? R_Log1_Exp(*w1) : 0.5 - *w1 + 0.5;
#ifdef DEBUG_bratio
REprintf(" L110: *w1 := bpser(*) = %15g\n", *w1);
#endif
goto L_end_after_log;
L120:
*w = bfrac(a0, b0, x0, y0, lambda, eps * 15.0, log_p);
*w1 = log_p ? R_Log1_Exp(*w) : 0.5 - *w + 0.5;
#ifdef DEBUG_bratio
REprintf(" L120: *w := bfrac(*) = %g\n", *w);
#endif
goto L_end_after_log;
L140:
/* b0 := fractional_part( b0 ) in (0, 1] */
n = (int) b0;
b0 -= n;
if (b0 == 0.) {
--n; b0 = 1.;
}
*w = bup(b0, a0, y0, x0, n, eps);
#ifdef DEBUG_bratio
REprintf(" L140: *w := bup(b0=%g, *) = %15g; ", b0, *w);
#endif
if(*w < DBL_MIN && log_p) { /* do not believe it; try bpser() : */
/*revert: */ b0 += n;
/* which is only valid if b0 <= 1 || b0*x0 <= 0.7 */
goto L100;
}
if (x0 <= 0.7) {
/* log_p : TODO: w = bup(.) + bpser(.) -- not so easy to use log-scale */
*w += bpser(a0, b0, x0, eps, /* log_p = */ FALSE);
#ifdef DEBUG_bratio
REprintf(" x0 <= 0.7: *w := *w + bpser(*) = %15g\n", *w);
#endif
goto L_end_from_w;
}
/* L150: */
if (a0 <= 15.0) {
n = 20;
*w += bup(a0, b0, x0, y0, n, eps);
#ifdef DEBUG_bratio
REprintf("\n a0 <= 15: *w := *w + bup(*) = %15g;", *w);
#endif
a0 += n;
}
bgrat(a0, b0, x0, y0, w, 15*eps, &ierr1);
#ifdef DEBUG_bratio
REprintf(" bgrat(*) ==> *w = %g\n", *w);
#endif
goto L_end_from_w;
/* TERMINATION OF THE PROCEDURE */
L200:
if (a == 0.0) { *ierr = 6; return; }
L201:
*w = R_D__0;
*w1 = R_D__1;
return;
L210:
if (b == 0.0) { *ierr = 7; return; }
L211:
*w = R_D__1;
*w1 = R_D__0;
return;
L_end_from_w:
if(log_p) {
*w1 = log1p(-*w);
*w = log(*w);
} else {
*w1 = 0.5 - *w + 0.5;
}
goto L_end_after_log;
L_end_from_w1:
if(log_p) {
*w = log1p(-*w1);
*w1 = log(*w1);
} else {
*w = 0.5 - *w1 + 0.5;
}
L_end_after_log:
if (do_swap) { /* swap */
double t = *w; *w = *w1; *w1 = t;
}
return;
} /* bratio */
#undef SET_0_noswap
#undef SET_0_swap
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.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.0;
t = x;
s = t / an;
do {
an += 1.0;
t = x * t;
c = t / an;
s += c;
} while (fabs(c) > tol);
if (log_p)
ans += log1p(a * s);
else
ans *= a * s + 1.0;
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
c = log(bx) + g + t;
tol = eps * 5.0 * fabs(c);
j = 1.;
s = 0.;
do {
j += 1.0;
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.
* ----------------------------------------------------------------------- */
int i, m;
double ans, c, n, t, u, w, z, a0, b0, apb, tol, sum;
if (x == 0.) {
return R_D__0;
}
/* ----------------------------------------------------------------------- */
/* compute the factor x^a/(a*Beta(a,b)) */
/* ----------------------------------------------------------------------- */
a0 = min(a,b);
if (a0 >= 1.0) { /* ------ 1 <= a0 <= b0 ------ */
z = a * log(x) - betaln(a, b);
ans = log_p ? z - log(a) : exp(z) / a;
}
else {
b0 = max(a,b);
if (b0 < 8.0) {
if (b0 <= 1.0) { /* ------ 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.0) {
u = a + b - 1.;
z = (gam1(u) + 1.0) / apb;
} else {
z = gam1(apb) + 1.0;
}
c = (gam1(a) + 1.0) * (gam1(b) + 1.0) / 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 = b0 - 1.0;
if (m >= 1) {
c = 1.0;
for (i = 1; i <= m; ++i) {
b0 += -1.0;
c *= b0 / (a0 + b0);
}
u += log(c);
}
z = a * log(x) - u;
b0 += -1.0;
apb = a0 + b0;
if (apb > 1.0) {
u = a0 + b0 - 1.;
t = (gam1(u) + 1.0) / apb;
} else {
t = gam1(apb) + 1.0;
}
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.0) / 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 (!log_p && (ans == 0.0 || a <= eps * 0.1)) {
return ans;
}
/* ----------------------------------------------------------------------- */
/* COMPUTE THE SERIES */
/* ----------------------------------------------------------------------- */
sum = 0.;
n = 0.;
c = 1.;
tol = eps / a;
do {
n += 1.;
c *= (0.5 - b / n + 0.5) * x;
w = c / (a + n);
sum += w;
} while (fabs(w) > tol);
if(log_p) {
if (a*sum > -1.0) ans += log1p(a * sum);
else ans = ML_NEGINF;
} else
ans *= a * sum + 1.0;
return ans;
} /* bpser */
static double bup(double a, double b, double x, double y, int n, double eps)
{
/* ----------------------------------------------------------------------- */
/* EVALUATION OF I_x(A,B) - I_x(A+N,B) WHERE N IS A POSITIVE INT. */
/* EPS IS THE TOLERANCE USED. */
/* ----------------------------------------------------------------------- */
/* System generated locals */
double ret_val;
/* Local variables */
int i, k, mu, nm1;
double d, l, r, t, w;
double ap1, apb;
/* OBTAIN THE SCALING FACTOR EXP(-MU) AND */
/* EXP(MU)*(X^A * Y^B / BETA(A,B))/A */
apb = a + b;
ap1 = a + 1.0;
if (n > 1 && a >= 1. && apb >= ap1 * 1.1) {
mu = fabs(exparg(1));
k = (int) exparg(0);
if (k < mu) {
mu = k;
}
t = (double) mu;
d = exp(-t);
}
else {
mu = 0;
d = 1.0;
}
/* L10: */
ret_val = brcmp1(mu, a, b, x, y) / a;
if (n == 1 || ret_val == 0.0) {
return ret_val;
}
nm1 = n - 1;
w = d;
/* LET K BE THE INDEX OF THE MAXIMUM TERM */
k = 0;
if (b <= 1.0) {
goto L40;
}
if (y > 1e-4) {
r = (b - 1.0) * x / y - a;
if (r < 1.0) {
goto L40;
}
k = nm1;
t = (double) nm1;
if (r < t) {
k = (int) r;
}
} else {
k = nm1;
}
/* ADD THE INCREASING TERMS OF THE SERIES */
/* L30: */
for (i = 1; i <= k; ++i) {
l = (double) (i - 1);
d = (apb + l) / (ap1 + l) * x * d;
w += d;
/* L31: */
}
if (k == nm1) {
goto L50;
}
/* ADD THE REMAINING TERMS OF THE SERIES */
L40:
for (i = k+1; i <= nm1; ++i) {
l = (double) (i - 1);
d = (apb + l) / (ap1 + l) * x * d;
w += d;
if (d <= eps * w) /* relativ convergence (eps) */
break;
}
/* TERMINATE THE PROCEDURE */
L50:
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;
double brc = brcomp(a, b, x, y, log_p);
if (!log_p && brc == 0.) /* already underflowed to 0 */
return 0.;
c = lambda + 1.0;
c0 = b / a;
c1 = 1.0 / a + 1.0;
yp1 = y + 1.0;
n = 0.0;
p = 1.0;
s = a + 1.0;
an = 0.0;
bn = 1.0;
anp1 = 1.0;
bnp1 = c / c1;
r = c1 / c;
/* CONTINUED FRACTION CALCULATION */
do {
n += 1.0;
t = n / a;
w = n * (b - n) * x;
e = a / s;
alpha = p * (p + c0) * e * e * (w * x);
e = (t + 1.0) / (c1 + t + t);
beta = n + w / s + e * (c + n * yp1);
p = t + 1.0;
s += 2.0;
/* 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.0;
} while (1);
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, h, t, u, v, z, a0, b0, x0, y0, apb, lnx, lny;
double lambda;
if (x == 0.0 || y == 0.0) {
return R_D__0;
}
a0 = min(a, b);
if (a0 >= 8.0) {
goto L100;
}
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 = max(a, b);
if (b0 >= 8.0) { /* L80: */
u = gamln1(a0) + algdiv(a0, b0);
return (log_p ? log(a0) + (z - u) : a0 * exp(z - u));
}
/* else : */
if (b0 <= 1.0) { /* algorithm for max(a,b) = b0 <= 1 */
double e_z = R_D_exp(z);
if (!log_p && e_z == 0.0) /* exp() underflow */
return 0.;
apb = a + b;
if (apb > 1.0) {
u = a + b - 1.;
z = (gam1(u) + 1.0) / apb;
} else {
z = gam1(apb) + 1.0;
}
c = (gam1(a) + 1.0) * (gam1(b) + 1.0) / 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.0));
}
/* else : */
/* ALGORITHM FOR 1 < b0 < 8 */
u = gamln1(a0);
n = b0 - 1.0;
if (n >= 1) {
c = 1.0;
for (i = 1; i <= n; ++i) {
b0 += -1.0;
c *= b0 / (a0 + b0);
}
u = log(c) + u;
}
z -= u;
b0 += -1.0;
apb = a0 + b0;
if (apb > 1.0) {
u = a0 + b0 - 1.;
t = (gam1(u) + 1.0) / apb;
} else {
t = gam1(apb) + 1.0;
}
return (log_p
? log(a0) + z + log1p(gam1(b0)) - log(t)
: a0 * exp(z) * (gam1(b0) + 1.0) / t);
/* ----------------------------------------------------------------------- */
/* PROCEDURE FOR A >= 8 AND B >= 8 */
/* ----------------------------------------------------------------------- */
L100:
if (a <= b) {
h = a / b;
x0 = h / (h + 1.0);
y0 = 1.0 / (h + 1.0);
lambda = a - (a + b) * x;
} else {
h = b / a;
x0 = 1.0 / (h + 1.0);
y0 = h / (h + 1.0);
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 */
static double brcmp1(int mu, double a, double b, double x, double y)
{
/* -----------------------------------------------------------------------
* 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 */
/* System generated locals */
double ret_val, r1;
/* Local variables */
double c, e, h;
int i, n;
double t, u, v, z, a0, b0, x0, y0, apb, lnx, lny;
double lambda;
a0 = min(a,b);
if (a0 >= 8.0) {
goto L100;
}
if (x > .375) {
goto L10;
}
lnx = log(x);
lny = alnrel(-x);
goto L20;
L10:
if (y > .375) {
goto L11;
}
lnx = alnrel(-y);
lny = log(y);
goto L20;
L11:
lnx = log(x);
lny = log(y);
L20:
z = a * lnx + b * lny;
if (a0 < 1.0) {
goto L30;
}
z -= betaln(a, b);
ret_val = esum(mu, z);
return ret_val;
/* ----------------------------------------------------------------------- */
/* PROCEDURE FOR A < 1 OR B < 1 */
/* ----------------------------------------------------------------------- */
L30:
b0 = max(a,b);
if (b0 >= 8.0) {
goto L80;
}
if (b0 > 1.0) {
goto L60;
}
/* ALGORITHM FOR b0 <= 1 */
ret_val = esum(mu, z);
if (ret_val == 0.0) {
return ret_val;
}
apb = a + b;
if (apb > 1.0) {
goto L40;
}
z = gam1(apb) + 1.0;
goto L50;
L40:
u = a + b - 1.;
z = (gam1(u) + 1.0) / apb;
L50:
c = (gam1(a) + 1.0) * (gam1(b) + 1.0) / z;
ret_val = ret_val * (a0 * c) / (a0 / b0 + 1.0);
return ret_val;
/* ALGORITHM FOR 1 < b0 < 8 */
L60:
u = gamln1(a0);
n = b0 - 1.0;
if (n < 1) {
goto L70;
}
c = 1.0;
for (i = 1; i <= n; ++i) {
b0 += -1.0;