• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2015-2018 Hans Dembinski
2 //
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt
5 // or copy at http://www.boost.org/LICENSE_1_0.txt)
6 
7 #include <boost/core/lightweight_test.hpp>
8 #include <boost/histogram/accumulators/ostream.hpp>
9 #include <boost/histogram/accumulators/weighted_mean.hpp>
10 #include <boost/histogram/weight.hpp>
11 #include <sstream>
12 #include "is_close.hpp"
13 #include "throw_exception.hpp"
14 #include "utility_str.hpp"
15 
16 using namespace boost::histogram;
17 using namespace std::literals;
18 
main()19 int main() {
20   using m_t = accumulators::weighted_mean<double>;
21   m_t a;
22   BOOST_TEST_EQ(a.sum_of_weights(), 0);
23   BOOST_TEST_EQ(a, m_t{});
24 
25   a(weight(0.5), 1);
26   a(weight(1.0), 2);
27   a(weight(0.5), 3);
28 
29   BOOST_TEST_EQ(a.sum_of_weights(), 2);
30   BOOST_TEST_EQ(a.sum_of_weights_squared(), 1.5);
31   BOOST_TEST_EQ(a.value(), 2);
32   BOOST_TEST_IS_CLOSE(a.variance(), 0.8, 1e-3);
33 
34   BOOST_TEST_EQ(str(a), "weighted_mean(2, 2, 0.8)"s);
35   BOOST_TEST_EQ(str(a, 25, false), " weighted_mean(2, 2, 0.8)"s);
36   BOOST_TEST_EQ(str(a, 25, true), "weighted_mean(2, 2, 0.8) "s);
37 
38   auto b = a;
39   b += a; // same as feeding all samples twice
40 
41   BOOST_TEST_EQ(b.sum_of_weights(), 4);
42   BOOST_TEST_EQ(b.value(), 2);
43   BOOST_TEST_IS_CLOSE(b.variance(), 0.615, 1e-3);
44 
45   BOOST_TEST_EQ(m_t() += m_t(), m_t());
46   BOOST_TEST_EQ(m_t(1, 2, 3, 4) += m_t(), m_t(1, 2, 3, 4));
47   BOOST_TEST_EQ(m_t() += m_t(1, 2, 3, 4), m_t(1, 2, 3, 4));
48 
49   return boost::report_errors();
50 }
51