| 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 (weighted feature) |
| 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 | |
| 18 | using namespace boost; |
| 19 | using namespace unit_test; |
| 20 | using namespace boost::accumulators; |
| 21 | |
| 22 | /////////////////////////////////////////////////////////////////////////////// |
| 23 | // test_stat |
| 24 | // |
| 25 | void test_stat() |
| 26 | { |
| 27 | // tolerance in % |
| 28 | double epsilon = 1.; |
| 29 | |
| 30 | double mu1, mu2, l; |
| 31 | |
| 32 | mu1 = 1.; |
| 33 | mu2 = -1.; |
| 34 | l = 0.5; |
| 35 | |
| 36 | // two random number generators |
| 37 | boost::lagged_fibonacci607 rng; |
| 38 | boost::normal_distribution<> mean_sigma1(mu1,1); |
| 39 | boost::normal_distribution<> mean_sigma2(mu2,1); |
| 40 | boost::exponential_distribution<> lambda(l); |
| 41 | boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal1(rng, mean_sigma1); |
| 42 | boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal2(rng, mean_sigma2); |
| 43 | boost::variate_generator<boost::lagged_fibonacci607&, boost::exponential_distribution<> > exponential(rng, lambda); |
| 44 | |
| 45 | accumulator_set<double, stats<tag::weighted_pot_quantile<right>(with_threshold_value)>, double > acc1( |
| 46 | pot_threshold_value = 3. |
| 47 | ); |
| 48 | accumulator_set<double, stats<tag::weighted_pot_quantile<right>(with_threshold_probability)>, double > acc2( |
| 49 | right_tail_cache_size = 10000 |
| 50 | , pot_threshold_probability = 0.99 |
| 51 | ); |
| 52 | accumulator_set<double, stats<tag::weighted_pot_quantile<left>(with_threshold_value)>, double > acc3( |
| 53 | pot_threshold_value = -3. |
| 54 | ); |
| 55 | accumulator_set<double, stats<tag::weighted_pot_quantile<left>(with_threshold_probability)>, double > acc4( |
| 56 | left_tail_cache_size = 10000 |
| 57 | , pot_threshold_probability = 0.01 |
| 58 | ); |
| 59 | |
| 60 | accumulator_set<double, stats<tag::weighted_pot_quantile<right>(with_threshold_value)>, double > acc5( |
| 61 | pot_threshold_value = 5. |
| 62 | ); |
| 63 | accumulator_set<double, stats<tag::weighted_pot_quantile<right>(with_threshold_probability)>, double > acc6( |
| 64 | right_tail_cache_size = 10000 |
| 65 | , pot_threshold_probability = 0.995 |
| 66 | ); |
| 67 | |
| 68 | for (std::size_t i = 0; i < 100000; ++i) |
| 69 | { |
| 70 | double sample1 = normal1(); |
| 71 | double sample2 = normal2(); |
| 72 | acc1(sample1, weight = std::exp(x: -mu1 * (sample1 - 0.5 * mu1))); |
| 73 | acc2(sample1, weight = std::exp(x: -mu1 * (sample1 - 0.5 * mu1))); |
| 74 | acc3(sample2, weight = std::exp(x: -mu2 * (sample2 - 0.5 * mu2))); |
| 75 | acc4(sample2, weight = std::exp(x: -mu2 * (sample2 - 0.5 * mu2))); |
| 76 | } |
| 77 | |
| 78 | for (std::size_t i = 0; i < 100000; ++i) |
| 79 | { |
| 80 | double sample = exponential(); |
| 81 | acc5(sample, weight = 1./l * std::exp(x: -sample * (1. - l))); |
| 82 | acc6(sample, weight = 1./l * std::exp(x: -sample * (1. - l))); |
| 83 | } |
| 84 | |
| 85 | BOOST_CHECK_CLOSE( quantile(acc1, quantile_probability = 0.999), 3.090232, epsilon ); |
| 86 | BOOST_CHECK_CLOSE( quantile(acc2, quantile_probability = 0.999), 3.090232, epsilon ); |
| 87 | BOOST_CHECK_CLOSE( quantile(acc3, quantile_probability = 0.001), -3.090232, epsilon ); |
| 88 | BOOST_CHECK_CLOSE( quantile(acc4, quantile_probability = 0.001), -3.090232, epsilon ); |
| 89 | |
| 90 | BOOST_CHECK_CLOSE( quantile(acc5, quantile_probability = 0.999), 6.908, epsilon ); |
| 91 | BOOST_CHECK_CLOSE( quantile(acc6, quantile_probability = 0.999), 6.908, epsilon ); |
| 92 | } |
| 93 | |
| 94 | /////////////////////////////////////////////////////////////////////////////// |
| 95 | // init_unit_test_suite |
| 96 | // |
| 97 | test_suite* init_unit_test_suite( int argc, char* argv[] ) |
| 98 | { |
| 99 | test_suite *test = BOOST_TEST_SUITE("weighted_pot_quantile test" ); |
| 100 | |
| 101 | test->add(BOOST_TEST_CASE(&test_stat)); |
| 102 | |
| 103 | return test; |
| 104 | } |
| 105 | |