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
15using namespace boost;
16using namespace unit_test;
17using namespace accumulators;
18
19///////////////////////////////////////////////////////////////////////////////
20// test_stat
21//
22void test_stat()
23{
24 std::vector<double> dummy;
25 dummy.push_back(x: 0);
26 dummy.push_back(x: 0);
27
28 accumulator_set<double, stats<tag::covariance<double, tag::covariate1> > > acc;
29 accumulator_set<std::vector<double>, stats<tag::covariance<double, tag::covariate1> > > acc2(sample = dummy);
30 accumulator_set<double, stats<tag::covariance<std::vector<double>, tag::covariate1> > > acc3(covariate1 = dummy);
31 accumulator_set<std::vector<double>, stats<tag::covariance<std::vector<double>, tag::covariate1> > > acc4(sample = dummy, covariate1 = dummy);
32
33 std::vector<double> a;
34 a.push_back(x: 1.);
35 a.push_back(x: 2.);
36 std::vector<double> b;
37 b.push_back(x: 3.);
38 b.push_back(x: 4.);
39 std::vector<double> c;
40 c.push_back(x: 2.);
41 c.push_back(x: 5.);
42 std::vector<double> d;
43 d.push_back(x: 4.);
44 d.push_back(x: 2.);
45
46 // double - double
47 {
48 acc(1., covariate1 = 2.);
49 acc(1., covariate1 = 4.);
50 acc(2., covariate1 = 3.);
51 acc(6., covariate1 = 1.);
52 }
53
54 // vector - double
55 {
56 acc2(a, covariate1 = 1.);
57 acc2(b, covariate1 = 1.);
58 acc2(c, covariate1 = 2.);
59 acc2(d, covariate1 = 6.);
60 }
61
62 // double - vector
63 {
64 acc3(1., covariate1 = a);
65 acc3(1., covariate1 = b);
66 acc3(2., covariate1 = c);
67 acc3(6., covariate1 = d);
68 }
69
70 // vector - vector
71 {
72 acc4(a, covariate1 = b);
73 acc4(b, covariate1 = c);
74 acc4(a, covariate1 = c);
75 acc4(d, covariate1 = b);
76 }
77
78 double epsilon = 1e-6;
79
80 BOOST_CHECK_CLOSE((covariance(acc)), -1.75, epsilon);
81 BOOST_CHECK_CLOSE((covariance(acc2))[0], 1.75, epsilon);
82 BOOST_CHECK_CLOSE((covariance(acc2))[1], -1.125, epsilon);
83 BOOST_CHECK_CLOSE((covariance(acc3))[0], 1.75, epsilon);
84 BOOST_CHECK_CLOSE((covariance(acc3))[1], -1.125, epsilon);
85 BOOST_CHECK_CLOSE((covariance(acc4))(0,0), 0.125, epsilon);
86 BOOST_CHECK_CLOSE((covariance(acc4))(0,1), -0.25, epsilon);
87 BOOST_CHECK_CLOSE((covariance(acc4))(1,0), -0.125, epsilon);
88 BOOST_CHECK_CLOSE((covariance(acc4))(1,1), 0.25, epsilon);
89}
90
91///////////////////////////////////////////////////////////////////////////////
92// init_unit_test_suite
93//
94test_suite* init_unit_test_suite( int argc, char* argv[] )
95{
96 test_suite *test = BOOST_TEST_SUITE("covariance test");
97
98 test->add(BOOST_TEST_CASE(&test_stat));
99
100 return test;
101}
102

source code of boost/libs/accumulators/test/covariance.cpp