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