mirror of
https://github.com/boostorg/histogram.git
synced 2025-05-11 13:14:06 +00:00
adding weight support for mean accumulator (#212)
changed type of sum to RealType for mean and added serialization tests for builtin accumulators
This commit is contained in:
parent
14f36ea932
commit
0d893d4eff
@ -28,12 +28,19 @@ public:
|
|||||||
: sum_(n), mean_(mean), sum_of_deltas_squared_(variance * (sum_ - 1)) {}
|
: sum_(n), mean_(mean), sum_of_deltas_squared_(variance * (sum_ - 1)) {}
|
||||||
|
|
||||||
void operator()(const RealType& x) {
|
void operator()(const RealType& x) {
|
||||||
sum_ += 1;
|
sum_ += static_cast<RealType>(1);
|
||||||
const auto delta = x - mean_;
|
const auto delta = x - mean_;
|
||||||
mean_ += delta / sum_;
|
mean_ += delta / sum_;
|
||||||
sum_of_deltas_squared_ += delta * (x - mean_);
|
sum_of_deltas_squared_ += delta * (x - mean_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void operator()(const RealType& w, const RealType& x) {
|
||||||
|
sum_ += w;
|
||||||
|
const auto delta = x - mean_;
|
||||||
|
mean_ += w * delta / sum_;
|
||||||
|
sum_of_deltas_squared_ += w * delta * (x - mean_);
|
||||||
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
mean& operator+=(const mean<T>& rhs) {
|
mean& operator+=(const mean<T>& rhs) {
|
||||||
const auto tmp = mean_ * sum_ + static_cast<RealType>(rhs.mean_ * rhs.sum_);
|
const auto tmp = mean_ * sum_ + static_cast<RealType>(rhs.mean_ * rhs.sum_);
|
||||||
@ -60,7 +67,7 @@ public:
|
|||||||
return !operator==(rhs);
|
return !operator==(rhs);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t count() const noexcept { return sum_; }
|
const RealType& count() const noexcept { return sum_; }
|
||||||
const RealType& value() const noexcept { return mean_; }
|
const RealType& value() const noexcept { return mean_; }
|
||||||
RealType variance() const { return sum_of_deltas_squared_ / (sum_ - 1); }
|
RealType variance() const { return sum_of_deltas_squared_ / (sum_ - 1); }
|
||||||
|
|
||||||
@ -68,8 +75,7 @@ public:
|
|||||||
void serialize(Archive&, unsigned /* version */);
|
void serialize(Archive&, unsigned /* version */);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::size_t sum_ = 0;
|
RealType sum_ = 0, mean_ = 0, sum_of_deltas_squared_ = 0;
|
||||||
RealType mean_ = 0, sum_of_deltas_squared_ = 0;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace accumulators
|
} // namespace accumulators
|
||||||
|
@ -32,6 +32,7 @@
|
|||||||
#include <boost/serialization/string.hpp>
|
#include <boost/serialization/string.hpp>
|
||||||
#include <boost/serialization/throw_exception.hpp>
|
#include <boost/serialization/throw_exception.hpp>
|
||||||
#include <boost/serialization/vector.hpp>
|
#include <boost/serialization/vector.hpp>
|
||||||
|
#include <boost/serialization/version.hpp>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
@ -44,6 +45,18 @@
|
|||||||
|
|
||||||
#ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
|
#ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
|
||||||
|
|
||||||
|
namespace boost {
|
||||||
|
namespace serialization {
|
||||||
|
// version 1 for boost::histogram::accumulators::mean<RealType>
|
||||||
|
template <class RealType>
|
||||||
|
struct version<::boost::histogram::accumulators::mean<RealType>> {
|
||||||
|
typedef mpl::int_<1> type;
|
||||||
|
typedef mpl::integral_c_tag tag;
|
||||||
|
BOOST_STATIC_CONSTANT(int, value = version::type::value);
|
||||||
|
};
|
||||||
|
} // namespace serialization
|
||||||
|
} // namespace boost
|
||||||
|
|
||||||
namespace std {
|
namespace std {
|
||||||
template <class Archive, class... Ts>
|
template <class Archive, class... Ts>
|
||||||
void serialize(Archive& ar, tuple<Ts...>& t, unsigned /* version */) {
|
void serialize(Archive& ar, tuple<Ts...>& t, unsigned /* version */) {
|
||||||
@ -72,8 +85,14 @@ void weighted_sum<RealType>::serialize(Archive& ar, unsigned /* version */) {
|
|||||||
|
|
||||||
template <class RealType>
|
template <class RealType>
|
||||||
template <class Archive>
|
template <class Archive>
|
||||||
void mean<RealType>::serialize(Archive& ar, unsigned /* version */) {
|
void mean<RealType>::serialize(Archive& ar, unsigned version) {
|
||||||
ar& serialization::make_nvp("sum", sum_);
|
if (version == 0 && Archive::is_loading::value) {
|
||||||
|
std::size_t sum;
|
||||||
|
ar& serialization::make_nvp("sum", sum);
|
||||||
|
sum_ = static_cast<RealType>(sum);
|
||||||
|
} else {
|
||||||
|
ar& serialization::make_nvp("sum", sum_);
|
||||||
|
}
|
||||||
ar& serialization::make_nvp("mean", mean_);
|
ar& serialization::make_nvp("mean", mean_);
|
||||||
ar& serialization::make_nvp("sum_of_deltas_squared", sum_of_deltas_squared_);
|
ar& serialization::make_nvp("sum_of_deltas_squared", sum_of_deltas_squared_);
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,8 @@ boost_test(SOURCES check_cmake_version.cpp ARGUMENTS ${PROJECT_VERSION}
|
|||||||
# keep in sync with Jamfile, this should be automatized...
|
# keep in sync with Jamfile, this should be automatized...
|
||||||
boost_test(TYPE compile-fail SOURCES make_histogram_fail0.cpp)
|
boost_test(TYPE compile-fail SOURCES make_histogram_fail0.cpp)
|
||||||
boost_test(TYPE compile-fail SOURCES make_histogram_fail1.cpp)
|
boost_test(TYPE compile-fail SOURCES make_histogram_fail1.cpp)
|
||||||
|
boost_test(TYPE run SOURCES accumulators_test.cpp
|
||||||
|
LIBRARIES Boost::histogram Boost::core)
|
||||||
boost_test(TYPE run SOURCES algorithm_project_test.cpp
|
boost_test(TYPE run SOURCES algorithm_project_test.cpp
|
||||||
LIBRARIES Boost::histogram Boost::core)
|
LIBRARIES Boost::histogram Boost::core)
|
||||||
boost_test(TYPE run SOURCES algorithm_reduce_test.cpp
|
boost_test(TYPE run SOURCES algorithm_reduce_test.cpp
|
||||||
@ -78,8 +80,6 @@ boost_test(TYPE run SOURCES histogram_test.cpp
|
|||||||
LIBRARIES Boost::histogram Boost::core)
|
LIBRARIES Boost::histogram Boost::core)
|
||||||
boost_test(TYPE run SOURCES indexed_test.cpp
|
boost_test(TYPE run SOURCES indexed_test.cpp
|
||||||
LIBRARIES Boost::histogram Boost::core)
|
LIBRARIES Boost::histogram Boost::core)
|
||||||
boost_test(TYPE run SOURCES internal_accumulators_test.cpp
|
|
||||||
LIBRARIES Boost::histogram Boost::core)
|
|
||||||
boost_test(TYPE run SOURCES storage_adaptor_test.cpp
|
boost_test(TYPE run SOURCES storage_adaptor_test.cpp
|
||||||
LIBRARIES Boost::histogram Boost::core)
|
LIBRARIES Boost::histogram Boost::core)
|
||||||
boost_test(TYPE run SOURCES unlimited_storage_test.cpp
|
boost_test(TYPE run SOURCES unlimited_storage_test.cpp
|
||||||
|
@ -22,6 +22,7 @@ project
|
|||||||
;
|
;
|
||||||
|
|
||||||
alias cxx14 :
|
alias cxx14 :
|
||||||
|
[ run accumulators_test.cpp ]
|
||||||
[ run algorithm_project_test.cpp ]
|
[ run algorithm_project_test.cpp ]
|
||||||
[ run algorithm_reduce_test.cpp ]
|
[ run algorithm_reduce_test.cpp ]
|
||||||
[ run algorithm_sum_test.cpp ]
|
[ run algorithm_sum_test.cpp ]
|
||||||
@ -57,7 +58,6 @@ alias cxx14 :
|
|||||||
[ run histogram_operators_test.cpp ]
|
[ run histogram_operators_test.cpp ]
|
||||||
[ run histogram_test.cpp ]
|
[ run histogram_test.cpp ]
|
||||||
[ run indexed_test.cpp ]
|
[ run indexed_test.cpp ]
|
||||||
[ run internal_accumulators_test.cpp ]
|
|
||||||
[ run storage_adaptor_test.cpp ]
|
[ run storage_adaptor_test.cpp ]
|
||||||
[ run unlimited_storage_test.cpp ]
|
[ run unlimited_storage_test.cpp ]
|
||||||
[ run utility_test.cpp ]
|
[ run utility_test.cpp ]
|
||||||
@ -86,6 +86,7 @@ alias accumulators : [ run boost_accumulators_support_test.cpp ] : <warnings>off
|
|||||||
alias range : [ run boost_range_support_test.cpp ] : <warnings>off ;
|
alias range : [ run boost_range_support_test.cpp ] : <warnings>off ;
|
||||||
alias units : [ run boost_units_support_test.cpp ] : <warnings>off ;
|
alias units : [ run boost_units_support_test.cpp ] : <warnings>off ;
|
||||||
alias serialization :
|
alias serialization :
|
||||||
|
[ run accumulators_serialization_test.cpp libserial : $(THIS_PATH) ]
|
||||||
[ run axis_variant_serialization_test.cpp libserial : $(THIS_PATH) ]
|
[ run axis_variant_serialization_test.cpp libserial : $(THIS_PATH) ]
|
||||||
[ run histogram_serialization_test.cpp libserial : $(THIS_PATH) ]
|
[ run histogram_serialization_test.cpp libserial : $(THIS_PATH) ]
|
||||||
[ run storage_adaptor_serialization_test.cpp libserial : $(THIS_PATH) ]
|
[ run storage_adaptor_serialization_test.cpp libserial : $(THIS_PATH) ]
|
||||||
|
90
test/accumulators_serialization_test.cpp
Normal file
90
test/accumulators_serialization_test.cpp
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
// Copyright 2019 Hans Dembinski
|
||||||
|
//
|
||||||
|
// Distributed under 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)
|
||||||
|
|
||||||
|
#include <boost/assert.hpp>
|
||||||
|
#include <boost/core/lightweight_test.hpp>
|
||||||
|
#include <boost/histogram/accumulators.hpp>
|
||||||
|
#include <boost/histogram/serialization.hpp>
|
||||||
|
#include "throw_exception.hpp"
|
||||||
|
#include "utility_serialization.hpp"
|
||||||
|
|
||||||
|
using namespace boost::histogram;
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
BOOST_ASSERT(argc == 2);
|
||||||
|
|
||||||
|
// mean v0
|
||||||
|
{
|
||||||
|
const auto filename = join(argv[1], "accumulators_serialization_test_mean_v0.xml");
|
||||||
|
accumulators::mean<> a;
|
||||||
|
load_xml(filename, a);
|
||||||
|
BOOST_TEST_EQ(a.count(), 3);
|
||||||
|
BOOST_TEST_EQ(a.value(), 2);
|
||||||
|
BOOST_TEST_EQ(a.variance(), 0.5);
|
||||||
|
}
|
||||||
|
|
||||||
|
// mean
|
||||||
|
{
|
||||||
|
const auto filename = join(argv[1], "accumulators_serialization_test_mean.xml");
|
||||||
|
accumulators::mean<> a;
|
||||||
|
a(1);
|
||||||
|
a(0.5, 2);
|
||||||
|
a(3);
|
||||||
|
print_xml(filename, a);
|
||||||
|
|
||||||
|
accumulators::mean<> b;
|
||||||
|
BOOST_TEST_NOT(a == b);
|
||||||
|
load_xml(filename, b);
|
||||||
|
BOOST_TEST(a == b);
|
||||||
|
}
|
||||||
|
|
||||||
|
// sum
|
||||||
|
{
|
||||||
|
const auto filename = join(argv[1], "accumulators_serialization_test_sum.xml");
|
||||||
|
accumulators::sum<> a;
|
||||||
|
a += 1e100;
|
||||||
|
a += 1;
|
||||||
|
print_xml(filename, a);
|
||||||
|
|
||||||
|
accumulators::sum<> b;
|
||||||
|
BOOST_TEST_NOT(a == b);
|
||||||
|
load_xml(filename, b);
|
||||||
|
BOOST_TEST(a == b);
|
||||||
|
}
|
||||||
|
|
||||||
|
// weighted_mean
|
||||||
|
{
|
||||||
|
const auto filename =
|
||||||
|
join(argv[1], "accumulators_serialization_test_weighted_mean.xml");
|
||||||
|
accumulators::weighted_mean<> a;
|
||||||
|
a(1);
|
||||||
|
a(0.5, 2);
|
||||||
|
a(3);
|
||||||
|
print_xml(filename, a);
|
||||||
|
|
||||||
|
accumulators::weighted_mean<> b;
|
||||||
|
BOOST_TEST_NOT(a == b);
|
||||||
|
load_xml(filename, b);
|
||||||
|
BOOST_TEST(a == b);
|
||||||
|
}
|
||||||
|
|
||||||
|
// weighted_sum
|
||||||
|
{
|
||||||
|
const auto filename =
|
||||||
|
join(argv[1], "accumulators_serialization_test_weighted_sum.xml");
|
||||||
|
accumulators::weighted_sum<> a;
|
||||||
|
a += 1;
|
||||||
|
a += 10;
|
||||||
|
print_xml(filename, a);
|
||||||
|
|
||||||
|
accumulators::weighted_sum<> b;
|
||||||
|
BOOST_TEST_NOT(a == b);
|
||||||
|
load_xml(filename, b);
|
||||||
|
BOOST_TEST(a == b);
|
||||||
|
}
|
||||||
|
|
||||||
|
return boost::report_errors();
|
||||||
|
}
|
17
test/accumulators_serialization_test_mean.xml
Normal file
17
test/accumulators_serialization_test_mean.xml
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<!--
|
||||||
|
Copyright 2019 Hans Dembinski
|
||||||
|
|
||||||
|
Distributed under 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)
|
||||||
|
-->
|
||||||
|
|
||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||||
|
<!DOCTYPE boost_serialization>
|
||||||
|
<boost_serialization signature="serialization::archive" version="17">
|
||||||
|
<item class_id="0" tracking_level="0" version="1">
|
||||||
|
<sum>2.50000000000000000e+00</sum>
|
||||||
|
<mean>2.00000000000000000e+00</mean>
|
||||||
|
<sum_of_deltas_squared>2.00000000000000000e+00</sum_of_deltas_squared>
|
||||||
|
</item>
|
||||||
|
</boost_serialization>
|
17
test/accumulators_serialization_test_mean_v0.xml
Normal file
17
test/accumulators_serialization_test_mean_v0.xml
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<!--
|
||||||
|
Copyright 2019 Hans Dembinski
|
||||||
|
|
||||||
|
Distributed under 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)
|
||||||
|
-->
|
||||||
|
|
||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||||
|
<!DOCTYPE boost_serialization>
|
||||||
|
<boost_serialization signature="serialization::archive" version="17">
|
||||||
|
<item class_id="0" tracking_level="0" version="0">
|
||||||
|
<sum>3</sum>
|
||||||
|
<mean>2.00000000000000000e+00</mean>
|
||||||
|
<sum_of_deltas_squared>1.00000000000000000e+00</sum_of_deltas_squared>
|
||||||
|
</item>
|
||||||
|
</boost_serialization>
|
16
test/accumulators_serialization_test_sum.xml
Normal file
16
test/accumulators_serialization_test_sum.xml
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<!--
|
||||||
|
Copyright 2019 Hans Dembinski
|
||||||
|
|
||||||
|
Distributed under 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)
|
||||||
|
-->
|
||||||
|
|
||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||||
|
<!DOCTYPE boost_serialization>
|
||||||
|
<boost_serialization signature="serialization::archive" version="17">
|
||||||
|
<item class_id="0" tracking_level="0" version="0">
|
||||||
|
<large>1.00000000000000002e+100</large>
|
||||||
|
<small>1.00000000000000000e+00</small>
|
||||||
|
</item>
|
||||||
|
</boost_serialization>
|
18
test/accumulators_serialization_test_weighted_mean.xml
Normal file
18
test/accumulators_serialization_test_weighted_mean.xml
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<!--
|
||||||
|
Copyright 2019 Hans Dembinski
|
||||||
|
|
||||||
|
Distributed under 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)
|
||||||
|
-->
|
||||||
|
|
||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||||
|
<!DOCTYPE boost_serialization>
|
||||||
|
<boost_serialization signature="serialization::archive" version="17">
|
||||||
|
<item class_id="0" tracking_level="0" version="0">
|
||||||
|
<sum_of_weights>2.50000000000000000e+00</sum_of_weights>
|
||||||
|
<sum_of_weights_squared>2.25000000000000000e+00</sum_of_weights_squared>
|
||||||
|
<weighted_mean>2.00000000000000000e+00</weighted_mean>
|
||||||
|
<sum_of_weighted_deltas_squared>2.00000000000000000e+00</sum_of_weighted_deltas_squared>
|
||||||
|
</item>
|
||||||
|
</boost_serialization>
|
16
test/accumulators_serialization_test_weighted_sum.xml
Normal file
16
test/accumulators_serialization_test_weighted_sum.xml
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<!--
|
||||||
|
Copyright 2019 Hans Dembinski
|
||||||
|
|
||||||
|
Distributed under 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)
|
||||||
|
-->
|
||||||
|
|
||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||||
|
<!DOCTYPE boost_serialization>
|
||||||
|
<boost_serialization signature="serialization::archive" version="17">
|
||||||
|
<item class_id="0" tracking_level="0" version="0">
|
||||||
|
<sum_of_weights>1.10000000000000000e+01</sum_of_weights>
|
||||||
|
<sum_of_weights_squared>1.01000000000000000e+02</sum_of_weights_squared>
|
||||||
|
</item>
|
||||||
|
</boost_serialization>
|
@ -11,9 +11,9 @@
|
|||||||
#include <boost/histogram/accumulators/thread_safe.hpp>
|
#include <boost/histogram/accumulators/thread_safe.hpp>
|
||||||
#include <boost/histogram/accumulators/weighted_mean.hpp>
|
#include <boost/histogram/accumulators/weighted_mean.hpp>
|
||||||
#include <boost/histogram/accumulators/weighted_sum.hpp>
|
#include <boost/histogram/accumulators/weighted_sum.hpp>
|
||||||
#include "throw_exception.hpp"
|
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include "is_close.hpp"
|
#include "is_close.hpp"
|
||||||
|
#include "throw_exception.hpp"
|
||||||
|
|
||||||
using namespace boost::histogram;
|
using namespace boost::histogram;
|
||||||
using namespace std::literals;
|
using namespace std::literals;
|
||||||
@ -98,6 +98,15 @@ int main() {
|
|||||||
BOOST_TEST_EQ(c.count(), 8);
|
BOOST_TEST_EQ(c.count(), 8);
|
||||||
BOOST_TEST_EQ(c.value(), 10);
|
BOOST_TEST_EQ(c.value(), 10);
|
||||||
BOOST_TEST_IS_CLOSE(c.variance(), 25.714, 1e-3);
|
BOOST_TEST_IS_CLOSE(c.variance(), 25.714, 1e-3);
|
||||||
|
|
||||||
|
// also same as feeding all samples twice
|
||||||
|
m_t d;
|
||||||
|
d(2, 4);
|
||||||
|
d(2, 7);
|
||||||
|
d(2, 13);
|
||||||
|
d(2, 16);
|
||||||
|
|
||||||
|
BOOST_TEST_EQ(d, c);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
@ -241,7 +241,7 @@ void run_tests() {
|
|||||||
BOOST_TEST_EQ(h, h2);
|
BOOST_TEST_EQ(h, h2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1D with samples
|
// 1D profile with samples
|
||||||
{
|
{
|
||||||
auto h = make_s(Tag(), profile_storage(), in(1, 3));
|
auto h = make_s(Tag(), profile_storage(), in(1, 3));
|
||||||
auto h2 = h;
|
auto h2 = h;
|
||||||
@ -253,9 +253,14 @@ void run_tests() {
|
|||||||
h2.fill(x, sample(x));
|
h2.fill(x, sample(x));
|
||||||
|
|
||||||
BOOST_TEST_EQ(h, h2);
|
BOOST_TEST_EQ(h, h2);
|
||||||
|
|
||||||
|
for (auto&& xi : x) h(xi, sample(xi), weight(xi));
|
||||||
|
h2.fill(x, sample(x), weight(x));
|
||||||
|
|
||||||
|
BOOST_TEST_EQ(h, h2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2D with samples and weights
|
// 2D weighted profile with samples and weights
|
||||||
{
|
{
|
||||||
auto h = make_s(Tag(), weighted_profile_storage(), in(1, 3), in0(1, 3));
|
auto h = make_s(Tag(), weighted_profile_storage(), in(1, 3), in0(1, 3));
|
||||||
auto h2 = h;
|
auto h2 = h;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user