| 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 | |
| 6 | // Test case for tail_mean.hpp |
| 7 | |
| 8 | #include <boost/random.hpp> |
| 9 | #include <boost/test/unit_test.hpp> |
| 10 | #include <boost/test/tools/floating_point_comparison.hpp> |
| 11 | #include <boost/accumulators/numeric/functional/vector.hpp> |
| 12 | #include <boost/accumulators/numeric/functional/complex.hpp> |
| 13 | #include <boost/accumulators/numeric/functional/valarray.hpp> |
| 14 | #include <boost/accumulators/accumulators.hpp> |
| 15 | #include <boost/accumulators/statistics/stats.hpp> |
| 16 | #include <boost/accumulators/statistics/tail_mean.hpp> |
| 17 | #include <boost/accumulators/statistics/tail_quantile.hpp> |
| 18 | |
| 19 | using namespace boost; |
| 20 | using namespace unit_test; |
| 21 | using namespace boost::accumulators; |
| 22 | |
| 23 | /////////////////////////////////////////////////////////////////////////////// |
| 24 | // test_stat |
| 25 | // |
| 26 | void test_stat() |
| 27 | { |
| 28 | // tolerance in % |
| 29 | double epsilon = 1; |
| 30 | |
| 31 | std::size_t n = 100000; // number of MC steps |
| 32 | std::size_t c = 10000; // cache size |
| 33 | |
| 34 | typedef accumulator_set<double, stats<tag::non_coherent_tail_mean<right>, tag::tail_quantile<right> > > accumulator_t_right1; |
| 35 | typedef accumulator_set<double, stats<tag::non_coherent_tail_mean<left>, tag::tail_quantile<left> > > accumulator_t_left1; |
| 36 | typedef accumulator_set<double, stats<tag::coherent_tail_mean<right>, tag::tail_quantile<right> > > accumulator_t_right2; |
| 37 | typedef accumulator_set<double, stats<tag::coherent_tail_mean<left>, tag::tail_quantile<left> > > accumulator_t_left2; |
| 38 | |
| 39 | accumulator_t_right1 acc0( right_tail_cache_size = c ); |
| 40 | accumulator_t_left1 acc1( left_tail_cache_size = c ); |
| 41 | accumulator_t_right2 acc2( right_tail_cache_size = c ); |
| 42 | accumulator_t_left2 acc3( left_tail_cache_size = c ); |
| 43 | |
| 44 | // a random number generator |
| 45 | boost::lagged_fibonacci607 rng; |
| 46 | |
| 47 | for (std::size_t i = 0; i < n; ++i) |
| 48 | { |
| 49 | double sample = rng(); |
| 50 | acc0(sample); |
| 51 | acc1(sample); |
| 52 | acc2(sample); |
| 53 | acc3(sample); |
| 54 | } |
| 55 | |
| 56 | // check uniform distribution |
| 57 | BOOST_CHECK_CLOSE( non_coherent_tail_mean(acc0, quantile_probability = 0.95), 0.975, epsilon ); |
| 58 | BOOST_CHECK_CLOSE( non_coherent_tail_mean(acc0, quantile_probability = 0.975), 0.9875, epsilon ); |
| 59 | BOOST_CHECK_CLOSE( non_coherent_tail_mean(acc0, quantile_probability = 0.99), 0.995, epsilon ); |
| 60 | BOOST_CHECK_CLOSE( non_coherent_tail_mean(acc0, quantile_probability = 0.999), 0.9995, epsilon ); |
| 61 | BOOST_CHECK_CLOSE( non_coherent_tail_mean(acc1, quantile_probability = 0.05), 0.025, 5*epsilon ); |
| 62 | BOOST_CHECK_CLOSE( non_coherent_tail_mean(acc1, quantile_probability = 0.025), 0.0125, 6*epsilon ); |
| 63 | BOOST_CHECK_CLOSE( non_coherent_tail_mean(acc1, quantile_probability = 0.01), 0.005, 8*epsilon ); |
| 64 | BOOST_CHECK_CLOSE( non_coherent_tail_mean(acc1, quantile_probability = 0.001), 0.0005, 25*epsilon ); |
| 65 | BOOST_CHECK_CLOSE( tail_mean(acc2, quantile_probability = 0.95), 0.975, epsilon ); |
| 66 | BOOST_CHECK_CLOSE( tail_mean(acc2, quantile_probability = 0.975), 0.9875, epsilon ); |
| 67 | BOOST_CHECK_CLOSE( tail_mean(acc2, quantile_probability = 0.99), 0.995, epsilon ); |
| 68 | BOOST_CHECK_CLOSE( tail_mean(acc2, quantile_probability = 0.999), 0.9995, epsilon ); |
| 69 | BOOST_CHECK_CLOSE( tail_mean(acc3, quantile_probability = 0.05), 0.025, 5*epsilon ); |
| 70 | BOOST_CHECK_CLOSE( tail_mean(acc3, quantile_probability = 0.025), 0.0125, 6*epsilon ); |
| 71 | BOOST_CHECK_CLOSE( tail_mean(acc3, quantile_probability = 0.01), 0.005, 8*epsilon ); |
| 72 | BOOST_CHECK_CLOSE( tail_mean(acc3, quantile_probability = 0.001), 0.0005, 25*epsilon ); |
| 73 | } |
| 74 | |
| 75 | /////////////////////////////////////////////////////////////////////////////// |
| 76 | // init_unit_test_suite |
| 77 | // |
| 78 | test_suite* init_unit_test_suite( int argc, char* argv[] ) |
| 79 | { |
| 80 | test_suite *test = BOOST_TEST_SUITE("tail_mean test" ); |
| 81 | |
| 82 | test->add(BOOST_TEST_CASE(&test_stat)); |
| 83 | |
| 84 | return test; |
| 85 | } |
| 86 | |
| 87 | |