covariance.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #define BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT
  6. #include <boost/test/unit_test.hpp>
  7. #include <boost/test/floating_point_comparison.hpp>
  8. #include <boost/accumulators/accumulators.hpp>
  9. #include <boost/accumulators/statistics/variates/covariate.hpp>
  10. #include <boost/accumulators/statistics/stats.hpp>
  11. #include <boost/accumulators/statistics/covariance.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. ///////////////////////////////////////////////////////////////////////////////
  19. // test_stat
  20. //
  21. void test_stat()
  22. {
  23. std::vector<double> dummy;
  24. dummy.push_back(0);
  25. dummy.push_back(0);
  26. accumulator_set<double, stats<tag::covariance<double, tag::covariate1> > > acc;
  27. accumulator_set<std::vector<double>, stats<tag::covariance<double, tag::covariate1> > > acc2(sample = dummy);
  28. accumulator_set<double, stats<tag::covariance<std::vector<double>, tag::covariate1> > > acc3(covariate1 = dummy);
  29. accumulator_set<std::vector<double>, stats<tag::covariance<std::vector<double>, tag::covariate1> > > acc4(sample = dummy, covariate1 = dummy);
  30. std::vector<double> a;
  31. a.push_back(1.);
  32. a.push_back(2.);
  33. std::vector<double> b;
  34. b.push_back(3.);
  35. b.push_back(4.);
  36. std::vector<double> c;
  37. c.push_back(2.);
  38. c.push_back(5.);
  39. std::vector<double> d;
  40. d.push_back(4.);
  41. d.push_back(2.);
  42. // double - double
  43. {
  44. acc(1., covariate1 = 2.);
  45. acc(1., covariate1 = 4.);
  46. acc(2., covariate1 = 3.);
  47. acc(6., covariate1 = 1.);
  48. }
  49. // vector - double
  50. {
  51. acc2(a, covariate1 = 1.);
  52. acc2(b, covariate1 = 1.);
  53. acc2(c, covariate1 = 2.);
  54. acc2(d, covariate1 = 6.);
  55. }
  56. // double - vector
  57. {
  58. acc3(1., covariate1 = a);
  59. acc3(1., covariate1 = b);
  60. acc3(2., covariate1 = c);
  61. acc3(6., covariate1 = d);
  62. }
  63. // vector - vector
  64. {
  65. acc4(a, covariate1 = b);
  66. acc4(b, covariate1 = c);
  67. acc4(a, covariate1 = c);
  68. acc4(d, covariate1 = b);
  69. }
  70. double epsilon = 1e-6;
  71. BOOST_CHECK_CLOSE((covariance(acc)), -1.75, epsilon);
  72. BOOST_CHECK_CLOSE((covariance(acc2))[0], 1.75, epsilon);
  73. BOOST_CHECK_CLOSE((covariance(acc2))[1], -1.125, epsilon);
  74. BOOST_CHECK_CLOSE((covariance(acc3))[0], 1.75, epsilon);
  75. BOOST_CHECK_CLOSE((covariance(acc3))[1], -1.125, epsilon);
  76. BOOST_CHECK_CLOSE((covariance(acc4))(0,0), 0.125, epsilon);
  77. BOOST_CHECK_CLOSE((covariance(acc4))(0,1), -0.25, epsilon);
  78. BOOST_CHECK_CLOSE((covariance(acc4))(1,0), -0.125, epsilon);
  79. BOOST_CHECK_CLOSE((covariance(acc4))(1,1), 0.25, epsilon);
  80. }
  81. ///////////////////////////////////////////////////////////////////////////////
  82. // test_persistency
  83. //
  84. void test_persistency()
  85. {
  86. // "persistent" storage
  87. std::stringstream ss;
  88. // tolerance
  89. double epsilon = 1e-6;
  90. {
  91. accumulator_set<double, stats<tag::covariance<double, tag::covariate1> > > acc;
  92. acc(1., covariate1 = 2.);
  93. acc(1., covariate1 = 4.);
  94. acc(2., covariate1 = 3.);
  95. acc(6., covariate1 = 1.);
  96. BOOST_CHECK_CLOSE((covariance(acc)), -1.75, epsilon);
  97. boost::archive::text_oarchive oa(ss);
  98. acc.serialize(oa, 0);
  99. }
  100. accumulator_set<double, stats<tag::covariance<double, tag::covariate1> > > acc;
  101. BOOST_CHECK_CLOSE((covariance(acc)), 0., epsilon);
  102. boost::archive::text_iarchive ia(ss);
  103. acc.serialize(ia, 0);
  104. BOOST_CHECK_CLOSE((covariance(acc)), -1.75, epsilon);
  105. }
  106. ///////////////////////////////////////////////////////////////////////////////
  107. // init_unit_test_suite
  108. //
  109. test_suite* init_unit_test_suite( int argc, char* argv[] )
  110. {
  111. test_suite *test = BOOST_TEST_SUITE("covariance test");
  112. test->add(BOOST_TEST_CASE(&test_stat));
  113. test->add(BOOST_TEST_CASE(&test_persistency));
  114. return test;
  115. }