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