1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
/* test_bernoulli.cpp
*
* Copyright Steven Watanabe 2011
* Distributed under the Boost Software License, Version 1.0. (See
* accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*
* $Id$
*
*/
#include <boost/random/bernoulli_distribution.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/random/uniform_01.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/math/distributions/binomial.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/exception/diagnostic_information.hpp>
#include <vector>
#include <iostream>
#include <numeric>
#include "chi_squared_test.hpp"
bool do_test(double p, long long max) {
std::cout << "running bernoulli(" << p << ")" << " " << max << " times: " << std::flush;
boost::math::binomial expected(static_cast<double>(max), p);
boost::random::bernoulli_distribution<> dist(p);
boost::mt19937 gen;
long long count = 0;
for(long long i = 0; i < max; ++i) {
if(dist(gen)) ++count;
}
double prob = cdf(expected, count);
bool result = prob < 0.99 && prob > 0.01;
const char* err = result? "" : "*";
std::cout << std::setprecision(17) << prob << err << std::endl;
std::cout << std::setprecision(6);
return result;
}
bool do_tests(int repeat, long long trials) {
boost::mt19937 gen;
boost::uniform_01<> rdist;
int errors = 0;
for(int i = 0; i < repeat; ++i) {
if(!do_test(rdist(gen), trials)) {
++errors;
}
}
if(errors != 0) {
std::cout << "*** " << errors << " errors detected ***" << std::endl;
}
return errors == 0;
}
int usage() {
std::cerr << "Usage: test_bernoulli_distribution -r <repeat> -t <trials>" << std::endl;
return 2;
}
template<class T>
bool handle_option(int& argc, char**& argv, char opt, T& value) {
if(argv[0][1] == opt && argc > 1) {
--argc;
++argv;
value = boost::lexical_cast<T>(argv[0]);
return true;
} else {
return false;
}
}
int main(int argc, char** argv) {
int repeat = 10;
long long trials = 1000000ll;
if(argc > 0) {
--argc;
++argv;
}
while(argc > 0) {
if(argv[0][0] != '-') return usage();
else if(!handle_option(argc, argv, 'r', repeat)
&& !handle_option(argc, argv, 't', trials)) {
return usage();
}
--argc;
++argv;
}
try {
if(do_tests(repeat, trials)) {
return 0;
} else {
return EXIT_FAILURE;
}
} catch(...) {
std::cerr << boost::current_exception_diagnostic_information() << std::endl;
return EXIT_FAILURE;
}
}
|