/* cdf/gammainv.c * * Copyright (C) 2003 Brian Gough * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at * your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include #include double gsl_cdf_gamma_Pinv (double p, double a, double b) { double x; if (p == 1.0) { return GSL_POSINF; } else if (p == 0.0) { return 0.0; } if (p < 0.05) { double x0 = exp((lgamma(b) + log(p))/a); x = x0; } else if (p > 0.95) { double x0 = exp(-log(1-p)-lgamma(b)); x = x0; } else { double xg = gsl_cdf_ugaussian_Pinv (p); double x0 = (xg < -sqrt(b)) ? b : sqrt (b) * xg + b; x = x0; } { double dp = p - gsl_cdf_gamma_P (x, 1.0, b); double u = log(x); while (fabs (dp) > 1e-10 * p) { double dp1, step = dp / exp((b-1.0)*u); do { u = u + step; x = exp(u); dp1 = p - gsl_cdf_gamma_P (x, 1.0, b); step *= 2.0; } while((dp > 0 && dp1 > 0) || (dp < 0 && dp1 < 0)); dp = dp1; } } return a * x; } double gsl_cdf_gamma_Qinv (double p, double a, double b) { double x; if (p == 1.0) { return 0.0; } else if (p == 0.0) { return GSL_POSINF; } return x; }