weighted_skewness.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. // Test case for weighted_skewness.hpp
  6. #include <boost/random.hpp>
  7. #include <boost/test/unit_test.hpp>
  8. #include <boost/test/floating_point_comparison.hpp>
  9. #include <boost/accumulators/numeric/functional/vector.hpp>
  10. #include <boost/accumulators/numeric/functional/complex.hpp>
  11. #include <boost/accumulators/numeric/functional/valarray.hpp>
  12. #include <boost/accumulators/accumulators.hpp>
  13. #include <boost/accumulators/statistics/stats.hpp>
  14. #include <boost/accumulators/statistics/weighted_skewness.hpp>
  15. using namespace boost;
  16. using namespace unit_test;
  17. using namespace boost::accumulators;
  18. ///////////////////////////////////////////////////////////////////////////////
  19. // test_stat
  20. //
  21. void test_stat()
  22. {
  23. // tolerance in %
  24. // double epsilon = 1;
  25. accumulator_set<double, stats<tag::weighted_skewness>, double > acc1;
  26. accumulator_set<int, stats<tag::weighted_skewness>, int > acc2;
  27. // two random number generators
  28. boost::lagged_fibonacci607 rng;
  29. boost::normal_distribution<> mean_sigma(0,1);
  30. boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal(rng, mean_sigma);
  31. for (std::size_t i=0; i<100000; ++i)
  32. {
  33. acc1(normal(), weight = rng());
  34. }
  35. // This check fails because epsilon is relative and not absolute
  36. // BOOST_CHECK_CLOSE( weighted_skewness(acc1), 0., epsilon );
  37. acc2(2, weight = 4);
  38. acc2(7, weight = 1);
  39. acc2(4, weight = 3);
  40. acc2(9, weight = 1);
  41. acc2(3, weight = 2);
  42. BOOST_CHECK_EQUAL( weighted_mean(acc2), 42./11. );
  43. BOOST_CHECK_EQUAL( accumulators::weighted_moment<2>(acc2), 212./11. );
  44. BOOST_CHECK_EQUAL( accumulators::weighted_moment<3>(acc2), 1350./11. );
  45. BOOST_CHECK_CLOSE( weighted_skewness(acc2), 1.30708406282, 1e-6 );
  46. }
  47. ///////////////////////////////////////////////////////////////////////////////
  48. // init_unit_test_suite
  49. //
  50. test_suite* init_unit_test_suite( int argc, char* argv[] )
  51. {
  52. test_suite *test = BOOST_TEST_SUITE("weighted_skewness test");
  53. test->add(BOOST_TEST_CASE(&test_stat));
  54. return test;
  55. }