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/core/nvp.hpp>
11 #include <boost/histogram/fwd.hpp> // for mean<>
12 #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 ValueType>
29 using value_type = ValueType;
30 using const_reference = const value_type&;
34 /// Allow implicit conversion from mean<T>
36 mean(const mean<T>& o) noexcept
37 : sum_{o.sum_}, mean_{o.mean_}, sum_of_deltas_squared_{o.sum_of_deltas_squared_} {}
39 /// Initialize to external count, mean, and variance
40 mean(const_reference n, const_reference mean, const_reference variance) noexcept
41 : sum_(n), mean_(mean), sum_of_deltas_squared_(variance * (n - 1)) {}
44 void operator()(const_reference x) noexcept {
45 sum_ += static_cast<value_type>(1);
46 const auto delta = x - mean_;
47 mean_ += delta / sum_;
48 sum_of_deltas_squared_ += delta * (x - mean_);
51 /// Insert sample x with weight w
52 void operator()(const weight_type<value_type>& w, const_reference x) noexcept {
54 const auto delta = x - mean_;
55 mean_ += w.value * delta / sum_;
56 sum_of_deltas_squared_ += w.value * delta * (x - mean_);
59 /// Add another mean accumulator
60 mean& operator+=(const mean& rhs) noexcept {
61 if (sum_ != 0 || rhs.sum_ != 0) {
62 const auto tmp = mean_ * sum_ + rhs.mean_ * rhs.sum_;
66 sum_of_deltas_squared_ += rhs.sum_of_deltas_squared_;
72 This acts as if all samples were scaled by the value.
74 mean& operator*=(const_reference s) noexcept {
76 sum_of_deltas_squared_ *= s * s;
80 bool operator==(const mean& rhs) const noexcept {
81 return sum_ == rhs.sum_ && mean_ == rhs.mean_ &&
82 sum_of_deltas_squared_ == rhs.sum_of_deltas_squared_;
85 bool operator!=(const mean& rhs) const noexcept { return !operator==(rhs); }
87 /// Return how many samples were accumulated
88 const_reference count() const noexcept { return sum_; }
90 /** Return mean value of accumulated samples.
92 The result is undefined, if `count() < 1`.
94 const_reference value() const noexcept { return mean_; }
96 /** Return variance of accumulated samples
98 The result is undefined, if `count() < 2`.
100 value_type variance() const noexcept { return sum_of_deltas_squared_ / (sum_ - 1); }
102 template <class Archive>
103 void serialize(Archive& ar, unsigned version) {
107 ar& make_nvp("sum", sum);
108 sum_ = static_cast<value_type>(sum);
110 ar& make_nvp("sum", sum_);
112 ar& make_nvp("mean", mean_);
113 ar& make_nvp("sum_of_deltas_squared", sum_of_deltas_squared_);
119 value_type sum_of_deltas_squared_{};
122 } // namespace accumulators
123 } // namespace histogram
126 #ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
129 namespace serialization {
134 // version 1 for boost::histogram::accumulators::mean<T>
136 struct version<boost::histogram::accumulators::mean<T>> : std::integral_constant<int, 1> {
139 } // namespace serialization
143 template <class T, class U>
144 /// Specialization for boost::histogram::accumulators::mean.
145 struct common_type<boost::histogram::accumulators::mean<T>,
146 boost::histogram::accumulators::mean<U>> {
147 using type = boost::histogram::accumulators::mean<common_type_t<T, U>>;