Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.

Class template weighted_mean

boost::histogram::accumulators::weighted_mean — Calculates mean and variance of weighted sample.


// In header: <boost/histogram/accumulators/weighted_mean.hpp>

template<typename ValueType> 
class weighted_mean {
  // types
  typedef ValueType          value_type;     
  typedef const value_type & const_reference;

  // construct/copy/destruct
  weighted_mean() = default;
  template<typename T> weighted_mean(const weighted_mean< T > &);
  weighted_mean(const_reference, const_reference, const_reference, 

  // public member functions
  void operator()(const_reference);
  void operator()(const weight_type< value_type > &, const_reference);
  weighted_mean & operator+=(const weighted_mean &);
  weighted_mean & operator *=(const_reference) noexcept;
  bool operator==(const weighted_mean &) const noexcept;
  bool operator!=(const weighted_mean &) const noexcept;
  const_reference sum_of_weights() const noexcept;
  const_reference sum_of_weights_squared() const noexcept;
  value_type count() const noexcept;
  const_reference value() const noexcept;
  value_type variance() const;
  template<typename Archive> void serialize(Archive &, unsigned);


Uses West's incremental algorithm to improve numerical stability of mean and variance computation.

weighted_mean public construct/copy/destruct

  1. weighted_mean() = default;
  2. template<typename T> weighted_mean(const weighted_mean< T > & o);
    Allow implicit conversion from other weighted_means.
  3. weighted_mean(const_reference wsum, const_reference wsum2, 
                  const_reference mean, const_reference variance);
    Initialize to external sum of weights, sum of weights squared, mean, and variance.

weighted_mean public member functions

  1. void operator()(const_reference x);
    Insert sample x.
  2. void operator()(const weight_type< value_type > & w, const_reference x);
    Insert sample x with weight w.
  3. weighted_mean & operator+=(const weighted_mean & rhs);
    Add another weighted_mean.
  4. weighted_mean & operator *=(const_reference s) noexcept;
    Scale by value.

    This acts as if all samples were scaled by the value.

  5. bool operator==(const weighted_mean & rhs) const noexcept;
  6. bool operator!=(const weighted_mean & rhs) const noexcept;
  7. const_reference sum_of_weights() const noexcept;
    Return sum of weights.
  8. const_reference sum_of_weights_squared() const noexcept;
    Return sum of weights squared (variance of weight distribution).
  9. value_type count() const noexcept;
    Return effective counts.

    This corresponds to the equivalent number of unweighted samples that would have the same variance as this sample. count() should be used to check whether value() and variance() are defined, see documentation of value() and variance(). count() can be used to compute the variance of the mean by dividing variance() by count().

  10. const_reference value() const noexcept;
    Return mean value of accumulated weighted samples.

    The result is undefined, if count() == 0.

  11. value_type variance() const;
    Return variance of accumulated weighted samples.

    The result is undefined, if count() == 0 or count() == 1.

  12. template<typename Archive> void serialize(Archive & ar, unsigned);