test_discrete.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* test_poisson.cpp
  2. *
  3. * Copyright Steven Watanabe 2010
  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/discrete_distribution.hpp>
  12. #include <boost/random/uniform_int.hpp>
  13. #include <boost/random/mersenne_twister.hpp>
  14. #include <boost/lexical_cast.hpp>
  15. #include <boost/exception/diagnostic_information.hpp>
  16. #include <vector>
  17. #include <iostream>
  18. #include <numeric>
  19. #include "chi_squared_test.hpp"
  20. bool do_test(int n, long long max) {
  21. std::cout << "running discrete(p0, p1, ..., p" << n-1 << ")" << " " << max << " times: " << std::flush;
  22. std::vector<double> expected;
  23. {
  24. boost::mt19937 egen;
  25. for(int i = 0; i < n; ++i) {
  26. expected.push_back(egen());
  27. }
  28. double sum = std::accumulate(expected.begin(), expected.end(), 0.0);
  29. for(std::vector<double>::iterator iter = expected.begin(), end = expected.end(); iter != end; ++iter) {
  30. *iter /= sum;
  31. }
  32. }
  33. boost::random::discrete_distribution<> dist(expected);
  34. boost::mt19937 gen;
  35. std::vector<long long> results(expected.size());
  36. for(long long i = 0; i < max; ++i) {
  37. ++results[dist(gen)];
  38. }
  39. long long sum = std::accumulate(results.begin(), results.end(), 0ll);
  40. if(sum != max) {
  41. std::cout << "*** Failed: incorrect total: " << sum << " ***" << std::endl;
  42. return false;
  43. }
  44. double chsqr = chi_squared_test(results, expected, max);
  45. bool result = chsqr < 0.99;
  46. const char* err = result? "" : "*";
  47. std::cout << std::setprecision(17) << chsqr << err << std::endl;
  48. std::cout << std::setprecision(6);
  49. return result;
  50. }
  51. bool do_tests(int repeat, int max_n, long long trials) {
  52. boost::mt19937 gen;
  53. boost::uniform_int<> idist(1, max_n);
  54. int errors = 0;
  55. for(int i = 0; i < repeat; ++i) {
  56. if(!do_test(idist(gen), trials)) {
  57. ++errors;
  58. }
  59. }
  60. if(errors != 0) {
  61. std::cout << "*** " << errors << " errors detected ***" << std::endl;
  62. }
  63. return errors == 0;
  64. }
  65. int usage() {
  66. std::cerr << "Usage: test_discrete -r <repeat> -n <max n> -t <trials>" << std::endl;
  67. return 2;
  68. }
  69. template<class T>
  70. bool handle_option(int& argc, char**& argv, char opt, T& value) {
  71. if(argv[0][1] == opt && argc > 1) {
  72. --argc;
  73. ++argv;
  74. value = boost::lexical_cast<T>(argv[0]);
  75. return true;
  76. } else {
  77. return false;
  78. }
  79. }
  80. int main(int argc, char** argv) {
  81. int repeat = 10;
  82. int max_n = 10000;
  83. long long trials = 1000000ll;
  84. if(argc > 0) {
  85. --argc;
  86. ++argv;
  87. }
  88. while(argc > 0) {
  89. if(argv[0][0] != '-') return usage();
  90. else if(!handle_option(argc, argv, 'r', repeat)
  91. && !handle_option(argc, argv, 'n', max_n)
  92. && !handle_option(argc, argv, 't', trials)) {
  93. return usage();
  94. }
  95. --argc;
  96. ++argv;
  97. }
  98. try {
  99. if(do_tests(repeat, max_n, trials)) {
  100. return 0;
  101. } else {
  102. return EXIT_FAILURE;
  103. }
  104. } catch(...) {
  105. std::cerr << boost::current_exception_diagnostic_information() << std::endl;
  106. return EXIT_FAILURE;
  107. }
  108. }