mean.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // (C) Copyright Eric Niebler 2005.
  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. #include <boost/test/unit_test.hpp>
  6. #include <boost/test/floating_point_comparison.hpp>
  7. #include <boost/accumulators/accumulators.hpp>
  8. #include <boost/accumulators/statistics/stats.hpp>
  9. #include <boost/accumulators/statistics/weighted_mean.hpp>
  10. #include <boost/accumulators/statistics/variates/covariate.hpp>
  11. #include <sstream>
  12. #include <boost/archive/text_oarchive.hpp>
  13. #include <boost/archive/text_iarchive.hpp>
  14. using namespace boost;
  15. using namespace unit_test;
  16. using namespace accumulators;
  17. template<typename T>
  18. void assert_is_double(T const &)
  19. {
  20. BOOST_MPL_ASSERT((is_same<T, double>));
  21. }
  22. typedef accumulator_set<int, stats<tag::mean,
  23. tag::mean_of_variates<int, tag::covariate1> > > mean_t;
  24. typedef accumulator_set<int, stats<tag::mean(immediate),
  25. tag::mean_of_variates<int, tag::covariate1>(immediate) > > immediate_mean_t;
  26. ///////////////////////////////////////////////////////////////////////////////
  27. // test_stat
  28. //
  29. void test_stat()
  30. {
  31. mean_t acc, test_acc(sample = 0);
  32. acc(1, covariate1 = 3);
  33. BOOST_CHECK_CLOSE(1., mean(acc), 1e-5);
  34. BOOST_CHECK_EQUAL(1u, count(acc));
  35. BOOST_CHECK_EQUAL(1, sum(acc));
  36. BOOST_CHECK_CLOSE(3., (accumulators::mean_of_variates<int, tag::covariate1>(acc)), 1e-5);
  37. acc(0, covariate1 = 4);
  38. BOOST_CHECK_CLOSE(0.5, mean(acc), 1e-5);
  39. BOOST_CHECK_EQUAL(2u, count(acc));
  40. BOOST_CHECK_EQUAL(1, sum(acc));
  41. BOOST_CHECK_CLOSE(3.5, (accumulators::mean_of_variates<int, tag::covariate1>(acc)), 1e-5);
  42. acc(2, covariate1 = 8);
  43. BOOST_CHECK_CLOSE(1., mean(acc), 1e-5);
  44. BOOST_CHECK_EQUAL(3u, count(acc));
  45. BOOST_CHECK_EQUAL(3, sum(acc));
  46. BOOST_CHECK_CLOSE(5., (accumulators::mean_of_variates<int, tag::covariate1>(acc)), 1e-5);
  47. assert_is_double(mean(acc));
  48. immediate_mean_t acc2, test_acc2(sample = 0);
  49. acc2(1, covariate1 = 3);
  50. BOOST_CHECK_CLOSE(1., mean(acc2), 1e-5);
  51. BOOST_CHECK_EQUAL(1u, count(acc2));
  52. BOOST_CHECK_CLOSE(3., (accumulators::mean_of_variates<int, tag::covariate1>(acc2)), 1e-5);
  53. acc2(0, covariate1 = 4);
  54. BOOST_CHECK_CLOSE(0.5, mean(acc2), 1e-5);
  55. BOOST_CHECK_EQUAL(2u, count(acc2));
  56. BOOST_CHECK_CLOSE(3.5, (accumulators::mean_of_variates<int, tag::covariate1>(acc2)), 1e-5);
  57. acc2(2, covariate1 = 8);
  58. BOOST_CHECK_CLOSE(1., mean(acc2), 1e-5);
  59. BOOST_CHECK_EQUAL(3u, count(acc2));
  60. BOOST_CHECK_CLOSE(5., (accumulators::mean_of_variates<int, tag::covariate1>(acc2)), 1e-5);
  61. assert_is_double(mean(acc2));
  62. }
  63. ///////////////////////////////////////////////////////////////////////////////
  64. // test_persistency
  65. //
  66. void test_persistency()
  67. {
  68. std::stringstream ss;
  69. {
  70. mean_t acc1;
  71. immediate_mean_t acc2;
  72. acc1(1, covariate1 = 3);
  73. acc1(0, covariate1 = 4);
  74. acc1(2, covariate1 = 8);
  75. acc2(1, covariate1 = 3);
  76. acc2(0, covariate1 = 4);
  77. acc2(2, covariate1 = 8);
  78. BOOST_CHECK_CLOSE(1., mean(acc1), 1e-5);
  79. BOOST_CHECK_CLOSE(5., (accumulators::mean_of_variates<int, tag::covariate1>(acc1)), 1e-5);
  80. BOOST_CHECK_CLOSE(1., mean(acc2), 1e-5);
  81. BOOST_CHECK_CLOSE(5., (accumulators::mean_of_variates<int, tag::covariate1>(acc2)), 1e-5);
  82. boost::archive::text_oarchive oa(ss);
  83. acc1.serialize(oa, 0);
  84. acc2.serialize(oa, 0);
  85. }
  86. mean_t acc1;
  87. immediate_mean_t acc2;
  88. boost::archive::text_iarchive ia(ss);
  89. acc1.serialize(ia, 0);
  90. acc2.serialize(ia, 0);
  91. BOOST_CHECK_CLOSE(1., mean(acc1), 1e-5);
  92. BOOST_CHECK_CLOSE(5., (accumulators::mean_of_variates<int, tag::covariate1>(acc2)), 1e-5);
  93. }
  94. ///////////////////////////////////////////////////////////////////////////////
  95. // init_unit_test_suite
  96. //
  97. test_suite* init_unit_test_suite( int argc, char* argv[] )
  98. {
  99. test_suite *test = BOOST_TEST_SUITE("mean test");
  100. test->add(BOOST_TEST_CASE(&test_stat));
  101. test->add(BOOST_TEST_CASE(&test_persistency));
  102. return test;
  103. }