]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/histogram/test/accumulators_weighted_mean_test.cpp
1 // Copyright 2015-2018 Hans Dembinski
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)
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>
12 #include "is_close.hpp"
13 #include "throw_exception.hpp"
14 #include "utility_str.hpp"
16 using namespace boost::histogram
;
17 using namespace std::literals
;
20 using m_t
= accumulators::weighted_mean
<double>;
23 // basic interface, string conversion
25 // see https://en.wikipedia.org/wiki/Weighted_arithmetic_mean#Reliability_weights
28 BOOST_TEST_EQ(a
.sum_of_weights(), 0);
29 BOOST_TEST_EQ(a
, m_t
{});
35 BOOST_TEST_EQ(a
.sum_of_weights(), 1 + 2 * 0.5);
36 BOOST_TEST_EQ(a
.sum_of_weights_squared(), 1 + 2 * 0.5 * 0.5);
37 // https://en.wikipedia.org/wiki/Effective_sample_size#weighted_samples
38 BOOST_TEST_EQ(a
.count(), square(a
.sum_of_weights()) / a
.sum_of_weights_squared());
39 BOOST_TEST_EQ(a
.value(), (0.5 * 1 + 1.0 * 2 + 0.5 * 3) / a
.sum_of_weights());
40 const auto m
= a
.value();
43 (0.5 * square(1 - m
) + square(2 - m
) + 0.5 * square(3 - m
)) /
44 (a
.sum_of_weights() - a
.sum_of_weights_squared() / a
.sum_of_weights()),
47 BOOST_TEST_EQ(str(a
), "weighted_mean(2, 2, 0.8)"s
);
48 BOOST_TEST_EQ(str(a
, 25, false), " weighted_mean(2, 2, 0.8)"s
);
49 BOOST_TEST_EQ(str(a
, 25, true), "weighted_mean(2, 2, 0.8) "s
);
52 // addition of zero element
54 BOOST_TEST_EQ(m_t() += m_t(), m_t());
55 BOOST_TEST_EQ(m_t(1, 2, 3, 4) += m_t(), m_t(1, 2, 3, 4));
56 BOOST_TEST_EQ(m_t() += m_t(1, 2, 3, 4), m_t(1, 2, 3, 4));
65 BOOST_TEST_EQ(a
.sum_of_weights(), 4 + 3);
66 BOOST_TEST_EQ(a
.sum_of_weights_squared(), 4 * 4 + 3 * 3);
67 BOOST_TEST_EQ(a
.value(), (4 * 2 + 3 * 3) / 7.);
68 BOOST_TEST_IS_CLOSE(a
.variance(), 0.5, 1e-3);
72 BOOST_TEST_EQ(b
.sum_of_weights(), 3);
73 BOOST_TEST_EQ(b
.sum_of_weights_squared(), 1 + 2 * 2);
74 BOOST_TEST_EQ(b
.value(), (2 * 4 + 1 * 6) / (2. + 1.));
75 BOOST_TEST_IS_CLOSE(b
.variance(), 2, 1e-3);
84 BOOST_TEST_EQ(c
.sum_of_weights(), d
.sum_of_weights());
85 BOOST_TEST_EQ(c
.sum_of_weights_squared(), d
.sum_of_weights_squared());
86 BOOST_TEST_EQ(c
.value(), d
.value());
87 BOOST_TEST_IS_CLOSE(c
.variance(), d
.variance(), 1e-3);
90 // using weights * 2 compared to adding weighted samples twice must
91 // - give same for sum_of_weights and mean
92 // - give twice sum_of_weights_squared
93 // - give half effective count
94 // - variance is complicated, but larger
98 for (int i
= 0; i
< 2; ++i
) {
108 BOOST_TEST_EQ(a
.sum_of_weights(), b
.sum_of_weights());
109 BOOST_TEST_EQ(2 * a
.sum_of_weights_squared(), b
.sum_of_weights_squared());
110 BOOST_TEST_EQ(a
.count(), 2 * b
.count());
111 BOOST_TEST_EQ(a
.value(), b
.value());
112 BOOST_TEST_LT(a
.variance(), b
.variance());
115 return boost::report_errors();