weighted_tail_quantile.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // (C) Copyright 2006 Eric Niebler, Olivier Gygi.
  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 weighted_tail_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/weighted_tail_quantile.hpp>
  15. using namespace boost;
  16. using namespace unit_test;
  17. using namespace boost::accumulators;
  18. ///////////////////////////////////////////////////////////////////////////////
  19. // test_stat
  20. //
  21. void test_stat()
  22. {
  23. // tolerance in %
  24. double epsilon = 1;
  25. std::size_t n = 100000; // number of MC steps
  26. std::size_t c = 20000; // cache size
  27. double mu1 = 1.0;
  28. double mu2 = -1.0;
  29. boost::lagged_fibonacci607 rng;
  30. boost::normal_distribution<> mean_sigma1(mu1,1);
  31. boost::normal_distribution<> mean_sigma2(mu2,1);
  32. boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal1(rng, mean_sigma1);
  33. boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal2(rng, mean_sigma2);
  34. accumulator_set<double, stats<tag::weighted_tail_quantile<right> >, double>
  35. acc1(right_tail_cache_size = c);
  36. accumulator_set<double, stats<tag::weighted_tail_quantile<left> >, double>
  37. acc2(left_tail_cache_size = c);
  38. for (std::size_t i = 0; i < n; ++i)
  39. {
  40. double sample1 = normal1();
  41. double sample2 = normal2();
  42. acc1(sample1, weight = std::exp(-mu1 * (sample1 - 0.5 * mu1)));
  43. acc2(sample2, weight = std::exp(-mu2 * (sample2 - 0.5 * mu2)));
  44. }
  45. // check standard normal distribution
  46. BOOST_CHECK_CLOSE( quantile(acc1, quantile_probability = 0.975), 1.959963, epsilon );
  47. BOOST_CHECK_CLOSE( quantile(acc1, quantile_probability = 0.999), 3.090232, epsilon );
  48. BOOST_CHECK_CLOSE( quantile(acc2, quantile_probability = 0.025), -1.959963, epsilon );
  49. BOOST_CHECK_CLOSE( quantile(acc2, quantile_probability = 0.001), -3.090232, epsilon );
  50. }
  51. ///////////////////////////////////////////////////////////////////////////////
  52. // init_unit_test_suite
  53. //
  54. test_suite* init_unit_test_suite( int argc, char* argv[] )
  55. {
  56. test_suite *test = BOOST_TEST_SUITE("weighted_tail_quantile test");
  57. test->add(BOOST_TEST_CASE(&test_stat));
  58. return test;
  59. }