| 1 |
/* specfunc/beta_inc.c |
| 2 |
* |
| 3 |
* Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 3 of the License, or (at |
| 8 |
* your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* Author: G. Jungman */ |
| 21 |
/* Modified for cdfs by Brian Gough, June 2003 */ |
| 22 |
|
| 23 |
static double |
| 24 |
beta_cont_frac (const double a, const double b, const double x, |
| 25 |
const double epsabs) |
| 26 |
{ |
| 27 |
const unsigned int max_iter = 512; /* control iterations */ |
| 28 |
const double cutoff = 2.0 * GSL_DBL_MIN; /* control the zero cutoff */ |
| 29 |
unsigned int iter_count = 0; |
| 30 |
double cf; |
| 31 |
|
| 32 |
/* standard initialization for continued fraction */ |
| 33 |
double num_term = 1.0; |
| 34 |
double den_term = 1.0 - (a + b) * x / (a + 1.0); |
| 35 |
|
| 36 |
if (fabs (den_term) < cutoff) |
| 37 |
den_term = GSL_NAN; |
| 38 |
|
| 39 |
den_term = 1.0 / den_term; |
| 40 |
cf = den_term; |
| 41 |
|
| 42 |
while (iter_count < max_iter) |
| 43 |
{ |
| 44 |
const int k = iter_count + 1; |
| 45 |
double coeff = k * (b - k) * x / (((a - 1.0) + 2 * k) * (a + 2 * k)); |
| 46 |
double delta_frac; |
| 47 |
|
| 48 |
/* first step */ |
| 49 |
den_term = 1.0 + coeff * den_term; |
| 50 |
num_term = 1.0 + coeff / num_term; |
| 51 |
|
| 52 |
if (fabs (den_term) < cutoff) |
| 53 |
den_term = GSL_NAN; |
| 54 |
|
| 55 |
if (fabs (num_term) < cutoff) |
| 56 |
num_term = GSL_NAN; |
| 57 |
|
| 58 |
den_term = 1.0 / den_term; |
| 59 |
|
| 60 |
delta_frac = den_term * num_term; |
| 61 |
cf *= delta_frac; |
| 62 |
|
| 63 |
coeff = -(a + k) * (a + b + k) * x / ((a + 2 * k) * (a + 2 * k + 1.0)); |
| 64 |
|
| 65 |
/* second step */ |
| 66 |
den_term = 1.0 + coeff * den_term; |
| 67 |
num_term = 1.0 + coeff / num_term; |
| 68 |
|
| 69 |
if (fabs (den_term) < cutoff) |
| 70 |
den_term = GSL_NAN; |
| 71 |
|
| 72 |
if (fabs (num_term) < cutoff) |
| 73 |
num_term = GSL_NAN; |
| 74 |
|
| 75 |
den_term = 1.0 / den_term; |
| 76 |
|
| 77 |
delta_frac = den_term * num_term; |
| 78 |
cf *= delta_frac; |
| 79 |
|
| 80 |
if (fabs (delta_frac - 1.0) < 2.0 * GSL_DBL_EPSILON) |
| 81 |
break; |
| 82 |
|
| 83 |
if (cf * fabs (delta_frac - 1.0) < epsabs) |
| 84 |
break; |
| 85 |
|
| 86 |
++iter_count; |
| 87 |
} |
| 88 |
|
| 89 |
if (iter_count >= max_iter) |
| 90 |
return GSL_NAN; |
| 91 |
|
| 92 |
return cf; |
| 93 |
} |
| 94 |
|
| 95 |
/* The function beta_inc_AXPY(A,Y,a,b,x) computes A * beta_inc(a,b,x) |
| 96 |
+ Y taking account of possible cancellations when using the |
| 97 |
hypergeometric transformation beta_inc(a,b,x)=1-beta_inc(b,a,1-x). |
| 98 |
|
| 99 |
It also adjusts the accuracy of beta_inc() to fit the overall |
| 100 |
absolute error when A*beta_inc is added to Y. (e.g. if Y >> |
| 101 |
A*beta_inc then the accuracy of beta_inc can be reduced) */ |
| 102 |
|
| 103 |
static double |
| 104 |
beta_inc_AXPY (const double A, const double Y, |
| 105 |
const double a, const double b, const double x) |
| 106 |
{ |
| 107 |
if (x == 0.0) |
| 108 |
{ |
| 109 |
return A * 0 + Y; |
| 110 |
} |
| 111 |
else if (x == 1.0) |
| 112 |
{ |
| 113 |
return A * 1 + Y; |
| 114 |
} |
| 115 |
else |
| 116 |
{ |
| 117 |
double ln_beta = gsl_sf_lnbeta (a, b); |
| 118 |
double ln_pre = -ln_beta + a * log (x) + b * log1p (-x); |
| 119 |
|
| 120 |
double prefactor = exp (ln_pre); |
| 121 |
|
| 122 |
if (x < (a + 1.0) / (a + b + 2.0)) |
| 123 |
{ |
| 124 |
/* Apply continued fraction directly. */ |
| 125 |
double epsabs = fabs (Y / (A * prefactor / a)) * GSL_DBL_EPSILON; |
| 126 |
|
| 127 |
double cf = beta_cont_frac (a, b, x, epsabs); |
| 128 |
|
| 129 |
return A * (prefactor * cf / a) + Y; |
| 130 |
} |
| 131 |
else |
| 132 |
{ |
| 133 |
/* Apply continued fraction after hypergeometric transformation. */ |
| 134 |
double epsabs = |
| 135 |
fabs ((A + Y) / (A * prefactor / b)) * GSL_DBL_EPSILON; |
| 136 |
double cf = beta_cont_frac (b, a, 1.0 - x, epsabs); |
| 137 |
double term = prefactor * cf / b; |
| 138 |
|
| 139 |
if (A == -Y) |
| 140 |
{ |
| 141 |
return -A * term; |
| 142 |
} |
| 143 |
else |
| 144 |
{ |
| 145 |
return A * (1 - term) + Y; |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/* Direct series evaluation for testing purposes only */ |
| 152 |
|
| 153 |
#if 0 |
| 154 |
static double |
| 155 |
beta_series (const double a, const double b, const double x, |
| 156 |
const double epsabs) |
| 157 |
{ |
| 158 |
double f = x / (1 - x); |
| 159 |
double c = (b - 1) / (a + 1) * f; |
| 160 |
double s = 1; |
| 161 |
double n = 0; |
| 162 |
|
| 163 |
s += c; |
| 164 |
|
| 165 |
do |
| 166 |
{ |
| 167 |
n++; |
| 168 |
c *= -f * (2 + n - b) / (2 + n + a); |
| 169 |
s += c; |
| 170 |
} |
| 171 |
while (n < 512 && fabs (c) > GSL_DBL_EPSILON * fabs (s) + epsabs); |
| 172 |
|
| 173 |
s /= (1 - x); |
| 174 |
|
| 175 |
return s; |
| 176 |
} |
| 177 |
#endif |