extended_p_square.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // (C) Copyright Eric Niebler 2005.
  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 extended_p_square.hpp
  6. #include <iostream>
  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/extended_p_square.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. typedef accumulator_set<double, stats<tag::extended_p_square> > accumulator_t;
  23. ///////////////////////////////////////////////////////////////////////////////
  24. // test_stat
  25. //
  26. void test_stat()
  27. {
  28. // tolerance
  29. double epsilon = 3;
  30. // a random number generator
  31. boost::lagged_fibonacci607 rng;
  32. std::vector<double> probs;
  33. probs.push_back(0.001);
  34. probs.push_back(0.01 );
  35. probs.push_back(0.1 );
  36. probs.push_back(0.25 );
  37. probs.push_back(0.5 );
  38. probs.push_back(0.75 );
  39. probs.push_back(0.9 );
  40. probs.push_back(0.99 );
  41. probs.push_back(0.999);
  42. accumulator_t acc(extended_p_square_probabilities = probs);
  43. for (int i=0; i<10000; ++i)
  44. acc(rng());
  45. BOOST_CHECK_GE(extended_p_square(acc)[0], 0.0005);
  46. BOOST_CHECK_LE(extended_p_square(acc)[0], 0.0015);
  47. BOOST_CHECK_CLOSE(extended_p_square(acc)[1], probs[1], 15);
  48. BOOST_CHECK_CLOSE(extended_p_square(acc)[2], probs[2], 5);
  49. for (std::size_t i=3; i<probs.size(); ++i)
  50. {
  51. BOOST_CHECK_CLOSE(extended_p_square(acc)[i], probs[i], epsilon);
  52. }
  53. }
  54. ///////////////////////////////////////////////////////////////////////////////
  55. // test_persistency
  56. //
  57. void test_persistency()
  58. {
  59. // "persistent" storage
  60. std::stringstream ss;
  61. // tolerance
  62. double epsilon = 3.;
  63. std::vector<double> probs;
  64. probs.push_back(0.75);
  65. {
  66. accumulator_t acc(extended_p_square_probabilities = probs);
  67. // a random number generator
  68. boost::lagged_fibonacci607 rng;
  69. for (int i=0; i<10000; ++i)
  70. acc(rng());
  71. BOOST_CHECK_CLOSE(extended_p_square(acc)[0], probs[0], epsilon);
  72. boost::archive::text_oarchive oa(ss);
  73. acc.serialize(oa, 0);
  74. }
  75. accumulator_t acc(extended_p_square_probabilities = probs);
  76. boost::archive::text_iarchive ia(ss);
  77. acc.serialize(ia, 0);
  78. BOOST_CHECK_CLOSE(extended_p_square(acc)[0], probs[0], epsilon);
  79. }
  80. ///////////////////////////////////////////////////////////////////////////////
  81. // init_unit_test_suite
  82. //
  83. test_suite* init_unit_test_suite( int argc, char* argv[] )
  84. {
  85. test_suite *test = BOOST_TEST_SUITE("extended_p_square test");
  86. test->add(BOOST_TEST_CASE(&test_stat));
  87. test->add(BOOST_TEST_CASE(&test_persistency));
  88. return test;
  89. }