skewness.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 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/skewness.hpp>
  15. #include <sstream>
  16. #include <boost/archive/text_oarchive.hpp>
  17. #include <boost/archive/text_iarchive.hpp>
  18. using namespace boost;
  19. using namespace unit_test;
  20. using namespace boost::accumulators;
  21. ///////////////////////////////////////////////////////////////////////////////
  22. // test_stat
  23. //
  24. void test_stat()
  25. {
  26. // tolerance in %
  27. // double epsilon = 1;
  28. accumulator_set<double, stats<tag::skewness > > acc1;
  29. accumulator_set<int, stats<tag::skewness > > acc2;
  30. // two random number generators
  31. boost::lagged_fibonacci607 rng;
  32. boost::normal_distribution<> mean_sigma(0,1);
  33. boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal(rng, mean_sigma);
  34. for (std::size_t i=0; i<100000; ++i)
  35. {
  36. acc1(normal());
  37. }
  38. // This check fails because epsilon is relative and not absolute
  39. // BOOST_CHECK_CLOSE( skewness(acc1), 0., epsilon );
  40. acc2(2);
  41. acc2(7);
  42. acc2(4);
  43. acc2(9);
  44. acc2(3);
  45. BOOST_CHECK_EQUAL( mean(acc2), 5 );
  46. BOOST_CHECK_EQUAL( accumulators::moment<2>(acc2), 159./5. );
  47. BOOST_CHECK_EQUAL( accumulators::moment<3>(acc2), 1171./5. );
  48. BOOST_CHECK_CLOSE( skewness(acc2), 0.406040288214, 1e-6 );
  49. }
  50. ///////////////////////////////////////////////////////////////////////////////
  51. // test_persistency
  52. //
  53. void test_persistency()
  54. {
  55. double epsilon = 1e-6;
  56. std::stringstream ss;
  57. {
  58. accumulator_set<int, stats<tag::skewness > > acc;
  59. acc(2);
  60. acc(7);
  61. acc(4);
  62. acc(9);
  63. acc(3);
  64. BOOST_CHECK_EQUAL(accumulators::moment<3>(acc), 1171./5.);
  65. BOOST_CHECK_CLOSE(skewness(acc), 0.406040288214, epsilon);
  66. boost::archive::text_oarchive oa(ss);
  67. acc.serialize(oa, 0);
  68. }
  69. accumulator_set<int, stats<tag::skewness > > acc;
  70. boost::archive::text_iarchive ia(ss);
  71. acc.serialize(ia, 0);
  72. BOOST_CHECK_EQUAL(accumulators::moment<3>(acc), 1171./5.);
  73. BOOST_CHECK_CLOSE(skewness(acc), 0.406040288214, epsilon);
  74. }
  75. ///////////////////////////////////////////////////////////////////////////////
  76. // init_unit_test_suite
  77. //
  78. test_suite* init_unit_test_suite( int argc, char* argv[] )
  79. {
  80. test_suite *test = BOOST_TEST_SUITE("skewness test");
  81. test->add(BOOST_TEST_CASE(&test_stat));
  82. test->add(BOOST_TEST_CASE(&test_persistency));
  83. return test;
  84. }