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 #ifndef BOOST_HISTOGRAM_ACCUMULATORS_MEAN_HPP
8 #define BOOST_HISTOGRAM_ACCUMULATORS_MEAN_HPP
10 #include <boost/assert.hpp>
11 #include <boost/core/nvp.hpp>
12 #include <boost/histogram/fwd.hpp> // for mean<>
13 #include <boost/throw_exception.hpp>
15 #include <type_traits>
19 namespace accumulators {
21 /** Calculates mean and variance of sample.
23 Uses Welfords's incremental algorithm to improve the numerical
24 stability of mean and variance computation.
26 template <class RealType>
30 mean(const RealType& n, const RealType& mean, const RealType& variance) noexcept
31 : sum_(n), mean_(mean), sum_of_deltas_squared_(variance * (n - 1)) {}
33 void operator()(const RealType& x) noexcept {
34 sum_ += static_cast<RealType>(1);
35 const auto delta = x - mean_;
36 mean_ += delta / sum_;
37 sum_of_deltas_squared_ += delta * (x - mean_);
40 void operator()(const weight_type<RealType>& w, const RealType& x) noexcept {
42 const auto delta = x - mean_;
43 mean_ += w.value * delta / sum_;
44 sum_of_deltas_squared_ += w.value * delta * (x - mean_);
48 mean& operator+=(const mean<T>& rhs) noexcept {
49 if (sum_ != 0 || rhs.sum_ != 0) {
50 const auto tmp = mean_ * sum_ + static_cast<RealType>(rhs.mean_ * rhs.sum_);
54 sum_of_deltas_squared_ += static_cast<RealType>(rhs.sum_of_deltas_squared_);
58 mean& operator*=(const RealType& s) noexcept {
60 sum_of_deltas_squared_ *= s * s;
65 bool operator==(const mean<T>& rhs) const noexcept {
66 return sum_ == rhs.sum_ && mean_ == rhs.mean_ &&
67 sum_of_deltas_squared_ == rhs.sum_of_deltas_squared_;
71 bool operator!=(const mean<T>& rhs) const noexcept {
72 return !operator==(rhs);
75 const RealType& count() const noexcept { return sum_; }
76 const RealType& value() const noexcept { return mean_; }
77 RealType variance() const noexcept { return sum_of_deltas_squared_ / (sum_ - 1); }
79 template <class Archive>
80 void serialize(Archive& ar, unsigned version) {
84 ar& make_nvp("sum", sum);
85 sum_ = static_cast<RealType>(sum);
87 ar& make_nvp("sum", sum_);
89 ar& make_nvp("mean", mean_);
90 ar& make_nvp("sum_of_deltas_squared", sum_of_deltas_squared_);
94 RealType sum_ = 0, mean_ = 0, sum_of_deltas_squared_ = 0;
97 } // namespace accumulators
98 } // namespace histogram
101 #ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
104 namespace serialization {
109 // version 1 for boost::histogram::accumulators::mean<RealType>
110 template <class RealType>
111 struct version<boost::histogram::accumulators::mean<RealType>>
112 : std::integral_constant<int, 1> {};
114 } // namespace serialization
118 template <class T, class U>
119 /// Specialization for boost::histogram::accumulators::mean.
120 struct common_type<boost::histogram::accumulators::mean<T>,
121 boost::histogram::accumulators::mean<U>> {
122 using type = boost::histogram::accumulators::mean<common_type_t<T, U>>;