weighted_variance.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #include <boost/test/unit_test.hpp>
  6. #include <boost/test/floating_point_comparison.hpp>
  7. #include <boost/random.hpp>
  8. #include <boost/accumulators/accumulators.hpp>
  9. #include <boost/accumulators/statistics/stats.hpp>
  10. #include <boost/accumulators/statistics/weighted_variance.hpp>
  11. using namespace boost;
  12. using namespace unit_test;
  13. using namespace accumulators;
  14. ///////////////////////////////////////////////////////////////////////////////
  15. // test_stat
  16. //
  17. void test_stat()
  18. {
  19. // lazy weighted_variance
  20. accumulator_set<int, stats<tag::weighted_variance(lazy)>, int> acc1;
  21. acc1(1, weight = 2); // 2
  22. acc1(2, weight = 3); // 6
  23. acc1(3, weight = 1); // 3
  24. acc1(4, weight = 4); // 16
  25. acc1(5, weight = 1); // 5
  26. // weighted_mean = (2+6+3+16+5) / (2+3+1+4+1) = 32 / 11 = 2.9090909090909090909090909090909
  27. BOOST_CHECK_EQUAL(5u, count(acc1));
  28. BOOST_CHECK_CLOSE(2.9090909, weighted_mean(acc1), 1e-5);
  29. BOOST_CHECK_CLOSE(10.1818182, accumulators::weighted_moment<2>(acc1), 1e-5);
  30. BOOST_CHECK_CLOSE(1.7190083, weighted_variance(acc1), 1e-5);
  31. accumulator_set<int, stats<tag::weighted_variance>, int> acc2;
  32. acc2(1, weight = 2);
  33. acc2(2, weight = 3);
  34. acc2(3, weight = 1);
  35. acc2(4, weight = 4);
  36. acc2(5, weight = 1);
  37. BOOST_CHECK_EQUAL(5u, count(acc2));
  38. BOOST_CHECK_CLOSE(2.9090909, weighted_mean(acc2), 1e-5);
  39. BOOST_CHECK_CLOSE(1.7190083, weighted_variance(acc2), 1e-5);
  40. // check lazy and immediate variance with random numbers
  41. // two random number generators
  42. boost::lagged_fibonacci607 rng;
  43. boost::normal_distribution<> mean_sigma(0,1);
  44. boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal(rng, mean_sigma);
  45. accumulator_set<double, stats<tag::weighted_variance(lazy)>, double > acc_lazy;
  46. accumulator_set<double, stats<tag::weighted_variance>, double > acc_immediate;
  47. for (std::size_t i=0; i<10000; ++i)
  48. {
  49. double value = normal();
  50. acc_lazy(value, weight = rng());
  51. acc_immediate(value, weight = rng());
  52. }
  53. BOOST_CHECK_CLOSE(1., weighted_variance(acc_lazy), 5.);
  54. BOOST_CHECK_CLOSE(1., weighted_variance(acc_immediate), 5.);
  55. }
  56. ///////////////////////////////////////////////////////////////////////////////
  57. // init_unit_test_suite
  58. //
  59. test_suite* init_unit_test_suite( int argc, char* argv[] )
  60. {
  61. test_suite *test = BOOST_TEST_SUITE("weighted_variance test");
  62. test->add(BOOST_TEST_CASE(&test_stat));
  63. return test;
  64. }