test_piecewise_constant.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* test_piecewise_constant.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/piecewise_constant_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 <boost/range/algorithm/lower_bound.hpp>
  17. #include <boost/range/numeric.hpp>
  18. #include <vector>
  19. #include <iostream>
  20. #include <iomanip>
  21. #include "statistic_tests.hpp"
  22. class piecewise_constant
  23. {
  24. public:
  25. piecewise_constant(const std::vector<double>& intervals, const std::vector<double>& weights)
  26. : intervals(intervals),
  27. cumulative(1, 0.0)
  28. {
  29. boost::partial_sum(weights, std::back_inserter(cumulative));
  30. for(std::vector<double>::iterator iter = cumulative.begin(), end = cumulative.end();
  31. iter != end; ++iter)
  32. {
  33. *iter /= cumulative.back();
  34. }
  35. }
  36. double cdf(double x) const
  37. {
  38. std::size_t index = boost::lower_bound(intervals, x) - intervals.begin();
  39. if(index == 0) return 0;
  40. else if(index == intervals.size()) return 1;
  41. else {
  42. double lower_weight = cumulative[index - 1];
  43. double upper_weight = cumulative[index];
  44. double lower = intervals[index - 1];
  45. double upper = intervals[index];
  46. return lower_weight + (x - lower) / (upper - lower) * (upper_weight - lower_weight);
  47. }
  48. }
  49. private:
  50. std::vector<double> intervals;
  51. std::vector<double> cumulative;
  52. };
  53. double cdf(const piecewise_constant& dist, double x)
  54. {
  55. return dist.cdf(x);
  56. }
  57. bool do_test(int n, int max) {
  58. std::cout << "running piecewise_constant(p0, p1, ..., p" << n-1 << ")" << " " << max << " times: " << std::flush;
  59. std::vector<double> weights;
  60. {
  61. boost::mt19937 egen;
  62. for(int i = 0; i < n; ++i) {
  63. weights.push_back(egen());
  64. }
  65. }
  66. std::vector<double> intervals;
  67. for(int i = 0; i <= n; ++i) {
  68. intervals.push_back(i);
  69. }
  70. piecewise_constant expected(intervals, weights);
  71. boost::random::piecewise_constant_distribution<> dist(intervals, weights);
  72. boost::mt19937 gen;
  73. kolmogorov_experiment test(max);
  74. boost::variate_generator<boost::mt19937&, boost::random::piecewise_constant_distribution<> > vgen(gen, dist);
  75. double prob = test.probability(test.run(vgen, expected));
  76. bool result = prob < 0.99;
  77. const char* err = result? "" : "*";
  78. std::cout << std::setprecision(17) << prob << err << std::endl;
  79. std::cout << std::setprecision(6);
  80. return result;
  81. }
  82. bool do_tests(int repeat, int max_n, int trials) {
  83. boost::mt19937 gen;
  84. boost::uniform_int<> idist(1, max_n);
  85. int errors = 0;
  86. for(int i = 0; i < repeat; ++i) {
  87. if(!do_test(idist(gen), trials)) {
  88. ++errors;
  89. }
  90. }
  91. if(errors != 0) {
  92. std::cout << "*** " << errors << " errors detected ***" << std::endl;
  93. }
  94. return errors == 0;
  95. }
  96. int usage() {
  97. std::cerr << "Usage: test_piecewise_constant -r <repeat> -n <max n> -t <trials>" << std::endl;
  98. return 2;
  99. }
  100. template<class T>
  101. bool handle_option(int& argc, char**& argv, char opt, T& value) {
  102. if(argv[0][1] == opt && argc > 1) {
  103. --argc;
  104. ++argv;
  105. value = boost::lexical_cast<T>(argv[0]);
  106. return true;
  107. } else {
  108. return false;
  109. }
  110. }
  111. int main(int argc, char** argv) {
  112. int repeat = 10;
  113. int max_n = 10;
  114. int trials = 1000000;
  115. if(argc > 0) {
  116. --argc;
  117. ++argv;
  118. }
  119. while(argc > 0) {
  120. if(argv[0][0] != '-') return usage();
  121. else if(!handle_option(argc, argv, 'r', repeat)
  122. && !handle_option(argc, argv, 'n', max_n)
  123. && !handle_option(argc, argv, 't', trials)) {
  124. return usage();
  125. }
  126. --argc;
  127. ++argv;
  128. }
  129. try {
  130. if(do_tests(repeat, max_n, trials)) {
  131. return 0;
  132. } else {
  133. return EXIT_FAILURE;
  134. }
  135. } catch(...) {
  136. std::cerr << boost::current_exception_diagnostic_information() << std::endl;
  137. return EXIT_FAILURE;
  138. }
  139. }