test_bernoulli.cpp 2.7 KB

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