1 // (C) Copyright Eric Niebler 2005.
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)
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/stats.hpp>
10 #include <boost/accumulators/statistics/weighted_mean.hpp>
11 #include <boost/accumulators/statistics/variates/covariate.hpp>
13 using namespace boost
;
14 using namespace unit_test
;
15 using namespace accumulators
;
18 void assert_is_double(T
const &)
20 BOOST_MPL_ASSERT((is_same
<T
, double>));
23 ///////////////////////////////////////////////////////////////////////////////
32 , tag::mean_of_weights
33 , tag::weighted_mean_of_variates
<int, tag::covariate1
>
36 > acc
, test_acc(sample
= 0);
38 acc(1, weight
= 2, covariate1
= 3);
39 BOOST_CHECK_CLOSE(1., weighted_mean(acc
), 1e-5);
40 BOOST_CHECK_EQUAL(1u, count(acc
));
41 BOOST_CHECK_EQUAL(2, sum(acc
));
42 BOOST_CHECK_CLOSE(2., mean_of_weights(acc
), 1e-5);
43 BOOST_CHECK_CLOSE(3., (accumulators::weighted_mean_of_variates
<int, tag::covariate1
>(acc
)), 1e-5);
45 acc(0, weight
= 4, covariate1
= 4);
46 BOOST_CHECK_CLOSE(1./3., weighted_mean(acc
), 1e-5);
47 BOOST_CHECK_EQUAL(2u, count(acc
));
48 BOOST_CHECK_EQUAL(2, sum(acc
));
49 BOOST_CHECK_CLOSE(3., mean_of_weights(acc
), 1e-5);
50 BOOST_CHECK_CLOSE(11./3., (accumulators::weighted_mean_of_variates
<int, tag::covariate1
>(acc
)), 1e-5);
52 acc(2, weight
= 9, covariate1
= 8);
53 BOOST_CHECK_CLOSE(4./3., weighted_mean(acc
), 1e-5);
54 BOOST_CHECK_EQUAL(3u, count(acc
));
55 BOOST_CHECK_EQUAL(20, sum(acc
));
56 BOOST_CHECK_CLOSE(5., mean_of_weights(acc
), 1e-5);
57 BOOST_CHECK_CLOSE(94./15., (accumulators::weighted_mean_of_variates
<int, tag::covariate1
>(acc
)), 1e-5);
59 assert_is_double(mean(acc
));
64 tag::weighted_mean(immediate
)
65 , tag::mean_of_weights(immediate
)
66 , tag::weighted_mean_of_variates
<int, tag::covariate1
>(immediate
)
69 > acc2
, test_acc2(sample
= 0);
71 acc2(1, weight
= 2, covariate1
= 3);
72 BOOST_CHECK_CLOSE(1., weighted_mean(acc2
), 1e-5);
73 BOOST_CHECK_EQUAL(1u, count(acc2
));
74 BOOST_CHECK_CLOSE(2., mean_of_weights(acc2
), 1e-5);
75 BOOST_CHECK_CLOSE(3., (accumulators::weighted_mean_of_variates
<int, tag::covariate1
>(acc2
)), 1e-5);
77 acc2(0, weight
= 4, covariate1
= 4);
78 BOOST_CHECK_CLOSE(1./3., weighted_mean(acc2
), 1e-5);
79 BOOST_CHECK_EQUAL(2u, count(acc2
));
80 BOOST_CHECK_CLOSE(3., mean_of_weights(acc2
), 1e-5);
81 BOOST_CHECK_CLOSE(11./3., (accumulators::weighted_mean_of_variates
<int, tag::covariate1
>(acc2
)), 1e-5);
83 acc2(2, weight
= 9, covariate1
= 8);
84 BOOST_CHECK_CLOSE(4./3., weighted_mean(acc2
), 1e-5);
85 BOOST_CHECK_EQUAL(3u, count(acc2
));
86 BOOST_CHECK_CLOSE(5., mean_of_weights(acc2
), 1e-5);
87 BOOST_CHECK_CLOSE(94./15., (accumulators::mean_of_variates
<int, tag::covariate1
>(acc2
)), 1e-5);
89 assert_is_double(mean(acc2
));
92 ///////////////////////////////////////////////////////////////////////////////
93 // init_unit_test_suite
95 test_suite
* init_unit_test_suite( int argc
, char* argv
[] )
97 test_suite
*test
= BOOST_TEST_SUITE("weighted_mean test");
99 test
->add(BOOST_TEST_CASE(&test_stat
));