weighted_mean.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 <typename RealType>
  22. class weighted_mean {
  23. public:
  24. weighted_mean() = default;
  25. weighted_mean(const RealType& wsum, const RealType& wsum2, const RealType& mean,
  26. const RealType& variance)
  27. : sum_of_weights_(wsum)
  28. , sum_of_weights_squared_(wsum2)
  29. , weighted_mean_(mean)
  30. , sum_of_weighted_deltas_squared_(
  31. variance * (sum_of_weights_ - sum_of_weights_squared_ / sum_of_weights_)) {}
  32. void operator()(const RealType& x) { operator()(weight(1), x); }
  33. void operator()(const weight_type<RealType>& w, const RealType& x) {
  34. sum_of_weights_ += w.value;
  35. sum_of_weights_squared_ += w.value * w.value;
  36. const auto delta = x - weighted_mean_;
  37. weighted_mean_ += w.value * delta / sum_of_weights_;
  38. sum_of_weighted_deltas_squared_ += w.value * delta * (x - weighted_mean_);
  39. }
  40. template <typename T>
  41. weighted_mean& operator+=(const weighted_mean<T>& rhs) {
  42. if (sum_of_weights_ != 0 || rhs.sum_of_weights_ != 0) {
  43. const auto tmp = weighted_mean_ * sum_of_weights_ +
  44. static_cast<RealType>(rhs.weighted_mean_ * rhs.sum_of_weights_);
  45. sum_of_weights_ += static_cast<RealType>(rhs.sum_of_weights_);
  46. sum_of_weights_squared_ += static_cast<RealType>(rhs.sum_of_weights_squared_);
  47. weighted_mean_ = tmp / sum_of_weights_;
  48. }
  49. sum_of_weighted_deltas_squared_ +=
  50. static_cast<RealType>(rhs.sum_of_weighted_deltas_squared_);
  51. return *this;
  52. }
  53. weighted_mean& operator*=(const RealType& s) {
  54. weighted_mean_ *= s;
  55. sum_of_weighted_deltas_squared_ *= s * s;
  56. return *this;
  57. }
  58. template <typename T>
  59. bool operator==(const weighted_mean<T>& rhs) const noexcept {
  60. return sum_of_weights_ == rhs.sum_of_weights_ &&
  61. sum_of_weights_squared_ == rhs.sum_of_weights_squared_ &&
  62. weighted_mean_ == rhs.weighted_mean_ &&
  63. sum_of_weighted_deltas_squared_ == rhs.sum_of_weighted_deltas_squared_;
  64. }
  65. template <typename T>
  66. bool operator!=(const T& rhs) const noexcept {
  67. return !operator==(rhs);
  68. }
  69. const RealType& sum_of_weights() const noexcept { return sum_of_weights_; }
  70. const RealType& sum_of_weights_squared() const noexcept {
  71. return sum_of_weights_squared_;
  72. }
  73. const RealType& value() const noexcept { return weighted_mean_; }
  74. RealType variance() const {
  75. return sum_of_weighted_deltas_squared_ /
  76. (sum_of_weights_ - sum_of_weights_squared_ / sum_of_weights_);
  77. }
  78. template <class Archive>
  79. void serialize(Archive& ar, unsigned /* version */) {
  80. ar& make_nvp("sum_of_weights", sum_of_weights_);
  81. ar& make_nvp("sum_of_weights_squared", sum_of_weights_squared_);
  82. ar& make_nvp("weighted_mean", weighted_mean_);
  83. ar& make_nvp("sum_of_weighted_deltas_squared", sum_of_weighted_deltas_squared_);
  84. }
  85. private:
  86. RealType sum_of_weights_ = RealType(), sum_of_weights_squared_ = RealType(),
  87. weighted_mean_ = RealType(), sum_of_weighted_deltas_squared_ = RealType();
  88. };
  89. } // namespace accumulators
  90. } // namespace histogram
  91. } // namespace boost
  92. #ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
  93. namespace std {
  94. template <class T, class U>
  95. /// Specialization for boost::histogram::accumulators::weighted_mean.
  96. struct common_type<boost::histogram::accumulators::weighted_mean<T>,
  97. boost::histogram::accumulators::weighted_mean<U>> {
  98. using type = boost::histogram::accumulators::weighted_mean<common_type_t<T, U>>;
  99. };
  100. } // namespace std
  101. #endif
  102. #endif