test_piecewise_linear_distribution.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* test_piecewise_linear_distribution.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/linear_congruential.hpp>
  13. #include <boost/assign/list_of.hpp>
  14. #include <sstream>
  15. #include <vector>
  16. #include "concepts.hpp"
  17. #define BOOST_TEST_MAIN
  18. #include <boost/test/unit_test.hpp>
  19. using boost::random::test::RandomNumberDistribution;
  20. using boost::random::piecewise_linear_distribution;
  21. BOOST_CONCEPT_ASSERT((RandomNumberDistribution< piecewise_linear_distribution<> >));
  22. struct gen {
  23. double operator()(double arg) {
  24. if(arg < 97) return 100;
  25. else if(arg < 101) return 3;
  26. else if(arg < 105) return 1;
  27. else if(arg < 109) return 2;
  28. else if(arg < 113) return 1;
  29. else if(arg < 117) return 5;
  30. else return 100;
  31. }
  32. };
  33. #define CHECK_SEQUENCE(actual, expected) \
  34. do { \
  35. std::vector<double> _actual = (actual); \
  36. std::vector<double> _expected = (expected); \
  37. BOOST_CHECK_EQUAL_COLLECTIONS( \
  38. _actual.begin(), _actual.end(), \
  39. _expected.begin(), _expected.end()); \
  40. } while(false)
  41. using boost::assign::list_of;
  42. BOOST_AUTO_TEST_CASE(test_constructors) {
  43. boost::random::piecewise_linear_distribution<> dist;
  44. CHECK_SEQUENCE(dist.intervals(), list_of(0.0)(1.0));
  45. CHECK_SEQUENCE(dist.densities(), list_of(1.0)(1.0));
  46. #ifndef BOOST_NO_CXX11_HDR_INITIALIZER_LIST
  47. boost::random::piecewise_linear_distribution<> dist_il = {
  48. { 99, 103, 107, 111, 115 },
  49. gen()
  50. };
  51. CHECK_SEQUENCE(dist_il.intervals(), list_of(99)(103)(107)(111)(115));
  52. CHECK_SEQUENCE(dist_il.densities(),
  53. list_of(.09375)(.03125)(0.0625)(.03125)(.15625));
  54. boost::random::piecewise_linear_distribution<> dist_il2 = {
  55. { 99 },
  56. gen()
  57. };
  58. CHECK_SEQUENCE(dist_il2.intervals(), list_of(0.0)(1.0));
  59. CHECK_SEQUENCE(dist_il2.densities(), list_of(1.0)(1.0));
  60. #endif
  61. std::vector<double> intervals = boost::assign::list_of(0)(1)(2)(3)(5);
  62. std::vector<double> weights = boost::assign::list_of(3)(1)(2)(1)(2);
  63. std::vector<double> intervals2 = boost::assign::list_of(99);
  64. std::vector<double> weights2 = boost::assign::list_of(2);
  65. boost::random::piecewise_linear_distribution<> dist_r(intervals, weights);
  66. CHECK_SEQUENCE(dist_r.intervals(), list_of(0)(1)(2)(3)(5));
  67. CHECK_SEQUENCE(dist_r.densities(), list_of(.375)(.125)(.25)(.125)(.25));
  68. boost::random::piecewise_linear_distribution<>
  69. dist_r2(intervals2, weights2);
  70. CHECK_SEQUENCE(dist_r2.intervals(), list_of(0.0)(1.0));
  71. CHECK_SEQUENCE(dist_r2.densities(), list_of(1.0)(1.0));
  72. boost::random::piecewise_linear_distribution<> dist_it(
  73. intervals.begin(), intervals.end(), weights.begin());
  74. CHECK_SEQUENCE(dist_it.intervals(), list_of(0)(1)(2)(3)(5));
  75. CHECK_SEQUENCE(dist_it.densities(), list_of(.375)(.125)(.25)(.125)(.25));
  76. boost::random::piecewise_linear_distribution<> dist_it2(
  77. intervals2.begin(), intervals2.end(), weights2.begin());
  78. CHECK_SEQUENCE(dist_it2.intervals(), list_of(0.0)(1.0));
  79. CHECK_SEQUENCE(dist_it2.densities(), list_of(1.0)(1.0));
  80. boost::random::piecewise_linear_distribution<> dist_fun(4, 99,115, gen());
  81. CHECK_SEQUENCE(dist_fun.intervals(), list_of(99)(103)(107)(111)(115));
  82. CHECK_SEQUENCE(dist_fun.densities(),
  83. list_of(.09375)(.03125)(0.0625)(.03125)(.15625));
  84. boost::random::piecewise_linear_distribution<>
  85. dist_fun2(1, 99, 115, gen());
  86. CHECK_SEQUENCE(dist_fun2.intervals(), list_of(99)(115));
  87. CHECK_SEQUENCE(dist_fun2.densities(), list_of(0.046875)(0.078125));
  88. boost::random::piecewise_linear_distribution<> copy(dist);
  89. BOOST_CHECK_EQUAL(dist, copy);
  90. boost::random::piecewise_linear_distribution<> copy_r(dist_r);
  91. BOOST_CHECK_EQUAL(dist_r, copy_r);
  92. boost::random::piecewise_linear_distribution<> notpow2(3, 99, 111, gen());
  93. BOOST_REQUIRE_EQUAL(notpow2.densities().size(), 4u);
  94. BOOST_CHECK_CLOSE_FRACTION(notpow2.densities()[0], 0.15, 1e-12);
  95. BOOST_CHECK_CLOSE_FRACTION(notpow2.densities()[1], 0.05, 1e-12);
  96. BOOST_CHECK_CLOSE_FRACTION(notpow2.densities()[2], 0.1, 1e-12);
  97. BOOST_CHECK_CLOSE_FRACTION(notpow2.densities()[3], 0.05, 1e-12);
  98. boost::random::piecewise_linear_distribution<> copy_notpow2(notpow2);
  99. BOOST_CHECK_EQUAL(notpow2, copy_notpow2);
  100. }
  101. BOOST_AUTO_TEST_CASE(test_param) {
  102. std::vector<double> intervals = boost::assign::list_of(0)(1)(2)(3)(5);
  103. std::vector<double> weights = boost::assign::list_of(3)(1)(2)(1)(2);
  104. std::vector<double> intervals2 = boost::assign::list_of(99);
  105. std::vector<double> weights2 = boost::assign::list_of(2);
  106. boost::random::piecewise_linear_distribution<> dist(intervals, weights);
  107. boost::random::piecewise_linear_distribution<>::param_type
  108. param = dist.param();
  109. CHECK_SEQUENCE(param.intervals(), list_of(0)(1)(2)(3)(5));
  110. CHECK_SEQUENCE(param.densities(), list_of(.375)(.125)(.25)(.125)(.25));
  111. boost::random::piecewise_linear_distribution<> copy1(param);
  112. BOOST_CHECK_EQUAL(dist, copy1);
  113. boost::random::piecewise_linear_distribution<> copy2;
  114. copy2.param(param);
  115. BOOST_CHECK_EQUAL(dist, copy2);
  116. boost::random::piecewise_linear_distribution<>::param_type
  117. param_copy = param;
  118. BOOST_CHECK_EQUAL(param, param_copy);
  119. BOOST_CHECK(param == param_copy);
  120. BOOST_CHECK(!(param != param_copy));
  121. boost::random::piecewise_linear_distribution<>::param_type param_default;
  122. CHECK_SEQUENCE(param_default.intervals(), list_of(0.0)(1.0));
  123. CHECK_SEQUENCE(param_default.densities(), list_of(1.0)(1.0));
  124. BOOST_CHECK(param != param_default);
  125. BOOST_CHECK(!(param == param_default));
  126. #ifndef BOOST_NO_CXX11_HDR_INITIALIZER_LIST
  127. boost::random::piecewise_linear_distribution<>::param_type parm_il = {
  128. { 99, 103, 107, 111, 115 },
  129. gen()
  130. };
  131. CHECK_SEQUENCE(parm_il.intervals(), list_of(99)(103)(107)(111)(115));
  132. CHECK_SEQUENCE(parm_il.densities(),
  133. list_of(.09375)(.03125)(0.0625)(.03125)(.15625));
  134. boost::random::piecewise_linear_distribution<>::param_type parm_il2 = {
  135. { 99 },
  136. gen()
  137. };
  138. CHECK_SEQUENCE(parm_il2.intervals(), list_of(0.0)(1.0));
  139. CHECK_SEQUENCE(parm_il2.densities(), list_of(1.0)(1.0));
  140. #endif
  141. boost::random::piecewise_linear_distribution<>::param_type
  142. parm_r(intervals, weights);
  143. CHECK_SEQUENCE(parm_r.intervals(), list_of(0)(1)(2)(3)(5));
  144. CHECK_SEQUENCE(parm_r.densities(), list_of(.375)(.125)(.25)(.125)(.25));
  145. boost::random::piecewise_linear_distribution<>::param_type
  146. parm_r2(intervals2, weights2);
  147. CHECK_SEQUENCE(parm_r2.intervals(), list_of(0.0)(1.0));
  148. CHECK_SEQUENCE(parm_r2.densities(), list_of(1.0)(1.0));
  149. boost::random::piecewise_linear_distribution<>::param_type
  150. parm_it(intervals.begin(), intervals.end(), weights.begin());
  151. CHECK_SEQUENCE(parm_it.intervals(), list_of(0)(1)(2)(3)(5));
  152. CHECK_SEQUENCE(parm_it.densities(), list_of(.375)(.125)(.25)(.125)(.25));
  153. boost::random::piecewise_linear_distribution<>::param_type
  154. parm_it2(intervals2.begin(), intervals2.end(), weights2.begin());
  155. CHECK_SEQUENCE(parm_it2.intervals(), list_of(0.0)(1.0));
  156. CHECK_SEQUENCE(parm_it2.densities(), list_of(1.0)(1.0));
  157. boost::random::piecewise_linear_distribution<>::param_type
  158. parm_fun(4, 99, 115, gen());
  159. CHECK_SEQUENCE(parm_fun.intervals(), list_of(99)(103)(107)(111)(115));
  160. CHECK_SEQUENCE(parm_fun.densities(),
  161. list_of(.09375)(.03125)(0.0625)(.03125)(.15625));
  162. boost::random::piecewise_linear_distribution<>::param_type
  163. parm_fun2(1, 99, 115, gen());
  164. CHECK_SEQUENCE(parm_fun2.intervals(), list_of(99)(115));
  165. CHECK_SEQUENCE(parm_fun2.densities(), list_of(0.046875)(0.078125));
  166. }
  167. BOOST_AUTO_TEST_CASE(test_min_max) {
  168. std::vector<double> intervals = boost::assign::list_of(0)(1)(2)(3)(5);
  169. std::vector<double> weights = boost::assign::list_of(3)(1)(2)(1)(2);
  170. boost::random::piecewise_linear_distribution<> dist;
  171. BOOST_CHECK_EQUAL((dist.min)(), 0.0);
  172. BOOST_CHECK_EQUAL((dist.max)(), 1.0);
  173. boost::random::piecewise_linear_distribution<> dist_r(intervals, weights);
  174. BOOST_CHECK_EQUAL((dist_r.min)(), 0.0);
  175. BOOST_CHECK_EQUAL((dist_r.max)(), 5.0);
  176. }
  177. BOOST_AUTO_TEST_CASE(test_comparison) {
  178. std::vector<double> intervals = boost::assign::list_of(0)(1)(2)(3)(5);
  179. std::vector<double> weights = boost::assign::list_of(3)(1)(2)(1)(2);
  180. boost::random::piecewise_linear_distribution<> dist;
  181. boost::random::piecewise_linear_distribution<> dist_copy(dist);
  182. boost::random::piecewise_linear_distribution<> dist_r(intervals, weights);
  183. boost::random::piecewise_linear_distribution<> dist_r_copy(dist_r);
  184. BOOST_CHECK(dist == dist_copy);
  185. BOOST_CHECK(!(dist != dist_copy));
  186. BOOST_CHECK(dist_r == dist_r_copy);
  187. BOOST_CHECK(!(dist_r != dist_r_copy));
  188. BOOST_CHECK(dist != dist_r);
  189. BOOST_CHECK(!(dist == dist_r));
  190. }
  191. BOOST_AUTO_TEST_CASE(test_streaming) {
  192. std::vector<double> intervals = boost::assign::list_of(0)(1)(2)(3)(5);
  193. std::vector<double> weights = boost::assign::list_of(3)(1)(2)(1)(2);
  194. boost::random::piecewise_linear_distribution<> dist(intervals, weights);
  195. std::stringstream stream;
  196. stream << dist;
  197. boost::random::piecewise_linear_distribution<> restored_dist;
  198. stream >> restored_dist;
  199. BOOST_CHECK_EQUAL(dist, restored_dist);
  200. }
  201. BOOST_AUTO_TEST_CASE(test_generation) {
  202. std::vector<double> intervals = boost::assign::list_of(1)(2);
  203. std::vector<double> weights = boost::assign::list_of(1)(1);
  204. boost::minstd_rand0 gen;
  205. boost::random::piecewise_linear_distribution<> dist;
  206. boost::random::piecewise_linear_distribution<> dist_r(intervals, weights);
  207. for(int i = 0; i < 10; ++i) {
  208. double value = dist(gen);
  209. BOOST_CHECK_GE(value, 0.0);
  210. BOOST_CHECK_LT(value, 1.0);
  211. double value_r = dist_r(gen);
  212. BOOST_CHECK_GE(value_r, 1.0);
  213. BOOST_CHECK_LT(value_r, 2.0);
  214. double value_param = dist_r(gen, dist.param());
  215. BOOST_CHECK_GE(value_param, 0.0);
  216. BOOST_CHECK_LT(value_param, 1.0);
  217. double value_r_param = dist(gen, dist_r.param());
  218. BOOST_CHECK_GE(value_r_param, 1.0);
  219. BOOST_CHECK_LT(value_r_param, 2.0);
  220. }
  221. }