The R Project SVN R

Rev

Rev 62109 | Rev 65522 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 62109 Rev 62123
Line 20... Line 20...
20
   to make log1p and expm1 declared */
20
   to make log1p and expm1 declared */
21
 
21
 
22
/**----------- DEBUGGING -------------
22
/**----------- DEBUGGING -------------
23
 *
23
 *
24
 *	make CFLAGS='-DDEBUG_bratio  ...'
24
 *	make CFLAGS='-DDEBUG_bratio  ...'
-
 
25
 *MM:
25
 *MM: (cd `R-devel RHOME`/src/nmath ; gcc -I. -I../../src/include -I../../../R/src/include  -DHAVE_CONFIG_H -fopenmp -g -pedantic -Wall --std=gnu99 -DDEBUG_q -DDEBUG_bratio -Wcast-align -Wclobbered  -c ../../../R/src/nmath/toms708.c -o toms708.o; cd ../..; make R)
26
 (cd `R-devel-pbeta-dbg RHOME`/src/nmath ; gcc -I. -I../../src/include -I../../../R/src/include  -DHAVE_CONFIG_H -fopenmp -g -pedantic -Wall --std=gnu99 -DDEBUG_q -DDEBUG_bratio -Wcast-align -Wclobbered  -c ../../../R/src/nmath/toms708.c -o toms708.o; cd ../..; make R)
26
*/
27
*/
27
#ifdef DEBUG_bratio
28
#ifdef DEBUG_bratio
28
/* for REprintf */
29
/* for REprintf */
29
# include <R_ext/Print.h>
30
# include <R_ext/Print.h>
30
#endif
31
#endif
Line 33... Line 34...
33
#undef R_Log1_Exp
34
#undef R_Log1_Exp
34
#define R_Log1_Exp(x)   ((x) > -M_LN2 ? log(-rexpm1(x)) : log1p(-exp(x)))
35
#define R_Log1_Exp(x)   ((x) > -M_LN2 ? log(-rexpm1(x)) : log1p(-exp(x)))
35
 
36
 
36
 
37
 
37
static double bfrac(double, double, double, double, double, double, int log_p);
38
static double bfrac(double, double, double, double, double, double, int log_p);
38
static void bgrat(double, double, double, double, double *, double, int *);
39
static void bgrat(double, double, double, double, double *, double, int *, Rboolean log_w);
39
static void grat1(double, double, double, double *, double *, double);
40
static double grat_r(double a, double x, double r, double eps);
40
static double apser(double, double, double, double);
41
static double apser(double, double, double, double);
41
static double bpser(double, double, double, double, int log_p);
42
static double bpser(double, double, double, double, int log_p);
42
static double basym(double, double, double, double, int log_p);
43
static double basym(double, double, double, double, int log_p);
43
static double fpser(double, double, double, double, int log_p);
44
static double fpser(double, double, double, double, int log_p);
44
static double bup(double, double, double, double, int, double);
45
static double bup(double, double, double, double, int, double, int give_log);
45
static double exparg(int);
46
static double exparg(int);
46
static double psi(double);
47
static double psi(double);
47
static double gam1(double);
48
static double gam1(double);
48
static double gamln1(double);
49
static double gamln1(double);
49
static double betaln(double, double);
50
static double betaln(double, double);
Line 178... Line 179...
178
 
179
 
179
	if (b0 < min(eps, eps * a0)) { /* L80: */
180
	if (b0 < min(eps, eps * a0)) { /* L80: */
180
	    *w = fpser(a0, b0, x0, eps, log_p);
181
	    *w = fpser(a0, b0, x0, eps, log_p);
181
	    *w1 = log_p ? R_Log1_Exp(*w) : 0.5 - *w + 0.5;
182
	    *w1 = log_p ? R_Log1_Exp(*w) : 0.5 - *w + 0.5;
182
#ifdef DEBUG_bratio
183
#ifdef DEBUG_bratio
183
	    REprintf("  b0 small -> w := fpser(*) = %15g\n", *w);
184
	    REprintf("  b0 small -> w := fpser(*) = %.15g\n", *w);
184
#endif
185
#endif
185
	    goto L_end_after_log;
186
	    goto L_end;
186
	}
187
	}
187
 
188
 
188
	if (a0 < min(eps, eps * b0) && b0 * x0 <= 1.0) { /* L90: */
189
	if (a0 < min(eps, eps * b0) && b0 * x0 <= 1.0) { /* L90: */
189
	    *w1 = apser(a0, b0, x0, eps);
190
	    *w1 = apser(a0, b0, x0, eps);
190
#ifdef DEBUG_bratio
191
#ifdef DEBUG_bratio
191
	    REprintf("  a0 small -> w1 := apser(*) = %15g\n", *w1);
192
	    REprintf("  a0 small -> w1 := apser(*) = %.15g\n", *w1);
192
#endif
193
#endif
193
	    goto L_end_from_w1;
194
	    goto L_end_from_w1;
194
	}
195
	}
195
 
196
 
-
 
197
	Rboolean did_bup = FALSE;
196
	if (max(a0,b0) > 1.0) { /* L20:  min(a,b) <= 1 < max(a,b)  */
198
	if (max(a0,b0) > 1.0) { /* L20:  min(a,b) <= 1 < max(a,b)  */
197
#ifdef DEBUG_bratio
199
#ifdef DEBUG_bratio
198
	    REprintf(" L20:  min(a,b) <= 1 < max(a,b); ");
200
	    REprintf("\n L20:  min(a,b) <= 1 < max(a,b); ");
199
#endif
201
#endif
200
	    if (b0 <= 1.0) goto L_w_bpser;
202
	    if (b0 <= 1.0) goto L_w_bpser;
201
 
203
 
202
	    if (x0 >= 0.29) /* was 0.3, PR#13786 */	goto L_w1_bpser;
204
	    if (x0 >= 0.29) /* was 0.3, PR#13786 */	goto L_w1_bpser;
203
 
205
 
Line 207... Line 209...
207
		*w1 = 0.;
209
		*w1 = 0.;
208
		goto L131;
210
		goto L131;
209
	    }
211
	    }
