...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
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 { public: // 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, 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); 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; 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/destructweighted_mean() = default;
template<typename T> weighted_mean(const weighted_mean< T > & o);Allow implicit conversion from other weighted_means.
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 functionsvoid operator()(const_reference x);Insert sample x.
void operator()(const weight_type< value_type > & w, const_reference x);Insert sample x with weight w.
weighted_mean & operator+=(const weighted_mean & rhs);Add another
weighted_mean
.weighted_mean & operator *=(const_reference s);Scale by value.
This acts as if all samples were scaled by the value.
bool operator==(const weighted_mean & rhs) const noexcept;
bool operator!=(const weighted_mean & rhs) const noexcept;
const_reference sum_of_weights() const noexcept;Return sum of weights.
const_reference sum_of_weights_squared() const noexcept;Return sum of weights squared (variance of weight distribution)
const_reference value() const noexcept;Return mean value of accumulated weighted samples.
The result is undefined, if sum_of_weights() == 0
.
value_type variance() const;Return variance of accumulated weighted samples.
The result is undefined, if sum_of_weights() == 0
orsum_of_weights() == sum_of_weights_squared()
.
template<typename Archive> void serialize(Archive & ar, unsigned);