test_hypergeometric_dist.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. // Copyright John Maddock 2008
  2. // Copyright Paul A. Bristow
  3. // Copyright Gautam Sewani
  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. #define BOOST_MATH_OVERFLOW_ERROR_POLICY throw_on_error
  9. #include <boost/math/concepts/real_concept.hpp> // for real_concept
  10. #include <boost/math/distributions/hypergeometric.hpp>
  11. #define BOOST_TEST_MAIN
  12. #include <boost/test/unit_test.hpp> // Boost.Test
  13. #include <boost/test/results_collector.hpp>
  14. #include <boost/test/unit_test.hpp>
  15. #include <boost/test/tools/floating_point_comparison.hpp>
  16. #include <iostream>
  17. using std::cout;
  18. using std::endl;
  19. using std::setprecision;
  20. #include <boost/array.hpp>
  21. #include "functor.hpp"
  22. #include "handle_test_result.hpp"
  23. #include "table_type.hpp"
  24. #define BOOST_CHECK_EX(a) \
  25. {\
  26. unsigned int failures = boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed;\
  27. BOOST_CHECK(a); \
  28. if(failures != boost::unit_test::results_collector.results( boost::unit_test::framework::current_test_case().p_id ).p_assertions_failed)\
  29. {\
  30. std::cerr << "Failure was with data ";\
  31. std::cerr << std::setprecision(35); \
  32. std::cerr << "x = " << x << ", r = " << r << ", n = " << n\
  33. << ", N = " << N << ", p = " << cp << ", q = " << ccp << std::endl;\
  34. }\
  35. }
  36. void expected_results()
  37. {
  38. //
  39. // Define the max and mean errors expected for
  40. // various compilers and platforms.
  41. //
  42. const char* largest_type;
  43. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  44. if(boost::math::policies::digits<double, boost::math::policies::policy<> >() == boost::math::policies::digits<long double, boost::math::policies::policy<> >())
  45. {
  46. largest_type = "(long\\s+)?double|real_concept";
  47. }
  48. else
  49. {
  50. largest_type = "long double|real_concept";
  51. }
  52. #else
  53. largest_type = "(long\\s+)?double";
  54. #endif
  55. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  56. if((boost::math::tools::digits<long double>() > boost::math::tools::digits<double>())
  57. && (boost::math::tools::digits<long double>() < 100))
  58. {
  59. //
  60. // Some split of errors from long double into double:
  61. //
  62. add_expected_result(
  63. ".*", // compiler
  64. ".*", // stdlib
  65. ".*", // platform
  66. "double", // test type(s)
  67. "Random.*", // test data group
  68. ".*", 1500, 1500); // test function
  69. add_expected_result(
  70. ".*", // compiler
  71. ".*", // stdlib
  72. ".*", // platform
  73. "double", // test type(s)
  74. ".*", // test data group
  75. ".*", 10, 10); // test function
  76. }
  77. #endif
  78. add_expected_result(
  79. ".*", // compiler
  80. ".*", // stdlib
  81. ".*", // platform
  82. "real_concept", // test type(s)
  83. "Random.*", // test data group
  84. ".*", 250000000, 25000000); // test function
  85. add_expected_result(
  86. ".*", // compiler
  87. ".*", // stdlib
  88. ".*", // platform
  89. largest_type, // test type(s)
  90. "Random.*", // test data group
  91. ".*", 10000000, 5000000); // test function
  92. add_expected_result(
  93. ".*", // compiler
  94. ".*", // stdlib
  95. ".*", // platform
  96. largest_type, // test type(s)
  97. ".*", // test data group
  98. ".*", 50, 20); // test function
  99. }
  100. template <class T>
  101. inline unsigned make_unsigned(T x)
  102. {
  103. return static_cast<unsigned>(x);
  104. }
  105. template<>
  106. inline unsigned make_unsigned(boost::math::concepts::real_concept x)
  107. {
  108. return static_cast<unsigned>(x.value());
  109. }
  110. template <class T>
  111. T pdf_tester(T r, T n, T N, T x)
  112. {
  113. boost::math::hypergeometric_distribution<T> d(make_unsigned(r), make_unsigned(n), make_unsigned(N));
  114. return pdf(d, x);
  115. }
  116. template <class T>
  117. T cdf_tester(T r, T n, T N, T x)
  118. {
  119. boost::math::hypergeometric_distribution<T> d(make_unsigned(r), make_unsigned(n), make_unsigned(N));
  120. return cdf(d, x);
  121. }
  122. template <class T>
  123. T ccdf_tester(T r, T n, T N, T x)
  124. {
  125. boost::math::hypergeometric_distribution<T> d(make_unsigned(r), make_unsigned(n), make_unsigned(N));
  126. return cdf(complement(d, x));
  127. }
  128. template <class Real, class T>
  129. void do_test_hypergeometric(const T& data, const char* type_name, const char* test_name)
  130. {
  131. // warning suppression:
  132. (void)data;
  133. (void)type_name;
  134. (void)test_name;
  135. #if !defined(TEST_QUANT) || (TEST_QUANT == 0)
  136. typedef Real value_type;
  137. typedef value_type (*pg)(value_type, value_type, value_type, value_type);
  138. #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  139. pg funcp = pdf_tester<value_type>;
  140. #else
  141. pg funcp = pdf_tester;
  142. #endif
  143. boost::math::tools::test_result<value_type> result;
  144. std::cout << "Testing " << test_name << " with type " << type_name
  145. << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  146. //
  147. // test hypergeometric against data:
  148. //
  149. result = boost::math::tools::test_hetero<Real>(
  150. data,
  151. bind_func<Real>(funcp, 0, 1, 2, 3),
  152. extract_result<Real>(4));
  153. handle_test_result(result, data[result.worst()], result.worst(), type_name, "hypergeometric PDF", test_name);
  154. #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  155. funcp = cdf_tester<value_type>;
  156. #else
  157. funcp = cdf_tester;
  158. #endif
  159. //
  160. // test hypergeometric against data:
  161. //
  162. result = boost::math::tools::test_hetero<Real>(
  163. data,
  164. bind_func<Real>(funcp, 0, 1, 2, 3),
  165. extract_result<Real>(5));
  166. handle_test_result(result, data[result.worst()], result.worst(), type_name, "hypergeometric CDF", test_name);
  167. #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  168. funcp = ccdf_tester<value_type>;
  169. #else
  170. funcp = ccdf_tester;
  171. #endif
  172. //
  173. // test hypergeometric against data:
  174. //
  175. result = boost::math::tools::test_hetero<Real>(
  176. data,
  177. bind_func<Real>(funcp, 0, 1, 2, 3),
  178. extract_result<Real>(6));
  179. handle_test_result(result, data[result.worst()], result.worst(), type_name, "hypergeometric CDF complement", test_name);
  180. std::cout << std::endl;
  181. #endif
  182. }
  183. template <class Real, class T>
  184. void do_test_hypergeometric_quantile(const T& data, const char* type_name, const char* test_name)
  185. {
  186. typedef Real value_type;
  187. std::cout << "Checking quantiles with " << test_name << " with type " << type_name
  188. << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  189. if(boost::math::tools::digits<value_type>() > 50)
  190. {
  191. for(unsigned i = 0; i < data.size(); ++i)
  192. {
  193. using namespace boost::math::policies;
  194. unsigned r = make_unsigned(data[i][0]);
  195. unsigned n = make_unsigned(data[i][1]);
  196. unsigned N = make_unsigned(data[i][2]);
  197. unsigned x = make_unsigned(data[i][3]);
  198. value_type cp = data[i][5];
  199. value_type ccp = data[i][6];
  200. //
  201. // A bit of warning suppression:
  202. //
  203. (void)x;
  204. (void)n;
  205. (void)r;
  206. (void)N;
  207. (void)cp;
  208. (void)ccp;
  209. #if !defined(TEST_QUANT) || (TEST_QUANT == 1)
  210. boost::math::hypergeometric_distribution<value_type,
  211. policy<discrete_quantile<integer_round_up> > > du(r, n, N);
  212. if((cp < 0.9) && (cp > boost::math::tools::min_value<value_type>()))
  213. {
  214. BOOST_CHECK_EX(quantile(du, cp) >= x);
  215. }
  216. if((ccp < 0.9) && (ccp > boost::math::tools::min_value<value_type>()))
  217. {
  218. BOOST_CHECK_EX(quantile(complement(du, ccp)) >= x);
  219. }
  220. #endif
  221. #if !defined(TEST_QUANT) || (TEST_QUANT == 2)
  222. boost::math::hypergeometric_distribution<value_type,
  223. policy<discrete_quantile<integer_round_down> > > dl(r, n, N);
  224. if((cp < 0.9) && (cp > boost::math::tools::min_value<value_type>()))
  225. {
  226. BOOST_CHECK_EX(quantile(dl, cp) <= x);
  227. }
  228. if((ccp < 0.9) && (ccp > boost::math::tools::min_value<value_type>()))
  229. {
  230. BOOST_CHECK_EX(quantile(complement(dl, ccp)) <= x);
  231. }
  232. #endif
  233. #if !defined(TEST_QUANT) || (TEST_QUANT == 3)
  234. boost::math::hypergeometric_distribution<value_type,
  235. policy<discrete_quantile<integer_round_nearest> > > dn(r, n, N);
  236. if((cp < 0.9) && (cp > boost::math::tools::min_value<value_type>()))
  237. {
  238. BOOST_CHECK_EX(quantile(dn, cp) == x);
  239. }
  240. if((ccp < 0.9) && (ccp > boost::math::tools::min_value<value_type>()))
  241. {
  242. BOOST_CHECK_EX(quantile(complement(dn, ccp)) == x);
  243. }
  244. #endif
  245. #if !defined(TEST_QUANT) || (TEST_QUANT == 4)
  246. boost::math::hypergeometric_distribution<value_type,
  247. policy<discrete_quantile<integer_round_outwards> > > dou(r, n, N);
  248. if((cp < 0.9) && (cp > boost::math::tools::min_value<value_type>()))
  249. {
  250. if(cp < 0.5)
  251. {
  252. BOOST_CHECK_EX(quantile(dou, cp) <= x);
  253. }
  254. else
  255. {
  256. BOOST_CHECK_EX(quantile(dou, cp) >= x);
  257. }
  258. }
  259. if((ccp < 0.9) && (ccp > boost::math::tools::min_value<value_type>()))
  260. {
  261. if(ccp < 0.5)
  262. {
  263. BOOST_CHECK_EX(quantile(complement(dou, ccp)) >= x);
  264. }
  265. else
  266. {
  267. BOOST_CHECK_EX(quantile(complement(dou, ccp)) <= x);
  268. }
  269. }
  270. #endif
  271. #if !defined(TEST_QUANT) || (TEST_QUANT == 5)
  272. boost::math::hypergeometric_distribution<value_type,
  273. policy<discrete_quantile<integer_round_inwards> > > di(r, n, N);
  274. if((cp < 0.9) && (cp > boost::math::tools::min_value<value_type>()))
  275. {
  276. if(cp < 0.5)
  277. {
  278. BOOST_CHECK_EX(quantile(di, cp) >= x);
  279. }
  280. else
  281. {
  282. BOOST_CHECK_EX(quantile(di, cp) <= x);
  283. }
  284. }
  285. if((ccp < 0.9) && (ccp > boost::math::tools::min_value<value_type>()))
  286. {
  287. if(ccp < 0.5)
  288. {
  289. BOOST_CHECK_EX(quantile(complement(di, ccp)) <= x);
  290. }
  291. else
  292. {
  293. BOOST_CHECK_EX(quantile(complement(di, ccp)) >= x);
  294. }
  295. }
  296. #endif
  297. }
  298. }
  299. }
  300. template <class RealType>
  301. void test_spot(unsigned x, unsigned n, unsigned r, unsigned N,
  302. RealType p, RealType cp, RealType ccp, RealType tol)
  303. {
  304. //
  305. // A bit of warning suppression:
  306. //
  307. (void)x;
  308. (void)n;
  309. (void)r;
  310. (void)N;
  311. (void)p;
  312. (void)cp;
  313. (void)ccp;
  314. (void)tol;
  315. #if !defined(TEST_QUANT) || (TEST_QUANT == 0)
  316. boost::math::hypergeometric_distribution<RealType> d(r, n, N);
  317. std::pair<unsigned, unsigned> extent = range(d);
  318. // CDF's:
  319. BOOST_CHECK_CLOSE(pdf(d, x), p, tol);
  320. BOOST_CHECK_CLOSE(cdf(d, x), cp, tol);
  321. BOOST_CHECK_CLOSE(cdf(complement(d, x)), ccp, tol);
  322. // Again with real-value arguments:
  323. BOOST_CHECK_CLOSE(pdf(d, static_cast<RealType>(x)), p, tol);
  324. BOOST_CHECK_CLOSE(cdf(d, static_cast<RealType>(x)), cp, tol);
  325. BOOST_CHECK_CLOSE(cdf(complement(d, static_cast<RealType>(x))), ccp, tol);
  326. //
  327. // Quantiles, don't bother checking these for type float
  328. // as there's not enough precision in the p and q values
  329. // to get back to where we started:
  330. //
  331. if(boost::math::tools::digits<RealType>() > 50)
  332. {
  333. using namespace boost::math::policies;
  334. boost::math::hypergeometric_distribution<RealType,
  335. policy<discrete_quantile<integer_round_up> > > du(r, n, N);
  336. BOOST_CHECK_EX(quantile(du, cp) >= x);
  337. BOOST_CHECK_EX(quantile(complement(du, ccp)) >= x);
  338. boost::math::hypergeometric_distribution<RealType,
  339. policy<discrete_quantile<integer_round_down> > > dl(r, n, N);
  340. BOOST_CHECK_EX(quantile(dl, cp) <= x);
  341. BOOST_CHECK_EX(quantile(complement(dl, ccp)) <= x);
  342. boost::math::hypergeometric_distribution<RealType,
  343. policy<discrete_quantile<integer_round_nearest> > > dn(r, n, N);
  344. BOOST_CHECK_EX(quantile(dn, cp) == x);
  345. BOOST_CHECK_EX(quantile(complement(dn, ccp)) == x);
  346. }
  347. //
  348. // Error checking of out of bounds arguments:
  349. //
  350. BOOST_MATH_CHECK_THROW(pdf(d, extent.second + 1), std::domain_error);
  351. BOOST_MATH_CHECK_THROW(cdf(d, extent.second + 1), std::domain_error);
  352. BOOST_MATH_CHECK_THROW(cdf(complement(d, extent.second + 1)), std::domain_error);
  353. if(extent.first > 0)
  354. {
  355. BOOST_MATH_CHECK_THROW(pdf(d, extent.first - 1), std::domain_error);
  356. BOOST_MATH_CHECK_THROW(cdf(d, extent.first - 1), std::domain_error);
  357. BOOST_MATH_CHECK_THROW(cdf(complement(d, extent.first - 1)), std::domain_error);
  358. }
  359. BOOST_MATH_CHECK_THROW(quantile(d, 1.1f), std::domain_error);
  360. BOOST_MATH_CHECK_THROW(quantile(complement(d, 1.1f)), std::domain_error);
  361. BOOST_MATH_CHECK_THROW(quantile(d, -0.001f), std::domain_error);
  362. BOOST_MATH_CHECK_THROW(quantile(complement(d, -0.001f)), std::domain_error);
  363. //
  364. // Checking of extreme values:
  365. //
  366. BOOST_CHECK_EQUAL(quantile(d, 0), extent.first);
  367. BOOST_CHECK_EQUAL(quantile(d, 1), extent.second);
  368. BOOST_CHECK_EQUAL(quantile(complement(d, 0)), extent.second);
  369. BOOST_CHECK_EQUAL(quantile(complement(d, 1)), extent.first);
  370. BOOST_CHECK_EQUAL(cdf(d, extent.second), 1);
  371. BOOST_CHECK_EQUAL(cdf(complement(d, extent.second)), 0);
  372. #endif
  373. }
  374. template <class RealType>
  375. void test_spots(RealType /*T*/, const char* type_name)
  376. {
  377. // Basic sanity checks.
  378. // 50 eps as a percentage, up to a maximum of double precision
  379. // Test data taken from Mathematica 6
  380. #define T RealType
  381. #include "hypergeometric_test_data.ipp"
  382. do_test_hypergeometric<T>(hypergeometric_test_data, type_name, "Mathematica data");
  383. #include "hypergeometric_dist_data2.ipp"
  384. if(boost::is_floating_point<RealType>::value)
  385. {
  386. //
  387. // Don't test this for real_concept: it's too slow!!!
  388. //
  389. do_test_hypergeometric<T>(hypergeometric_dist_data2, type_name, "Random large data");
  390. }
  391. do_test_hypergeometric_quantile<T>(hypergeometric_test_data, type_name, "Mathematica data");
  392. if(boost::is_floating_point<RealType>::value)
  393. {
  394. //
  395. // Don't test this for real_concept: it's too slow!!!
  396. //
  397. do_test_hypergeometric_quantile<T>(hypergeometric_dist_data2, type_name, "Random large data");
  398. }
  399. RealType tolerance = (std::max)(
  400. static_cast<RealType>(2e-16L), // limit of test data
  401. boost::math::tools::epsilon<RealType>());
  402. cout<<"Absolute tolerance:"<<tolerance<<endl;
  403. tolerance *= 50 * 100; // 50eps as a persentage
  404. cout << "Tolerance for type " << typeid(RealType).name() << " is " << tolerance << " %" << endl;
  405. //
  406. // These sanity check values were calculated using the online
  407. // calculator at http://stattrek.com/Tables/Hypergeometric.aspx
  408. // It's assumed that the test values are accurate to no more than
  409. // double precision.
  410. //
  411. test_spot(20, 200, 50, 500, static_cast<T>(0.120748236361163), static_cast<T>(0.563532430195156), static_cast<T>(1 - 0.563532430195156), tolerance);
  412. test_spot(53, 452, 64, 500, static_cast<T>(0.0184749573044286), static_cast<T>(0.0299118078796907), static_cast<T>(1 - 0.0299118078796907), tolerance);
  413. test_spot(32, 1287, 128, 5000, static_cast<T>(0.0807012167418264), static_cast<T>(0.469768774237742), static_cast<T>(1 - 0.469768774237742), tolerance);
  414. test_spot(1, 13, 4, 26, static_cast<T>(0.248695652173913), static_cast<T>(0.296521739130435), static_cast<T>(1 - 0.296521739130435), tolerance);
  415. test_spot(2, 13, 4, 26, static_cast<T>(0.40695652173913), static_cast<T>(0.703478260869565), static_cast<T>(1 - 0.703478260869565), tolerance);
  416. test_spot(3, 13, 4, 26, static_cast<T>(0.248695652173913), static_cast<T>(0.952173913043478), static_cast<T>(1 - 0.952173913043478), tolerance);
  417. test_spot(40, 70, 89, 170, static_cast<T>(0.0721901023798991), static_cast<T>(0.885447799131944), static_cast<T>(1 - 0.885447799131944), tolerance);
  418. boost::math::hypergeometric_distribution<RealType> d(50, 200, 500);
  419. BOOST_CHECK_EQUAL(range(d).first, 0u);
  420. BOOST_CHECK_EQUAL(range(d).second, 50u);
  421. BOOST_CHECK_CLOSE(mean(d), static_cast<RealType>(20), tolerance);
  422. BOOST_CHECK_CLOSE(mode(d), static_cast<RealType>(20), tolerance);
  423. BOOST_CHECK_CLOSE(variance(d), static_cast<RealType>(10.821643286573146292585170340681L), tolerance);
  424. BOOST_CHECK_CLOSE(skewness(d), static_cast<RealType>(0.048833071022952084732902910189366L), tolerance);
  425. BOOST_CHECK_CLOSE(kurtosis_excess(d), static_cast<RealType>(2.5155486690782804816404001878293L), tolerance);
  426. BOOST_CHECK_CLOSE(kurtosis(d), kurtosis_excess(d) + 3, tolerance);
  427. BOOST_CHECK_EQUAL(quantile(d, 0.5f), median(d));
  428. BOOST_MATH_CHECK_THROW(d = boost::math::hypergeometric_distribution<RealType>(501, 40, 500), std::domain_error);
  429. BOOST_MATH_CHECK_THROW(d = boost::math::hypergeometric_distribution<RealType>(40, 501, 500), std::domain_error);
  430. }
  431. BOOST_AUTO_TEST_CASE( test_main )
  432. {
  433. expected_results();
  434. // Basic sanity-check spot values.
  435. // (Parameter value, arbitrarily zero, only communicates the floating point type).
  436. test_spots(0.0F, "float"); // Test float. OK at decdigits = 0 tolerance = 0.0001 %
  437. test_spots(0.0, "double"); // Test double. OK at decdigits 7, tolerance = 1e07 %
  438. #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  439. test_spots(0.0L, "long double"); // Test long double.
  440. #ifndef BOOST_MATH_NO_REAL_CONCEPT_TESTS
  441. test_spots(boost::math::concepts::real_concept(0), "real_concept"); // Test real_concept.
  442. #endif
  443. #else
  444. std::cout << "<note>The long double tests have been disabled on this platform "
  445. "either because the long double overloads of the usual math functions are "
  446. "not available at all, or because they are too inaccurate for these tests "
  447. "to pass.</note>" << std::endl;
  448. #endif
  449. } // BOOST_AUTO_TEST_CASE( test_main )