210
	} else { /*  a, b <= 1 */
212
	} else { /*  a, b <= 1 */
211
#ifdef DEBUG_bratio
213
#ifdef DEBUG_bratio
212
	    REprintf("  both a,b <= 1; ");
214
	    REprintf("\n      both a,b <= 1; ");
213
#endif
215
#endif
214
	    if (a0 >= min(0.2, b0))	goto L_w_bpser;
216
	    if (a0 >= min(0.2, b0))	goto L_w_bpser;
215
 
217
 
216
	    if (pow(x0, a0) <= 0.9) 	goto L_w_bpser;
218
	    if (pow(x0, a0) <= 0.9) 	goto L_w_bpser;
217
 
219
 
218
	    if (x0 >= 0.3)		goto L_w1_bpser;
220
	    if (x0 >= 0.3)		goto L_w1_bpser;
219
	}
221
	}
220
	n = 20; /* goto L130; */
222
	n = 20; /* goto L130; */
221
	*w1 = bup(b0, a0, y0, x0, n, eps);
223
	*w1 = bup(b0, a0, y0, x0, n, eps, FALSE); did_bup = TRUE;
222
#ifdef DEBUG_bratio
224
#ifdef DEBUG_bratio
223
	REprintf("  ... n=20 and *w1 := bup(*) = %19.15g; ", *w1);
225
	REprintf("  ... n=20 and *w1 := bup(*) = %.15g; ", *w1);
224
#endif
226
#endif
225
	b0 += n;
227
	b0 += n;
226
    L131:
228
    L131:
-
 
229
#ifdef DEBUG_bratio
-
 
230
	REprintf(" L131: bgrat(*, w1=%.15g) ", *w1);
-
 
231
#endif
227
	bgrat(b0, a0, y0, x0, w1, 15*eps, &ierr1);//TODO: <-- log_p=TRUE  option !!
232
	bgrat(b0, a0, y0, x0, w1, 15*eps, &ierr1, FALSE);
228
	if(ierr1) *ierr = 8;
233
	if(ierr1) *ierr = 8;
229
#ifdef DEBUG_bratio
234
#ifdef DEBUG_bratio
230
	REprintf(" L131: bgrat(*, w1) ==> %s w1=%19.15g\n",
235
	REprintf(" ==> new w1=%.15g", *w1);
231
		 (ierr1)? "ERROR" : "", *w1);
236
	if(ierr1) REprintf(" ERROR(code=%d)\n", ierr1) ; else REprintf("\n");
232
#endif
237
#endif
-
 
238
	if(*w1 == 0.) { // "almost surely" from underflow, try more: [2013-03-04]
-
 
239
// FIXME: it is even better to do this in bgrat *directly* at least for the case
-
 
240
//  !did_bup, i.e., where *w1 = (0 or -Inf) on entry
-
 
241
#ifdef DEBUG_bratio
-
 
242
	    REprintf(" underflow? -> retrying: ");
-
 
243
#endif
-
 
244
	    if(did_bup) { // re-do that part on log scale:
-
 
245
		*w1 = bup(b0-n, a0, y0, x0, n, eps, TRUE);
-
 
246
	    }
-
 
247
	    else *w1 = ML_NEGINF; // = 0 on log-scale
-
 
248
	    bgrat(b0, a0, y0, x0, w1, 15*eps, &ierr1, TRUE);
-
 
249
	    if(ierr1) *ierr = 8;
-
 
250
#ifdef DEBUG_bratio
-
 
251
	    REprintf(" ==> new log(w1)=%.15g", *w1);
-
 
252
	    if(ierr1) REprintf(" Error(code=%d)\n", ierr1) ; else REprintf("\n");
-
 
253
#endif
-
 
254
	    goto L_end_from_w1_log;
-
 
255
	}
-
 
256
	// else
233
	if(*w1 < 0)
257
	if(*w1 < 0)
234
	    MATHLIB_WARNING4("bratio(a=%g, b=%g, x=%g): bgrat() -> w1 = %g",
258
	    MATHLIB_WARNING4("bratio(a=%g, b=%g, x=%g): bgrat() -> w1 = %g",
235
			     a,b,x, *w1);
259
			     a,b,x, *w1);
236
	goto L_end_from_w1;
260
	goto L_end_from_w1;
237
    }
261
    }
Line 287... Line 311...
287
 
311
 
288
	/* else if none of the above    L180: */
312
	/* else if none of the above    L180: */
289
	*w = basym(a0, b0, lambda, eps * 100.0, log_p);
313
	*w = basym(a0, b0, lambda, eps * 100.0, log_p);
290
	*w1 = log_p ? R_Log1_Exp(*w) : 0.5 - *w + 0.5;
314
	*w1 = log_p ? R_Log1_Exp(*w) : 0.5 - *w + 0.5;
291
#ifdef DEBUG_bratio
315
#ifdef DEBUG_bratio
292
	REprintf("  b0 >= a0 > 100; lambda <= a0 * 0.03: *w:= basym(*) =%15g\n",
316
	REprintf("  b0 >= a0 > 100; lambda <= a0 * 0.03: *w:= basym(*) =%.15g\n",
293
		 *w);
317
		 *w);
294
#endif
318
#endif
295
	goto L_end_after_log;
319
	goto L_end;
296
 
320
 
297
    } /* else: a, b > 1 */
321
    } /* else: a, b > 1 */
298
 
322
 
299
/*            EVALUATION OF THE APPROPRIATE ALGORITHM */
323
/*            EVALUATION OF THE APPROPRIATE ALGORITHM */
300
 
324
 
