weighted_mean.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. using namespace boost;
  12. using namespace unit_test;
  13. using namespace accumulators;
  14. template<typename T>
  15. void assert_is_double(T const &)
  16. {
  17. BOOST_MPL_ASSERT((is_same<T, double>));
  18. }
  19. ///////////////////////////////////////////////////////////////////////////////
  20. // test_stat
  21. //
  22. void test_stat()
  23. {
  24. accumulator_set<
  25. int
  26. , stats<
  27. tag::weighted_mean
  28. , tag::mean_of_weights
  29. , tag::weighted_mean_of_variates<int, tag::covariate1>
  30. >
  31. , int
  32. > acc, test_acc(sample = 0);
  33. acc(1, weight = 2, covariate1 = 3);
  34. BOOST_CHECK_CLOSE(1., weighted_mean(acc), 1e-5);
  35. BOOST_CHECK_EQUAL(1u, count(acc));
  36. BOOST_CHECK_EQUAL(2, sum(acc));
  37. BOOST_CHECK_CLOSE(2., mean_of_weights(acc), 1e-5);
  38. BOOST_CHECK_CLOSE(3., (accumulators::weighted_mean_of_variates<int, tag::covariate1>(acc)), 1e-5);
  39. acc(0, weight = 4, covariate1 = 4);
  40. BOOST_CHECK_CLOSE(1./3., weighted_mean(acc), 1e-5);
  41. BOOST_CHECK_EQUAL(2u, count(acc));
  42. BOOST_CHECK_EQUAL(2, sum(acc));
  43. BOOST_CHECK_CLOSE(3., mean_of_weights(acc), 1e-5);
  44. BOOST_CHECK_CLOSE(11./3., (accumulators::weighted_mean_of_variates<int, tag::covariate1>(acc)), 1e-5);
  45. acc(2, weight = 9, covariate1 = 8);
  46. BOOST_CHECK_CLOSE(4./3., weighted_mean(acc), 1e-5);
  47. BOOST_CHECK_EQUAL(3u, count(acc));
  48. BOOST_CHECK_EQUAL(20, sum(acc));
  49. BOOST_CHECK_CLOSE(5., mean_of_weights(acc), 1e-5);
  50. BOOST_CHECK_CLOSE(94./15., (accumulators::weighted_mean_of_variates<int, tag::covariate1>(acc)), 1e-5);
  51. assert_is_double(mean(acc));
  52. accumulator_set<
  53. int
  54. , stats<
  55. tag::weighted_mean(immediate)
  56. , tag::mean_of_weights(immediate)
  57. , tag::weighted_mean_of_variates<int, tag::covariate1>(immediate)
  58. >
  59. , int
  60. > acc2, test_acc2(sample = 0);
  61. acc2(1, weight = 2, covariate1 = 3);
  62. BOOST_CHECK_CLOSE(1., weighted_mean(acc2), 1e-5);
  63. BOOST_CHECK_EQUAL(1u, count(acc2));
  64. BOOST_CHECK_CLOSE(2., mean_of_weights(acc2), 1e-5);
  65. BOOST_CHECK_CLOSE(3., (accumulators::weighted_mean_of_variates<int, tag::covariate1>(acc2)), 1e-5);
  66. acc2(0, weight = 4, covariate1 = 4);
  67. BOOST_CHECK_CLOSE(1./3., weighted_mean(acc2), 1e-5);
  68. BOOST_CHECK_EQUAL(2u, count(acc2));
  69. BOOST_CHECK_CLOSE(3., mean_of_weights(acc2), 1e-5);
  70. BOOST_CHECK_CLOSE(11./3., (accumulators::weighted_mean_of_variates<int, tag::covariate1>(acc2)), 1e-5);
  71. acc2(2, weight = 9, covariate1 = 8);
  72. BOOST_CHECK_CLOSE(4./3., weighted_mean(acc2), 1e-5);
  73. BOOST_CHECK_EQUAL(3u, count(acc2));
  74. BOOST_CHECK_CLOSE(5., mean_of_weights(acc2), 1e-5);
  75. BOOST_CHECK_CLOSE(94./15., (accumulators::mean_of_variates<int, tag::covariate1>(acc2)), 1e-5);
  76. assert_is_double(mean(acc2));
  77. }
  78. ///////////////////////////////////////////////////////////////////////////////
  79. // init_unit_test_suite
  80. //
  81. test_suite* init_unit_test_suite( int argc, char* argv[] )
  82. {
  83. test_suite *test = BOOST_TEST_SUITE("weighted_mean test");
  84. test->add(BOOST_TEST_CASE(&test_stat));
  85. return test;
  86. }