weighted_pot_quantile.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 (weighted feature)
  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. using namespace boost;
  15. using namespace unit_test;
  16. using namespace boost::accumulators;
  17. ///////////////////////////////////////////////////////////////////////////////
  18. // test_stat
  19. //
  20. void test_stat()
  21. {
  22. // tolerance in %
  23. double epsilon = 1.;
  24. double mu1, mu2, l;
  25. mu1 = 1.;
  26. mu2 = -1.;
  27. l = 0.5;
  28. // two random number generators
  29. boost::lagged_fibonacci607 rng;
  30. boost::normal_distribution<> mean_sigma1(mu1,1);
  31. boost::normal_distribution<> mean_sigma2(mu2,1);
  32. boost::exponential_distribution<> lambda(l);
  33. boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal1(rng, mean_sigma1);
  34. boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal2(rng, mean_sigma2);
  35. boost::variate_generator<boost::lagged_fibonacci607&, boost::exponential_distribution<> > exponential(rng, lambda);
  36. accumulator_set<double, stats<tag::weighted_pot_quantile<right>(with_threshold_value)>, double > acc1(
  37. pot_threshold_value = 3.
  38. );
  39. accumulator_set<double, stats<tag::weighted_pot_quantile<right>(with_threshold_probability)>, double > acc2(
  40. right_tail_cache_size = 10000
  41. , pot_threshold_probability = 0.99
  42. );
  43. accumulator_set<double, stats<tag::weighted_pot_quantile<left>(with_threshold_value)>, double > acc3(
  44. pot_threshold_value = -3.
  45. );
  46. accumulator_set<double, stats<tag::weighted_pot_quantile<left>(with_threshold_probability)>, double > acc4(
  47. left_tail_cache_size = 10000
  48. , pot_threshold_probability = 0.01
  49. );
  50. accumulator_set<double, stats<tag::weighted_pot_quantile<right>(with_threshold_value)>, double > acc5(
  51. pot_threshold_value = 5.
  52. );
  53. accumulator_set<double, stats<tag::weighted_pot_quantile<right>(with_threshold_probability)>, double > acc6(
  54. right_tail_cache_size = 10000
  55. , pot_threshold_probability = 0.995
  56. );
  57. for (std::size_t i = 0; i < 100000; ++i)
  58. {
  59. double sample1 = normal1();
  60. double sample2 = normal2();
  61. acc1(sample1, weight = std::exp(-mu1 * (sample1 - 0.5 * mu1)));
  62. acc2(sample1, weight = std::exp(-mu1 * (sample1 - 0.5 * mu1)));
  63. acc3(sample2, weight = std::exp(-mu2 * (sample2 - 0.5 * mu2)));
  64. acc4(sample2, weight = std::exp(-mu2 * (sample2 - 0.5 * mu2)));
  65. }
  66. for (std::size_t i = 0; i < 100000; ++i)
  67. {
  68. double sample = exponential();
  69. acc5(sample, weight = 1./l * std::exp(-sample * (1. - l)));
  70. acc6(sample, weight = 1./l * std::exp(-sample * (1. - l)));
  71. }
  72. BOOST_CHECK_CLOSE( quantile(acc1, quantile_probability = 0.999), 3.090232, epsilon );
  73. BOOST_CHECK_CLOSE( quantile(acc2, quantile_probability = 0.999), 3.090232, epsilon );
  74. BOOST_CHECK_CLOSE( quantile(acc3, quantile_probability = 0.001), -3.090232, epsilon );
  75. BOOST_CHECK_CLOSE( quantile(acc4, quantile_probability = 0.001), -3.090232, epsilon );
  76. BOOST_CHECK_CLOSE( quantile(acc5, quantile_probability = 0.999), 6.908, epsilon );
  77. BOOST_CHECK_CLOSE( quantile(acc6, quantile_probability = 0.999), 6.908, epsilon );
  78. }
  79. ///////////////////////////////////////////////////////////////////////////////
  80. // init_unit_test_suite
  81. //
  82. test_suite* init_unit_test_suite( int argc, char* argv[] )
  83. {
  84. test_suite *test = BOOST_TEST_SUITE("weighted_pot_quantile test");
  85. test->add(BOOST_TEST_CASE(&test_stat));
  86. return test;
  87. }