1 // (C) Copyright John Maddock 2006.
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #ifndef BOOST_MATH_SPECIAL_FUNCTIONS_IGAMMA_INVERSE_HPP
7 #define BOOST_MATH_SPECIAL_FUNCTIONS_IGAMMA_INVERSE_HPP
13 #include <boost/math/tools/tuple.hpp>
14 #include <boost/math/special_functions/gamma.hpp>
15 #include <boost/math/special_functions/sign.hpp>
16 #include <boost/math/tools/roots.hpp>
17 #include <boost/math/policies/error_handling.hpp>
19 namespace boost{ namespace math{
24 T find_inverse_s(T p, T q)
27 // Computation of the Incomplete Gamma Function Ratios and their Inverse
28 // ARMIDO R. DIDONATO and ALFRED H. MORRIS, JR.
29 // ACM Transactions on Mathematical Software, Vol. 12, No. 4,
30 // December 1986, Pages 377-393.
38 t = sqrt(-2 * log(p));
42 t = sqrt(-2 * log(q));
44 static const double a[4] = { 3.31125922108741, 11.6616720288968, 4.28342155967104, 0.213623493715853 };
45 static const double b[5] = { 1, 6.61053765625462, 6.40691597760039, 1.27364489782223, 0.3611708101884203e-1 };
46 T s = t - tools::evaluate_polynomial(a, t) / tools::evaluate_polynomial(b, t);
53 T didonato_SN(T a, T x, unsigned N, T tolerance = 0)
56 // Computation of the Incomplete Gamma Function Ratios and their Inverse
57 // ARMIDO R. DIDONATO and ALFRED H. MORRIS, JR.
58 // ACM Transactions on Mathematical Software, Vol. 12, No. 4,
59 // December 1986, Pages 377-393.
66 T partial = x / (a + 1);
68 for(unsigned i = 2; i <= N; ++i)
70 partial *= x / (a + i);
72 if(partial < tolerance)
79 template <class T, class Policy>
80 inline T didonato_FN(T p, T a, T x, unsigned N, T tolerance, const Policy& pol)
83 // Computation of the Incomplete Gamma Function Ratios and their Inverse
84 // ARMIDO R. DIDONATO and ALFRED H. MORRIS, JR.
85 // ACM Transactions on Mathematical Software, Vol. 12, No. 4,
86 // December 1986, Pages 377-393.
91 T u = log(p) + boost::math::lgamma(a + 1, pol);
92 return exp((u + x - log(didonato_SN(a, x, N, tolerance))) / a);
95 template <class T, class Policy>
96 T find_inverse_gamma(T a, T p, T q, const Policy& pol, bool* p_has_10_digits)
99 // In order to understand what's going on here, you will
102 // Computation of the Incomplete Gamma Function Ratios and their Inverse
103 // ARMIDO R. DIDONATO and ALFRED H. MORRIS, JR.
104 // ACM Transactions on Mathematical Software, Vol. 12, No. 4,
105 // December 1986, Pages 377-393.
110 *p_has_10_digits = false;
115 BOOST_MATH_INSTRUMENT_VARIABLE(result);
119 T g = boost::math::tgamma(a, pol);
121 BOOST_MATH_INSTRUMENT_VARIABLE(g);
122 BOOST_MATH_INSTRUMENT_VARIABLE(b);
123 if((b > 0.6) || ((b >= 0.45) && (a >= 0.3)))
125 // DiDonato & Morris Eq 21:
127 // There is a slight variation from DiDonato and Morris here:
128 // the first form given here is unstable when p is close to 1,
129 // making it impossible to compute the inverse of Q(a,x) for small
130 // q. Fortunately the second form works perfectly well in this case.
133 if((b * q > 1e-8) && (q > 1e-5))
135 u = pow(p * g * a, 1 / a);
136 BOOST_MATH_INSTRUMENT_VARIABLE(u);
140 u = exp((-q / a) - constants::euler<T>());
141 BOOST_MATH_INSTRUMENT_VARIABLE(u);
143 result = u / (1 - (u / (a + 1)));
144 BOOST_MATH_INSTRUMENT_VARIABLE(result);
146 else if((a < 0.3) && (b >= 0.35))
148 // DiDonato & Morris Eq 22:
149 T t = exp(-constants::euler<T>() - b);
152 BOOST_MATH_INSTRUMENT_VARIABLE(result);
154 else if((b > 0.15) || (a >= 0.3))
156 // DiDonato & Morris Eq 23:
158 T u = y - (1 - a) * log(y);
159 result = y - (1 - a) * log(u) - log(1 + (1 - a) / (1 + u));
160 BOOST_MATH_INSTRUMENT_VARIABLE(result);
164 // DiDonato & Morris Eq 24:
166 T u = y - (1 - a) * log(y);
167 result = y - (1 - a) * log(u) - log((u * u + 2 * (3 - a) * u + (2 - a) * (3 - a)) / (u * u + (5 - a) * u + 2));
168 BOOST_MATH_INSTRUMENT_VARIABLE(result);
172 // DiDonato & Morris Eq 25:
174 T c1 = (a - 1) * log(y);
177 T c1_4 = c1_2 * c1_2;
181 T c2 = (a - 1) * (1 + c1);
182 T c3 = (a - 1) * (-(c1_2 / 2) + (a - 2) * c1 + (3 * a - 5) / 2);
183 T c4 = (a - 1) * ((c1_3 / 3) - (3 * a - 5) * c1_2 / 2 + (a_2 - 6 * a + 7) * c1 + (11 * a_2 - 46 * a + 47) / 6);
184 T c5 = (a - 1) * (-(c1_4 / 4)
185 + (11 * a - 17) * c1_3 / 6
186 + (-3 * a_2 + 13 * a -13) * c1_2
187 + (2 * a_3 - 25 * a_2 + 72 * a - 61) * c1 / 2
188 + (25 * a_3 - 195 * a_2 + 477 * a - 379) / 12);
193 result = y + c1 + (c2 / y) + (c3 / y_2) + (c4 / y_3) + (c5 / y_4);
194 BOOST_MATH_INSTRUMENT_VARIABLE(result);
196 *p_has_10_digits = true;
201 // DiDonato and Morris Eq 31:
202 T s = find_inverse_s(p, q);
204 BOOST_MATH_INSTRUMENT_VARIABLE(s);
212 BOOST_MATH_INSTRUMENT_VARIABLE(ra);
214 T w = a + s * ra + (s * s -1) / 3;
215 w += (s_3 - 7 * s) / (36 * ra);
216 w -= (3 * s_4 + 7 * s_2 - 16) / (810 * a);
217 w += (9 * s_5 + 256 * s_3 - 433 * s) / (38880 * a * ra);
219 BOOST_MATH_INSTRUMENT_VARIABLE(w);
221 if((a >= 500) && (fabs(1 - w / a) < 1e-6))
224 *p_has_10_digits = true;
225 BOOST_MATH_INSTRUMENT_VARIABLE(result);
232 BOOST_MATH_INSTRUMENT_VARIABLE(result);
236 T D = (std::max)(T(2), T(a * (a - 1)));
237 T lg = boost::math::lgamma(a, pol);
241 // DiDonato and Morris Eq 25:
243 T c1 = (a - 1) * log(y);
246 T c1_4 = c1_2 * c1_2;
250 T c2 = (a - 1) * (1 + c1);
251 T c3 = (a - 1) * (-(c1_2 / 2) + (a - 2) * c1 + (3 * a - 5) / 2);
252 T c4 = (a - 1) * ((c1_3 / 3) - (3 * a - 5) * c1_2 / 2 + (a_2 - 6 * a + 7) * c1 + (11 * a_2 - 46 * a + 47) / 6);
253 T c5 = (a - 1) * (-(c1_4 / 4)
254 + (11 * a - 17) * c1_3 / 6
255 + (-3 * a_2 + 13 * a -13) * c1_2
256 + (2 * a_3 - 25 * a_2 + 72 * a - 61) * c1 / 2
257 + (25 * a_3 - 195 * a_2 + 477 * a - 379) / 12);
262 result = y + c1 + (c2 / y) + (c3 / y_2) + (c4 / y_3) + (c5 / y_4);
263 BOOST_MATH_INSTRUMENT_VARIABLE(result);
267 // DiDonato and Morris Eq 33:
268 T u = -lb + (a - 1) * log(w) - log(1 + (1 - a) / (1 + w));
269 result = -lb + (a - 1) * log(u) - log(1 + (1 - a) / (1 + u));
270 BOOST_MATH_INSTRUMENT_VARIABLE(result);
281 // DiDonato and Morris Eq 35:
282 T v = log(p) + boost::math::lgamma(ap1, pol);
283 z = exp((v + w) / a);
284 s = boost::math::log1p(z / ap1 * (1 + z / ap2));
285 z = exp((v + z - s) / a);
286 s = boost::math::log1p(z / ap1 * (1 + z / ap2));
287 z = exp((v + z - s) / a);
288 s = boost::math::log1p(z / ap1 * (1 + z / ap2 * (1 + z / (a + 3))));
289 z = exp((v + z - s) / a);
290 BOOST_MATH_INSTRUMENT_VARIABLE(z);
293 if((z <= 0.01 * ap1) || (z > 0.7 * ap1))
297 *p_has_10_digits = true;
298 BOOST_MATH_INSTRUMENT_VARIABLE(result);
302 // DiDonato and Morris Eq 36:
303 T ls = log(didonato_SN(a, z, 100, T(1e-4)));
304 T v = log(p) + boost::math::lgamma(ap1, pol);
305 z = exp((v + z - ls) / a);
306 result = z * (1 - (a * log(z) - z - v + ls) / (a - z));
308 BOOST_MATH_INSTRUMENT_VARIABLE(result);
315 template <class T, class Policy>
316 struct gamma_p_inverse_func
318 gamma_p_inverse_func(T a_, T p_, bool inv) : a(a_), p(p_), invert(inv)
321 // If p is too near 1 then P(x) - p suffers from cancellation
322 // errors causing our root-finding algorithms to "thrash", better
323 // to invert in this case and calculate Q(x) - (1-p) instead.
325 // Of course if p is *very* close to 1, then the answer we get will
326 // be inaccurate anyway (because there's not enough information in p)
327 // but at least we will converge on the (inaccurate) answer quickly.
336 boost::math::tuple<T, T, T> operator()(const T& x)const
338 BOOST_FPU_EXCEPTION_GUARD
340 // Calculate P(x) - p and the first two derivates, or if the invert
341 // flag is set, then Q(x) - q and it's derivatives.
343 typedef typename policies::evaluation<T, Policy>::type value_type;
344 // typedef typename lanczos::lanczos<T, Policy>::type evaluation_type;
345 typedef typename policies::normalise<
347 policies::promote_float<false>,
348 policies::promote_double<false>,
349 policies::discrete_quantile<>,
350 policies::assert_undefined<> >::type forwarding_policy;
352 BOOST_MATH_STD_USING // For ADL of std functions.
356 f = static_cast<T>(boost::math::detail::gamma_incomplete_imp(
357 static_cast<value_type>(a),
358 static_cast<value_type>(x),
360 forwarding_policy(), &ft));
361 f1 = static_cast<T>(ft);
363 T div = (a - x - 1) / x;
365 if((fabs(div) > 1) && (tools::max_value<T>() / fabs(div) < f2))
368 f2 = -tools::max_value<T>() / 2;
381 return boost::math::make_tuple(static_cast<T>(f - p), f1, f2);
388 template <class T, class Policy>
389 T gamma_p_inv_imp(T a, T p, const Policy& pol)
391 BOOST_MATH_STD_USING // ADL of std functions.
393 static const char* function = "boost::math::gamma_p_inv<%1%>(%1%, %1%)";
395 BOOST_MATH_INSTRUMENT_VARIABLE(a);
396 BOOST_MATH_INSTRUMENT_VARIABLE(p);
399 policies::raise_domain_error<T>(function, "Argument a in the incomplete gamma function inverse must be >= 0 (got a=%1%).", a, pol);
400 if((p < 0) || (p > 1))
401 policies::raise_domain_error<T>(function, "Probabilty must be in the range [0,1] in the incomplete gamma function inverse (got p=%1%).", p, pol);
403 return tools::max_value<T>();
407 T guess = detail::find_inverse_gamma<T>(a, p, 1 - p, pol, &has_10_digits);
408 if((policies::digits<T, Policy>() <= 36) && has_10_digits)
410 T lower = tools::min_value<T>();
412 guess = tools::min_value<T>();
413 BOOST_MATH_INSTRUMENT_VARIABLE(guess);
415 // Work out how many digits to converge to, normally this is
416 // 2/3 of the digits in T, but if the first derivative is very
417 // large convergence is slow, so we'll bump it up to full
418 // precision to prevent premature termination of the root-finding routine.
420 unsigned digits = policies::digits<T, Policy>();
431 if((a < 0.125) && (fabs(gamma_p_derivative(a, guess, pol)) > 1 / sqrt(tools::epsilon<T>())))
432 digits = policies::digits<T, Policy>() - 2;
434 // Go ahead and iterate:
436 boost::uintmax_t max_iter = policies::get_max_root_iterations<Policy>();
437 guess = tools::halley_iterate(
438 detail::gamma_p_inverse_func<T, Policy>(a, p, false),
441 tools::max_value<T>(),
444 policies::check_root_iterations<T>(function, max_iter, pol);
445 BOOST_MATH_INSTRUMENT_VARIABLE(guess);
447 guess = policies::raise_underflow_error<T>(function, "Expected result known to be non-zero, but is smaller than the smallest available number.", pol);
451 template <class T, class Policy>
452 T gamma_q_inv_imp(T a, T q, const Policy& pol)
454 BOOST_MATH_STD_USING // ADL of std functions.
456 static const char* function = "boost::math::gamma_q_inv<%1%>(%1%, %1%)";
459 policies::raise_domain_error<T>(function, "Argument a in the incomplete gamma function inverse must be >= 0 (got a=%1%).", a, pol);
460 if((q < 0) || (q > 1))
461 policies::raise_domain_error<T>(function, "Probabilty must be in the range [0,1] in the incomplete gamma function inverse (got q=%1%).", q, pol);
463 return tools::max_value<T>();
467 T guess = detail::find_inverse_gamma<T>(a, 1 - q, q, pol, &has_10_digits);
468 if((policies::digits<T, Policy>() <= 36) && has_10_digits)
470 T lower = tools::min_value<T>();
472 guess = tools::min_value<T>();
474 // Work out how many digits to converge to, normally this is
475 // 2/3 of the digits in T, but if the first derivative is very
476 // large convergence is slow, so we'll bump it up to full
477 // precision to prevent premature termination of the root-finding routine.
479 unsigned digits = policies::digits<T, Policy>();
490 if((a < 0.125) && (fabs(gamma_p_derivative(a, guess, pol)) > 1 / sqrt(tools::epsilon<T>())))
491 digits = policies::digits<T, Policy>();
493 // Go ahead and iterate:
495 boost::uintmax_t max_iter = policies::get_max_root_iterations<Policy>();
496 guess = tools::halley_iterate(
497 detail::gamma_p_inverse_func<T, Policy>(a, q, true),
500 tools::max_value<T>(),
503 policies::check_root_iterations<T>(function, max_iter, pol);
505 guess = policies::raise_underflow_error<T>(function, "Expected result known to be non-zero, but is smaller than the smallest available number.", pol);
509 } // namespace detail
511 template <class T1, class T2, class Policy>
512 inline typename tools::promote_args<T1, T2>::type
513 gamma_p_inv(T1 a, T2 p, const Policy& pol)
515 typedef typename tools::promote_args<T1, T2>::type result_type;
516 return detail::gamma_p_inv_imp(
517 static_cast<result_type>(a),
518 static_cast<result_type>(p), pol);
521 template <class T1, class T2, class Policy>
522 inline typename tools::promote_args<T1, T2>::type
523 gamma_q_inv(T1 a, T2 p, const Policy& pol)
525 typedef typename tools::promote_args<T1, T2>::type result_type;
526 return detail::gamma_q_inv_imp(
527 static_cast<result_type>(a),
528 static_cast<result_type>(p), pol);
531 template <class T1, class T2>
532 inline typename tools::promote_args<T1, T2>::type
533 gamma_p_inv(T1 a, T2 p)
535 return gamma_p_inv(a, p, policies::policy<>());
538 template <class T1, class T2>
539 inline typename tools::promote_args<T1, T2>::type
540 gamma_q_inv(T1 a, T2 p)
542 return gamma_q_inv(a, p, policies::policy<>());
548 #endif // BOOST_MATH_SPECIAL_FUNCTIONS_IGAMMA_INVERSE_HPP