hypergeometric_cdf.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2008 John Maddock
  2. //
  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)
  7. #ifndef BOOST_MATH_DISTRIBUTIONS_DETAIL_HG_CDF_HPP
  8. #define BOOST_MATH_DISTRIBUTIONS_DETAIL_HG_CDF_HPP
  9. #include <boost/math/policies/error_handling.hpp>
  10. #include <boost/math/distributions/detail/hypergeometric_pdf.hpp>
  11. #include <cstdint>
  12. namespace boost{ namespace math{ namespace detail{
  13. template <class T, class Policy>
  14. T hypergeometric_cdf_imp(std::uint64_t x, std::uint64_t r, std::uint64_t n, std::uint64_t N, bool invert, const Policy& pol)
  15. {
  16. #ifdef _MSC_VER
  17. # pragma warning(push)
  18. # pragma warning(disable:4267)
  19. #endif
  20. BOOST_MATH_STD_USING
  21. T result = 0;
  22. T mode = floor(T(r + 1) * T(n + 1) / (N + 2));
  23. if(x < mode)
  24. {
  25. result = hypergeometric_pdf<T>(x, r, n, N, pol);
  26. T diff = result;
  27. const auto lower_limit = static_cast<std::uint64_t>((std::max)(INT64_C(0), static_cast<std::int64_t>(n + r) - static_cast<std::int64_t>(N)));
  28. while(diff > (invert ? T(1) : result) * tools::epsilon<T>())
  29. {
  30. diff = T(x) * T((N + x) - n - r) * diff / (T(1 + n - x) * T(1 + r - x));
  31. result += diff;
  32. BOOST_MATH_INSTRUMENT_VARIABLE(x);
  33. BOOST_MATH_INSTRUMENT_VARIABLE(diff);
  34. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  35. if(x == lower_limit)
  36. break;
  37. --x;
  38. }
  39. }
  40. else
  41. {
  42. invert = !invert;
  43. const auto upper_limit = (std::min)(r, n);
  44. if(x != upper_limit)
  45. {
  46. ++x;
  47. result = hypergeometric_pdf<T>(x, r, n, N, pol);
  48. T diff = result;
  49. while((x <= upper_limit) && (diff > (invert ? T(1) : result) * tools::epsilon<T>()))
  50. {
  51. diff = T(n - x) * T(r - x) * diff / (T(x + 1) * T((N + x + 1) - n - r));
  52. result += diff;
  53. ++x;
  54. BOOST_MATH_INSTRUMENT_VARIABLE(x);
  55. BOOST_MATH_INSTRUMENT_VARIABLE(diff);
  56. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  57. }
  58. }
  59. }
  60. if(invert)
  61. result = 1 - result;
  62. return result;
  63. #ifdef _MSC_VER
  64. # pragma warning(pop)
  65. #endif
  66. }
  67. template <class T, class Policy>
  68. inline T hypergeometric_cdf(std::uint64_t x, std::uint64_t r, std::uint64_t n, std::uint64_t N, bool invert, const Policy&)
  69. {
  70. BOOST_FPU_EXCEPTION_GUARD
  71. typedef typename tools::promote_args<T>::type result_type;
  72. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  73. typedef typename policies::normalise<
  74. Policy,
  75. policies::promote_float<false>,
  76. policies::promote_double<false>,
  77. policies::discrete_quantile<>,
  78. policies::assert_undefined<> >::type forwarding_policy;
  79. value_type result;
  80. result = detail::hypergeometric_cdf_imp<value_type>(x, r, n, N, invert, forwarding_policy());
  81. if(result > 1)
  82. {
  83. result = 1;
  84. }
  85. if(result < 0)
  86. {
  87. result = 0;
  88. }
  89. return policies::checked_narrowing_cast<result_type, forwarding_policy>(result, "boost::math::hypergeometric_cdf<%1%>(%1%,%1%,%1%,%1%)");
  90. }
  91. }}} // namespaces
  92. #endif