1 // Copyright 2008 John Maddock
3 // Use, modification and distribution are subject to the
4 // Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt
6 // or copy at http://www.boost.org/LICENSE_1_0.txt)
8 #ifndef BOOST_MATH_DISTRIBUTIONS_DETAIL_HG_QUANTILE_HPP
9 #define BOOST_MATH_DISTRIBUTIONS_DETAIL_HG_QUANTILE_HPP
11 #include <boost/math/policies/error_handling.hpp>
12 #include <boost/math/distributions/detail/hypergeometric_pdf.hpp>
14 namespace boost{ namespace math{ namespace detail{
17 inline unsigned round_x_from_p(unsigned x, T p, T cum, T fudge_factor, unsigned lbound, unsigned /*ubound*/, const policies::discrete_quantile<policies::integer_round_down>&)
19 if((p < cum * fudge_factor) && (x != lbound))
21 BOOST_MATH_INSTRUMENT_VARIABLE(x-1);
28 inline unsigned round_x_from_p(unsigned x, T p, T cum, T fudge_factor, unsigned /*lbound*/, unsigned ubound, const policies::discrete_quantile<policies::integer_round_up>&)
30 if((cum < p * fudge_factor) && (x != ubound))
32 BOOST_MATH_INSTRUMENT_VARIABLE(x+1);
39 inline unsigned round_x_from_p(unsigned x, T p, T cum, T fudge_factor, unsigned lbound, unsigned ubound, const policies::discrete_quantile<policies::integer_round_inwards>&)
42 return round_x_from_p(x, p, cum, fudge_factor, lbound, ubound, policies::discrete_quantile<policies::integer_round_down>());
43 return round_x_from_p(x, p, cum, fudge_factor, lbound, ubound, policies::discrete_quantile<policies::integer_round_up>());
47 inline unsigned round_x_from_p(unsigned x, T p, T cum, T fudge_factor, unsigned lbound, unsigned ubound, const policies::discrete_quantile<policies::integer_round_outwards>&)
50 return round_x_from_p(x, p, cum, fudge_factor, lbound, ubound, policies::discrete_quantile<policies::integer_round_up>());
51 return round_x_from_p(x, p, cum, fudge_factor, lbound, ubound, policies::discrete_quantile<policies::integer_round_down>());
55 inline unsigned round_x_from_p(unsigned x, T /*p*/, T /*cum*/, T /*fudge_factor*/, unsigned /*lbound*/, unsigned /*ubound*/, const policies::discrete_quantile<policies::integer_round_nearest>&)
61 inline unsigned round_x_from_q(unsigned x, T q, T cum, T fudge_factor, unsigned lbound, unsigned /*ubound*/, const policies::discrete_quantile<policies::integer_round_down>&)
63 if((q * fudge_factor > cum) && (x != lbound))
65 BOOST_MATH_INSTRUMENT_VARIABLE(x-1);
72 inline unsigned round_x_from_q(unsigned x, T q, T cum, T fudge_factor, unsigned /*lbound*/, unsigned ubound, const policies::discrete_quantile<policies::integer_round_up>&)
74 if((q < cum * fudge_factor) && (x != ubound))
76 BOOST_MATH_INSTRUMENT_VARIABLE(x+1);
83 inline unsigned round_x_from_q(unsigned x, T q, T cum, T fudge_factor, unsigned lbound, unsigned ubound, const policies::discrete_quantile<policies::integer_round_inwards>&)
86 return round_x_from_q(x, q, cum, fudge_factor, lbound, ubound, policies::discrete_quantile<policies::integer_round_down>());
87 return round_x_from_q(x, q, cum, fudge_factor, lbound, ubound, policies::discrete_quantile<policies::integer_round_up>());
91 inline unsigned round_x_from_q(unsigned x, T q, T cum, T fudge_factor, unsigned lbound, unsigned ubound, const policies::discrete_quantile<policies::integer_round_outwards>&)
94 return round_x_from_q(x, q, cum, fudge_factor, lbound, ubound, policies::discrete_quantile<policies::integer_round_down>());
95 return round_x_from_q(x, q, cum, fudge_factor, lbound, ubound, policies::discrete_quantile<policies::integer_round_up>());
99 inline unsigned round_x_from_q(unsigned x, T /*q*/, T /*cum*/, T /*fudge_factor*/, unsigned /*lbound*/, unsigned /*ubound*/, const policies::discrete_quantile<policies::integer_round_nearest>&)
104 template <class T, class Policy>
105 unsigned hypergeometric_quantile_imp(T p, T q, unsigned r, unsigned n, unsigned N, const Policy& pol)
108 # pragma warning(push)
109 # pragma warning(disable:4267)
111 typedef typename Policy::discrete_quantile_type discrete_quantile_type;
113 BOOST_FPU_EXCEPTION_GUARD
115 T fudge_factor = 1 + tools::epsilon<T>() * ((N <= boost::math::prime(boost::math::max_prime - 1)) ? 50 : 2 * N);
116 unsigned base = static_cast<unsigned>((std::max)(0, (int)(n + r) - (int)(N)));
117 unsigned lim = (std::min)(r, n);
119 BOOST_MATH_INSTRUMENT_VARIABLE(p);
120 BOOST_MATH_INSTRUMENT_VARIABLE(q);
121 BOOST_MATH_INSTRUMENT_VARIABLE(r);
122 BOOST_MATH_INSTRUMENT_VARIABLE(n);
123 BOOST_MATH_INSTRUMENT_VARIABLE(N);
124 BOOST_MATH_INSTRUMENT_VARIABLE(fudge_factor);
125 BOOST_MATH_INSTRUMENT_VARIABLE(base);
126 BOOST_MATH_INSTRUMENT_VARIABLE(lim);
131 result = hypergeometric_pdf<T>(x, r, n, N, pol);
136 // We want to skip through x values as fast as we can until we start getting non-zero values,
137 // otherwise we're just making lots of expensive PDF calls:
138 T log_pdf = boost::math::lgamma(static_cast<T>(n + 1), pol)
139 + boost::math::lgamma(static_cast<T>(r + 1), pol)
140 + boost::math::lgamma(static_cast<T>(N - n + 1), pol)
141 + boost::math::lgamma(static_cast<T>(N - r + 1), pol)
142 - boost::math::lgamma(static_cast<T>(N + 1), pol)
143 - boost::math::lgamma(static_cast<T>(x + 1), pol)
144 - boost::math::lgamma(static_cast<T>(n - x + 1), pol)
145 - boost::math::lgamma(static_cast<T>(r - x + 1), pol)
146 - boost::math::lgamma(static_cast<T>(N - n - r + x + 1), pol);
147 while (log_pdf < tools::log_min_value<T>())
149 log_pdf += -log(static_cast<T>(x + 1)) + log(static_cast<T>(n - x)) + log(static_cast<T>(r - x)) - log(static_cast<T>(N - n - r + x + 1));
152 // By the time we get here, log_pdf may be fairly inaccurate due to
153 // roundoff errors, get a fresh PDF calculation before proceding:
154 diff = hypergeometric_pdf<T>(x, r, n, N, pol);
158 diff = (diff > tools::min_value<T>() * 8)
159 ? T(n - x) * T(r - x) * diff / (T(x + 1) * T(N + x + 1 - n - r))
160 : hypergeometric_pdf<T>(x + 1, r, n, N, pol);
161 if(result + diff / 2 > p)
165 #ifdef BOOST_MATH_INSTRUMENT
168 BOOST_MATH_INSTRUMENT_VARIABLE(x);
169 BOOST_MATH_INSTRUMENT_VARIABLE(diff);
170 BOOST_MATH_INSTRUMENT_VARIABLE(result);
174 return round_x_from_p(x, p, result, fudge_factor, base, lim, discrete_quantile_type());
180 T diff = hypergeometric_pdf<T>(x, r, n, N, pol);
183 // We want to skip through x values as fast as we can until we start getting non-zero values,
184 // otherwise we're just making lots of expensive PDF calls:
186 T log_pdf = boost::math::lgamma(static_cast<T>(n + 1), pol)
187 + boost::math::lgamma(static_cast<T>(r + 1), pol)
188 + boost::math::lgamma(static_cast<T>(N - n + 1), pol)
189 + boost::math::lgamma(static_cast<T>(N - r + 1), pol)
190 - boost::math::lgamma(static_cast<T>(N + 1), pol)
191 - boost::math::lgamma(static_cast<T>(x + 1), pol)
192 - boost::math::lgamma(static_cast<T>(n - x + 1), pol)
193 - boost::math::lgamma(static_cast<T>(r - x + 1), pol)
194 - boost::math::lgamma(static_cast<T>(N - n - r + x + 1), pol);
195 while (log_pdf < tools::log_min_value<T>())
197 log_pdf += log(static_cast<T>(x)) - log(static_cast<T>(n - x + 1)) - log(static_cast<T>(r - x + 1)) + log(static_cast<T>(N - n - r + x));
200 // By the time we get here, log_pdf may be fairly inaccurate due to
201 // roundoff errors, get a fresh PDF calculation before proceding:
202 diff = hypergeometric_pdf<T>(x, r, n, N, pol);
204 while(result + diff / 2 < q)
207 diff = (diff > tools::min_value<T>() * 8)
208 ? x * T(N + x - n - r) * diff / (T(1 + n - x) * T(1 + r - x))
209 : hypergeometric_pdf<T>(x - 1, r, n, N, pol);
211 #ifdef BOOST_MATH_INSTRUMENT
214 BOOST_MATH_INSTRUMENT_VARIABLE(x);
215 BOOST_MATH_INSTRUMENT_VARIABLE(diff);
216 BOOST_MATH_INSTRUMENT_VARIABLE(result);
220 return round_x_from_q(x, q, result, fudge_factor, base, lim, discrete_quantile_type());
223 # pragma warning(pop)
227 template <class T, class Policy>
228 inline unsigned hypergeometric_quantile(T p, T q, unsigned r, unsigned n, unsigned N, const Policy&)
230 BOOST_FPU_EXCEPTION_GUARD
231 typedef typename tools::promote_args<T>::type result_type;
232 typedef typename policies::evaluation<result_type, Policy>::type value_type;
233 typedef typename policies::normalise<
235 policies::promote_float<false>,
236 policies::promote_double<false>,
237 policies::assert_undefined<> >::type forwarding_policy;
239 return detail::hypergeometric_quantile_imp<value_type>(p, q, r, n, N, forwarding_policy());