hypergeometric_dist_data.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // (C) Copyright John Maddock 2009.
  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. //#define BOOST_MATH_INSTRUMENT
  6. #include <boost/math/distributions/hypergeometric.hpp>
  7. #include <boost/math/special_functions/trunc.hpp>
  8. #include <boost/math/constants/constants.hpp>
  9. #include <boost/lexical_cast.hpp>
  10. #include <boost/random/uniform_int.hpp>
  11. #include <fstream>
  12. #include <boost/math/tools/test_data.hpp>
  13. #include "mp_t.hpp"
  14. using namespace boost::math::tools;
  15. std::mt19937 rnd;
  16. struct hypergeometric_generator
  17. {
  18. boost::math::tuple<
  19. mp_t,
  20. mp_t,
  21. mp_t,
  22. mp_t,
  23. mp_t,
  24. mp_t,
  25. mp_t> operator()(mp_t rN, mp_t rr, mp_t rn)
  26. {
  27. using namespace std;
  28. using namespace boost;
  29. using namespace boost::math;
  30. if((rr > rN) || (rr < rn))
  31. throw std::domain_error("");
  32. try{
  33. int N = itrunc(rN);
  34. int r = itrunc(rr);
  35. int n = itrunc(rn);
  36. boost::uniform_int<> ui((std::max)(0, n + r - N), (std::min)(n, r));
  37. int k = ui(rnd);
  38. hypergeometric_distribution<mp_t> d(r, n, N);
  39. mp_t p = pdf(d, k);
  40. if((p == 1) || (p == 0))
  41. {
  42. // trivial case, don't clutter up our table with it:
  43. throw std::domain_error("");
  44. }
  45. mp_t c = cdf(d, k);
  46. mp_t cc = cdf(complement(d, k));
  47. std::cout << "N = " << N << " r = " << r << " n = " << n << " PDF = " << p << " CDF = " << c << " CCDF = " << cc << std::endl;
  48. return boost::math::make_tuple(r, n, N, k, p, c, cc);
  49. }
  50. catch(const std::exception& e)
  51. {
  52. std::cout << e.what() << std::endl;
  53. throw std::domain_error("");
  54. }
  55. }
  56. };
  57. int main(int argc, char*argv [])
  58. {
  59. std::string line;
  60. parameter_info<mp_t> arg1, arg2, arg3;
  61. test_data<mp_t> data;
  62. std::cout << "Welcome.\n"
  63. "This program will generate spot tests hypergeoemtric distribution:\n";
  64. arg1 = make_power_param(mp_t(0), 1, 21);
  65. arg2 = make_power_param(mp_t(0), 1, 21);
  66. arg3 = make_power_param(mp_t(0), 1, 21);
  67. arg1.type |= dummy_param;
  68. arg2.type |= dummy_param;
  69. arg3.type |= dummy_param;
  70. data.insert(hypergeometric_generator(), arg1, arg2, arg3);
  71. line = "hypergeometric_dist_data2.ipp";
  72. std::ofstream ofs(line.c_str());
  73. ofs << std::scientific << std::setprecision(40);
  74. write_code(ofs, data, "hypergeometric_dist_data2");
  75. return 0;
  76. }