3 * Copyright (C) 2007 Brian Gough
4 * Copyright (C) 2002 Jason H. Stover.
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 3 of the License, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
23 #include <gsl/gsl_cdf.h>
24 #include <gsl/gsl_math.h>
25 #include <gsl/gsl_randist.h>
26 #include <gsl/gsl_sf_gamma.h>
31 inv_cornish_fisher (double z, double nu)
33 double a = 1 / (nu - 0.5);
34 double b = 48.0 / (a * a);
36 double cf1 = z * (3 + z * z);
37 double cf2 = z * (945 + z * z * (360 + z * z * (63 + z * z * 4)));
39 double y = z - cf1 / b + cf2 / (10 * b * b);
41 double t = GSL_SIGN (z) * sqrt (nu * expm1 (a * y * y));
48 gsl_cdf_tdist_Pinv (const double P, const double nu)
63 x = tan (M_PI * (P - 0.5));
68 x = a / sqrt (2 * (1 - a * a));
71 ptail = (P < 0.5) ? P : 1 - P;
73 if (sqrt (M_PI * nu / 2) * ptail > pow (0.05, nu / 2))
75 double xg = gsl_cdf_ugaussian_Pinv (P);
76 x = inv_cornish_fisher (xg, nu);
80 /* Use an asymptotic expansion of the tail of integral */
82 double beta = gsl_sf_beta (0.5, nu / 2);
86 x = -sqrt (nu) * pow (beta * nu * P, -1.0 / nu);
90 x = sqrt (nu) * pow (beta * nu * (1 - P), -1.0 / nu);
93 /* Correct nu -> nu/(1+nu/x^2) in the leading term to account
94 for higher order terms. This avoids overestimating x, which
95 makes the iteration unstable due to the rapidly decreasing
96 tails of the distribution. */
98 x /= sqrt (1 + nu / (x * x));
106 dP = P - gsl_cdf_tdist_P (x, nu);
107 phi = gsl_ran_tdist_pdf (x, nu);
109 if (dP == 0.0 || n++ > 32)
113 double lambda = dP / phi;
114 double step0 = lambda;
115 double step1 = ((nu + 1) * x / (x * x + nu)) * (lambda * lambda / 4.0);
119 if (fabs (step1) < fabs (step0))
124 if (P > 0.5 && x + step < 0)
126 else if (P < 0.5 && x + step > 0)
131 if (fabs (step) > 1e-10 * fabs (x))
136 if (fabs(dP) > GSL_SQRT_DBL_EPSILON * P)
138 GSL_ERROR_VAL("inverse failed to converge", GSL_EFAILED, GSL_NAN);
146 gsl_cdf_tdist_Qinv (const double Q, const double nu)
161 x = tan (M_PI * (0.5 - Q));
165 double a = 2 * (1 - Q) - 1;
166 x = a / sqrt (2 * (1 - a * a));
169 qtail = (Q < 0.5) ? Q : 1 - Q;
171 if (sqrt (M_PI * nu / 2) * qtail > pow (0.05, nu / 2))
173 double xg = gsl_cdf_ugaussian_Qinv (Q);
174 x = inv_cornish_fisher (xg, nu);
178 /* Use an asymptotic expansion of the tail of integral */
180 double beta = gsl_sf_beta (0.5, nu / 2);
184 x = sqrt (nu) * pow (beta * nu * Q, -1.0 / nu);
188 x = -sqrt (nu) * pow (beta * nu * (1 - Q), -1.0 / nu);
191 /* Correct nu -> nu/(1+nu/x^2) in the leading term to account
192 for higher order terms. This avoids overestimating x, which
193 makes the iteration unstable due to the rapidly decreasing
194 tails of the distribution. */
196 x /= sqrt (1 + nu / (x * x));
204 dQ = Q - gsl_cdf_tdist_Q (x, nu);
205 phi = gsl_ran_tdist_pdf (x, nu);
207 if (dQ == 0.0 || n++ > 32)
211 double lambda = - dQ / phi;
212 double step0 = lambda;
213 double step1 = ((nu + 1) * x / (x * x + nu)) * (lambda * lambda / 4.0);
217 if (fabs (step1) < fabs (step0))
222 if (Q < 0.5 && x + step < 0)
224 else if (Q > 0.5 && x + step > 0)
229 if (fabs (step) > 1e-10 * fabs (x))