weighted_tail_mean.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_mean.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_mean.hpp>
  15. #include <boost/accumulators/statistics/weighted_tail_quantile.hpp>
  16. using namespace boost;
  17. using namespace unit_test;
  18. using namespace boost::accumulators;
  19. ///////////////////////////////////////////////////////////////////////////////
  20. // test_stat
  21. //
  22. void test_stat()
  23. {
  24. // tolerance in %
  25. double epsilon = 1;
  26. std::size_t n = 100000; // number of MC steps
  27. std::size_t c = 25000; // cache size
  28. accumulator_set<double, stats<tag::non_coherent_weighted_tail_mean<right> >, double >
  29. acc0( right_tail_cache_size = c );
  30. accumulator_set<double, stats<tag::non_coherent_weighted_tail_mean<left> >, double >
  31. acc1( left_tail_cache_size = c );
  32. // random number generators
  33. boost::lagged_fibonacci607 rng;
  34. for (std::size_t i = 0; i < n; ++i)
  35. {
  36. double smpl = std::sqrt(rng());
  37. acc0(smpl, weight = 1./smpl);
  38. }
  39. for (std::size_t i = 0; i < n; ++i)
  40. {
  41. double smpl = rng();
  42. acc1(smpl*smpl, weight = smpl);
  43. }
  44. // check uniform distribution
  45. BOOST_CHECK_CLOSE( non_coherent_weighted_tail_mean(acc0, quantile_probability = 0.95), 0.975, epsilon );
  46. BOOST_CHECK_CLOSE( non_coherent_weighted_tail_mean(acc0, quantile_probability = 0.975), 0.9875, epsilon );
  47. BOOST_CHECK_CLOSE( non_coherent_weighted_tail_mean(acc0, quantile_probability = 0.99), 0.995, epsilon );
  48. BOOST_CHECK_CLOSE( non_coherent_weighted_tail_mean(acc0, quantile_probability = 0.999), 0.9995, epsilon );
  49. BOOST_CHECK_CLOSE( non_coherent_weighted_tail_mean(acc1, quantile_probability = 0.05), 0.025, epsilon );
  50. BOOST_CHECK_CLOSE( non_coherent_weighted_tail_mean(acc1, quantile_probability = 0.025), 0.0125, epsilon );
  51. BOOST_CHECK_CLOSE( non_coherent_weighted_tail_mean(acc1, quantile_probability = 0.01), 0.005, epsilon );
  52. BOOST_CHECK_CLOSE( non_coherent_weighted_tail_mean(acc1, quantile_probability = 0.001), 0.0005, 5*epsilon );
  53. }
  54. ///////////////////////////////////////////////////////////////////////////////
  55. // init_unit_test_suite
  56. //
  57. test_suite* init_unit_test_suite( int argc, char* argv[] )
  58. {
  59. test_suite *test = BOOST_TEST_SUITE("weighted_tail_mean test");
  60. test->add(BOOST_TEST_CASE(&test_stat));
  61. return test;
  62. }