301
L_w_bpser: // was L100
325
L_w_bpser: // was L100
302
    *w = bpser(a0, b0, x0, eps, log_p);
326
    *w = bpser(a0, b0, x0, eps, log_p);
303
    *w1 = log_p ? R_Log1_Exp(*w) : 0.5 - *w + 0.5;
327
    *w1 = log_p ? R_Log1_Exp(*w) : 0.5 - *w + 0.5;
304
#ifdef DEBUG_bratio
328
#ifdef DEBUG_bratio
305
    REprintf(" L_w_bpser: *w := bpser(*) = %15g\n", *w);
329
    REprintf(" L_w_bpser: *w := bpser(*) = %.15g\n", *w);
306
#endif
330
#endif
307
    goto L_end_after_log;
331
    goto L_end;
308
 
332
 
309
L_w1_bpser:  // was L110
333
L_w1_bpser:  // was L110
310
    *w1 = bpser(b0, a0, y0, eps, log_p);
334
    *w1 = bpser(b0, a0, y0, eps, log_p);
311
    *w  = log_p ? R_Log1_Exp(*w1) : 0.5 - *w1 + 0.5;
335
    *w  = log_p ? R_Log1_Exp(*w1) : 0.5 - *w1 + 0.5;
312
#ifdef DEBUG_bratio
336
#ifdef DEBUG_bratio
313
    REprintf(" L_w1_bpser: *w1 := bpser(*) = %15g\n", *w1);
337
    REprintf(" L_w1_bpser: *w1 := bpser(*) = %.15g\n", *w1);
314
#endif
338
#endif
315
    goto L_end_after_log;
339
    goto L_end;
316
 
340
 
317
L_bfrac:
341
L_bfrac:
318
    *w = bfrac(a0, b0, x0, y0, lambda, eps * 15.0, log_p);
342
    *w = bfrac(a0, b0, x0, y0, lambda, eps * 15.0, log_p);
319
    *w1 = log_p ? R_Log1_Exp(*w) : 0.5 - *w + 0.5;
343
    *w1 = log_p ? R_Log1_Exp(*w) : 0.5 - *w + 0.5;
320
#ifdef DEBUG_bratio
344
#ifdef DEBUG_bratio
321
    REprintf(" L_bfrac: *w := bfrac(*) = %g\n", *w);
345
    REprintf(" L_bfrac: *w := bfrac(*) = %g\n", *w);
322
#endif
346
#endif
323
    goto L_end_after_log;
347
    goto L_end;
324
 
348
 
325
L140:
349
L140:
326
    /* b0 := fractional_part( b0 )  in (0, 1]  */
350
    /* b0 := fractional_part( b0 )  in (0, 1]  */
327
    n = (int) b0;
351
    n = (int) b0;
328
    b0 -= n;
352
    b0 -= n;
329
    if (b0 == 0.) {
353
    if (b0 == 0.) {
330
	--n; b0 = 1.;
354
	--n; b0 = 1.;
331
    }
355
    }
332
 
356
 
333
    *w = bup(b0, a0, y0, x0, n, eps);
357
    *w = bup(b0, a0, y0, x0, n, eps, FALSE);
334
 
358
 
335
#ifdef DEBUG_bratio
359
#ifdef DEBUG_bratio
336
    REprintf(" L140: *w := bup(b0=%g,..) = %19.15g; ", b0, *w);
360
    REprintf(" L140: *w := bup(b0=%g,..) = %.15g; ", b0, *w);
337
#endif
361
#endif
338
    if(*w < DBL_MIN && log_p) { /* do not believe it; try bpser() : */
362
    if(*w < DBL_MIN && log_p) { /* do not believe it; try bpser() : */
339
	/*revert: */ b0 += n;
363
	/*revert: */ b0 += n;
340
	/* which is only valid if b0 <= 1 || b0*x0 <= 0.7 */
364
	/* which is only valid if b0 <= 1 || b0*x0 <= 0.7 */
341
	goto L_w_bpser;
365
	goto L_w_bpser;
342
    }
366
    }
343
    if (x0 <= 0.7) {
367
    if (x0 <= 0.7) {
344
	/* log_p :  TODO:  w = bup(.) + bpser(.)  -- not so easy to use log-scale */
368
	/* log_p :  TODO:  w = bup(.) + bpser(.)  -- not so easy to use log-scale */
345
	*w += bpser(a0, b0, x0, eps, /* log_p = */ FALSE);
369
	*w += bpser(a0, b0, x0, eps, /* log_p = */ FALSE);
346
#ifdef DEBUG_bratio
370
#ifdef DEBUG_bratio
347
	REprintf(" x0 <= 0.7: *w := *w + bpser(*) = %19.15g\n", *w);
371
	REprintf(" x0 <= 0.7: *w := *w + bpser(*) = %.15g\n", *w);
348
#endif
372
#endif
349
	goto L_end_from_w;
373
	goto L_end_from_w;
350
    }
374
    }
351
    /* L150: */
375
    /* L150: */
352
    if (a0 <= 15.0) {
376
    if (a0 <= 15.0) {
353
	n = 20;
377
	n = 20;
354
	*w += bup(a0, b0, x0, y0, n, eps);
378
	*w += bup(a0, b0, x0, y0, n, eps, FALSE);
355
#ifdef DEBUG_bratio
379
#ifdef DEBUG_bratio
356
	REprintf("\n a0 <= 15: *w := *w + bup(*) = %19.15g;", *w);
380
	REprintf("\n a0 <= 15: *w := *w + bup(*) = %.15g;", *w);
357
#endif
381
#endif
358
	a0 += n;
382
	a0 += n;
359
    }
383
    }
-
 
384
#ifdef DEBUG_bratio
-
 
385
    REprintf(" bgrat(*, w=%.15g) ", *w);
-
 
386
#endif
360
    bgrat(a0, b0, x0, y0, w, 15*eps, &ierr1);
