variance.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // (C) Copyright 2005 Daniel Egloff, Eric Niebler
  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/variance.hpp>
  10. #include <sstream>
  11. #include <boost/archive/text_oarchive.hpp>
  12. #include <boost/archive/text_iarchive.hpp>
  13. using namespace boost;
  14. using namespace unit_test;
  15. using namespace accumulators;
  16. ///////////////////////////////////////////////////////////////////////////////
  17. // test_stat
  18. //
  19. void test_stat()
  20. {
  21. // matlab
  22. // >> samples = [1:5];
  23. // >> mean(samples)
  24. // ans = 3
  25. // >> sum(samples .* samples) / length(samples)
  26. // ans = 11
  27. // >> sum(samples .* samples) / length(samples) - mean(samples)^2
  28. // ans = 2
  29. // lazy variance, now lazy with syntactic sugar, thanks to Eric
  30. accumulator_set<int, stats<tag::variance(lazy)> > acc1;
  31. acc1(1);
  32. acc1(2);
  33. acc1(3);
  34. acc1(4);
  35. acc1(5);
  36. BOOST_CHECK_EQUAL(5u, count(acc1));
  37. BOOST_CHECK_CLOSE(3., mean(acc1), 1e-5);
  38. BOOST_CHECK_CLOSE(11., accumulators::moment<2>(acc1), 1e-5);
  39. BOOST_CHECK_CLOSE(2., variance(acc1), 1e-5);
  40. // immediate variance
  41. accumulator_set<int, stats<tag::variance> > acc2;
  42. acc2(1);
  43. acc2(2);
  44. acc2(3);
  45. acc2(4);
  46. acc2(5);
  47. BOOST_CHECK_EQUAL(5u, count(acc2));
  48. BOOST_CHECK_CLOSE(3., mean(acc2), 1e-5);
  49. BOOST_CHECK_CLOSE(2., variance(acc2), 1e-5);
  50. }
  51. ///////////////////////////////////////////////////////////////////////////////
  52. // test_persistency
  53. //
  54. void test_persistency()
  55. {
  56. double epsilon = 1e-5;
  57. std::stringstream ss;
  58. {
  59. accumulator_set<int, stats<tag::variance(lazy)> > acc1;
  60. accumulator_set<int, stats<tag::variance> > acc2;
  61. acc1(1);
  62. acc1(2);
  63. acc1(3);
  64. acc1(4);
  65. acc1(5);
  66. acc2(1);
  67. acc2(2);
  68. acc2(3);
  69. acc2(4);
  70. acc2(5);
  71. BOOST_CHECK_CLOSE(2., variance(acc2), epsilon);
  72. BOOST_CHECK_CLOSE(2., variance(acc1), epsilon);
  73. boost::archive::text_oarchive oa(ss);
  74. acc1.serialize(oa, 0);
  75. acc2.serialize(oa, 0);
  76. }
  77. accumulator_set<int, stats<tag::variance(lazy)> > acc1;
  78. accumulator_set<int, stats<tag::variance> > acc2;
  79. boost::archive::text_iarchive ia(ss);
  80. acc1.serialize(ia, 0);
  81. acc2.serialize(ia, 0);
  82. BOOST_CHECK_CLOSE(2., variance(acc2), epsilon);
  83. BOOST_CHECK_CLOSE(2., variance(acc1), epsilon);
  84. }
  85. ///////////////////////////////////////////////////////////////////////////////
  86. // init_unit_test_suite
  87. //
  88. test_suite* init_unit_test_suite( int argc, char* argv[] )
  89. {
  90. test_suite *test = BOOST_TEST_SUITE("variance test");
  91. test->add(BOOST_TEST_CASE(&test_stat));
  92. test->add(BOOST_TEST_CASE(&test_persistency));
  93. return test;
  94. }