• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
6 #define BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT
7 
8 #include <boost/test/unit_test.hpp>
9 #include <boost/test/floating_point_comparison.hpp>
10 #include <boost/accumulators/accumulators.hpp>
11 #include <boost/accumulators/statistics/variates/covariate.hpp>
12 #include <boost/accumulators/statistics/stats.hpp>
13 #include <boost/accumulators/statistics/weighted_covariance.hpp>
14 
15 using namespace boost;
16 using namespace unit_test;
17 using namespace accumulators;
18 
19 ///////////////////////////////////////////////////////////////////////////////
20 // test_stat
21 //
test_stat()22 void test_stat()
23 {
24     std::vector<double> dummy;
25     dummy.push_back(0);
26     dummy.push_back(0);
27 
28     accumulator_set<double, stats<tag::weighted_covariance<double, tag::covariate1> >, double > acc;
29     accumulator_set<std::vector<double>, stats<tag::weighted_covariance<double, tag::covariate1> >, double > acc2(sample = dummy);
30     accumulator_set<double, stats<tag::weighted_covariance<std::vector<double>, tag::covariate1> >, double > acc3(covariate1 = dummy);
31     accumulator_set<std::vector<double>, stats<tag::weighted_covariance<std::vector<double>, tag::covariate1> >, double > acc4(sample = dummy, covariate1 = dummy);
32 
33     std::vector<double> a;
34     a.push_back(1.);
35     a.push_back(2.);
36     std::vector<double> b;
37     b.push_back(3.);
38     b.push_back(4.);
39     std::vector<double> c;
40     c.push_back(2.);
41     c.push_back(5.);
42     std::vector<double> d;
43     d.push_back(4.);
44     d.push_back(2.);
45 
46     // double - double
47     {
48         acc(1., weight = 1.1, covariate1 = 2.);
49         acc(1., weight = 2.2, covariate1 = 4.);
50         acc(2., weight = 3.3, covariate1 = 3.);
51         acc(6., weight = 4.4, covariate1 = 1.);
52     }
53 
54     // vector - double
55     {
56         acc2(a, weight = 1.1, covariate1 = 1.);
57         acc2(b, weight = 2.2, covariate1 = 1.);
58         acc2(c, weight = 3.3, covariate1 = 2.);
59         acc2(d, weight = 4.4, covariate1 = 6.);
60     }
61 
62     // double - vector
63     {
64         acc3(1., weight = 1.1, covariate1 = a);
65         acc3(1., weight = 2.2, covariate1 = b);
66         acc3(2., weight = 3.3, covariate1 = c);
67         acc3(6., weight = 4.4, covariate1 = d);
68     }
69 
70     // vector - vector
71     {
72         acc4(a, weight = 1.1, covariate1 = b);
73         acc4(b, weight = 2.2, covariate1 = c);
74         acc4(a, weight = 3.3, covariate1 = c);
75         acc4(d, weight = 4.4, covariate1 = b);
76     }
77 
78     double epsilon = 1e-6;
79 
80     BOOST_CHECK_CLOSE((weighted_covariance(acc)), -2.39, epsilon);
81     BOOST_CHECK_CLOSE((weighted_covariance(acc2))[0], 1.93, epsilon);
82     BOOST_CHECK_CLOSE((weighted_covariance(acc2))[1], -2.09, epsilon);
83     BOOST_CHECK_CLOSE((weighted_covariance(acc3))[0], 1.93, epsilon);
84     BOOST_CHECK_CLOSE((weighted_covariance(acc3))[1], -2.09, epsilon);
85     BOOST_CHECK_CLOSE((weighted_covariance(acc4))(0,0), 0.4, epsilon);
86     BOOST_CHECK_CLOSE((weighted_covariance(acc4))(0,1), -0.2, epsilon);
87     BOOST_CHECK_CLOSE((weighted_covariance(acc4))(1,0), -0.4, epsilon);
88     BOOST_CHECK_CLOSE((weighted_covariance(acc4))(1,1), 0.2, epsilon);
89 }
90 
91 ///////////////////////////////////////////////////////////////////////////////
92 // init_unit_test_suite
93 //
init_unit_test_suite(int argc,char * argv[])94 test_suite* init_unit_test_suite( int argc, char* argv[] )
95 {
96     test_suite *test = BOOST_TEST_SUITE("weighted_covariance test");
97 
98     test->add(BOOST_TEST_CASE(&test_stat));
99 
100     return test;
101 }
102