387
    bgrat(a0, b0, x0, y0, w, 15*eps, &ierr1, FALSE);
361
    if(ierr1) *ierr = 8;
388
    if(ierr1) *ierr = 8;
362
#ifdef DEBUG_bratio
389
#ifdef DEBUG_bratio
363
    REprintf(" bgrat(*) ==> %s w=%g\n",
390
    REprintf("==> new w=%.15g", *w);
364
		 (ierr1)? "error" : "", *w);
391
    if(ierr1) REprintf(" Error(code=%d)\n", ierr1) ; else REprintf("\n");
365
#endif
392
#endif
366
    goto L_end_from_w;
393
    goto L_end_from_w;
367
 
394
 
368
 
395
 
369
/* TERMINATION OF THE PROCEDURE */
396
/* TERMINATION OF THE PROCEDURE */
Line 389... Line 416...
389
	*w1 = log1p(-*w);
416
	*w1 = log1p(-*w);
390
	*w  = log(*w);
417
	*w  = log(*w);
391
    } else {
418
    } else {
392
	*w1 = 0.5 - *w + 0.5;
419
	*w1 = 0.5 - *w + 0.5;
393
    }
420
    }
394
    goto L_end_after_log;
421
    goto L_end;
395
 
422
 
396
L_end_from_w1:
423
L_end_from_w1:
397
    if(log_p) {
424
    if(log_p) {
398
	*w  = log1p(-*w1);
425
	*w  = log1p(-*w1);
399
	*w1 = log(*w1);
426
	*w1 = log(*w1);
400
    } else {
427
    } else {
401
	*w = 0.5 - *w1 + 0.5;
428
	*w = 0.5 - *w1 + 0.5;
402
    }
429
    }
-
 
430
    goto L_end;
-
 
431
 
-
 
432
L_end_from_w1_log:
-
 
433
    // *w1 = log(w1) already; w = 1 - w1  ==> log(w) = log(1 - w1) = log(1 - exp(*w1))
-
 
434
    if(log_p) {
-
 
435
	*w = R_Log1_Exp(*w1);
-
 
436
    } else {
-
 
437
	*w  = /* 1 - exp(*w1) */ -expm1(*w1);
-
 
438
	*w1 = exp(*w1);
-
 
439
    }
-
 
440
    goto L_end;
403
 
441
 
-
 
442
 
404
L_end_after_log:
443
L_end:
405
    if (do_swap) { /* swap */
444
    if (do_swap) { /* swap */
406
	double t = *w; *w = *w1; *w1 = t;
445
	double t = *w; *w = *w1; *w1 = t;
407
    }
446
    }
408
    return;
447
    return;
409
 
448
 
Line 609... Line 648...
609
    } else
648
    } else
610
	ans *= a * sum + 1.0;
649
	ans *= a * sum + 1.0;
611
    return ans;
650
    return ans;
612
} /* bpser */
651
} /* bpser */
613
 
652
 
614
static double bup(double a, double b, double x, double y, int n, double eps)
653
static double bup(double a, double b, double x, double y, int n, double eps,
-
 
654
		  int give_log)
615
{
655
{
616
/* ----------------------------------------------------------------------- */
656
/* ----------------------------------------------------------------------- */
617
/*     EVALUATION OF I_x(A,B) - I_x(A+N,B) WHERE N IS A POSITIVE INT. */
657
/*     EVALUATION OF I_x(A,B) - I_x(A+N,B) WHERE N IS A POSITIVE INT. */
618
/*     EPS IS THE TOLERANCE USED. */
658
/*     EPS IS THE TOLERANCE USED. */
619
/* ----------------------------------------------------------------------- */
659
/* ----------------------------------------------------------------------- */
Line 641... Line 681...
641
	mu = 0;
681
	mu = 0;
642
	d = 1.0;
682
	d = 1.0;
643
    }
683
    }
644
 
684
 
645
    /* L10: */
685
    /* L10: */
-
 
686
    ret_val = log_p
-
 
687
	? brcmp1(mu, a, b, x, y, TRUE) - log(a)
646
    ret_val = brcmp1(mu, a, b, x, y, FALSE) / a;
688
	: brcmp1(mu, a, b, x, y, FALSE)  / a;
647
    if (n == 1 || ret_val == 0.0)
689
    if (n == 1 || (log_p && ret_val == ML_NEGINF) || (!log_p && ret_val == 0.))
648
	return ret_val;
690
	return ret_val;
649
 
691
 
650
    int nm1 = n - 1;
692
    int nm1 = n - 1;
651
    double w = d;
693
    double w = d;
652
 
694
 
Line 690... Line 732...
690
		break;
732
		break;
691
	}
733
	}
692
    }
734
    }
693
 
735
 
694
    // L50: TERMINATE THE PROCEDURE
736
    // L50: TERMINATE THE PROCEDURE
-
 
737
    if(give_log) {
-
 
738
	ret_val += log(w);
-
 
739
    } else
695
    ret_val *= w;
740
	ret_val *= w;
-
 
741
 
696
    return ret_val;
742
    return ret_val;
697
} /* bup */
743
} /* bup */
698
 
744
 
699
static double bfrac(double a, double b, double x, double y, double lambda,
745
static double bfrac(double a, double b, double x, double y, double lambda,
700
		    double eps, int log_p)
746
		    double eps, int log_p)
Line 898... Line 944...
898
	       ? -M_LN_SQRT_2PI + .5*log(b * x0) + z - bcorr(a,b)
944
	       ? -M_LN_SQRT_2PI + .5*log(b * x0) + z - bcorr(a,b)
899
	       : const__ * sqrt(b * x0) * z * exp(-bcorr(a, b)));
945
	       : const__ * sqrt(b * x0) * z * exp(-bcorr(a, b)));
900
    }
946
    }
901
} /* brcomp */
947
} /* brcomp */
902
 
948
 
-
 
