weighted_mean.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 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. #ifndef BOOST_HISTOGRAM_ACCUMULATORS_WEIGHTED_MEAN_HPP
  7. #define BOOST_HISTOGRAM_ACCUMULATORS_WEIGHTED_MEAN_HPP
  8. #include <boost/core/nvp.hpp>
  9. #include <boost/histogram/detail/square.hpp>
  10. #include <boost/histogram/fwd.hpp> // for weighted_mean<>
  11. #include <boost/histogram/weight.hpp>
  12. #include <cassert>
  13. #include <type_traits>
  14. namespace boost {
  15. namespace histogram {
  16. namespace accumulators {
  17. /**
  18. Calculates mean and variance of weighted sample.
  19. Uses West's incremental algorithm to improve numerical stability
  20. of mean and variance computation.
  21. */
  22. template <class ValueType>
  23. class weighted_mean {
  24. public:
  25. using value_type = ValueType;
  26. using const_reference = const value_type&;
  27. weighted_mean() = default;
  28. /// Allow implicit conversion from other weighted_means.
  29. template <class T>
  30. weighted_mean(const weighted_mean<T>& o)
  31. : sum_of_weights_{o.sum_of_weights_}
  32. , sum_of_weights_squared_{o.sum_of_weights_squared_}
  33. , weighted_mean_{o.weighted_mean_}
  34. , sum_of_weighted_deltas_squared_{o.sum_of_weighted_deltas_squared_} {}
  35. /// Initialize to external sum of weights, sum of weights squared, mean, and variance.
  36. weighted_mean(const_reference wsum, const_reference wsum2, const_reference mean,
  37. const_reference variance)
  38. : sum_of_weights_(wsum)
  39. , sum_of_weights_squared_(wsum2)
  40. , weighted_mean_(mean)
  41. , sum_of_weighted_deltas_squared_(
  42. variance * (sum_of_weights_ - sum_of_weights_squared_ / sum_of_weights_)) {}
  43. /// Insert sample x.
  44. void operator()(const_reference x) { operator()(weight(1), x); }
  45. /// Insert sample x with weight w.
  46. void operator()(const weight_type<value_type>& w, const_reference x) {
  47. sum_of_weights_ += w.value;
  48. sum_of_weights_squared_ += w.value * w.value;
  49. const auto delta = x - weighted_mean_;
  50. weighted_mean_ += w.value * delta / sum_of_weights_;
  51. sum_of_weighted_deltas_squared_ += w.value * delta * (x - weighted_mean_);
  52. }
  53. /// Add another weighted_mean.
  54. weighted_mean& operator+=(const weighted_mean& rhs) {
  55. if (rhs.sum_of_weights_ == 0) return *this;
  56. // see mean.hpp for derivation of correct formula
  57. const auto n1 = sum_of_weights_;
  58. const auto mu1 = weighted_mean_;
  59. const auto n2 = rhs.sum_of_weights_;
  60. const auto mu2 = rhs.weighted_mean_;
  61. sum_of_weights_ += rhs.sum_of_weights_;
  62. sum_of_weights_squared_ += rhs.sum_of_weights_squared_;
  63. weighted_mean_ = (n1 * mu1 + n2 * mu2) / sum_of_weights_;
  64. sum_of_weighted_deltas_squared_ += rhs.sum_of_weighted_deltas_squared_;
  65. sum_of_weighted_deltas_squared_ += n1 * detail::square(weighted_mean_ - mu1);
  66. sum_of_weighted_deltas_squared_ += n2 * detail::square(weighted_mean_ - mu2);
  67. return *this;
  68. }
  69. /** Scale by value.
  70. This acts as if all samples were scaled by the value.
  71. */
  72. weighted_mean& operator*=(const_reference s) noexcept {
  73. weighted_mean_ *= s;
  74. sum_of_weighted_deltas_squared_ *= s * s;
  75. return *this;
  76. }
  77. bool operator==(const weighted_mean& rhs) const noexcept {
  78. return sum_of_weights_ == rhs.sum_of_weights_ &&
  79. sum_of_weights_squared_ == rhs.sum_of_weights_squared_ &&
  80. weighted_mean_ == rhs.weighted_mean_ &&
  81. sum_of_weighted_deltas_squared_ == rhs.sum_of_weighted_deltas_squared_;
  82. }
  83. bool operator!=(const weighted_mean& rhs) const noexcept { return !operator==(rhs); }
  84. /// Return sum of weights.
  85. const_reference sum_of_weights() const noexcept { return sum_of_weights_; }
  86. /// Return sum of weights squared (variance of weight distribution).
  87. const_reference sum_of_weights_squared() const noexcept {
  88. return sum_of_weights_squared_;
  89. }
  90. /** Return mean value of accumulated weighted samples.
  91. The result is undefined, if `sum_of_weights() == 0`.
  92. */
  93. const_reference value() const noexcept { return weighted_mean_; }
  94. /** Return variance of accumulated weighted samples.
  95. The result is undefined, if `sum_of_weights() == 0` or
  96. `sum_of_weights() == sum_of_weights_squared()`.
  97. */
  98. value_type variance() const {
  99. // see https://en.wikipedia.org/wiki/Weighted_arithmetic_mean#Reliability_weights
  100. return sum_of_weighted_deltas_squared_ /
  101. (sum_of_weights_ - sum_of_weights_squared_ / sum_of_weights_);
  102. }
  103. template <class Archive>
  104. void serialize(Archive& ar, unsigned /* version */) {
  105. ar& make_nvp("sum_of_weights", sum_of_weights_);
  106. ar& make_nvp("sum_of_weights_squared", sum_of_weights_squared_);
  107. ar& make_nvp("weighted_mean", weighted_mean_);
  108. ar& make_nvp("sum_of_weighted_deltas_squared", sum_of_weighted_deltas_squared_);
  109. }
  110. private:
  111. value_type sum_of_weights_{};
  112. value_type sum_of_weights_squared_{};
  113. value_type weighted_mean_{};
  114. value_type sum_of_weighted_deltas_squared_{};
  115. };
  116. } // namespace accumulators
  117. } // namespace histogram
  118. } // namespace boost
  119. #ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
  120. namespace std {
  121. template <class T, class U>
  122. /// Specialization for boost::histogram::accumulators::weighted_mean.
  123. struct common_type<boost::histogram::accumulators::weighted_mean<T>,
  124. boost::histogram::accumulators::weighted_mean<U>> {
  125. using type = boost::histogram::accumulators::weighted_mean<common_type_t<T, U>>;
  126. };
  127. } // namespace std
  128. #endif
  129. #endif