generic_quantile.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright John Maddock 2008.
  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. #ifndef BOOST_MATH_DISTIBUTIONS_DETAIL_GENERIC_QUANTILE_HPP
  6. #define BOOST_MATH_DISTIBUTIONS_DETAIL_GENERIC_QUANTILE_HPP
  7. namespace boost{ namespace math{ namespace detail{
  8. template <class Dist>
  9. struct generic_quantile_finder
  10. {
  11. using value_type = typename Dist::value_type;
  12. using policy_type = typename Dist::policy_type;
  13. generic_quantile_finder(const Dist& d, value_type t, bool c)
  14. : dist(d), target(t), comp(c) {}
  15. value_type operator()(const value_type& x)
  16. {
  17. return comp ?
  18. value_type(target - cdf(complement(dist, x)))
  19. : value_type(cdf(dist, x) - target);
  20. }
  21. private:
  22. Dist dist;
  23. value_type target;
  24. bool comp;
  25. };
  26. template <class T, class Policy>
  27. inline T check_range_result(const T& x, const Policy& pol, const char* function)
  28. {
  29. if((x >= 0) && (x < tools::min_value<T>()))
  30. {
  31. return policies::raise_underflow_error<T>(function, nullptr, pol);
  32. }
  33. if(x <= -tools::max_value<T>())
  34. {
  35. return -policies::raise_overflow_error<T>(function, nullptr, pol);
  36. }
  37. if(x >= tools::max_value<T>())
  38. {
  39. return policies::raise_overflow_error<T>(function, nullptr, pol);
  40. }
  41. return x;
  42. }
  43. template <class Dist>
  44. typename Dist::value_type generic_quantile(const Dist& dist, const typename Dist::value_type& p, const typename Dist::value_type& guess, bool comp, const char* function)
  45. {
  46. using value_type = typename Dist::value_type;
  47. using policy_type = typename Dist::policy_type;
  48. using forwarding_policy = typename policies::normalise<
  49. policy_type,
  50. policies::promote_float<false>,
  51. policies::promote_double<false>,
  52. policies::discrete_quantile<>,
  53. policies::assert_undefined<> >::type;
  54. //
  55. // Special cases first:
  56. //
  57. if(p == 0)
  58. {
  59. return comp
  60. ? check_range_result(range(dist).second, forwarding_policy(), function)
  61. : check_range_result(range(dist).first, forwarding_policy(), function);
  62. }
  63. if(p == 1)
  64. {
  65. return !comp
  66. ? check_range_result(range(dist).second, forwarding_policy(), function)
  67. : check_range_result(range(dist).first, forwarding_policy(), function);
  68. }
  69. generic_quantile_finder<Dist> f(dist, p, comp);
  70. tools::eps_tolerance<value_type> tol(policies::digits<value_type, forwarding_policy>() - 3);
  71. std::uintmax_t max_iter = policies::get_max_root_iterations<forwarding_policy>();
  72. std::pair<value_type, value_type> ir = tools::bracket_and_solve_root(
  73. f, guess, value_type(2), true, tol, max_iter, forwarding_policy());
  74. value_type result = ir.first + (ir.second - ir.first) / 2;
  75. if(max_iter >= policies::get_max_root_iterations<forwarding_policy>())
  76. {
  77. return policies::raise_evaluation_error<value_type>(function, "Unable to locate solution in a reasonable time:" // LCOV_EXCL_LINE
  78. " either there is no answer to quantile or the answer is infinite. Current best guess is %1%", result, forwarding_policy()); // LCOV_EXCL_LINE
  79. }
  80. return result;
  81. }
  82. }}} // namespaces
  83. #endif // BOOST_MATH_DISTIBUTIONS_DETAIL_GENERIC_QUANTILE_HPP