949
// called only once from  bup(),  as   r = brcmp1(mu, a, b, x, y, FALSE) / a;
-
 
950
//                        -----
903
static double brcmp1(int mu, double a, double b, double x, double y, int give_log)
951
static double brcmp1(int mu, double a, double b, double x, double y, int give_log)
904
{
952
{
905
/* -----------------------------------------------------------------------
953
/* -----------------------------------------------------------------------
906
 *          Evaluation of    exp(mu) * x^a * y^b / beta(a,b)
954
 *          Evaluation of    exp(mu) * x^a * y^b / beta(a,b)
907
 * ----------------------------------------------------------------------- */
955
 * ----------------------------------------------------------------------- */
Line 941... Line 989...
941
	b0 = max(a,b);
989
	b0 = max(a,b);
942
	if (b0 >= 8.0) {
990
	if (b0 >= 8.0) {
943
	/* L80:                  ALGORITHM FOR b0 >= 8 */
991
	/* L80:                  ALGORITHM FOR b0 >= 8 */
944
	    u = gamln1(a0) + algdiv(a0, b0);
992
	    u = gamln1(a0) + algdiv(a0, b0);
945
#ifdef DEBUG_bratio
993
#ifdef DEBUG_bratio
946
	    REprintf(" brcmp1(mu,a,b,*): a0 < 1, b0 >= 8;  z=%19.15g\n", z);
994
	    REprintf(" brcmp1(mu,a,b,*): a0 < 1, b0 >= 8;  z=%.15g\n", z);
947
#endif
995
#endif
948
	    return give_log
996
	    return give_log
949
		? log(a0) + esum(mu, z - u, TRUE)
997
		? log(a0) + esum(mu, z - u, TRUE)
950
		:     a0  * esum(mu, z - u, FALSE);
998
		:     a0  * esum(mu, z - u, FALSE);
951
 
999
 
Line 966... Line 1014...
966
	    // L50:
1014
	    // L50:
967
	    c = give_log
1015
	    c = give_log
968
		? log1p(gam1(a)) + log1p(gam1(b)) - log(z)
1016
		? log1p(gam1(a)) + log1p(gam1(b)) - log(z)
969
		: (gam1(a) + 1.0) * (gam1(b) + 1.0) / z;
1017
		: (gam1(a) + 1.0) * (gam1(b) + 1.0) / z;
970
#ifdef DEBUG_bratio
1018
#ifdef DEBUG_bratio
971
	    REprintf(" brcmp1(mu,a,b,*): a0 < 1, b0 <= 1;  c=%19.15g\n", c);
1019
	    REprintf(" brcmp1(mu,a,b,*): a0 < 1, b0 <= 1;  c=%.15g\n", c);
972
#endif
1020
#endif
973
	    return give_log
1021
	    return give_log
974
		? ans + log(a0) + c - log1p(a0 / b0)
1022
		? ans + log(a0) + c - log1p(a0 / b0)
975
		: ans * (a0 * c) / (a0 / b0 + 1.0);
1023
		: ans * (a0 * c) / (a0 / b0 + 1.0);
976
	}
1024
	}
Line 996... Line 1044...
996
	    t = (gam1(apb - 1.) + 1.0) / apb;
1044
	    t = (gam1(apb - 1.) + 1.0) / apb;
997
	} else {
1045
	} else {
998
	    t = gam1(apb) + 1.0;
1046
	    t = gam1(apb) + 1.0;
999
	}
1047
	}
1000
#ifdef DEBUG_bratio
1048
#ifdef DEBUG_bratio
1001
	REprintf(" brcmp1(mu,a,b,*): a0 < 1 < b0 < 8;  t=%19.15g\n", t);
1049
	REprintf(" brcmp1(mu,a,b,*): a0 < 1 < b0 < 8;  t=%.15g\n", t);
1002
#endif
1050
#endif
1003
	// L72:
1051
	// L72:
1004
	return give_log
1052
	return give_log
1005
	    ? log(a0)+ esum(mu, z, TRUE) + log1p(gam1(b0)) - log(t) // TODO? log(t) = log1p(..)
1053
	    ? log(a0)+ esum(mu, z, TRUE) + log1p(gam1(b0)) - log(t) // TODO? log(t) = log1p(..)
1006
	    :     a0 * esum(mu, z, FALSE) * (gam1(b0) + 1.0) / t;
1054
	    :     a0 * esum(mu, z, FALSE) * (gam1(b0) + 1.0) / t;
Line 1025... Line 1073...
1025
	    lambda = a - (a + b) * x;
1073
	    lambda = a - (a + b) * x;
1026
	}
1074
	}
1027
	double lx0 = -log1p(b/a); // in both cases
1075
	double lx0 = -log1p(b/a); // in both cases
1028
 
1076
 
1029
#ifdef DEBUG_bratio
1077
#ifdef DEBUG_bratio
1030
	REprintf(" brcmp1(mu,a,b,*): a,b >= 8;  x0=%19.15g, lx0=log(x0)=%19.15g\n", x0, lx0);
1078
	REprintf(" brcmp1(mu,a,b,*): a,b >= 8;	x0=%.15g, lx0=log(x0)=%.15g\n",
-
 
1079
		 x0, lx0);
1031
#endif
1080
#endif
1032
	// L110:
1081
	// L110:
1033
	double e = -lambda / a;
1082
	double e = -lambda / a;
1034
	if (fabs(e) > 0.6) {
1083
	if (fabs(e) > 0.6) {
1035
	    // L111:
1084
	    // L111:
Line 1055... Line 1104...
1055
    }
1104
    }
1056
 
1105
 
1057
} /* brcmp1 */
1106
} /* brcmp1 */
1058
 
1107
 
1059
static void bgrat(double a, double b, double x, double y, double *w,
1108
static void bgrat(double a, double b, double x, double y, double *w,
1060
		  double eps, int *ierr)
1109
		  double eps, int *ierr, Rboolean log_w)
