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)
6 // Test case for pot_quantile.hpp
8 #define BOOST_NUMERIC_FUNCTIONAL_STD_COMPLEX_SUPPORT
9 #define BOOST_NUMERIC_FUNCTIONAL_STD_VALARRAY_SUPPORT
10 #define BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT
12 #include <boost/random.hpp>
13 #include <boost/test/unit_test.hpp>
14 #include <boost/test/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>
19 #include <boost/archive/text_oarchive.hpp>
20 #include <boost/archive/text_iarchive.hpp>
22 using namespace boost
;
23 using namespace unit_test
;
24 using namespace boost::accumulators
;
26 ///////////////////////////////////////////////////////////////////////////////
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
);
41 accumulator_set
<double, stats
<tag::pot_quantile
<right
>(with_threshold_value
)> > acc1(
42 pot_threshold_value
= 3.
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
48 accumulator_set
<double, stats
<tag::pot_quantile
<left
>(with_threshold_value
)> > acc3(
49 pot_threshold_value
= -3.
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
56 accumulator_set
<double, stats
<tag::pot_quantile
<right
>(with_threshold_value
)> > acc5(
57 pot_threshold_value
= 5.
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
64 for (std::size_t i
= 0; i
< 100000; ++i
)
66 double sample
= normal();
73 for (std::size_t i
= 0; i
< 100000; ++i
)
75 double sample
= exponential();
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
);
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
);
89 ///////////////////////////////////////////////////////////////////////////////
92 void test_persistency()
96 // "persistent" storage
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
);
104 accumulator_set
<double, stats
<tag::pot_quantile
<right
>(with_threshold_value
)> > acc(pot_threshold_value
= 3.);
106 for (std::size_t i
= 0; i
< 100000; ++i
)
109 BOOST_CHECK_CLOSE(quantile(acc
, quantile_probability
= 0.999), 3.090232, 3*epsilon
);
110 boost::archive::text_oarchive
oa(ss
);
111 acc
.serialize(oa
, 0);
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(ia
, 0);
116 BOOST_CHECK_CLOSE(quantile(acc
, quantile_probability
= 0.999), 3.090232, 3*epsilon
);
119 ///////////////////////////////////////////////////////////////////////////////
120 // init_unit_test_suite
122 test_suite
* init_unit_test_suite( int argc
, char* argv
[] )
124 test_suite
*test
= BOOST_TEST_SUITE("pot_quantile test");
126 test
->add(BOOST_TEST_CASE(&test_stat
));
127 test
->add(BOOST_TEST_CASE(&test_persistency
));