weighted_covariance.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/weighted_covariance.hpp>
  12. using namespace boost;
  13. using namespace unit_test;
  14. using namespace accumulators;
  15. ///////////////////////////////////////////////////////////////////////////////
  16. // test_stat
  17. //
  18. void test_stat()
  19. {
  20. std::vector<double> dummy;
  21. dummy.push_back(0);
  22. dummy.push_back(0);
  23. accumulator_set<double, stats<tag::weighted_covariance<double, tag::covariate1> >, double > acc;
  24. accumulator_set<std::vector<double>, stats<tag::weighted_covariance<double, tag::covariate1> >, double > acc2(sample = dummy);
  25. accumulator_set<double, stats<tag::weighted_covariance<std::vector<double>, tag::covariate1> >, double > acc3(covariate1 = dummy);
  26. accumulator_set<std::vector<double>, stats<tag::weighted_covariance<std::vector<double>, tag::covariate1> >, double > acc4(sample = dummy, covariate1 = dummy);
  27. std::vector<double> a;
  28. a.push_back(1.);
  29. a.push_back(2.);
  30. std::vector<double> b;
  31. b.push_back(3.);
  32. b.push_back(4.);
  33. std::vector<double> c;
  34. c.push_back(2.);
  35. c.push_back(5.);
  36. std::vector<double> d;
  37. d.push_back(4.);
  38. d.push_back(2.);
  39. // double - double
  40. {
  41. acc(1., weight = 1.1, covariate1 = 2.);
  42. acc(1., weight = 2.2, covariate1 = 4.);
  43. acc(2., weight = 3.3, covariate1 = 3.);
  44. acc(6., weight = 4.4, covariate1 = 1.);
  45. }
  46. // vector - double
  47. {
  48. acc2(a, weight = 1.1, covariate1 = 1.);
  49. acc2(b, weight = 2.2, covariate1 = 1.);
  50. acc2(c, weight = 3.3, covariate1 = 2.);
  51. acc2(d, weight = 4.4, covariate1 = 6.);
  52. }
  53. // double - vector
  54. {
  55. acc3(1., weight = 1.1, covariate1 = a);
  56. acc3(1., weight = 2.2, covariate1 = b);
  57. acc3(2., weight = 3.3, covariate1 = c);
  58. acc3(6., weight = 4.4, covariate1 = d);
  59. }
  60. // vector - vector
  61. {
  62. acc4(a, weight = 1.1, covariate1 = b);
  63. acc4(b, weight = 2.2, covariate1 = c);
  64. acc4(a, weight = 3.3, covariate1 = c);
  65. acc4(d, weight = 4.4, covariate1 = b);
  66. }
  67. double epsilon = 1e-6;
  68. BOOST_CHECK_CLOSE((weighted_covariance(acc)), -2.39, epsilon);
  69. BOOST_CHECK_CLOSE((weighted_covariance(acc2))[0], 1.93, epsilon);
  70. BOOST_CHECK_CLOSE((weighted_covariance(acc2))[1], -2.09, epsilon);
  71. BOOST_CHECK_CLOSE((weighted_covariance(acc3))[0], 1.93, epsilon);
  72. BOOST_CHECK_CLOSE((weighted_covariance(acc3))[1], -2.09, epsilon);
  73. BOOST_CHECK_CLOSE((weighted_covariance(acc4))(0,0), 0.4, epsilon);
  74. BOOST_CHECK_CLOSE((weighted_covariance(acc4))(0,1), -0.2, epsilon);
  75. BOOST_CHECK_CLOSE((weighted_covariance(acc4))(1,0), -0.4, epsilon);
  76. BOOST_CHECK_CLOSE((weighted_covariance(acc4))(1,1), 0.2, epsilon);
  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_covariance test");
  84. test->add(BOOST_TEST_CASE(&test_stat));
  85. return test;
  86. }