weighted_sum.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2015-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_SUM_HPP
  7. #define BOOST_HISTOGRAM_ACCUMULATORS_WEIGHTED_SUM_HPP
  8. #include <boost/core/nvp.hpp>
  9. #include <boost/histogram/detail/square.hpp>
  10. #include <boost/histogram/fwd.hpp> // for weighted_sum<>
  11. #include <type_traits>
  12. namespace boost {
  13. namespace histogram {
  14. namespace accumulators {
  15. /// Holds sum of weights and its variance estimate
  16. template <class ValueType>
  17. class weighted_sum {
  18. public:
  19. using value_type = ValueType;
  20. using const_reference = const value_type&;
  21. weighted_sum() = default;
  22. /// Initialize sum to value and allow implicit conversion
  23. weighted_sum(const_reference value) noexcept : weighted_sum(value, value) {}
  24. /// Allow implicit conversion from sum<T>
  25. template <class T>
  26. weighted_sum(const weighted_sum<T>& s) noexcept
  27. : weighted_sum(s.value(), s.variance()) {}
  28. /// Initialize sum to value and variance
  29. weighted_sum(const_reference value, const_reference variance) noexcept
  30. : sum_of_weights_(value), sum_of_weights_squared_(variance) {}
  31. /// Increment by one.
  32. weighted_sum& operator++() {
  33. ++sum_of_weights_;
  34. ++sum_of_weights_squared_;
  35. return *this;
  36. }
  37. /// Increment by weight.
  38. weighted_sum& operator+=(const weight_type<value_type>& w) {
  39. sum_of_weights_ += w.value;
  40. sum_of_weights_squared_ += detail::square(w.value);
  41. return *this;
  42. }
  43. /// Added another weighted sum.
  44. weighted_sum& operator+=(const weighted_sum& rhs) {
  45. sum_of_weights_ += rhs.sum_of_weights_;
  46. sum_of_weights_squared_ += rhs.sum_of_weights_squared_;
  47. return *this;
  48. }
  49. /// Divide by another weighted sum.
  50. weighted_sum& operator/=(const weighted_sum& rhs) {
  51. const auto v = sum_of_weights_;
  52. const auto w = sum_of_weights_squared_;
  53. const auto rv = rhs.sum_of_weights_;
  54. const auto rw = rhs.sum_of_weights_squared_;
  55. sum_of_weights_ /= rv;
  56. // error propagation for independent a, b:
  57. // c = a / b: var(c) = (var(a)/a^2 + var(b)/b^2) c^2
  58. using detail::square;
  59. sum_of_weights_squared_ = (w / square(v) + rw / square(rv)) * square(sum_of_weights_);
  60. return *this;
  61. }
  62. /// Scale by value.
  63. weighted_sum& operator*=(const_reference x) {
  64. sum_of_weights_ *= x;
  65. sum_of_weights_squared_ *= x * x;
  66. return *this;
  67. }
  68. bool operator==(const weighted_sum& rhs) const noexcept {
  69. return sum_of_weights_ == rhs.sum_of_weights_ &&
  70. sum_of_weights_squared_ == rhs.sum_of_weights_squared_;
  71. }
  72. bool operator!=(const weighted_sum& rhs) const noexcept { return !operator==(rhs); }
  73. /// Return value of the sum.
  74. const_reference value() const noexcept { return sum_of_weights_; }
  75. /// Return estimated variance of the sum.
  76. const_reference variance() const noexcept { return sum_of_weights_squared_; }
  77. // lossy conversion must be explicit
  78. explicit operator const_reference() const { return sum_of_weights_; }
  79. template <class Archive>
  80. void serialize(Archive& ar, unsigned /* version */) {
  81. ar& make_nvp("sum_of_weights", sum_of_weights_);
  82. ar& make_nvp("sum_of_weights_squared", sum_of_weights_squared_);
  83. }
  84. private:
  85. value_type sum_of_weights_{};
  86. value_type sum_of_weights_squared_{};
  87. };
  88. } // namespace accumulators
  89. } // namespace histogram
  90. } // namespace boost
  91. #ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
  92. namespace std {
  93. template <class T, class U>
  94. struct common_type<boost::histogram::accumulators::weighted_sum<T>,
  95. boost::histogram::accumulators::weighted_sum<U>> {
  96. using type = boost::histogram::accumulators::weighted_sum<common_type_t<T, U>>;
  97. };
  98. template <class T, class U>
  99. struct common_type<boost::histogram::accumulators::weighted_sum<T>, U> {
  100. using type = boost::histogram::accumulators::weighted_sum<common_type_t<T, U>>;
  101. };
  102. template <class T, class U>
  103. struct common_type<T, boost::histogram::accumulators::weighted_sum<U>> {
  104. using type = boost::histogram::accumulators::weighted_sum<common_type_t<T, U>>;
  105. };
  106. } // namespace std
  107. #endif
  108. #endif