p_square_quantile.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 p_square_quantile.hpp
  6. #include <boost/random.hpp>
  7. #include <boost/test/unit_test.hpp>
  8. #include <boost/test/floating_point_comparison.hpp>
  9. #include <boost/accumulators/numeric/functional/vector.hpp>
  10. #include <boost/accumulators/numeric/functional/complex.hpp>
  11. #include <boost/accumulators/numeric/functional/valarray.hpp>
  12. #include <boost/accumulators/accumulators.hpp>
  13. #include <boost/accumulators/statistics/stats.hpp>
  14. #include <boost/accumulators/statistics/p_square_quantile.hpp>
  15. #include <sstream>
  16. #include <boost/archive/text_oarchive.hpp>
  17. #include <boost/archive/text_iarchive.hpp>
  18. using namespace boost;
  19. using namespace unit_test;
  20. using namespace boost::accumulators;
  21. typedef accumulator_set<double, stats<tag::p_square_quantile> > accumulator_t;
  22. ///////////////////////////////////////////////////////////////////////////////
  23. // test_stat
  24. //
  25. void test_stat()
  26. {
  27. // tolerance in %
  28. double epsilon = 1;
  29. // a random number generator
  30. boost::lagged_fibonacci607 rng;
  31. accumulator_t acc0(quantile_probability = 0.001);
  32. accumulator_t acc1(quantile_probability = 0.01 );
  33. accumulator_t acc2(quantile_probability = 0.1 );
  34. accumulator_t acc3(quantile_probability = 0.25 );
  35. accumulator_t acc4(quantile_probability = 0.5 );
  36. accumulator_t acc5(quantile_probability = 0.75 );
  37. accumulator_t acc6(quantile_probability = 0.9 );
  38. accumulator_t acc7(quantile_probability = 0.99 );
  39. accumulator_t acc8(quantile_probability = 0.999);
  40. for (int i=0; i<100000; ++i)
  41. {
  42. double sample = rng();
  43. acc0(sample);
  44. acc1(sample);
  45. acc2(sample);
  46. acc3(sample);
  47. acc4(sample);
  48. acc5(sample);
  49. acc6(sample);
  50. acc7(sample);
  51. acc8(sample);
  52. }
  53. BOOST_CHECK_CLOSE( p_square_quantile(acc0), 0.001, 18*epsilon );
  54. BOOST_CHECK_CLOSE( p_square_quantile(acc1), 0.01 , 7*epsilon );
  55. BOOST_CHECK_CLOSE( p_square_quantile(acc2), 0.1 , 3*epsilon );
  56. BOOST_CHECK_CLOSE( p_square_quantile(acc3), 0.25 , 2*epsilon );
  57. BOOST_CHECK_CLOSE( p_square_quantile(acc4), 0.5 , epsilon );
  58. BOOST_CHECK_CLOSE( p_square_quantile(acc5), 0.75 , epsilon );
  59. BOOST_CHECK_CLOSE( p_square_quantile(acc6), 0.9 , epsilon );
  60. BOOST_CHECK_CLOSE( p_square_quantile(acc7), 0.99 , epsilon );
  61. BOOST_CHECK_CLOSE( p_square_quantile(acc8), 0.999, epsilon );
  62. }
  63. ///////////////////////////////////////////////////////////////////////////////
  64. // test_persistency
  65. //
  66. void test_persistency()
  67. {
  68. // "persistent" storage
  69. std::stringstream ss;
  70. // tolerance in %
  71. double epsilon = 1;
  72. // a random number generator
  73. boost::lagged_fibonacci607 rng;
  74. {
  75. accumulator_t acc1(quantile_probability = 0.75 );
  76. accumulator_t acc2(quantile_probability = 0.999);
  77. for (int i=0; i<100000; ++i)
  78. {
  79. double sample = rng();
  80. acc1(sample);
  81. acc2(sample);
  82. }
  83. BOOST_CHECK_CLOSE(p_square_quantile(acc1), 0.75 , epsilon);
  84. BOOST_CHECK_CLOSE(p_square_quantile(acc2), 0.999, epsilon);
  85. boost::archive::text_oarchive oa(ss);
  86. acc1.serialize(oa, 0);
  87. acc2.serialize(oa, 0);
  88. }
  89. accumulator_t acc1(quantile_probability = 0.75);
  90. accumulator_t acc2(quantile_probability = 0.999);
  91. boost::archive::text_iarchive ia(ss);
  92. acc1.serialize(ia, 0);
  93. acc2.serialize(ia, 0);
  94. BOOST_CHECK_CLOSE(p_square_quantile(acc1), 0.75 , epsilon);
  95. BOOST_CHECK_CLOSE(p_square_quantile(acc2), 0.999, epsilon);
  96. }
  97. ///////////////////////////////////////////////////////////////////////////////
  98. // init_unit_test_suite
  99. //
  100. test_suite* init_unit_test_suite( int argc, char* argv[] )
  101. {
  102. test_suite *test = BOOST_TEST_SUITE("p_square_quantile test");
  103. test->add(BOOST_TEST_CASE(&test_stat));
  104. test->add(BOOST_TEST_CASE(&test_persistency));
  105. return test;
  106. }