1061
{
1110
{
1062
/* -----------------------------------------------------------------------
1111
/* -----------------------------------------------------------------------
1063
*     Asymptotic Expansion for I_x(a,b)  when a is larger than b.
1112
*     Asymptotic Expansion for I_x(a,b)  when a is larger than b.
1064
*     Compute   w := w + I_x(a,b)
1113
*     Compute   w := w + I_x(a,b)
1065
*     It is assumed a >= 15 and b <= 1.
1114
*     It is assumed a >= 15 and b <= 1.
1066
*     eps is the tolerance used.
1115
*     eps is the tolerance used.
1067
*     ierr is a variable that reports the status of the results.
1116
*     ierr is a variable that reports the status of the results.
-
 
1117
*
-
 
1118
* TODO:  if(log_w) ,  *w  itself must be in log-space;
-
 
1119
*     compute   w := w + I_x(a,b)  but return *w = log(w):
-
 
1120
*          *w := log(exp(*w) + I_x(a,b)) = logspace_add(*w, log( I_x(a,b) )
1068
* ----------------------------------------------------------------------- */
1121
* ----------------------------------------------------------------------- */
1069
 
1122
 
1070
#define n_terms_bgrat 30
1123
#define n_terms_bgrat 30
1071
    double c[n_terms_bgrat], d[n_terms_bgrat];
1124
    double c[n_terms_bgrat], d[n_terms_bgrat];
1072
    double bm1 = b - 0.5 - 0.5,
1125
    double bm1 = b - 0.5 - 0.5,
Line 1081... Line 1134...
1081
	    a,b,x,y);
1134
	    a,b,x,y);
1082
	/* L_Error:    THE EXPANSION CANNOT BE COMPUTED */ *ierr = 1; return;
1135
	/* L_Error:    THE EXPANSION CANNOT BE COMPUTED */ *ierr = 1; return;
1083
    }
1136
    }
1084
 
1137
 
1085
/*                 COMPUTATION OF THE EXPANSION */
1138
/*                 COMPUTATION OF THE EXPANSION */
-
 
1139
    double
-
 
1140
	/* r1 = b * (gam1(b) + 1.0) * exp(b * log(z)),// = b/gamma(b+1) z^b = z^b / gamma(b)
-
 
1141
	 * set r := exp(-z) * z^b / gamma(b) ;
-
 
1142
	 *          gam1(b) = 1/gamma(b+1) - 1 , b in [-1/2, 3/2] */
-
 
1143
	// exp(a*lnx) underflows for large (a * lnx); e.g. large a ==> using log_r := log(r):
-
 
1144
	// r = r1 * exp(a * lnx) * exp(bm1 * 0.5 * lnx);
-
 
1145
	// log(r)=log(b) + log1p(gam1(b)) + b * log(z) + (a * lnx) + (bm1 * 0.5 * lnx),
-
 
1146
	log_r = log(b) + log1p(gam1(b)) + b * log(z) + nu * lnx,
-
 
1147
	// FIXME work with  log_u = log(u)  also when log_p=FALSE  (??)
-
 
1148
	// u is 'factored out' from the expansion {and multiplied back, at the end}:
-
 
1149
	log_u = log_r - (algdiv(b, a) + b * log(nu)),// algdiv(b,a) = log(gamma(a)/gamma(a+b))
-
 
1150
	/* u = (log_p) ? log_r - u : exp(log_r-u); // =: M  in (9.2) of {reference above} */
-
 
1151
	/* u = algdiv(b, a) + b * log(nu);// algdiv(b,a) = log(gamma(a)/gamma(a+b)) */
-
 
1152
	// u = (log_p) ? log_u : exp(log_u); // =: M  in (9.2) of {reference above}
-
 
1153
	u = exp(log_u);
1086
 
1154
 
1087
    double r, u, p, q;
-
 
1088
    // set r := exp(-z) * z^b / Gamma(b) : gam1(b) = 1/gamma(b+1) - 1 , b in [-1/2, 3/2]
-
 
1089
    r = b * (gam1(b) + 1.0) * exp(b * log(z));// = b/gamma(b+1) z^b =  z^b / gamma(b)
-
 
1090
    r = r * exp(a * lnx) * exp(bm1 * 0.5 * lnx);
-
 
1091
 
-
 
1092
    // u is 'factored out' from the expansion {and multiplied back, at the end}:
-
 
1093
    u = algdiv(b, a) + b * log(nu);// algdiv(b,a) = log(gamma(a)/gamma(a+b))
-
 
1094
    u = r * exp(-u); // =: M  in (9.2) of {reference above}
-
 
1095
    if (u == 0.0) {
1155
    if (log_u == ML_NEGINF) {
1096
#ifdef DEBUG_bratio
1156
#ifdef DEBUG_bratio
1097
	REprintf(" bgrat(*): underflow u = 0 = r*exp(-u'), r = %g ", r);
1157
	REprintf(" bgrat(*): underflow log_u = -Inf  = log_r -u', log_r = %g ",
-
 
1158
		 log_r);
1098
#endif
1159
#endif
1099
	/* L_Error:    THE EXPANSION CANNOT BE COMPUTED */ *ierr = 2; return;
1160
	/* L_Error:    THE EXPANSION CANNOT BE COMPUTED */ *ierr = 2; return;
1100
    }
1161
    }
-
 
1162
 
-
 
1163
    Rboolean u_0 = (u == 0.); // underflow --> do work with log(u) == log_u !
-
 
1164
    double l = // := *w/u .. but with care: such that it also works when u underflows to 0:
-
 
1165
	log_w
-
 
1166
	? ((*w == ML_NEGINF) ? 0. : exp(  *w    - log_u))
1101
    grat1(b, z, r, &p, &q, eps);
1167
	: ((*w == 0.)        ? 0. : exp(log(*w) - log_u));
-
 
