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)
7 // This is not a complete header file, it is included by gamma.hpp
8 // after it has defined it's definitions. This inverts the incomplete
9 // gamma functions P and Q on the first parameter "a" using a generic
10 // root finding algorithm (TOMS Algorithm 748).
13 #ifndef BOOST_MATH_SP_DETAIL_GAMMA_INVA
14 #define BOOST_MATH_SP_DETAIL_GAMMA_INVA
20 #include <boost/math/tools/toms748_solve.hpp>
21 #include <boost/cstdint.hpp>
23 namespace boost{ namespace math{ namespace detail{
25 template <class T, class Policy>
28 gamma_inva_t(T z_, T p_, bool invert_) : z(z_), p(p_), invert(invert_) {}
31 return invert ? p - boost::math::gamma_q(a, z, Policy()) : boost::math::gamma_p(a, z, Policy()) - p;
38 template <class T, class Policy>
39 T inverse_poisson_cornish_fisher(T lambda, T p, T q, const Policy& pol)
44 // standard deviation:
45 T sigma = sqrt(lambda);
50 // Get the inverse of a std normal distribution:
51 T x = boost::math::erfc_inv(p > q ? 2 * q : 2 * p, pol) * constants::root_two<T>();
56 // w is correction term due to skewness
57 T w = x + sk * (x2 - 1) / 6;
59 // Add on correction due to kurtosis.
60 // Disabled for now, seems to make things worse?
63 w += k * x * (x2 - 3) / 24 + sk * sk * x * (2 * x2 - 5) / -36;
66 return w > tools::min_value<T>() ? w : tools::min_value<T>();
69 template <class T, class Policy>
70 T gamma_inva_imp(const T& z, const T& p, const T& q, const Policy& pol)
72 BOOST_MATH_STD_USING // for ADL of std lib math functions
74 // Special cases first:
78 return tools::max_value<T>();
82 return tools::min_value<T>();
85 // Function object, this is the functor whose root
88 gamma_inva_t<T, Policy> f(z, (p < q) ? p : q, (p < q) ? false : true);
90 // Tolerance: full precision.
92 tools::eps_tolerance<T> tol(policies::digits<T, Policy>());
94 // Now figure out a starting guess for what a may be,
95 // we'll start out with a value that'll put p or q
96 // right bang in the middle of their range, the functions
97 // are quite sensitive so we should need too many steps
98 // to bracket the root from there:
105 // We can use the relationship between the incomplete
106 // gamma function and the poisson distribution to
107 // calculate an approximate inverse, for large z
108 // this is actually pretty accurate, but it fails badly
109 // when z is very small. Also set our step-factor according
110 // to how accurate we think the result is likely to be:
112 guess = 1 + inverse_poisson_cornish_fisher(z, q, p, pol);
133 guess = -0.4f / log(z);
136 // Max iterations permitted:
138 boost::uintmax_t max_iter = policies::get_max_root_iterations<Policy>();
140 // Use our generic derivative-free root finding procedure.
141 // We could use Newton steps here, taking the PDF of the
142 // Poisson distribution as our derivative, but that's
143 // even worse performance-wise than the generic method :-(
145 std::pair<T, T> r = bracket_and_solve_root(f, guess, factor, false, tol, max_iter, pol);
146 if(max_iter >= policies::get_max_root_iterations<Policy>())
147 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);
148 return (r.first + r.second) / 2;
151 } // namespace detail
153 template <class T1, class T2, class Policy>
154 inline typename tools::promote_args<T1, T2>::type
155 gamma_p_inva(T1 x, T2 p, const Policy& pol)
157 typedef typename tools::promote_args<T1, T2>::type result_type;
158 typedef typename policies::evaluation<result_type, Policy>::type value_type;
159 typedef typename policies::normalise<
161 policies::promote_float<false>,
162 policies::promote_double<false>,
163 policies::discrete_quantile<>,
164 policies::assert_undefined<> >::type forwarding_policy;
168 return tools::max_value<result_type>();
172 return tools::min_value<result_type>();
175 return policies::checked_narrowing_cast<result_type, forwarding_policy>(
176 detail::gamma_inva_imp(
177 static_cast<value_type>(x),
178 static_cast<value_type>(p),
179 static_cast<value_type>(1 - static_cast<value_type>(p)),
180 pol), "boost::math::gamma_p_inva<%1%>(%1%, %1%)");
183 template <class T1, class T2, class Policy>
184 inline typename tools::promote_args<T1, T2>::type
185 gamma_q_inva(T1 x, T2 q, const Policy& pol)
187 typedef typename tools::promote_args<T1, T2>::type result_type;
188 typedef typename policies::evaluation<result_type, Policy>::type value_type;
189 typedef typename policies::normalise<
191 policies::promote_float<false>,
192 policies::promote_double<false>,
193 policies::discrete_quantile<>,
194 policies::assert_undefined<> >::type forwarding_policy;
198 return tools::max_value<result_type>();
202 return tools::min_value<result_type>();
205 return policies::checked_narrowing_cast<result_type, forwarding_policy>(
206 detail::gamma_inva_imp(
207 static_cast<value_type>(x),
208 static_cast<value_type>(1 - static_cast<value_type>(q)),
209 static_cast<value_type>(q),
210 pol), "boost::math::gamma_q_inva<%1%>(%1%, %1%)");
213 template <class T1, class T2>
214 inline typename tools::promote_args<T1, T2>::type
215 gamma_p_inva(T1 x, T2 p)
217 return boost::math::gamma_p_inva(x, p, policies::policy<>());
220 template <class T1, class T2>
221 inline typename tools::promote_args<T1, T2>::type
222 gamma_q_inva(T1 x, T2 q)
224 return boost::math::gamma_q_inva(x, q, policies::policy<>());
230 #endif // BOOST_MATH_SP_DETAIL_GAMMA_INVA