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)
5
6#include <boost/test/unit_test.hpp>
7#include <boost/test/tools/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>
12
13using namespace boost;
14using namespace unit_test;
15using namespace accumulators;
16
17template<typename T>
18void assert_is_double(T const &)
19{
20 BOOST_MPL_ASSERT((is_same<T, double>));
21}
22
23///////////////////////////////////////////////////////////////////////////////
24// test_stat
25//
26void test_stat()
27{
28 accumulator_set<
29 int
30 , stats<
31 tag::weighted_mean
32 , tag::mean_of_weights
33 , tag::weighted_mean_of_variates<int, tag::covariate1>
34 >
35 , int
36 > acc, test_acc(sample = 0);
37
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);
44
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);
51
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);
58
59 assert_is_double(mean(acc));
60
61 accumulator_set<
62 int
63 , stats<
64 tag::weighted_mean(immediate)
65 , tag::mean_of_weights(immediate)
66 , tag::weighted_mean_of_variates<int, tag::covariate1>(immediate)
67 >
68 , int
69 > acc2, test_acc2(sample = 0);
70
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);
76
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);
82
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);
88
89 assert_is_double(mean(acc2));
90}
91
92///////////////////////////////////////////////////////////////////////////////
93// init_unit_test_suite
94//
95test_suite* init_unit_test_suite( int argc, char* argv[] )
96{
97 test_suite *test = BOOST_TEST_SUITE("weighted_mean test");
98
99 test->add(BOOST_TEST_CASE(&test_stat));
100
101 return test;
102}
103

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