| 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 p_square_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/p_square_quantile.hpp> |
| 17 | #include <sstream> |
| 18 | #include <boost/archive/text_oarchive.hpp> |
| 19 | #include <boost/archive/text_iarchive.hpp> |
| 20 | |
| 21 | using namespace boost; |
| 22 | using namespace unit_test; |
| 23 | using namespace boost::accumulators; |
| 24 | |
| 25 | typedef accumulator_set<double, stats<tag::p_square_quantile> > accumulator_t; |
| 26 | |
| 27 | /////////////////////////////////////////////////////////////////////////////// |
| 28 | // test_stat |
| 29 | // |
| 30 | void test_stat() |
| 31 | { |
| 32 | // tolerance in % |
| 33 | double epsilon = 1; |
| 34 | |
| 35 | // a random number generator |
| 36 | boost::lagged_fibonacci607 rng; |
| 37 | |
| 38 | accumulator_t acc0(quantile_probability = 0.001); |
| 39 | accumulator_t acc1(quantile_probability = 0.01 ); |
| 40 | accumulator_t acc2(quantile_probability = 0.1 ); |
| 41 | accumulator_t acc3(quantile_probability = 0.25 ); |
| 42 | accumulator_t acc4(quantile_probability = 0.5 ); |
| 43 | accumulator_t acc5(quantile_probability = 0.75 ); |
| 44 | accumulator_t acc6(quantile_probability = 0.9 ); |
| 45 | accumulator_t acc7(quantile_probability = 0.99 ); |
| 46 | accumulator_t acc8(quantile_probability = 0.999); |
| 47 | |
| 48 | for (int i=0; i<100000; ++i) |
| 49 | { |
| 50 | double sample = rng(); |
| 51 | acc0(sample); |
| 52 | acc1(sample); |
| 53 | acc2(sample); |
| 54 | acc3(sample); |
| 55 | acc4(sample); |
| 56 | acc5(sample); |
| 57 | acc6(sample); |
| 58 | acc7(sample); |
| 59 | acc8(sample); |
| 60 | } |
| 61 | |
| 62 | BOOST_CHECK_CLOSE( p_square_quantile(acc0), 0.001, 18*epsilon ); |
| 63 | BOOST_CHECK_CLOSE( p_square_quantile(acc1), 0.01 , 7*epsilon ); |
| 64 | BOOST_CHECK_CLOSE( p_square_quantile(acc2), 0.1 , 3*epsilon ); |
| 65 | BOOST_CHECK_CLOSE( p_square_quantile(acc3), 0.25 , 2*epsilon ); |
| 66 | BOOST_CHECK_CLOSE( p_square_quantile(acc4), 0.5 , epsilon ); |
| 67 | BOOST_CHECK_CLOSE( p_square_quantile(acc5), 0.75 , epsilon ); |
| 68 | BOOST_CHECK_CLOSE( p_square_quantile(acc6), 0.9 , epsilon ); |
| 69 | BOOST_CHECK_CLOSE( p_square_quantile(acc7), 0.99 , epsilon ); |
| 70 | BOOST_CHECK_CLOSE( p_square_quantile(acc8), 0.999, epsilon ); |
| 71 | } |
| 72 | |
| 73 | /////////////////////////////////////////////////////////////////////////////// |
| 74 | // test_persistency |
| 75 | // |
| 76 | void test_persistency() |
| 77 | { |
| 78 | // "persistent" storage |
| 79 | std::stringstream ss; |
| 80 | // tolerance in % |
| 81 | double epsilon = 1; |
| 82 | // a random number generator |
| 83 | boost::lagged_fibonacci607 rng; |
| 84 | { |
| 85 | accumulator_t acc1(quantile_probability = 0.75 ); |
| 86 | accumulator_t acc2(quantile_probability = 0.999); |
| 87 | |
| 88 | for (int i=0; i<100000; ++i) |
| 89 | { |
| 90 | double sample = rng(); |
| 91 | acc1(sample); |
| 92 | acc2(sample); |
| 93 | } |
| 94 | |
| 95 | BOOST_CHECK_CLOSE(p_square_quantile(acc1), 0.75 , epsilon); |
| 96 | BOOST_CHECK_CLOSE(p_square_quantile(acc2), 0.999, epsilon); |
| 97 | boost::archive::text_oarchive oa(ss); |
| 98 | acc1.serialize(ar&: oa, file_version: 0); |
| 99 | acc2.serialize(ar&: oa, file_version: 0); |
| 100 | } |
| 101 | accumulator_t acc1(quantile_probability = 0.75); |
| 102 | accumulator_t acc2(quantile_probability = 0.999); |
| 103 | boost::archive::text_iarchive ia(ss); |
| 104 | acc1.serialize(ar&: ia, file_version: 0); |
| 105 | acc2.serialize(ar&: ia, file_version: 0); |
| 106 | BOOST_CHECK_CLOSE(p_square_quantile(acc1), 0.75 , epsilon); |
| 107 | BOOST_CHECK_CLOSE(p_square_quantile(acc2), 0.999, epsilon); |
| 108 | } |
| 109 | |
| 110 | /////////////////////////////////////////////////////////////////////////////// |
| 111 | // init_unit_test_suite |
| 112 | // |
| 113 | test_suite* init_unit_test_suite( int argc, char* argv[] ) |
| 114 | { |
| 115 | test_suite *test = BOOST_TEST_SUITE("p_square_quantile test" ); |
| 116 | |
| 117 | test->add(BOOST_TEST_CASE(&test_stat)); |
| 118 | test->add(BOOST_TEST_CASE(&test_persistency)); |
| 119 | |
| 120 | return test; |
| 121 | } |
| 122 | |
| 123 | |