1168
 
-
 
1169
#ifdef DEBUG_bratio
-
 
1170
    REprintf(" bgrat(a=%g, b=%g, x=%g, *)\n -> u=%g, l='w/u'=%g, ", a,b,x, u, l);
-
 
1171
#endif
-
 
1172
    double
1102
//  -----     |--> (p , q)  {p + q = 1}  and we only use  q/r :
1173
	q_r = grat_r(b, z, log_r, eps), // = q/r of former grat1(b,z, r, &p, &q)
1103
    double v = 0.25 / (nu * nu),
1174
	v = 0.25 / (nu * nu),
1104
	t2 = lnx * 0.25 * lnx,
1175
	t2 = lnx * 0.25 * lnx,
1105
	l = *w / u,
-
 
1106
	j = q / r,
1176
	j = q_r,
1107
	sum = j,
1177
	sum = j,
1108
	t = 1.0, cn = 1.0, n2 = 0.;
1178
	t = 1.0, cn = 1.0, n2 = 0.;
1109
    for (int n = 1; n <= n_terms_bgrat; ++n) {
1179
    for (int n = 1; n <= n_terms_bgrat; ++n) {
1110
	double bp2n = b + n2;
1180
	double bp2n = b + n2;
1111
	j = (bp2n * (bp2n + 1.0) * j + (z + bp2n + 1.0) * t) * v;
1181
	j = (bp2n * (bp2n + 1.0) * j + (z + bp2n + 1.0) * t) * v;
Line 1139... Line 1209...
1139
		a,b,x, fabs(dj) /(sum + l));
1209
		a,b,x, fabs(dj) /(sum + l));
1140
    }
1210
    }
1141
 
1211
 
1142
/*                    ADD THE RESULTS TO W */
1212
/*                    ADD THE RESULTS TO W */
1143
    *ierr = 0;
1213
    *ierr = 0;
-
 
1214
    if(log_w) // *w is in log space already:
-
 
1215
	*w = logspace_add(*w, log_u + log(sum));
1144
    *w += u * sum;
1216
    else
-
 
1217
	*w += (u_0 ? exp(log_u + log(sum)) : u * sum);
1145
    return;
1218
    return;
1146
 
-
 
1147
} /* bgrat */
1219
} /* bgrat */
1148
 
1220
 
-
 
1221
 
-
 
1222
// called only from bgrat() , as   q_r = grat_r(b, z, log_r, eps)  :
1149
static void grat1(double a, double x, double r, double *p, double *q,
1223
static double grat_r(double a, double x, double log_r, double eps)
1150
		  double eps)
-
 
