forked from JuliaStats/Rmath-julia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoms708.c
More file actions
2297 lines (1981 loc) · 65.7 KB
/
toms708.c
File metadata and controls
2297 lines (1981 loc) · 65.7 KB
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
// -*- mode: C ; delete-old-versions: never -*-
/* 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.
More specifically, Brown & Levy (1994) "Certification of Algorithm 708" write
"
The number of significant digits of accuracy [..] was calculated [..] as
- log10 (2 RelativeError),
[....]
Accuracy ranged from 9.64 significant digits to 15.65 with a
median of 14.65 and a lower quartile of 13.81.
[...]
... overall accuracy increases slightly as a/b moves away from 1.
Linear regression indicates that
(1) an average of 13.71 significant digits are obtained in cases in which a = b and
(2) the number increases 0.14 significant digits for each unit change in log10(a/b).
"
*/
#undef min
#define min(a,b) ((a < b)?a:b)
#undef max
#define max(a,b) ((a > b)?a:b)
#include "nmath.h" /* includes config.h, math.h */
#include "dpq.h"
/* after config.h to avoid warning on Solaris */
#include <limits.h>
/**----------- DEBUGGING -------------
*
* make CFLAGS='-DDEBUG_bratio ...'
*MM (w/ Debug, w/o Optimization):
(cd `R-devel-pbeta-dbg RHOME`/src/nmath ; gcc -I. -I../../src/include -I../../../R/src/include -DHAVE_CONFIG_H -fopenmp -g -pedantic -Wall -DDEBUG_bratio -DDEBUG_q -Wcast-align -Wconversion -fno-common -Wno-sign-conversion -Wstrict-prototypes -Wclobbered -Werror=implicit-function-declaration -c ../../../R/src/nmath/toms708.c -o toms708.o; cd ../..; make R)
*/
#ifdef DEBUG_bratio
# define R_ifDEBUG_printf(...) REprintf(__VA_ARGS__)
#else
# define R_ifDEBUG_printf(...)
#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 *, Rboolean 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);
/* ALGORITHM 708, COLLECTED ALGORITHMS FROM ACM.
* This work published in Transactions On Mathematical Software,
* vol. 18, no. 3, September 1992, pp. 360-373.
*/
/* 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 (not used currently)
* ierr = 9 NaN in a, b, x, or y
* ierr = 10 (not used currently)
* ierr = 11 bgrat() error code 1 [+ warning in bgrat()]
* ierr = 12 bgrat() error code 2 (no warning here)
* ierr = 13 bgrat() error code 3 (no warning here)
* ierr = 14 bgrat() error code 4 [+ WARNING 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, lambda;
/* eps is a machine dependent constant: the smallest
* floating point number for which 1. + eps > 1.
* NOTE: for almost all purposes it is replaced by 1e-15 (~= 4.5 times larger) below */
double eps = 2. * Rf_d1mach(3); // == DBL_EPSILON (in R, Rmath), but then set to 1e-15 below
/* ----------------------------------------------------------------------- */
*w = R_D__0;
*w1 = R_D__0;
#ifdef IEEE_754
// safeguard, preventing infinite loops further down
if (ISNAN(x) || ISNAN(y) ||
ISNAN(a) || ISNAN(b)) { *ierr = 9; return; }
#endif
if (a < 0. || b < 0.) { *ierr = 1; return; }
if (a == 0. && b == 0.) { *ierr = 2; return; }
if (x < 0. || x > 1.) { *ierr = 3; return; }
if (y < 0. || y > 1.) { *ierr = 4; return; }
/* check that 'y == 1 - x' : */
z = x + y - 0.5 - 0.5;
if (fabs(z) > eps * 3.) { *ierr = 5; return; }
R_ifDEBUG_printf("bratio(a=%g, b=%g, x=%9g, y=%9g, .., log_p=%d): ",
a,b,x,y, log_p);
*ierr = 0;
if (x == 0.) goto L200;
if (y == 0.) goto L210;
if (a == 0.) goto L211;
if (b == 0.) goto L201;
eps = max(eps, 1e-15); // = 1e-15 (for IEEE 754)
Rboolean a_lt_b = (a < b);
if (/* max(a,b) */ (a_lt_b ? b : a) < eps * .001) { /* procedure for a and b < 0.001 * eps = 1e-18 */
// L230: -- result *independent* of x (!)
// *w = a/(a+b) and w1 = b/(a+b) :
if(log_p) {
if(a_lt_b) {
*w = log1p(-a/(a+b)); // notably if a << b
*w1 = log ( a/(a+b));
} else { // b <= a
*w = log ( b/(a+b));
*w1 = log1p(-b/(a+b));
}
} else {
*w = b / (a + b);
*w1 = a / (a + b);
}
R_ifDEBUG_printf("a & b very small -> simple ratios (%g,%g)\n", *w,*w1);
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) */
R_ifDEBUG_printf(" min(a,b) <= 1, do_swap=%d;", do_swap);
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;
R_ifDEBUG_printf(" b0 small -> w := fpser(*) = %.15g\n", *w);
goto L_end;
}
if (a0 < min(eps, eps * b0) && b0 * x0 <= 1.) { /* L90: */
*w1 = apser(a0, b0, x0, eps);
R_ifDEBUG_printf(" a0 small -> w1 := apser(*) = %.15g\n", *w1);
goto L_end_from_w1;
}
Rboolean did_bup = FALSE;
if (max(a0,b0) > 1.) { /* L20: min(a,b) <= 1 < max(a,b) */
R_ifDEBUG_printf("\n L20: min(a,b) <= 1 < max(a,b); ");
if (b0 <= 1.) goto L_w_bpser;
if (x0 >= 0.29) /* was 0.3, PR#13786 */ goto L_w1_bpser;
if (x0 < 0.1 && pow(x0*b0, a0) <= 0.7) goto L_w_bpser;
if (b0 > 15.) {
*w1 = 0.;
goto L131;
}
} else { /* a, b <= 1 */
R_ifDEBUG_printf("\n both a,b <= 1; ");
if (a0 >= min(0.2, b0)) goto L_w_bpser;
if (pow(x0, a0) <= 0.9) goto L_w_bpser;
if (x0 >= 0.3) goto L_w1_bpser;
}
n = 20; /* goto L130; */
*w1 = bup(b0, a0, y0, x0, n, eps, FALSE); did_bup = TRUE;
R_ifDEBUG_printf(" ... n=20 and *w1 := bup(*) = %.15g; ", *w1);
b0 += n;
L131:
R_ifDEBUG_printf(" L131: bgrat(*, w1=%.15g) ", *w1);
bgrat(b0, a0, y0, x0, w1, 15*eps, &ierr1, FALSE);
#ifdef DEBUG_bratio
REprintf(" ==> new w1=%.15g", *w1);
if(ierr1) REprintf(" ERROR(code=%d)\n", ierr1) ; else REprintf("\n");
#endif
if(*w1 == 0 || (0 < *w1 && *w1 < DBL_MIN)) { // w1=0 or very close:
// "almost surely" from underflow, try more: [2013-03-04]
// FIXME: it is even better to do this in bgrat *directly* at least for the case
// !did_bup, i.e., where *w1 = (0 or -Inf) on entry
R_ifDEBUG_printf(" denormalized or underflow (?) -> retrying: ");
if(did_bup) { // re-do that part on log scale:
*w1 = bup(b0-n, a0, y0, x0, n, eps, TRUE);
}
else *w1 = ML_NEGINF; // = 0 on log-scale
bgrat(b0, a0, y0, x0, w1, 15*eps, &ierr1, TRUE);
if(ierr1) *ierr = 10 + ierr1;
#ifdef DEBUG_bratio
REprintf(" ==> new log(w1)=%.15g", *w1);
if(ierr1) REprintf(" Error(code=%d)\n", ierr1) ; else REprintf("\n");
#endif
goto L_end_from_w1_log;
}
// else
if(ierr1) *ierr = 10 + ierr1;
if(*w1 < 0)
MATHLIB_WARNING4("bratio(a=%g, b=%g, x=%g): bgrat() -> w1 = %g",
a,b,x, *w1);
goto L_end_from_w1;
}
else { /* L30: -------------------- both a, b > 1 {a0 > 1 & b0 > 1} ---*/
/* lambda := a y - b x = (a + b)y - b = a - (a+b)x {using x + y == 1},
* ------ using the numerically best version : */
lambda = R_FINITE(a+b)
? ((a > b) ? (a + b) * y - b : a - (a + b) * x)
: a*y - b*x;
do_swap = (lambda < 0.);
if (do_swap) {
lambda = -lambda;
SET_0_swap;
} else {
SET_0_noswap;
}
R_ifDEBUG_printf(" L30: both a, b > 1; |lambda| = %#g, do_swap = %d\n",
lambda, do_swap);
if (b0 < 40.) {
R_ifDEBUG_printf(" b0 < 40;");
if (b0 * x0 <= 0.7
|| (log_p && lambda > 650.)) // << added 2010-03; svn r51327
goto L_w_bpser;
else
goto L140;
}
else if (a0 > b0) { /* ---- a0 > b0 >= 40 ---- */
R_ifDEBUG_printf(" a0 > b0 >= 40;");
if (b0 <= 100. || lambda > b0 * 0.03)
goto L_bfrac;
} else if (a0 <= 100.) {
R_ifDEBUG_printf(" a0 <= 100; a0 <= b0 >= 40;");
goto L_bfrac;
}
else if (lambda > a0 * 0.03) {
R_ifDEBUG_printf(" b0 >= a0 > 100; lambda > a0 * 0.03 ");
goto L_bfrac;
}
/* else if none of the above L180: */
*w = basym(a0, b0, lambda, eps * 100., log_p);
*w1 = log_p ? R_Log1_Exp(*w) : 0.5 - *w + 0.5;
R_ifDEBUG_printf(" b0 >= a0 > 100; lambda <= a0 * 0.03: *w:= basym(*) =%.15g\n",
*w);
goto L_end;
} /* else: a, b > 1 */
/* EVALUATION OF THE APPROPRIATE ALGORITHM */
L_w_bpser: // was L100
*w = bpser(a0, b0, x0, eps, log_p);
*w1 = log_p ? R_Log1_Exp(*w) : 0.5 - *w + 0.5;
R_ifDEBUG_printf(" L_w_bpser: *w := bpser(*) = %.15g\n", *w);
goto L_end;
L_w1_bpser: // was L110
*w1 = bpser(b0, a0, y0, eps, log_p);
*w = log_p ? R_Log1_Exp(*w1) : 0.5 - *w1 + 0.5;
R_ifDEBUG_printf(" L_w1_bpser: *w1 := bpser(*) = %.15g\n", *w1);
goto L_end;
L_bfrac:
*w = bfrac(a0, b0, x0, y0, lambda, eps * 15., log_p);
*w1 = log_p ? R_Log1_Exp(*w) : 0.5 - *w + 0.5;
R_ifDEBUG_printf(" L_bfrac: *w := bfrac(*) = %g\n", *w);
goto L_end;
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, FALSE);
if(*w < DBL_MIN && log_p) { /* do not believe it; try bpser() : */
R_ifDEBUG_printf(" L140: bup(b0=%g,..)=%.15g < DBL_MIN - not used;\n --> ", b0, *w);
/*revert: */ b0 += n;
/* which is only valid if b0 <= 1 || b0*x0 <= 0.7 */
goto L_w_bpser;
} // else :
R_ifDEBUG_printf(" L140: *w := bup(b0=%g,..) = %.15g; ", b0, *w);
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);
R_ifDEBUG_printf(" x0 <= 0.7: *w := *w + bpser(*) = %.15g\n", *w);
goto L_end_from_w;
}
/* L150: */
if (a0 <= 15.) {
n = 20;
*w += bup(a0, b0, x0, y0, n, eps, FALSE);
R_ifDEBUG_printf("\n a0 <= 15: *w := *w + bup(*) = %.15g;", *w);
a0 += n;
}
R_ifDEBUG_printf(" bgrat(*, w=%.15g) ", *w);
bgrat(a0, b0, x0, y0, w, 15*eps, &ierr1, FALSE);
if(ierr1) *ierr = 10 + ierr1;
#ifdef DEBUG_bratio
REprintf("==> new w=%.15g", *w);
if(ierr1) REprintf(" Error(code=%d)\n", ierr1) ; else REprintf("\n");
#endif
goto L_end_from_w;
/* TERMINATION OF THE PROCEDURE */
L200:
if (a == 0.) { *ierr = 6; return; }
// else:
L201: *w = R_D__0; *w1 = R_D__1; return;
L210:
if (b == 0.) { *ierr = 7; return; }
// else:
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;
L_end_from_w1:
if(log_p) {
*w = log1p(-*w1);
*w1 = log(*w1);
} else {
*w = 0.5 - *w1 + 0.5;
}
goto L_end;
L_end_from_w1_log:
// *w1 = log(w1) already; w = 1 - w1 ==> log(w) = log(1 - w1) = log(1 - exp(*w1))
if(log_p) {
*w = R_Log1_Exp(*w1);
} else {
*w = /* 1 - exp(*w1) */ -expm1(*w1);
*w1 = exp(*w1);
}
goto L_end;
L_end:
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.;
}
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 TRUE, also use it if (b < 40 & lambda > 650) where
* lambda := a y - b x = (a + b)y - b = a - (a+b)x {x + y == 1}
* ----------------------------------------------------------------------- */
if (x == 0.) {
return R_D__0;
}
/* ----------------------------------------------------------------------- */
/* compute the factor x^a/(a*Beta(a,b)) */
/* ----------------------------------------------------------------------- */
double ans, c, z, a0 = min(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 {
double t, u, apb, b0 = max(a,b);
if (b0 < 8.) {
if (b0 <= 1.) { /* ------ a0 < 1 and a0 <= b0 <= 1 ------ */
if(log_p) {
ans = a * log(x);
} else {
ans = pow(x, a);
if (ans == 0.) { /* once underflow, always underflow .. */
R_ifDEBUG_printf(" bpser(a=%g, b=%g, x=%g): x^a underflows to 0",
a,b,x);
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);
int m = (int)(b0 - 1.);
if (m >= 1) {
c = 1.;
for (int 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);
}
}
R_ifDEBUG_printf(" bpser(a=%g, b=%g, x=%g, log=%d, eps=%g): %s = %.14g;",
a,b,x, log_p, eps,
log_p ? "log(x^a/(a*B(a,b)))" : "x^a/(a*B(a,b))", ans);
if (ans == R_D__0 || (!log_p && a <= eps * 0.1)) {
R_ifDEBUG_printf(" = final answer\n");
return ans;
}
#ifdef DEBUG_bratio
else REprintf("\n");
#endif
/* ----------------------------------------------------------------------- */
/* 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(fabs(w) > tol) { // the series did not converge (in time)
// warn only when the result seems to matter:
if(( log_p && !(a*sum > -1. && fabs(log1p(a * sum)) < eps*fabs(ans))) ||
(!log_p && fabs(a*sum + 1.) != 1.))
MATHLIB_WARNING5(
" bpser(a=%g, b=%g, x=%g,...) did not converge (n=1e7, |w|/tol=%g > 1; A=%g)",
a,b,x, fabs(w)/tol, ans);
}
R_ifDEBUG_printf(" -> n=%.0f iterations, |w|=%g %s %g=tol:=eps/a ==> a*sum=%g %s -1\n",
n, fabs(w), (fabs(w) > tol) ? ">!!>" : "<=", tol,
a*sum, (a*sum > -1.) ? ">" : "<=");
if(log_p) {
if (a*sum > -1.) ans += log1p(a * sum);
else {
if(ans > ML_NEGINF)
MATHLIB_WARNING3(
"pbeta(*, log.p=TRUE) -> bpser(a=%g, b=%g, x=%g,...) underflow to -Inf",
a,b,x);
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); // = exp(-709) = 1.216780751..e-308 nowadays
}
else {
mu = 0;
d = 1.;
}
/* L10: */
ret_val = give_log
? brcmp1(mu, a, b, x, y, TRUE) - log(a)
: brcmp1(mu, a, b, x, y, FALSE) / 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;
if(!R_FINITE(lambda)) return ML_NAN;// TODO: can return 0 or 1 (?)
R_ifDEBUG_printf(" bfrac(a=%g, b=%g, x=%g, y=%g, lambda=%g, eps=%g, log_p=%d):",
a,b,x,y, lambda, eps, log_p);
brc = brcomp(a, b, x, y, log_p);
if(ISNAN(brc)) { // e.g. from L <- 1e308; pnbinom(L, L, mu = 5)
R_ifDEBUG_printf(" --> brcomp(a,b,x,y) = NaN\n");
ML_WARN_return_NAN; // TODO: could we know better?
}
if (!log_p && brc == 0.) {
R_ifDEBUG_printf(" --> brcomp(a,b,x,y) underflowed to 0.\n");
return 0.;
}
#ifdef DEBUG_bratio
else
REprintf("\n");
#endif
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;
#ifdef _not_normally_DEBUG_bfrac
R_ifDEBUG_printf(" n=%5.0f, a_{n,n+1}= (%12g,%12g), b_{n,n+1} = (%12g,%12g) => r0,r = (%14g,%14g)\n",
n, an,anp1, bn,bnp1, r0, r);
#endif
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
R_ifDEBUG_printf(" in bfrac(): n=%.0f terms cont.frac.; brc=%g, r=%g\n",
n, brc, r);
if(n >= 10000 && fabs(r - r0) > eps * r)
MATHLIB_WARNING5(
" bfrac(a=%g, b=%g, x=%g, y=%g, lambda=%g) did *not* converge (in 10000 steps)\n",
a,b,x,y, lambda);
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 = min(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 = max(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, FALSE) / 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 = min(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 */
/* ----------------------------------------------------------------------- */