tail_quantile.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 tail_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/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 = 10000; // cache size
  27. typedef accumulator_set<double, stats<tag::tail_quantile<right> > > accumulator_t_right;
  28. typedef accumulator_set<double, stats<tag::tail_quantile<left> > > accumulator_t_left;
  29. accumulator_t_right acc0( right_tail_cache_size = c );
  30. accumulator_t_right acc1( right_tail_cache_size = c );
  31. accumulator_t_left acc2( left_tail_cache_size = c );
  32. accumulator_t_left acc3( left_tail_cache_size = c );
  33. // two random number generators
  34. boost::lagged_fibonacci607 rng;
  35. boost::normal_distribution<> mean_sigma(0,1);
  36. boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal(rng, mean_sigma);
  37. for (std::size_t i = 0; i < n; ++i)
  38. {
  39. double sample1 = rng();
  40. double sample2 = normal();
  41. acc0(sample1);
  42. acc1(sample2);
  43. acc2(sample1);
  44. acc3(sample2);
  45. }
  46. // check uniform distribution
  47. BOOST_CHECK_CLOSE( quantile(acc0, quantile_probability = 0.95 ), 0.95, epsilon );
  48. BOOST_CHECK_CLOSE( quantile(acc0, quantile_probability = 0.975), 0.975, epsilon );
  49. BOOST_CHECK_CLOSE( quantile(acc0, quantile_probability = 0.99 ), 0.99, epsilon );
  50. BOOST_CHECK_CLOSE( quantile(acc0, quantile_probability = 0.999), 0.999, epsilon );
  51. BOOST_CHECK_CLOSE( quantile(acc2, quantile_probability = 0.05 ), 0.05, 4*epsilon );
  52. BOOST_CHECK_CLOSE( quantile(acc2, quantile_probability = 0.025), 0.025, 5*epsilon );
  53. BOOST_CHECK_CLOSE( quantile(acc2, quantile_probability = 0.01 ), 0.01, 7*epsilon );
  54. BOOST_CHECK_CLOSE( quantile(acc2, quantile_probability = 0.001), 0.001, 22*epsilon );
  55. // check standard normal distribution
  56. BOOST_CHECK_CLOSE( quantile(acc1, quantile_probability = 0.975), 1.959963, epsilon );
  57. BOOST_CHECK_CLOSE( quantile(acc1, quantile_probability = 0.999), 3.090232, 3*epsilon );
  58. BOOST_CHECK_CLOSE( quantile(acc3, quantile_probability = 0.025), -1.959963, 2*epsilon );
  59. BOOST_CHECK_CLOSE( quantile(acc3, quantile_probability = 0.001), -3.090232, 3*epsilon );
  60. }
  61. ///////////////////////////////////////////////////////////////////////////////
  62. // init_unit_test_suite
  63. //
  64. test_suite* init_unit_test_suite( int argc, char* argv[] )
  65. {
  66. test_suite *test = BOOST_TEST_SUITE("tail_quantile test");
  67. test->add(BOOST_TEST_CASE(&test_stat));
  68. return test;
  69. }