3 * Copyright (C) 2004 Free Software Foundation, Inc.
4 * Copyright (C) 2006, 2007 Brian Gough
5 * Written by Jason H. Stover.
6 * Modified for GSL by Brian Gough
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or (at
11 * your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
24 * Invert the Beta distribution.
28 * Roger W. Abernathy and Robert P. Smith. "Applying Series Expansion
29 * to the Inverse Beta Distribution to Find Percentiles of the
30 * F-Distribution," ACM Transactions on Mathematical Software, volume
31 * 19, number 4, December 1993, pages 474-480.
33 * G.W. Hill and A.W. Davis. "Generalized asymptotic expansions of a
34 * Cornish-Fisher type," Annals of Mathematical Statistics, volume 39,
35 * number 8, August 1968, pages 1264-1273.
40 #include <gsl/gsl_math.h>
41 #include <gsl/gsl_errno.h>
42 #include <gsl/gsl_sf_gamma.h>
43 #include <gsl/gsl_cdf.h>
44 #include <gsl/gsl_randist.h>
49 bisect (double x, double P, double a, double b, double xtol, double Ptol)
51 double x0 = 0, x1 = 1, Px;
53 while (fabs(x1 - x0) > xtol) {
54 Px = gsl_cdf_beta_P (x, a, b);
55 if (fabs(Px - P) < Ptol) {
56 /* return as soon as approximation is good enough, including on
57 the first iteration */
71 gsl_cdf_beta_Pinv (const double P, const double a, const double b)
75 if (P < 0.0 || P > 1.0)
77 CDF_ERROR ("P must be in range 0 < P < 1", GSL_EDOM);
82 CDF_ERROR ("a < 0", GSL_EDOM);
87 CDF_ERROR ("b < 0", GSL_EDOM);
102 return gsl_cdf_beta_Qinv (1 - P, a, b);
111 double lg_ab = gsl_sf_lngamma (a + b);
112 double lg_a = gsl_sf_lngamma (a);
113 double lg_b = gsl_sf_lngamma (b);
115 double lx = (log (a) + lg_a + lg_b - lg_ab + log (P)) / a;
117 x = exp (lx); /* first approximation */
118 x *= pow (1 - x, -(b - 1) / a); /* second approximation */
128 /* Use expected value as first guess */
132 /* Do bisection to get closer */
133 x = bisect (x, P, a, b, 0.01, 0.01);
136 double lambda, dP, phi;
140 dP = P - gsl_cdf_beta_P (x, a, b);
141 phi = gsl_ran_beta_pdf (x, a, b);
143 if (dP == 0.0 || n++ > 64)
146 lambda = dP / GSL_MAX (2 * fabs (dP / x), phi);
149 double step0 = lambda;
150 double step1 = -((a - 1) / x - (b - 1) / (1 - x)) * lambda * lambda / 2;
154 if (fabs (step1) < fabs (step0))
160 /* scale back step to a reasonable size when too large */
161 step *= 2 * fabs (step0 / step1);
164 if (x + step > 0 && x + step < 1)
170 x = sqrt (x) * sqrt (mean); /* try a new starting point */
173 if (fabs (step0) > 1e-10 * x)
179 if (fabs(dP) > GSL_SQRT_DBL_EPSILON * P)
181 GSL_ERROR_VAL("inverse failed to converge", GSL_EFAILED, GSL_NAN);
189 gsl_cdf_beta_Qinv (const double Q, const double a, const double b)
192 if (Q < 0.0 || Q > 1.0)
194 CDF_ERROR ("Q must be inside range 0 < Q < 1", GSL_EDOM);
199 CDF_ERROR ("a < 0", GSL_EDOM);
204 CDF_ERROR ("b < 0", GSL_EDOM);
219 return gsl_cdf_beta_Pinv (1 - Q, a, b);
223 return 1 - gsl_cdf_beta_Pinv (Q, b, a);