gamma_inva.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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)
  5. //
  6. // This is not a complete header file, it is included by gamma.hpp
  7. // after it has defined it's definitions. This inverts the incomplete
  8. // gamma functions P and Q on the first parameter "a" using a generic
  9. // root finding algorithm (TOMS Algorithm 748).
  10. //
  11. #ifndef BOOST_MATH_SP_DETAIL_GAMMA_INVA
  12. #define BOOST_MATH_SP_DETAIL_GAMMA_INVA
  13. #ifdef _MSC_VER
  14. #pragma once
  15. #endif
  16. #include <boost/math/tools/toms748_solve.hpp>
  17. #include <boost/cstdint.hpp>
  18. namespace boost{ namespace math{ namespace detail{
  19. template <class T, class Policy>
  20. struct gamma_inva_t
  21. {
  22. gamma_inva_t(T z_, T p_, bool invert_) : z(z_), p(p_), invert(invert_) {}
  23. T operator()(T a)
  24. {
  25. return invert ? p - boost::math::gamma_q(a, z, Policy()) : boost::math::gamma_p(a, z, Policy()) - p;
  26. }
  27. private:
  28. T z, p;
  29. bool invert;
  30. };
  31. template <class T, class Policy>
  32. T inverse_poisson_cornish_fisher(T lambda, T p, T q, const Policy& pol)
  33. {
  34. BOOST_MATH_STD_USING
  35. // mean:
  36. T m = lambda;
  37. // standard deviation:
  38. T sigma = sqrt(lambda);
  39. // skewness
  40. T sk = 1 / sigma;
  41. // kurtosis:
  42. // T k = 1/lambda;
  43. // Get the inverse of a std normal distribution:
  44. T x = boost::math::erfc_inv(p > q ? 2 * q : 2 * p, pol) * constants::root_two<T>();
  45. // Set the sign:
  46. if(p < 0.5)
  47. x = -x;
  48. T x2 = x * x;
  49. // w is correction term due to skewness
  50. T w = x + sk * (x2 - 1) / 6;
  51. /*
  52. // Add on correction due to kurtosis.
  53. // Disabled for now, seems to make things worse?
  54. //
  55. if(lambda >= 10)
  56. w += k * x * (x2 - 3) / 24 + sk * sk * x * (2 * x2 - 5) / -36;
  57. */
  58. w = m + sigma * w;
  59. return w > tools::min_value<T>() ? w : tools::min_value<T>();
  60. }
  61. template <class T, class Policy>
  62. T gamma_inva_imp(const T& z, const T& p, const T& q, const Policy& pol)
  63. {
  64. BOOST_MATH_STD_USING // for ADL of std lib math functions
  65. //
  66. // Special cases first:
  67. //
  68. if(p == 0)
  69. {
  70. return policies::raise_overflow_error<T>("boost::math::gamma_p_inva<%1%>(%1%, %1%)", 0, Policy());
  71. }
  72. if(q == 0)
  73. {
  74. return tools::min_value<T>();
  75. }
  76. //
  77. // Function object, this is the functor whose root
  78. // we have to solve:
  79. //
  80. gamma_inva_t<T, Policy> f(z, (p < q) ? p : q, (p < q) ? false : true);
  81. //
  82. // Tolerance: full precision.
  83. //
  84. tools::eps_tolerance<T> tol(policies::digits<T, Policy>());
  85. //
  86. // Now figure out a starting guess for what a may be,
  87. // we'll start out with a value that'll put p or q
  88. // right bang in the middle of their range, the functions
  89. // are quite sensitive so we should need too many steps
  90. // to bracket the root from there:
  91. //
  92. T guess;
  93. T factor = 8;
  94. if(z >= 1)
  95. {
  96. //
  97. // We can use the relationship between the incomplete
  98. // gamma function and the poisson distribution to
  99. // calculate an approximate inverse, for large z
  100. // this is actually pretty accurate, but it fails badly
  101. // when z is very small. Also set our step-factor according
  102. // to how accurate we think the result is likely to be:
  103. //
  104. guess = 1 + inverse_poisson_cornish_fisher(z, q, p, pol);
  105. if(z > 5)
  106. {
  107. if(z > 1000)
  108. factor = 1.01f;
  109. else if(z > 50)
  110. factor = 1.1f;
  111. else if(guess > 10)
  112. factor = 1.25f;
  113. else
  114. factor = 2;
  115. if(guess < 1.1)
  116. factor = 8;
  117. }
  118. }
  119. else if(z > 0.5)
  120. {
  121. guess = z * 1.2f;
  122. }
  123. else
  124. {
  125. guess = -0.4f / log(z);
  126. }
  127. //
  128. // Max iterations permitted:
  129. //
  130. boost::uintmax_t max_iter = policies::get_max_root_iterations<Policy>();
  131. //
  132. // Use our generic derivative-free root finding procedure.
  133. // We could use Newton steps here, taking the PDF of the
  134. // Poisson distribution as our derivative, but that's
  135. // even worse performance-wise than the generic method :-(
  136. //
  137. std::pair<T, T> r = bracket_and_solve_root(f, guess, factor, false, tol, max_iter, pol);
  138. if(max_iter >= policies::get_max_root_iterations<Policy>())
  139. return policies::raise_evaluation_error<T>("boost::math::gamma_p_inva<%1%>(%1%, %1%)", "Unable to locate the root within a reasonable number of iterations, closest approximation so far was %1%", r.first, pol);
  140. return (r.first + r.second) / 2;
  141. }
  142. } // namespace detail
  143. template <class T1, class T2, class Policy>
  144. inline typename tools::promote_args<T1, T2>::type
  145. gamma_p_inva(T1 x, T2 p, const Policy& pol)
  146. {
  147. typedef typename tools::promote_args<T1, T2>::type result_type;
  148. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  149. typedef typename policies::normalise<
  150. Policy,
  151. policies::promote_float<false>,
  152. policies::promote_double<false>,
  153. policies::discrete_quantile<>,
  154. policies::assert_undefined<> >::type forwarding_policy;
  155. if(p == 0)
  156. {
  157. policies::raise_overflow_error<result_type>("boost::math::gamma_p_inva<%1%>(%1%, %1%)", 0, Policy());
  158. }
  159. if(p == 1)
  160. {
  161. return tools::min_value<result_type>();
  162. }
  163. return policies::checked_narrowing_cast<result_type, forwarding_policy>(
  164. detail::gamma_inva_imp(
  165. static_cast<value_type>(x),
  166. static_cast<value_type>(p),
  167. static_cast<value_type>(1 - static_cast<value_type>(p)),
  168. pol), "boost::math::gamma_p_inva<%1%>(%1%, %1%)");
  169. }
  170. template <class T1, class T2, class Policy>
  171. inline typename tools::promote_args<T1, T2>::type
  172. gamma_q_inva(T1 x, T2 q, const Policy& pol)
  173. {
  174. typedef typename tools::promote_args<T1, T2>::type result_type;
  175. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  176. typedef typename policies::normalise<
  177. Policy,
  178. policies::promote_float<false>,
  179. policies::promote_double<false>,
  180. policies::discrete_quantile<>,
  181. policies::assert_undefined<> >::type forwarding_policy;
  182. if(q == 1)
  183. {
  184. policies::raise_overflow_error<result_type>("boost::math::gamma_q_inva<%1%>(%1%, %1%)", 0, Policy());
  185. }
  186. if(q == 0)
  187. {
  188. return tools::min_value<result_type>();
  189. }
  190. return policies::checked_narrowing_cast<result_type, forwarding_policy>(
  191. detail::gamma_inva_imp(
  192. static_cast<value_type>(x),
  193. static_cast<value_type>(1 - static_cast<value_type>(q)),
  194. static_cast<value_type>(q),
  195. pol), "boost::math::gamma_q_inva<%1%>(%1%, %1%)");
  196. }
  197. template <class T1, class T2>
  198. inline typename tools::promote_args<T1, T2>::type
  199. gamma_p_inva(T1 x, T2 p)
  200. {
  201. return boost::math::gamma_p_inva(x, p, policies::policy<>());
  202. }
  203. template <class T1, class T2>
  204. inline typename tools::promote_args<T1, T2>::type
  205. gamma_q_inva(T1 x, T2 q)
  206. {
  207. return boost::math::gamma_q_inva(x, q, policies::policy<>());
  208. }
  209. } // namespace math
  210. } // namespace boost
  211. #endif // BOOST_MATH_SP_DETAIL_GAMMA_INVA