p_square_cumul_dist.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // (C) Copyright Eric Niebler, Olivier Gygi 2006.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // Test case for p_square_cumul_dist.hpp
  6. #include <cmath>
  7. #include <boost/random.hpp>
  8. #include <boost/test/unit_test.hpp>
  9. #include <boost/test/floating_point_comparison.hpp>
  10. #include <boost/accumulators/numeric/functional/vector.hpp>
  11. #include <boost/accumulators/numeric/functional/complex.hpp>
  12. #include <boost/accumulators/numeric/functional/valarray.hpp>
  13. #include <boost/accumulators/accumulators.hpp>
  14. #include <boost/accumulators/statistics/stats.hpp>
  15. #include <boost/accumulators/statistics/p_square_cumul_dist.hpp>
  16. #include <sstream>
  17. #include <boost/archive/text_oarchive.hpp>
  18. #include <boost/archive/text_iarchive.hpp>
  19. using namespace boost;
  20. using namespace unit_test;
  21. using namespace boost::accumulators;
  22. ///////////////////////////////////////////////////////////////////////////////
  23. // erf() not known by VC++ compiler!
  24. // my_erf() computes error function by numerically integrating with trapezoidal rule
  25. //
  26. double my_erf(double const& x, int const& n = 1000)
  27. {
  28. double sum = 0.;
  29. double delta = x/n;
  30. for (int i = 1; i < n; ++i)
  31. sum += std::exp(-i*i*delta*delta) * delta;
  32. sum += 0.5 * delta * (1. + std::exp(-x*x));
  33. return sum * 2. / std::sqrt(3.141592653);
  34. }
  35. typedef accumulator_set<double, stats<tag::p_square_cumulative_distribution> > accumulator_t;
  36. typedef iterator_range<std::vector<std::pair<double, double> >::iterator > histogram_type;
  37. ///////////////////////////////////////////////////////////////////////////////
  38. // test_stat
  39. //
  40. void test_stat()
  41. {
  42. // tolerance in %
  43. double epsilon = 3;
  44. accumulator_t acc(p_square_cumulative_distribution_num_cells = 100);
  45. // two random number generators
  46. boost::lagged_fibonacci607 rng;
  47. boost::normal_distribution<> mean_sigma(0,1);
  48. boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal(rng, mean_sigma);
  49. for (std::size_t i=0; i<1000000; ++i)
  50. {
  51. acc(normal());
  52. }
  53. histogram_type histogram = p_square_cumulative_distribution(acc);
  54. for (std::size_t i = 0; i < histogram.size(); ++i)
  55. {
  56. // problem with small results: epsilon is relative (in percent), not absolute!
  57. if ( histogram[i].second > 0.001 )
  58. BOOST_CHECK_CLOSE( 0.5 * (1.0 + my_erf( histogram[i].first / std::sqrt(2.0) )), histogram[i].second, epsilon );
  59. }
  60. }
  61. ///////////////////////////////////////////////////////////////////////////////
  62. // test_persistency
  63. //
  64. void test_persistency()
  65. {
  66. // "persistent" storage
  67. std::stringstream ss;
  68. // tolerance in %
  69. double epsilon = 3;
  70. {
  71. accumulator_t acc(p_square_cumulative_distribution_num_cells = 100);
  72. // two random number generators
  73. boost::lagged_fibonacci607 rng;
  74. boost::normal_distribution<> mean_sigma(0,1);
  75. boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal(rng, mean_sigma);
  76. for (std::size_t i=0; i<1000000; ++i)
  77. {
  78. acc(normal());
  79. }
  80. histogram_type histogram = p_square_cumulative_distribution(acc);
  81. BOOST_CHECK_CLOSE(0.5 * (1.0 + my_erf(histogram[25].first / std::sqrt(2.0))), histogram[25].second, epsilon);
  82. BOOST_CHECK_CLOSE(0.5 * (1.0 + my_erf(histogram[50].first / std::sqrt(2.0))), histogram[50].second, epsilon);
  83. BOOST_CHECK_CLOSE(0.5 * (1.0 + my_erf(histogram[75].first / std::sqrt(2.0))), histogram[75].second, epsilon);
  84. boost::archive::text_oarchive oa(ss);
  85. acc.serialize(oa, 0);
  86. }
  87. accumulator_t acc(p_square_cumulative_distribution_num_cells = 100);
  88. boost::archive::text_iarchive ia(ss);
  89. acc.serialize(ia, 0);
  90. histogram_type histogram = p_square_cumulative_distribution(acc);
  91. BOOST_CHECK_CLOSE(0.5 * (1.0 + my_erf(histogram[25].first / std::sqrt(2.0))), histogram[25].second, epsilon);
  92. BOOST_CHECK_CLOSE(0.5 * (1.0 + my_erf(histogram[50].first / std::sqrt(2.0))), histogram[50].second, epsilon);
  93. BOOST_CHECK_CLOSE(0.5 * (1.0 + my_erf(histogram[75].first / std::sqrt(2.0))), histogram[75].second, epsilon);
  94. }
  95. ///////////////////////////////////////////////////////////////////////////////
  96. // init_unit_test_suite
  97. //
  98. test_suite* init_unit_test_suite( int argc, char* argv[] )
  99. {
  100. test_suite *test = BOOST_TEST_SUITE("p_square_cumulative_distribution test");
  101. test->add(BOOST_TEST_CASE(&test_stat));
  102. test->add(BOOST_TEST_CASE(&test_persistency));
  103. return test;
  104. }