| 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 weighted_skewness.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/weighted_skewness.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 | accumulator_set<double, stats<tag::weighted_skewness>, double > acc1; |
| 31 | accumulator_set<int, stats<tag::weighted_skewness>, int > acc2; |
| 32 | |
| 33 | // two random number generators |
| 34 | boost::lagged_fibonacci607 rng; |
| 35 | boost::normal_distribution<> mean_sigma(0,1); |
| 36 | boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal(rng, mean_sigma); |
| 37 | |
| 38 | for (std::size_t i=0; i<100000; ++i) |
| 39 | { |
| 40 | acc1(normal(), weight = rng()); |
| 41 | } |
| 42 | |
| 43 | // This check fails because epsilon is relative and not absolute |
| 44 | // BOOST_CHECK_CLOSE( weighted_skewness(acc1), 0., epsilon ); |
| 45 | |
| 46 | acc2(2, weight = 4); |
| 47 | acc2(7, weight = 1); |
| 48 | acc2(4, weight = 3); |
| 49 | acc2(9, weight = 1); |
| 50 | acc2(3, weight = 2); |
| 51 | |
| 52 | BOOST_CHECK_EQUAL( weighted_mean(acc2), 42./11. ); |
| 53 | BOOST_CHECK_EQUAL( accumulators::weighted_moment<2>(acc2), 212./11. ); |
| 54 | BOOST_CHECK_EQUAL( accumulators::weighted_moment<3>(acc2), 1350./11. ); |
| 55 | BOOST_CHECK_CLOSE( weighted_skewness(acc2), 1.30708406282, 1e-6 ); |
| 56 | } |
| 57 | |
| 58 | /////////////////////////////////////////////////////////////////////////////// |
| 59 | // init_unit_test_suite |
| 60 | // |
| 61 | test_suite* init_unit_test_suite( int argc, char* argv[] ) |
| 62 | { |
| 63 | test_suite *test = BOOST_TEST_SUITE("weighted_skewness test" ); |
| 64 | |
| 65 | test->add(BOOST_TEST_CASE(&test_stat)); |
| 66 | |
| 67 | return test; |
| 68 | } |
| 69 | |
| 70 | |