rolling_moment.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright (C) Eric Niebler 2005.
  2. // Copyright (C) Pieter Bastiaan Ober 2014.
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/test/unit_test.hpp>
  7. #include <boost/test/floating_point_comparison.hpp>
  8. #include <boost/mpl/assert.hpp>
  9. #include <boost/accumulators/accumulators.hpp>
  10. #include <boost/accumulators/statistics/stats.hpp>
  11. #include <boost/accumulators/statistics/rolling_moment.hpp>
  12. #include <sstream>
  13. #include <boost/archive/text_oarchive.hpp>
  14. #include <boost/archive/text_iarchive.hpp>
  15. using namespace boost;
  16. using namespace unit_test;
  17. using namespace accumulators;
  18. template<typename T>
  19. void assert_is_double(T const &)
  20. {
  21. BOOST_MPL_ASSERT((is_same<T, double>));
  22. }
  23. ///////////////////////////////////////////////////////////////////////////////
  24. // test_rolling_moment
  25. //
  26. void test_rolling_second_moment()
  27. {
  28. accumulator_set<int, stats<tag::rolling_moment<2> > > acc(tag::rolling_moment<2>::window_size = 3);
  29. acc(2);
  30. BOOST_CHECK_CLOSE(rolling_moment<2>(acc), 4.0/1 ,1e-5);
  31. acc(4);
  32. BOOST_CHECK_CLOSE(rolling_moment<2>(acc), (4.0 + 16.0)/2, 1e-5);
  33. acc(5);
  34. BOOST_CHECK_CLOSE(rolling_moment<2>(acc), (4.0 + 16.0 + 25.0)/3, 1e-5);
  35. acc(6);
  36. BOOST_CHECK_CLOSE(rolling_moment<2>(acc), (16.0 + 25.0 + 36.0)/3, 1e-5);
  37. assert_is_double(rolling_moment<2>(acc));
  38. }
  39. void test_rolling_fifth_moment()
  40. {
  41. accumulator_set<int, stats<tag::rolling_moment<5> > > acc(tag::rolling_moment<2>::window_size = 3);
  42. acc(2);
  43. BOOST_CHECK_CLOSE(rolling_moment<5>(acc), 32.0/1, 1e-5);
  44. acc(3);
  45. BOOST_CHECK_CLOSE(rolling_moment<5>(acc), (32.0 + 243.0)/2, 1e-5);
  46. acc(4);
  47. BOOST_CHECK_CLOSE(rolling_moment<5>(acc), (32.0 + 243.0 + 1024.0)/3, 1e-5);
  48. acc(5);
  49. BOOST_CHECK_CLOSE(rolling_moment<5>(acc), (243.0 + 1024.0 + 3125.0)/3, 1e-5);
  50. assert_is_double(rolling_moment<5>(acc));
  51. }
  52. ///////////////////////////////////////////////////////////////////////////////
  53. // test_persistency
  54. //
  55. void test_persistency()
  56. {
  57. std::stringstream ss;
  58. {
  59. accumulator_set<int, stats<tag::rolling_moment<2> > > acc2(tag::rolling_moment<2>::window_size = 3);
  60. accumulator_set<int, stats<tag::rolling_moment<5> > > acc5(tag::rolling_moment<5>::window_size = 3);
  61. acc2(2); acc5(2);
  62. acc2(4); acc5(3);
  63. acc2(5); acc5(4);
  64. acc2(6); acc5(5);
  65. BOOST_CHECK_CLOSE(rolling_moment<2>(acc2), (16.0 + 25.0 + 36.0)/3, 1e-5);
  66. BOOST_CHECK_CLOSE(rolling_moment<5>(acc5), (243.0 + 1024.0 + 3125.0)/3, 1e-5);
  67. boost::archive::text_oarchive oa(ss);
  68. acc2.serialize(oa, 0);
  69. acc5.serialize(oa, 0);
  70. }
  71. accumulator_set<int, stats<tag::rolling_moment<2> > > acc2(tag::rolling_moment<2>::window_size = 3);
  72. accumulator_set<int, stats<tag::rolling_moment<5> > > acc5(tag::rolling_moment<5>::window_size = 3);
  73. boost::archive::text_iarchive ia(ss);
  74. acc2.serialize(ia, 0);
  75. acc5.serialize(ia, 0);
  76. BOOST_CHECK_CLOSE(rolling_moment<2>(acc2), (16.0 + 25.0 + 36.0)/3, 1e-5);
  77. BOOST_CHECK_CLOSE(rolling_moment<5>(acc5), (243.0 + 1024.0 + 3125.0)/3, 1e-5);
  78. }
  79. ///////////////////////////////////////////////////////////////////////////////
  80. // init_unit_test_suite
  81. //
  82. test_suite* init_unit_test_suite( int argc, char* argv[] )
  83. {
  84. test_suite *test = BOOST_TEST_SUITE("rolling moment test");
  85. test->add(BOOST_TEST_CASE(&test_rolling_second_moment));
  86. test->add(BOOST_TEST_CASE(&test_rolling_fifth_moment));
  87. test->add(BOOST_TEST_CASE(&test_persistency));
  88. return test;
  89. }