weighted_mean.hpp 4.8 KB

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