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