The R Project SVN R

Rev

Rev 59222 | Rev 72156 | Go to most recent revision | 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
3
 *  Copyright (C) 1998 Ross Ihaka
59170 ripley 4
 *  Copyright (C) 2000-12 The R Core Team
42303 ripley 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
18
 *  http://www.r-project.org/Licenses/
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
 
45272 maechler 59
    int kMax;
59222 ripley 60
    double k, ncp2, dx2, d, D, term;
60268 ripley 61
    LDOUBLE sum, p_k, q;
42303 ripley 62
 
63
#ifdef IEEE_754
44785 ripley 64
    if (ISNAN(x) || ISNAN(a) || ISNAN(b) || ISNAN(ncp))
65
	return x + a + b + ncp;
42303 ripley 66
#endif
44785 ripley 67
    if (ncp < 0 || a <= 0 || b <= 0)
42303 ripley 68
	ML_ERR_return_NAN;
69
 
44785 ripley 70
    if (!R_FINITE(a) || !R_FINITE(b) || !R_FINITE(ncp))
42303 ripley 71
	ML_ERR_return_NAN;
72
 
73
    if (x < 0 || x > 1) return(R_D__0);
44785 ripley 74
    if(ncp == 0)
42303 ripley 75
	return dbeta(x, a, b, give_log);
76
 
45272 maechler 77
    /* New algorithm, starting with *largest* term : */
44785 ripley 78
    ncp2 = 0.5 * ncp;
45272 maechler 79
    dx2 = ncp2*x;
80
    d = (dx2 - a - 1)/2;
81
    D = d*d + dx2 * (a + b) - a;
82
    if(D <= 0) {
83
	kMax = 0;
44946 maechler 84
    } else {
45272 maechler 85
	D = ceil(d + sqrt(D));
86
	kMax = (D > 0) ? (int)D : 0;
42303 ripley 87
    }
45272 maechler 88
 
89
    /* The starting "middle term" --- first look at it's log scale: */
90
    term = dbeta(x, a + kMax, b, /* log = */ TRUE);
91
    p_k = dpois_raw(kMax, ncp2,              TRUE);
59222 ripley 92
    if(x == 0. || !R_FINITE(term) || !R_FINITE((double)p_k)) /* if term = +Inf */
59170 ripley 93
	return R_D_exp((double)(p_k + term));
45272 maechler 94
 
95
    /* Now if s_k := p_k * t_k  {here = exp(p_k + term)} would underflow,
96
     * we should rather scale everything and re-scale at the end:*/
97
 
98
    p_k += term; /* = log(p_k) + log(t_k) == log(s_k) -- used at end to rescale */
99
    /* mid = 1 = the rescaled value, instead of  mid = exp(p_k); */
100
 
101
    /* Now sum from the inside out */
102
    sum = term = 1. /* = mid term */;
103
    /* middle to the left */
104
    k = kMax;
105
    while(k > 0 && term > sum * eps) {
106
	k--;
107
	q = /* 1 / r_k = */ (k+1)*(k+a) / (k+a+b) / dx2;
108
	term *= q;
109
	sum += term;
110
    }
111
    /* middle to the right */
112
    term = 1.;
113
    k = kMax;
114
    do {
115
	q = /* r_{old k} = */ dx2 * (k+a+b) / (k+a) / (k+1);
116
	k++;
117
	term *= q;
118
	sum += term;
119
    } while (term > sum * eps);
120
 
60268 ripley 121
#ifdef HAVE_LONG_DOUBLE
59170 ripley 122
    return R_D_exp((double)(p_k + logl(sum)));
60268 ripley 123
#else
124
    return R_D_exp((double)(p_k + log(sum)));
125
#endif
42303 ripley 126
}