hypergeometric_pdf.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. // Copyright 2008 Gautam Sewani
  2. // Copyright 2008 John Maddock
  3. //
  4. // Use, modification and distribution are subject to the
  5. // Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt
  7. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. #ifndef BOOST_MATH_DISTRIBUTIONS_DETAIL_HG_PDF_HPP
  9. #define BOOST_MATH_DISTRIBUTIONS_DETAIL_HG_PDF_HPP
  10. #include <boost/math/constants/constants.hpp>
  11. #include <boost/math/special_functions/lanczos.hpp>
  12. #include <boost/math/special_functions/gamma.hpp>
  13. #include <boost/math/special_functions/pow.hpp>
  14. #include <boost/math/special_functions/prime.hpp>
  15. #include <boost/math/policies/error_handling.hpp>
  16. #ifdef BOOST_MATH_INSTRUMENT
  17. #include <typeinfo>
  18. #endif
  19. namespace boost{ namespace math{ namespace detail{
  20. template <class T, class Func>
  21. void bubble_down_one(T* first, T* last, Func f)
  22. {
  23. using std::swap;
  24. T* next = first;
  25. ++next;
  26. while((next != last) && (!f(*first, *next)))
  27. {
  28. swap(*first, *next);
  29. ++first;
  30. ++next;
  31. }
  32. }
  33. template <class T>
  34. struct sort_functor
  35. {
  36. sort_functor(const T* exponents) : m_exponents(exponents){}
  37. bool operator()(int i, int j)
  38. {
  39. return m_exponents[i] > m_exponents[j];
  40. }
  41. private:
  42. const T* m_exponents;
  43. };
  44. template <class T, class Lanczos, class Policy>
  45. T hypergeometric_pdf_lanczos_imp(T /*dummy*/, unsigned x, unsigned r, unsigned n, unsigned N, const Lanczos&, const Policy&)
  46. {
  47. BOOST_MATH_STD_USING
  48. BOOST_MATH_INSTRUMENT_FPU
  49. BOOST_MATH_INSTRUMENT_VARIABLE(x);
  50. BOOST_MATH_INSTRUMENT_VARIABLE(r);
  51. BOOST_MATH_INSTRUMENT_VARIABLE(n);
  52. BOOST_MATH_INSTRUMENT_VARIABLE(N);
  53. BOOST_MATH_INSTRUMENT_VARIABLE(typeid(Lanczos).name());
  54. T bases[9] = {
  55. T(n) + static_cast<T>(Lanczos::g()) + 0.5f,
  56. T(r) + static_cast<T>(Lanczos::g()) + 0.5f,
  57. T(N - n) + static_cast<T>(Lanczos::g()) + 0.5f,
  58. T(N - r) + static_cast<T>(Lanczos::g()) + 0.5f,
  59. 1 / (T(N) + static_cast<T>(Lanczos::g()) + 0.5f),
  60. 1 / (T(x) + static_cast<T>(Lanczos::g()) + 0.5f),
  61. 1 / (T(n - x) + static_cast<T>(Lanczos::g()) + 0.5f),
  62. 1 / (T(r - x) + static_cast<T>(Lanczos::g()) + 0.5f),
  63. 1 / (T(N - n - r + x) + static_cast<T>(Lanczos::g()) + 0.5f)
  64. };
  65. T exponents[9] = {
  66. n + T(0.5f),
  67. r + T(0.5f),
  68. N - n + T(0.5f),
  69. N - r + T(0.5f),
  70. N + T(0.5f),
  71. x + T(0.5f),
  72. n - x + T(0.5f),
  73. r - x + T(0.5f),
  74. N - n - r + x + T(0.5f)
  75. };
  76. int base_e_factors[9] = {
  77. -1, -1, -1, -1, 1, 1, 1, 1, 1
  78. };
  79. int sorted_indexes[9] = {
  80. 0, 1, 2, 3, 4, 5, 6, 7, 8
  81. };
  82. #ifdef BOOST_MATH_INSTRUMENT
  83. BOOST_MATH_INSTRUMENT_FPU
  84. for(unsigned i = 0; i < 9; ++i)
  85. {
  86. BOOST_MATH_INSTRUMENT_VARIABLE(i);
  87. BOOST_MATH_INSTRUMENT_VARIABLE(bases[i]);
  88. BOOST_MATH_INSTRUMENT_VARIABLE(exponents[i]);
  89. BOOST_MATH_INSTRUMENT_VARIABLE(base_e_factors[i]);
  90. BOOST_MATH_INSTRUMENT_VARIABLE(sorted_indexes[i]);
  91. }
  92. #endif
  93. std::sort(sorted_indexes, sorted_indexes + 9, sort_functor<T>(exponents));
  94. #ifdef BOOST_MATH_INSTRUMENT
  95. BOOST_MATH_INSTRUMENT_FPU
  96. for(unsigned i = 0; i < 9; ++i)
  97. {
  98. BOOST_MATH_INSTRUMENT_VARIABLE(i);
  99. BOOST_MATH_INSTRUMENT_VARIABLE(bases[i]);
  100. BOOST_MATH_INSTRUMENT_VARIABLE(exponents[i]);
  101. BOOST_MATH_INSTRUMENT_VARIABLE(base_e_factors[i]);
  102. BOOST_MATH_INSTRUMENT_VARIABLE(sorted_indexes[i]);
  103. }
  104. #endif
  105. do{
  106. exponents[sorted_indexes[0]] -= exponents[sorted_indexes[1]];
  107. bases[sorted_indexes[1]] *= bases[sorted_indexes[0]];
  108. if((bases[sorted_indexes[1]] < tools::min_value<T>()) && (exponents[sorted_indexes[1]] != 0))
  109. {
  110. return 0;
  111. }
  112. base_e_factors[sorted_indexes[1]] += base_e_factors[sorted_indexes[0]];
  113. bubble_down_one(sorted_indexes, sorted_indexes + 9, sort_functor<T>(exponents));
  114. #ifdef BOOST_MATH_INSTRUMENT
  115. for(unsigned i = 0; i < 9; ++i)
  116. {
  117. BOOST_MATH_INSTRUMENT_VARIABLE(i);
  118. BOOST_MATH_INSTRUMENT_VARIABLE(bases[i]);
  119. BOOST_MATH_INSTRUMENT_VARIABLE(exponents[i]);
  120. BOOST_MATH_INSTRUMENT_VARIABLE(base_e_factors[i]);
  121. BOOST_MATH_INSTRUMENT_VARIABLE(sorted_indexes[i]);
  122. }
  123. #endif
  124. }while(exponents[sorted_indexes[1]] > 1);
  125. //
  126. // Combine equal powers:
  127. //
  128. int j = 8;
  129. while(exponents[sorted_indexes[j]] == 0) --j;
  130. while(j)
  131. {
  132. while(j && (exponents[sorted_indexes[j-1]] == exponents[sorted_indexes[j]]))
  133. {
  134. bases[sorted_indexes[j-1]] *= bases[sorted_indexes[j]];
  135. exponents[sorted_indexes[j]] = 0;
  136. base_e_factors[sorted_indexes[j-1]] += base_e_factors[sorted_indexes[j]];
  137. bubble_down_one(sorted_indexes + j, sorted_indexes + 9, sort_functor<T>(exponents));
  138. --j;
  139. }
  140. --j;
  141. #ifdef BOOST_MATH_INSTRUMENT
  142. BOOST_MATH_INSTRUMENT_VARIABLE(j);
  143. for(unsigned i = 0; i < 9; ++i)
  144. {
  145. BOOST_MATH_INSTRUMENT_VARIABLE(i);
  146. BOOST_MATH_INSTRUMENT_VARIABLE(bases[i]);
  147. BOOST_MATH_INSTRUMENT_VARIABLE(exponents[i]);
  148. BOOST_MATH_INSTRUMENT_VARIABLE(base_e_factors[i]);
  149. BOOST_MATH_INSTRUMENT_VARIABLE(sorted_indexes[i]);
  150. }
  151. #endif
  152. }
  153. #ifdef BOOST_MATH_INSTRUMENT
  154. BOOST_MATH_INSTRUMENT_FPU
  155. for(unsigned i = 0; i < 9; ++i)
  156. {
  157. BOOST_MATH_INSTRUMENT_VARIABLE(i);
  158. BOOST_MATH_INSTRUMENT_VARIABLE(bases[i]);
  159. BOOST_MATH_INSTRUMENT_VARIABLE(exponents[i]);
  160. BOOST_MATH_INSTRUMENT_VARIABLE(base_e_factors[i]);
  161. BOOST_MATH_INSTRUMENT_VARIABLE(sorted_indexes[i]);
  162. }
  163. #endif
  164. T result;
  165. BOOST_MATH_INSTRUMENT_VARIABLE(bases[sorted_indexes[0]] * exp(static_cast<T>(base_e_factors[sorted_indexes[0]])));
  166. BOOST_MATH_INSTRUMENT_VARIABLE(exponents[sorted_indexes[0]]);
  167. {
  168. BOOST_FPU_EXCEPTION_GUARD
  169. result = pow(bases[sorted_indexes[0]] * exp(static_cast<T>(base_e_factors[sorted_indexes[0]])), exponents[sorted_indexes[0]]);
  170. }
  171. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  172. for(unsigned i = 1; (i < 9) && (exponents[sorted_indexes[i]] > 0); ++i)
  173. {
  174. BOOST_FPU_EXCEPTION_GUARD
  175. if(result < tools::min_value<T>())
  176. return 0; // short circuit further evaluation
  177. if(exponents[sorted_indexes[i]] == 1)
  178. result *= bases[sorted_indexes[i]] * exp(static_cast<T>(base_e_factors[sorted_indexes[i]]));
  179. else if(exponents[sorted_indexes[i]] == 0.5f)
  180. result *= sqrt(bases[sorted_indexes[i]] * exp(static_cast<T>(base_e_factors[sorted_indexes[i]])));
  181. else
  182. result *= pow(bases[sorted_indexes[i]] * exp(static_cast<T>(base_e_factors[sorted_indexes[i]])), exponents[sorted_indexes[i]]);
  183. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  184. }
  185. result *= Lanczos::lanczos_sum_expG_scaled(static_cast<T>(n + 1))
  186. * Lanczos::lanczos_sum_expG_scaled(static_cast<T>(r + 1))
  187. * Lanczos::lanczos_sum_expG_scaled(static_cast<T>(N - n + 1))
  188. * Lanczos::lanczos_sum_expG_scaled(static_cast<T>(N - r + 1))
  189. /
  190. ( Lanczos::lanczos_sum_expG_scaled(static_cast<T>(N + 1))
  191. * Lanczos::lanczos_sum_expG_scaled(static_cast<T>(x + 1))
  192. * Lanczos::lanczos_sum_expG_scaled(static_cast<T>(n - x + 1))
  193. * Lanczos::lanczos_sum_expG_scaled(static_cast<T>(r - x + 1))
  194. * Lanczos::lanczos_sum_expG_scaled(static_cast<T>(N - n - r + x + 1)));
  195. BOOST_MATH_INSTRUMENT_VARIABLE(result);
  196. return result;
  197. }
  198. template <class T, class Policy>
  199. T hypergeometric_pdf_lanczos_imp(T /*dummy*/, unsigned x, unsigned r, unsigned n, unsigned N, const boost::math::lanczos::undefined_lanczos&, const Policy& pol)
  200. {
  201. BOOST_MATH_STD_USING
  202. return exp(
  203. boost::math::lgamma(T(n + 1), pol)
  204. + boost::math::lgamma(T(r + 1), pol)
  205. + boost::math::lgamma(T(N - n + 1), pol)
  206. + boost::math::lgamma(T(N - r + 1), pol)
  207. - boost::math::lgamma(T(N + 1), pol)
  208. - boost::math::lgamma(T(x + 1), pol)
  209. - boost::math::lgamma(T(n - x + 1), pol)
  210. - boost::math::lgamma(T(r - x + 1), pol)
  211. - boost::math::lgamma(T(N - n - r + x + 1), pol));
  212. }
  213. template <class T>
  214. inline T integer_power(const T& x, int ex)
  215. {
  216. if(ex < 0)
  217. return 1 / integer_power(x, -ex);
  218. switch(ex)
  219. {
  220. case 0:
  221. return 1;
  222. case 1:
  223. return x;
  224. case 2:
  225. return x * x;
  226. case 3:
  227. return x * x * x;
  228. case 4:
  229. return boost::math::pow<4>(x);
  230. case 5:
  231. return boost::math::pow<5>(x);
  232. case 6:
  233. return boost::math::pow<6>(x);
  234. case 7:
  235. return boost::math::pow<7>(x);
  236. case 8:
  237. return boost::math::pow<8>(x);
  238. }
  239. BOOST_MATH_STD_USING
  240. #ifdef __SUNPRO_CC
  241. return pow(x, T(ex));
  242. #else
  243. return pow(x, ex);
  244. #endif
  245. }
  246. template <class T>
  247. struct hypergeometric_pdf_prime_loop_result_entry
  248. {
  249. T value;
  250. const hypergeometric_pdf_prime_loop_result_entry* next;
  251. };
  252. #ifdef BOOST_MSVC
  253. #pragma warning(push)
  254. #pragma warning(disable:4510 4512 4610)
  255. #endif
  256. struct hypergeometric_pdf_prime_loop_data
  257. {
  258. const unsigned x;
  259. const unsigned r;
  260. const unsigned n;
  261. const unsigned N;
  262. unsigned prime_index;
  263. unsigned current_prime;
  264. };
  265. #ifdef BOOST_MSVC
  266. #pragma warning(pop)
  267. #endif
  268. template <class T>
  269. T hypergeometric_pdf_prime_loop_imp(hypergeometric_pdf_prime_loop_data& data, hypergeometric_pdf_prime_loop_result_entry<T>& result)
  270. {
  271. while(data.current_prime <= data.N)
  272. {
  273. unsigned base = data.current_prime;
  274. int prime_powers = 0;
  275. while(base <= data.N)
  276. {
  277. prime_powers += data.n / base;
  278. prime_powers += data.r / base;
  279. prime_powers += (data.N - data.n) / base;
  280. prime_powers += (data.N - data.r) / base;
  281. prime_powers -= data.N / base;
  282. prime_powers -= data.x / base;
  283. prime_powers -= (data.n - data.x) / base;
  284. prime_powers -= (data.r - data.x) / base;
  285. prime_powers -= (data.N - data.n - data.r + data.x) / base;
  286. base *= data.current_prime;
  287. }
  288. if(prime_powers)
  289. {
  290. T p = integer_power<T>(static_cast<T>(data.current_prime), prime_powers);
  291. if((p > 1) && (tools::max_value<T>() / p < result.value))
  292. {
  293. //
  294. // The next calculation would overflow, use recursion
  295. // to sidestep the issue:
  296. //
  297. hypergeometric_pdf_prime_loop_result_entry<T> t = { p, &result };
  298. data.current_prime = prime(++data.prime_index);
  299. return hypergeometric_pdf_prime_loop_imp<T>(data, t);
  300. }
  301. if((p < 1) && (tools::min_value<T>() / p > result.value))
  302. {
  303. //
  304. // The next calculation would underflow, use recursion
  305. // to sidestep the issue:
  306. //
  307. hypergeometric_pdf_prime_loop_result_entry<T> t = { p, &result };
  308. data.current_prime = prime(++data.prime_index);
  309. return hypergeometric_pdf_prime_loop_imp<T>(data, t);
  310. }
  311. result.value *= p;
  312. }
  313. data.current_prime = prime(++data.prime_index);
  314. }
  315. //
  316. // When we get to here we have run out of prime factors,
  317. // the overall result is the product of all the partial
  318. // results we have accumulated on the stack so far, these
  319. // are in a linked list starting with "data.head" and ending
  320. // with "result".
  321. //
  322. // All that remains is to multiply them together, taking
  323. // care not to overflow or underflow.
  324. //
  325. // Enumerate partial results >= 1 in variable i
  326. // and partial results < 1 in variable j:
  327. //
  328. hypergeometric_pdf_prime_loop_result_entry<T> const *i, *j;
  329. i = &result;
  330. while(i && i->value < 1)
  331. i = i->next;
  332. j = &result;
  333. while(j && j->value >= 1)
  334. j = j->next;
  335. T prod = 1;
  336. while(i || j)
  337. {
  338. while(i && ((prod <= 1) || (j == 0)))
  339. {
  340. prod *= i->value;
  341. i = i->next;
  342. while(i && i->value < 1)
  343. i = i->next;
  344. }
  345. while(j && ((prod >= 1) || (i == 0)))
  346. {
  347. prod *= j->value;
  348. j = j->next;
  349. while(j && j->value >= 1)
  350. j = j->next;
  351. }
  352. }
  353. return prod;
  354. }
  355. template <class T, class Policy>
  356. inline T hypergeometric_pdf_prime_imp(unsigned x, unsigned r, unsigned n, unsigned N, const Policy&)
  357. {
  358. hypergeometric_pdf_prime_loop_result_entry<T> result = { 1, 0 };
  359. hypergeometric_pdf_prime_loop_data data = { x, r, n, N, 0, prime(0) };
  360. return hypergeometric_pdf_prime_loop_imp<T>(data, result);
  361. }
  362. template <class T, class Policy>
  363. T hypergeometric_pdf_factorial_imp(unsigned x, unsigned r, unsigned n, unsigned N, const Policy&)
  364. {
  365. BOOST_MATH_STD_USING
  366. BOOST_ASSERT(N <= boost::math::max_factorial<T>::value);
  367. T result = boost::math::unchecked_factorial<T>(n);
  368. T num[3] = {
  369. boost::math::unchecked_factorial<T>(r),
  370. boost::math::unchecked_factorial<T>(N - n),
  371. boost::math::unchecked_factorial<T>(N - r)
  372. };
  373. T denom[5] = {
  374. boost::math::unchecked_factorial<T>(N),
  375. boost::math::unchecked_factorial<T>(x),
  376. boost::math::unchecked_factorial<T>(n - x),
  377. boost::math::unchecked_factorial<T>(r - x),
  378. boost::math::unchecked_factorial<T>(N - n - r + x)
  379. };
  380. int i = 0;
  381. int j = 0;
  382. while((i < 3) || (j < 5))
  383. {
  384. while((j < 5) && ((result >= 1) || (i >= 3)))
  385. {
  386. result /= denom[j];
  387. ++j;
  388. }
  389. while((i < 3) && ((result <= 1) || (j >= 5)))
  390. {
  391. result *= num[i];
  392. ++i;
  393. }
  394. }
  395. return result;
  396. }
  397. template <class T, class Policy>
  398. inline typename tools::promote_args<T>::type
  399. hypergeometric_pdf(unsigned x, unsigned r, unsigned n, unsigned N, const Policy&)
  400. {
  401. BOOST_FPU_EXCEPTION_GUARD
  402. typedef typename tools::promote_args<T>::type result_type;
  403. typedef typename policies::evaluation<result_type, Policy>::type value_type;
  404. typedef typename lanczos::lanczos<value_type, Policy>::type evaluation_type;
  405. typedef typename policies::normalise<
  406. Policy,
  407. policies::promote_float<false>,
  408. policies::promote_double<false>,
  409. policies::discrete_quantile<>,
  410. policies::assert_undefined<> >::type forwarding_policy;
  411. value_type result;
  412. if(N <= boost::math::max_factorial<value_type>::value)
  413. {
  414. //
  415. // If N is small enough then we can evaluate the PDF via the factorials
  416. // directly: table lookup of the factorials gives the best performance
  417. // of the methods available:
  418. //
  419. result = detail::hypergeometric_pdf_factorial_imp<value_type>(x, r, n, N, forwarding_policy());
  420. }
  421. else if(N <= boost::math::prime(boost::math::max_prime - 1))
  422. {
  423. //
  424. // If N is no larger than the largest prime number in our lookup table
  425. // (104729) then we can use prime factorisation to evaluate the PDF,
  426. // this is slow but accurate:
  427. //
  428. result = detail::hypergeometric_pdf_prime_imp<value_type>(x, r, n, N, forwarding_policy());
  429. }
  430. else
  431. {
  432. //
  433. // Catch all case - use the lanczos approximation - where available -
  434. // to evaluate the ratio of factorials. This is reasonably fast
  435. // (almost as quick as using logarithmic evaluation in terms of lgamma)
  436. // but only a few digits better in accuracy than using lgamma:
  437. //
  438. result = detail::hypergeometric_pdf_lanczos_imp(value_type(), x, r, n, N, evaluation_type(), forwarding_policy());
  439. }
  440. if(result > 1)
  441. {
  442. result = 1;
  443. }
  444. if(result < 0)
  445. {
  446. result = 0;
  447. }
  448. return policies::checked_narrowing_cast<result_type, forwarding_policy>(result, "boost::math::hypergeometric_pdf<%1%>(%1%,%1%,%1%,%1%)");
  449. }
  450. }}} // namespaces
  451. #endif