3 * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
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.
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.
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.
20 /* Author: G. Jungman */
21 /* Modified for cdfs by Brian Gough, June 2003 */
23 #include <gsl/gsl_sf_gamma.h>
26 beta_cont_frac (const double a, const double b, const double x,
29 const unsigned int max_iter = 512; /* control iterations */
30 const double cutoff = 2.0 * GSL_DBL_MIN; /* control the zero cutoff */
31 unsigned int iter_count = 0;
34 /* standard initialization for continued fraction */
35 double num_term = 1.0;
36 double den_term = 1.0 - (a + b) * x / (a + 1.0);
38 if (fabs (den_term) < cutoff)
41 den_term = 1.0 / den_term;
44 while (iter_count < max_iter)
46 const int k = iter_count + 1;
47 double coeff = k * (b - k) * x / (((a - 1.0) + 2 * k) * (a + 2 * k));
51 den_term = 1.0 + coeff * den_term;
52 num_term = 1.0 + coeff / num_term;
54 if (fabs (den_term) < cutoff)
57 if (fabs (num_term) < cutoff)
60 den_term = 1.0 / den_term;
62 delta_frac = den_term * num_term;
65 coeff = -(a + k) * (a + b + k) * x / ((a + 2 * k) * (a + 2 * k + 1.0));
68 den_term = 1.0 + coeff * den_term;
69 num_term = 1.0 + coeff / num_term;
71 if (fabs (den_term) < cutoff)
74 if (fabs (num_term) < cutoff)
77 den_term = 1.0 / den_term;
79 delta_frac = den_term * num_term;
82 if (fabs (delta_frac - 1.0) < 2.0 * GSL_DBL_EPSILON)
85 if (cf * fabs (delta_frac - 1.0) < epsabs)
91 if (iter_count >= max_iter)
97 /* The function beta_inc_AXPY(A,Y,a,b,x) computes A * beta_inc(a,b,x)
98 + Y taking account of possible cancellations when using the
99 hypergeometric transformation beta_inc(a,b,x)=1-beta_inc(b,a,1-x).
101 It also adjusts the accuracy of beta_inc() to fit the overall
102 absolute error when A*beta_inc is added to Y. (e.g. if Y >>
103 A*beta_inc then the accuracy of beta_inc can be reduced) */
108 beta_inc_AXPY (const double A, const double Y,
109 const double a, const double b, const double x)
119 else if (a > 1e5 && b < 10 && x > a / (a + b))
121 /* Handle asymptotic regime, large a, small b, x > peak [AS 26.5.17] */
122 double N = a + (b - 1.0) / 2.0;
123 return A * gsl_sf_gamma_inc_Q (b, -N * log (x)) + Y;
125 else if (b > 1e5 && a < 10 && x < b / (a + b))
127 /* Handle asymptotic regime, small a, large b, x < peak [AS 26.5.17] */
128 double N = b + (a - 1.0) / 2.0;
129 return A * gsl_sf_gamma_inc_P (a, -N * log1p (-x)) + Y;
133 double ln_beta = gsl_sf_lnbeta (a, b);
134 double ln_pre = -ln_beta + a * log (x) + b * log1p (-x);
136 double prefactor = exp (ln_pre);
138 if (x < (a + 1.0) / (a + b + 2.0))
140 /* Apply continued fraction directly. */
141 double epsabs = fabs (Y / (A * prefactor / a)) * GSL_DBL_EPSILON;
143 double cf = beta_cont_frac (a, b, x, epsabs);
145 return A * (prefactor * cf / a) + Y;
149 /* Apply continued fraction after hypergeometric transformation. */
151 fabs ((A + Y) / (A * prefactor / b)) * GSL_DBL_EPSILON;
152 double cf = beta_cont_frac (b, a, 1.0 - x, epsabs);
153 double term = prefactor * cf / b;
161 return A * (1 - term) + Y;
167 /* Direct series evaluation for testing purposes only */
171 beta_series (const double a, const double b, const double x,
174 double f = x / (1 - x);
175 double c = (b - 1) / (a + 1) * f;
184 c *= -f * (2 + n - b) / (2 + n + a);
187 while (n < 512 && fabs (c) > GSL_DBL_EPSILON * fabs (s) + epsabs);