| 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 | |
| 6 | // Test case for pot_quantile.hpp |
| 7 | |
| 8 | #define BOOST_NUMERIC_FUNCTIONAL_STD_COMPLEX_SUPPORT |
| 9 | #define BOOST_NUMERIC_FUNCTIONAL_STD_VALARRAY_SUPPORT |
| 10 | #define BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT |
| 11 | |
| 12 | #include <boost/random.hpp> |
| 13 | #include <boost/test/unit_test.hpp> |
| 14 | #include <boost/test/tools/floating_point_comparison.hpp> |
| 15 | #include <boost/accumulators/accumulators.hpp> |
| 16 | #include <boost/accumulators/statistics.hpp> |
| 17 | #include <boost/accumulators/statistics/peaks_over_threshold.hpp> |
| 18 | #include <sstream> |
| 19 | #include <boost/archive/text_oarchive.hpp> |
| 20 | #include <boost/archive/text_iarchive.hpp> |
| 21 | |
| 22 | using namespace boost; |
| 23 | using namespace unit_test; |
| 24 | using namespace boost::accumulators; |
| 25 | |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | // test_stat |
| 28 | // |
| 29 | void test_stat() |
| 30 | { |
| 31 | // tolerance in % |
| 32 | double epsilon = 1.; |
| 33 | |
| 34 | // two random number generators |
| 35 | boost::lagged_fibonacci607 rng; |
| 36 | boost::normal_distribution<> mean_sigma(0,1); |
| 37 | boost::exponential_distribution<> lambda(1); |
| 38 | boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal(rng, mean_sigma); |
| 39 | boost::variate_generator<boost::lagged_fibonacci607&, boost::exponential_distribution<> > exponential(rng, lambda); |
| 40 | |
| 41 | accumulator_set<double, stats<tag::pot_quantile<right>(with_threshold_value)> > acc1( |
| 42 | pot_threshold_value = 3. |
| 43 | ); |
| 44 | accumulator_set<double, stats<tag::pot_quantile<right>(with_threshold_probability)> > acc2( |
| 45 | right_tail_cache_size = 2000 |
| 46 | , pot_threshold_probability = 0.99 |
| 47 | ); |
| 48 | accumulator_set<double, stats<tag::pot_quantile<left>(with_threshold_value)> > acc3( |
| 49 | pot_threshold_value = -3. |
| 50 | ); |
| 51 | accumulator_set<double, stats<tag::pot_quantile<left>(with_threshold_probability)> > acc4( |
| 52 | left_tail_cache_size = 2000 |
| 53 | , pot_threshold_probability = 0.01 |
| 54 | ); |
| 55 | |
| 56 | accumulator_set<double, stats<tag::pot_quantile<right>(with_threshold_value)> > acc5( |
| 57 | pot_threshold_value = 5. |
| 58 | ); |
| 59 | accumulator_set<double, stats<tag::pot_quantile<right>(with_threshold_probability)> > acc6( |
| 60 | right_tail_cache_size = 2000 |
| 61 | , pot_threshold_probability = 0.995 |
| 62 | ); |
| 63 | |
| 64 | for (std::size_t i = 0; i < 100000; ++i) |
| 65 | { |
| 66 | double sample = normal(); |
| 67 | acc1(sample); |
| 68 | acc2(sample); |
| 69 | acc3(sample); |
| 70 | acc4(sample); |
| 71 | } |
| 72 | |
| 73 | for (std::size_t i = 0; i < 100000; ++i) |
| 74 | { |
| 75 | double sample = exponential(); |
| 76 | acc5(sample); |
| 77 | acc6(sample); |
| 78 | } |
| 79 | |
| 80 | BOOST_CHECK_CLOSE( quantile(acc1, quantile_probability = 0.999), 3.090232, 3*epsilon ); |
| 81 | BOOST_CHECK_CLOSE( quantile(acc2, quantile_probability = 0.999), 3.090232, 2*epsilon ); |
| 82 | BOOST_CHECK_CLOSE( quantile(acc3, quantile_probability = 0.001), -3.090232, 2*epsilon ); |
| 83 | BOOST_CHECK_CLOSE( quantile(acc4, quantile_probability = 0.001), -3.090232, 2*epsilon ); |
| 84 | |
| 85 | BOOST_CHECK_CLOSE( quantile(acc5, quantile_probability = 0.999), 6.908, 3*epsilon ); |
| 86 | BOOST_CHECK_CLOSE( quantile(acc6, quantile_probability = 0.999), 6.908, 3*epsilon ); |
| 87 | } |
| 88 | |
| 89 | /////////////////////////////////////////////////////////////////////////////// |
| 90 | // test_persistency |
| 91 | // |
| 92 | void test_persistency() |
| 93 | { |
| 94 | // tolerance in % |
| 95 | double epsilon = 1.; |
| 96 | // "persistent" storage |
| 97 | std::stringstream ss; |
| 98 | { |
| 99 | // random number generators |
| 100 | boost::lagged_fibonacci607 rng; |
| 101 | boost::normal_distribution<> mean_sigma(0,1); |
| 102 | boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal(rng, mean_sigma); |
| 103 | |
| 104 | accumulator_set<double, stats<tag::pot_quantile<right>(with_threshold_value)> > acc(pot_threshold_value = 3.); |
| 105 | |
| 106 | for (std::size_t i = 0; i < 100000; ++i) |
| 107 | acc(normal()); |
| 108 | |
| 109 | BOOST_CHECK_CLOSE(quantile(acc, quantile_probability = 0.999), 3.090232, 3*epsilon); |
| 110 | boost::archive::text_oarchive oa(ss); |
| 111 | acc.serialize(ar&: oa, file_version: 0); |
| 112 | } |
| 113 | accumulator_set<double, stats<tag::pot_quantile<right>(with_threshold_value)> > acc(pot_threshold_value = 3.); |
| 114 | boost::archive::text_iarchive ia(ss); |
| 115 | acc.serialize(ar&: ia, file_version: 0); |
| 116 | BOOST_CHECK_CLOSE(quantile(acc, quantile_probability = 0.999), 3.090232, 3*epsilon); |
| 117 | } |
| 118 | |
| 119 | /////////////////////////////////////////////////////////////////////////////// |
| 120 | // init_unit_test_suite |
| 121 | // |
| 122 | test_suite* init_unit_test_suite( int argc, char* argv[] ) |
| 123 | { |
| 124 | test_suite *test = BOOST_TEST_SUITE("pot_quantile test" ); |
| 125 | |
| 126 | test->add(BOOST_TEST_CASE(&test_stat)); |
| 127 | test->add(BOOST_TEST_CASE(&test_persistency)); |
| 128 | |
| 129 | return test; |
| 130 | } |
| 131 | |
| 132 | |