The R Project SVN R

Rev

Rev 77685 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42303 ripley 1
/*
2
 *  Mathlib : A C Library of Special Functions
88401 maechler 3
 *  Copyright (C) 2000-2021 The R Core Team
42303 ripley 4
 *  Copyright (C) 1998 Ross Ihaka
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, a copy is available at
68947 ripley 18
 *  https://www.R-project.org/Licenses/
42303 ripley 19
 *
20
 *  SYNOPSIS
21
 *
22
 *    #include <Rmath.h>
44785 ripley 23
 *    double dnbeta(double x, double a, double b, double ncp, int give_log);
42303 ripley 24
 *
25
 *  DESCRIPTION
26
 *
27
 *    Computes the density of the noncentral beta distribution with
44785 ripley 28
 *    noncentrality parameter ncp.  The noncentral beta distribution
42303 ripley 29
 *    has density:
30
 *
31
 *		       Inf
44932 maechler 32
 *	f(x|a,b,ncp) = SUM  p(i) *  x^(a+i-1) * (1-x)^(b-1) / B(a+i,b)
42303 ripley 33
 *		       i=0
34
 *
35
 *    where:
36
 *
44932 maechler 37
 *		p(k) = exp(-ncp/2) (ncp/2)^k / k!
42303 ripley 38
 *
44932 maechler 39
 *	      B(a,b) = Gamma(a) * Gamma(b) / Gamma(a+b)
42303 ripley 40
 *
41
 *
42
 *    This can be computed efficiently by using the recursions:
43
 *
44932 maechler 44
 *	      p(k+1) = ncp/2 / (k+1) * p(k)
42303 ripley 45
 *
44932 maechler 46
 *      B(a+k+1,b)   = (a+k)/(a+b+k) * B(a+k,b)
42303 ripley 47
 *
45272 maechler 48
 * The new algorithm first determines for which k the k-th term is maximal,
49
 * and then sums outwards to both sides from the 'mid'.
42303 ripley 50
 */
51
 
52
#include "nmath.h"
53
#include "dpq.h"
54
 
44785 ripley 55
double dnbeta(double x, double a, double b, double ncp, int give_log)
42303 ripley 56
{
45272 maechler 57
    const static double eps = 1.e-15;
42303 ripley 58
 
59
#ifdef IEEE_754
44785 ripley 60
    if (ISNAN(x) || ISNAN(a) || ISNAN(b) || ISNAN(ncp))
61
	return x + a + b + ncp;
42303 ripley 62
#endif
44785 ripley 63
    if (ncp < 0 || a <= 0 || b <= 0)
77685 maechler 64
	ML_WARN_return_NAN;
42303 ripley 65
 
44785 ripley 66
    if (!R_FINITE(a) || !R_FINITE(b) || !R_FINITE(ncp))
77685 maechler 67
	ML_WARN_return_NAN;
42303 ripley 68
 
69
    if (x < 0 || x > 1) return(R_D__0);
44785 ripley 70
    if(ncp == 0)
42303 ripley 71
	return dbeta(x, a, b, give_log);
72
 
88401 maechler 73
    /* Non-central Beta:  New algorithm, starting with *largest* term : */
74
    double
75
	ncp2 = ldexp(ncp, -1), // = 0.5 * ncp
76
	dx2 = ncp2*x,
77
	d = ldexp(dx2 - a - 1, -1), // = (...)/2
78
	D = d*d + dx2 * (a + b) - a;
79
    int kMax;
45272 maechler 80
    if(D <= 0) {
81
	kMax = 0;
44946 maechler 82
    } else {
45272 maechler 83
	D = ceil(d + sqrt(D));
84
	kMax = (D > 0) ? (int)D : 0;
42303 ripley 85
    }
45272 maechler 86
 
88401 maechler 87
    LDOUBLE sum, term, p_k, q;
45272 maechler 88
    /* The starting "middle term" --- first look at it's log scale: */
89
    term = dbeta(x, a + kMax, b, /* log = */ TRUE);
90
    p_k = dpois_raw(kMax, ncp2,              TRUE);
59222 ripley 91
    if(x == 0. || !R_FINITE(term) || !R_FINITE((double)p_k)) /* if term = +Inf */
59170 ripley 92
	return R_D_exp((double)(p_k + term));
45272 maechler 93
 
94
    /* Now if s_k := p_k * t_k  {here = exp(p_k + term)} would underflow,
95
     * we should rather scale everything and re-scale at the end:*/
96
 
97
    p_k += term; /* = log(p_k) + log(t_k) == log(s_k) -- used at end to rescale */
98
    /* mid = 1 = the rescaled value, instead of  mid = exp(p_k); */
99
 
100
    /* Now sum from the inside out */
101
    sum = term = 1. /* = mid term */;
102
    /* middle to the left */
88401 maechler 103
    double k = kMax;
45272 maechler 104
    while(k > 0 && term > sum * eps) {
105
	k--;
106
	q = /* 1 / r_k = */ (k+1)*(k+a) / (k+a+b) / dx2;
107
	term *= q;
108
	sum += term;
109
    }
110
    /* middle to the right */
111
    term = 1.;
112
    k = kMax;
113
    do {
114
	q = /* r_{old k} = */ dx2 * (k+a+b) / (k+a) / (k+1);
115
	k++;
116
	term *= q;
117
	sum += term;
118
    } while (term > sum * eps);
119
 
60268 ripley 120
#ifdef HAVE_LONG_DOUBLE
59170 ripley 121
    return R_D_exp((double)(p_k + logl(sum)));
60268 ripley 122
#else
123
    return R_D_exp((double)(p_k + log(sum)));
124
#endif
42303 ripley 125
}