1151
{
1224
{
1152
/* -----------------------------------------------------------------------
1225
/* -----------------------------------------------------------------------
1153
 *        Evaluation of the incomplete gamma ratio functions
1226
 *        Scaled complement of incomplete gamma ratio function
1154
 *                      P(a,x) and Q(a,x)
1227
 *                   grat_r(a,x,r) :=  Q(a,x) / r
-
 
1228
 * where
1155
 *     P(a,x) = pgamma(x,a)  ---  Q(a,x) = pgamma(x,a, lower.tail=FALSE)
1229
 *               Q(a,x) = pgamma(x,a, lower.tail=FALSE)
-
 
1230
 *     and            r = e^(-x)* x^a / Gamma(a) ==  exp(log_r)
1156
 *
1231
 *
1157
 *     It is assumed that a <= 1.  eps is the tolerance to be used.
1232
 *     It is assumed that a <= 1.  eps is the tolerance to be used.
1158
 *     the input argument r has the value  r = e^(-x)* x^a / Gamma(a).
-
 
1159
 * ----------------------------------------------------------------------- */
1233
 * ----------------------------------------------------------------------- */
1160
 
1234
 
1161
// called only from bgrat() , as
-
 
1162
//  grat1(b, z, r, &p, &q, eps);  .. |-->  q  and we only use  q/r :
-
 
1163
 
-
 
1164
    if (a * x == 0.0) { /* L130: */
1235
    if (a * x == 0.0) { /* L130: */
1165
	if (x <= a) {
1236
	if (x <= a) {
1166
	    /* L100: */ *p = 0.0; *q = 1.0;
1237
	    /* L100: */ return exp(-log_r);
1167
	} else {
1238
	} else {
1168
	    /* L110:*/ *p = 1.0; *q = 0.0;
1239
	    /* L110:*/  return 0.;
1169
	}
1240
	}
1170
    }
1241
    }
1171
    else if (a == 0.5) { //--- FIXME not always good if (log_p): underflow!
1242
    else if (a == 0.5) { // e.g. when called from pt()
1172
	/* L120: */
1243
	/* L120: */
1173
	if (x < 0.25) {
1244
	if (x < 0.25) {
1174
	    *p = erf__(sqrt(x));
1245
	    double p = erf__(sqrt(x));
1175
	    *q = 0.5 - *p + 0.5;
-
 
1176
#ifdef DEBUG_bratio
1246
#ifdef DEBUG_bratio
1177
	    REprintf(" grat1(a=%g, x=%g ..)): a=1/2 --> p=erf__(.)= %g\n", a,x, *p);
1247
	    REprintf(" grat_r(a=%g, x=%g ..)): a=1/2 --> p=erf__(.)= %g\n", a,x, p);
1178
#endif
1248
#endif
1179
	} else {
1249
	    return (0.5 - p + 0.5)*exp(-log_r);
-
 
1250
 
-
 
1251
        } else { // 2013-02-27: improvement for "large" x: direct computation of q/r:
1180
	    *q = erfc1(0, sqrt(x));
1252
	    double sx = sqrt(x),
1181
	    *p = 0.5 - *q + 0.5;
1253
		q_r = erfc1(1, sx)/sx * M_SQRT_PI;
1182
#ifdef DEBUG_bratio
1254
#ifdef DEBUG_bratio
1183
	    REprintf(" grat1(a=%g, x=%g ..)): a=1/2 --> q=erfc1(..)= %g\n", a,x, *q);
1255
	    REprintf(" grat_r(a=%g, x=%g ..)): a=1/2 --> q_r=erfc1(..)/r= %g\n",
-
 
1256
		     a,x, q_r);
1184
#endif
1257
#endif
-
 
1258
	    return q_r;
1185
	}
1259
	}
1186
 
1260
 
1187
    } else if (x < 1.1) { /* L10:  Taylor series for  P(a,x)/x^a */
1261
    } else if (x < 1.1) { /* L10:  Taylor series for  P(a,x)/x^a */
1188
 
1262
 
1189
	double an = 3.,
1263
	double an = 3.,
Line 1196... Line 1270...
1196
	    t = c / (a + an);
1270
	    t = c / (a + an);
1197
	    sum += t;
1271
	    sum += t;
1198
	} while (fabs(t) > tol);
1272
	} while (fabs(t) > tol);
1199
 
1273
 
1200
#ifdef DEBUG_bratio
1274
#ifdef DEBUG_bratio
1201
	REprintf(" grat1(a=%g, x=%g, r=%g): sum=%g; Taylor used %.0f terms\n",
1275
	REprintf(" grat_r(a=%g, x=%g, log_r=%g): sum=%g; Taylor w/ %.0f terms",
1202
		 a,x,r, sum, an-3.);
1276
		 a,x,log_r, sum, an-3.);
1203
#endif
1277
#endif
1204
	double j = a * x * ((sum/6. - 0.5/(a + 2.)) * x + 1./(a + 1.)),
1278
	double j = a * x * ((sum/6. - 0.5/(a + 2.)) * x + 1./(a + 1.)),
1205
	    z = a * log(x),
1279
	    z = a * log(x),
1206
	    h = gam1(a),
1280
	    h = gam1(a),
1207
	    g = h + 1.0;
1281
	    g = h + 1.0;
1208
 
1282
 
1209
	if ((x >= 0.25 && (a < x / 2.59)) || (z > -0.13394)) {
1283
	if ((x >= 0.25 && (a < x / 2.59)) || (z > -0.13394)) {
1210
	    // L40:
1284
	    // L40:
1211
	    double l = rexpm1(z);
1285
	    double l = rexpm1(z),
1212
	    *q = ((l + 0.5 + 0.5) * j - l) * g - h;
1286
		q = ((l + 0.5 + 0.5) * j - l) * g - h;
1213
	    if (*q < 0.0) {
1287
	    if (q <= 0.0) {
-
 
1288
#ifdef DEBUG_bratio
-
 
1289
		REprintf(" => q_r= 0.\n");
-
 
1290
#endif
1214
		/* L110:*/ *p = 1.0; *q = 0.0;
1291
		/* L110:*/ return 0.;
1215
	    } else {
1292
	    } else {
-
 
1293
#ifdef DEBUG_bratio
-
 
1294
		REprintf(" => q_r=%.15g\n", q * exp(-log_r));
-
 
1295
#endif
1216
		*p = 0.5 - *q + 0.5;
1296
		return q * exp(-log_r);
1217
	    }
1297
	    }
1218
 
1298
 
1219
	} else {
1299
	} else {
1220
	    *p = exp(z) * g * (0.5 - j + 0.5);
1300
	    double p = exp(z) * g * (0.5 - j + 0.5);
-
 
1301
#ifdef DEBUG_bratio
-
 
1302
	    REprintf(" => q_r=%.15g\n", (0.5 - p + 0.5) * exp(-log_r));
-
 
1303
#endif
1221
	    *q = 0.5 - *p + 0.5;
1304
	    return /* q/r = */ (0.5 - p + 0.5) * exp(-log_r);
1222
	}
1305
	}
1223
 
1306
 
1224
    } else {
1307
    } else {
1225
	/* L50: ----  (x >= 1.1)  ---- Continued Fraction Expansion */
1308
	/* L50: ----  (x >= 1.1)  ---- Continued Fraction Expansion */
1226
 
1309
 
Line 1240... Line 1323...
1240
	    b2n = b2n_1 + c_a * b2n;
1323
	    b2n = b2n_1 + c_a * b2n;
1241
	    an0 = a2n / b2n;
1324
	    an0 = a2n / b2n;
1242
	} while (fabs(an0 - am0) >= eps * an0);
1325
	} while (fabs(an0 - am0) >= eps * an0);
1243
 
1326
 
1244
#ifdef DEBUG_bratio
1327
#ifdef DEBUG_bratio
1245
	REprintf(" grat1(a=%g, x=%g, r=%g): Cont.Fraction used %.0f terms\n",
1328
	REprintf(" grat_r(a=%g, x=%g, log_r=%g): Cont.frac. %.0f terms => q_r=%.15g\n",
1246
		 a,x,r, c-1.);
1329
		 a,x, log_r, c-1., an0);
1247
#endif
1330
#endif
1248
 
-
 
1249
	*q = r * an0;
1331
	return /* q/r = (r * an0)/r = */ an0;
1250
	*p = 0.5 - *q + 0.5;
-
 
1251
    }
1332
    }
-
 
1333
} /* grat_r */
-
 
1334
 
1252
 
1335
 
1253
    return;
-
 
1254
} /* grat1 */
-
 
1255
 
1336
 
1256
static double basym(double a, double b, double lambda, double eps, int log_p)
1337
static double basym(double a, double b, double lambda, double eps, int log_p)
1257
{
1338
{
1258
/* ----------------------------------------------------------------------- */
1339
/* ----------------------------------------------------------------------- */
1259
/*     ASYMPTOTIC EXPANSION FOR I_x(A,B) FOR LARGE A AND B. */
1340
/*     ASYMPTOTIC EXPANSION FOR I_x(A,B) FOR LARGE A AND B. */