kurtosis.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 kurtosis.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/kurtosis.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::kurtosis > > acc1;
  29. accumulator_set<int, stats<tag::kurtosis > > 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( kurtosis(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_EQUAL( accumulators::moment<4>(acc2), 1863 );
  49. BOOST_CHECK_CLOSE( kurtosis(acc2), -1.39965397924, 1e-6 );
  50. }
  51. ///////////////////////////////////////////////////////////////////////////////
  52. // test_persistency
  53. //
  54. void test_persistency()
  55. {
  56. // "persistent" storage
  57. std::stringstream ss;
  58. // tolerance
  59. double epsilon = 1e-6;
  60. double kurtosis_value = -1.39965397924;
  61. {
  62. accumulator_set<int, stats<tag::kurtosis > > acc;
  63. acc(2);
  64. acc(7);
  65. acc(4);
  66. acc(9);
  67. acc(3);
  68. BOOST_CHECK_CLOSE( kurtosis(acc), kurtosis_value, epsilon);
  69. boost::archive::text_oarchive oa(ss);
  70. acc.serialize(oa, 0);
  71. }
  72. accumulator_set<double, stats<tag::kurtosis > > acc;
  73. boost::archive::text_iarchive ia(ss);
  74. acc.serialize(ia, 0);
  75. BOOST_CHECK_CLOSE( kurtosis(acc), kurtosis_value, epsilon);
  76. }
  77. ///////////////////////////////////////////////////////////////////////////////
  78. // init_unit_test_suite
  79. //
  80. test_suite* init_unit_test_suite( int argc, char* argv[] )
  81. {
  82. test_suite *test = BOOST_TEST_SUITE("kurtosis test");
  83. test->add(BOOST_TEST_CASE(&test_stat));
  84. test->add(BOOST_TEST_CASE(&test_persistency));
  85. return test;
  86. }