Imported Upstream version 1.49.0
[platform/upstream/boost.git] / libs / math / tools / hypergeometric_dist_data.cpp
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
6 //#define BOOST_MATH_INSTRUMENT
7
8 #include <boost/math/bindings/rr.hpp>
9 #include <boost/test/included/test_exec_monitor.hpp>
10 #include <boost/math/distributions/hypergeometric.hpp>
11 #include <boost/math/special_functions/trunc.hpp>
12 #include <boost/math/constants/constants.hpp>
13 #include <boost/math/tools/test.hpp>
14 #include <boost/lexical_cast.hpp>
15 #include <boost/random/uniform_int.hpp>
16 #include <fstream>
17
18 #include <boost/math/tools/test_data.hpp>
19
20 using namespace boost::math::tools;
21
22 std::tr1::mt19937 rnd;
23
24 struct hypergeometric_generator
25 {
26    boost::math::tuple<
27       boost::math::ntl::RR, 
28       boost::math::ntl::RR, 
29       boost::math::ntl::RR, 
30       boost::math::ntl::RR, 
31       boost::math::ntl::RR,
32       boost::math::ntl::RR,
33       boost::math::ntl::RR> operator()(boost::math::ntl::RR rN, boost::math::ntl::RR rr, boost::math::ntl::RR rn)
34    {
35       using namespace std;
36       using namespace boost;
37       using namespace boost::math;
38
39       if((rr > rN) || (rr < rn))
40          throw std::domain_error("");
41
42       try{
43          int N = itrunc(rN);
44          int r = itrunc(rr);
45          int n = itrunc(rn);
46          boost::uniform_int<> ui((std::max)(0, n + r - N), (std::min)(n, r));
47          int k = ui(rnd);
48
49          hypergeometric_distribution<ntl::RR> d(r, n, N);
50
51          ntl::RR p = pdf(d, k);
52          if((p == 1) || (p == 0))
53          {
54             // trivial case, don't clutter up our table with it:
55             throw std::domain_error("");
56          }
57          ntl::RR c = cdf(d, k);
58          ntl::RR cc = cdf(complement(d, k));
59
60          std::cout << "N = " << N << " r = " << r << " n = " << n << " PDF = " << p << " CDF = " << c << " CCDF = " << cc << std::endl;
61
62          return boost::math::make_tuple(r, n, N, k, p, c, cc);
63       }
64       catch(const std::exception& e)
65       {
66          std::cout << e.what() << std::endl;
67          throw std::domain_error("");
68       }
69    }
70 };
71
72 int test_main(int argc, char*argv [])
73 {
74    boost::math::ntl::RR::SetPrecision(1000);
75    boost::math::ntl::RR::SetOutputPrecision(100);
76
77    std::string line;
78    parameter_info<boost::math::ntl::RR> arg1, arg2, arg3;
79    test_data<boost::math::ntl::RR> data;
80
81    std::cout << "Welcome.\n"
82       "This program will generate spot tests hypergeoemtric distribution:\n";
83
84    arg1 = make_power_param(boost::math::ntl::RR(0), 1, 21);
85    arg2 = make_power_param(boost::math::ntl::RR(0), 1, 21);
86    arg3 = make_power_param(boost::math::ntl::RR(0), 1, 21);
87
88    arg1.type |= dummy_param;
89    arg2.type |= dummy_param;
90    arg3.type |= dummy_param;
91
92    data.insert(hypergeometric_generator(), arg1, arg2, arg3);
93
94    line = "hypergeometric_dist_data2.ipp";
95    std::ofstream ofs(line.c_str());
96    write_code(ofs, data, "hypergeometric_dist_data2");
97    
98    return 0;
99 }
100