pot_quantile.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 pot_quantile.hpp
  6. #define BOOST_NUMERIC_FUNCTIONAL_STD_COMPLEX_SUPPORT
  7. #define BOOST_NUMERIC_FUNCTIONAL_STD_VALARRAY_SUPPORT
  8. #define BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT
  9. #include <boost/random.hpp>
  10. #include <boost/test/unit_test.hpp>
  11. #include <boost/test/floating_point_comparison.hpp>
  12. #include <boost/accumulators/accumulators.hpp>
  13. #include <boost/accumulators/statistics.hpp>
  14. #include <boost/accumulators/statistics/peaks_over_threshold.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. ///////////////////////////////////////////////////////////////////////////////
  22. // test_stat
  23. //
  24. void test_stat()
  25. {
  26. // tolerance in %
  27. double epsilon = 1.;
  28. // two random number generators
  29. boost::lagged_fibonacci607 rng;
  30. boost::normal_distribution<> mean_sigma(0,1);
  31. boost::exponential_distribution<> lambda(1);
  32. boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal(rng, mean_sigma);
  33. boost::variate_generator<boost::lagged_fibonacci607&, boost::exponential_distribution<> > exponential(rng, lambda);
  34. accumulator_set<double, stats<tag::pot_quantile<right>(with_threshold_value)> > acc1(
  35. pot_threshold_value = 3.
  36. );
  37. accumulator_set<double, stats<tag::pot_quantile<right>(with_threshold_probability)> > acc2(
  38. right_tail_cache_size = 2000
  39. , pot_threshold_probability = 0.99
  40. );
  41. accumulator_set<double, stats<tag::pot_quantile<left>(with_threshold_value)> > acc3(
  42. pot_threshold_value = -3.
  43. );
  44. accumulator_set<double, stats<tag::pot_quantile<left>(with_threshold_probability)> > acc4(
  45. left_tail_cache_size = 2000
  46. , pot_threshold_probability = 0.01
  47. );
  48. accumulator_set<double, stats<tag::pot_quantile<right>(with_threshold_value)> > acc5(
  49. pot_threshold_value = 5.
  50. );
  51. accumulator_set<double, stats<tag::pot_quantile<right>(with_threshold_probability)> > acc6(
  52. right_tail_cache_size = 2000
  53. , pot_threshold_probability = 0.995
  54. );
  55. for (std::size_t i = 0; i < 100000; ++i)
  56. {
  57. double sample = normal();
  58. acc1(sample);
  59. acc2(sample);
  60. acc3(sample);
  61. acc4(sample);
  62. }
  63. for (std::size_t i = 0; i < 100000; ++i)
  64. {
  65. double sample = exponential();
  66. acc5(sample);
  67. acc6(sample);
  68. }
  69. BOOST_CHECK_CLOSE( quantile(acc1, quantile_probability = 0.999), 3.090232, 3*epsilon );
  70. BOOST_CHECK_CLOSE( quantile(acc2, quantile_probability = 0.999), 3.090232, 2*epsilon );
  71. BOOST_CHECK_CLOSE( quantile(acc3, quantile_probability = 0.001), -3.090232, 2*epsilon );
  72. BOOST_CHECK_CLOSE( quantile(acc4, quantile_probability = 0.001), -3.090232, 2*epsilon );
  73. BOOST_CHECK_CLOSE( quantile(acc5, quantile_probability = 0.999), 6.908, 3*epsilon );
  74. BOOST_CHECK_CLOSE( quantile(acc6, quantile_probability = 0.999), 6.908, 3*epsilon );
  75. }
  76. ///////////////////////////////////////////////////////////////////////////////
  77. // test_persistency
  78. //
  79. void test_persistency()
  80. {
  81. // tolerance in %
  82. double epsilon = 1.;
  83. // "persistent" storage
  84. std::stringstream ss;
  85. {
  86. // random number generators
  87. boost::lagged_fibonacci607 rng;
  88. boost::normal_distribution<> mean_sigma(0,1);
  89. boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal(rng, mean_sigma);
  90. accumulator_set<double, stats<tag::pot_quantile<right>(with_threshold_value)> > acc(pot_threshold_value = 3.);
  91. for (std::size_t i = 0; i < 100000; ++i)
  92. acc(normal());
  93. BOOST_CHECK_CLOSE(quantile(acc, quantile_probability = 0.999), 3.090232, 3*epsilon);
  94. boost::archive::text_oarchive oa(ss);
  95. acc.serialize(oa, 0);
  96. }
  97. accumulator_set<double, stats<tag::pot_quantile<right>(with_threshold_value)> > acc(pot_threshold_value = 3.);
  98. boost::archive::text_iarchive ia(ss);
  99. acc.serialize(ia, 0);
  100. BOOST_CHECK_CLOSE(quantile(acc, quantile_probability = 0.999), 3.090232, 3*epsilon);
  101. }
  102. ///////////////////////////////////////////////////////////////////////////////
  103. // init_unit_test_suite
  104. //
  105. test_suite* init_unit_test_suite( int argc, char* argv[] )
  106. {
  107. test_suite *test = BOOST_TEST_SUITE("pot_quantile test");
  108. test->add(BOOST_TEST_CASE(&test_stat));
  109. test->add(BOOST_TEST_CASE(&test_persistency));
  110. return test;
  111. }