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