| 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_extended.hpp |
| 7 | |
| 8 | #include <iostream> |
| 9 | #include <boost/random.hpp> |
| 10 | #include <boost/test/unit_test.hpp> |
| 11 | #include <boost/test/tools/floating_point_comparison.hpp> |
| 12 | #include <boost/accumulators/numeric/functional/vector.hpp> |
| 13 | #include <boost/accumulators/numeric/functional/complex.hpp> |
| 14 | #include <boost/accumulators/numeric/functional/valarray.hpp> |
| 15 | #include <boost/accumulators/accumulators.hpp> |
| 16 | #include <boost/accumulators/statistics/stats.hpp> |
| 17 | #include <boost/accumulators/statistics/p_square_quantile_extended.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 | typedef accumulator_set<double, stats<tag::p_square_quantile_extended> > accumulator_t; |
| 27 | |
| 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | // test_stat |
| 30 | // |
| 31 | void test_stat() |
| 32 | { |
| 33 | // tolerance |
| 34 | double epsilon = 1e-6; |
| 35 | |
| 36 | // a random number generator |
| 37 | boost::lagged_fibonacci607 rng; |
| 38 | |
| 39 | std::vector<double> probs; |
| 40 | |
| 41 | probs.push_back(x: 0.001); |
| 42 | probs.push_back(x: 0.01 ); |
| 43 | probs.push_back(x: 0.1 ); |
| 44 | probs.push_back(x: 0.25 ); |
| 45 | probs.push_back(x: 0.5 ); |
| 46 | probs.push_back(x: 0.75 ); |
| 47 | probs.push_back(x: 0.9 ); |
| 48 | probs.push_back(x: 0.99 ); |
| 49 | probs.push_back(x: 0.999); |
| 50 | |
| 51 | accumulator_t acc(tag::p_square_quantile_extended::probabilities = probs); |
| 52 | |
| 53 | for (int i=0; i<10000; ++i) |
| 54 | acc(rng()); |
| 55 | |
| 56 | for (std::size_t i=0; i<probs.size(); ++i) |
| 57 | { |
| 58 | BOOST_CHECK_CLOSE(p_square_quantile_extended(acc)[i], probs[i], epsilon); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /////////////////////////////////////////////////////////////////////////////// |
| 63 | // test_persistency |
| 64 | // |
| 65 | void test_persistency() |
| 66 | { |
| 67 | // "persistent" storage |
| 68 | std::stringstream ss; |
| 69 | // tolerance in % |
| 70 | double epsilon = 1; |
| 71 | // a random number generator |
| 72 | boost::lagged_fibonacci607 rng; |
| 73 | |
| 74 | std::vector<double> probs; |
| 75 | probs.push_back(x: 0.001); |
| 76 | probs.push_back(x: 0.01 ); |
| 77 | probs.push_back(x: 0.1 ); |
| 78 | probs.push_back(x: 0.25 ); |
| 79 | probs.push_back(x: 0.5 ); |
| 80 | probs.push_back(x: 0.75 ); |
| 81 | probs.push_back(x: 0.9 ); |
| 82 | probs.push_back(x: 0.99 ); |
| 83 | probs.push_back(x: 0.999); |
| 84 | { |
| 85 | accumulator_t acc(tag::p_square_quantile_extended::probabilities = probs); |
| 86 | |
| 87 | for (int i=0; i<10000; ++i) |
| 88 | acc(rng()); |
| 89 | |
| 90 | for (std::size_t i=0; i<probs.size(); ++i) |
| 91 | { |
| 92 | BOOST_CHECK_CLOSE(p_square_quantile_extended(acc)[i], probs[i], epsilon); |
| 93 | } |
| 94 | boost::archive::text_oarchive oa(ss); |
| 95 | acc.serialize(oa, 0); |
| 96 | } |
| 97 | accumulator_t acc(tag::p_square_quantile_extended::probabilities = probs); |
| 98 | boost::archive::text_iarchive ia(ss); |
| 99 | acc.serialize(ia, 0); |
| 100 | for (std::size_t i=0; i<probs.size(); ++i) |
| 101 | { |
| 102 | BOOST_CHECK_CLOSE(p_square_quantile_extended(acc)[i], probs[i], epsilon); |
| 103 | } |
| 104 | } |
| 105 | /////////////////////////////////////////////////////////////////////////////// |
| 106 | // init_unit_test_suite |
| 107 | // |
| 108 | test_suite* init_unit_test_suite( int argc, char* argv[] ) |
| 109 | { |
| 110 | test_suite *test = BOOST_TEST_SUITE("p_square_quantile_extended test" ); |
| 111 | |
| 112 | test->add(BOOST_TEST_CASE(&test_stat)); |
| 113 | test->add(BOOST_TEST_CASE(&test_persistency)); |
| 114 | |
| 115 | return test; |
| 116 | } |
| 117 | |
| 118 | |