test_piecewise_linear.cpp 5.0 KB

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