| Home | Libraries | People | FAQ | More

## Class template weighted_mean

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

## Synopsis

    // 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;

      // public member functions
      weighted_mean() = default;
      template<typename T> weighted_mean(const weighted_mean< T > &);
      weighted_mean(const_reference, const_reference, const_reference,
                    const_reference);
      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);
    };

## Description

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

### `weighted_mean` public member functions

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.

4.     void operator()(const_reference x);

   Insert sample x.

5.     void operator()(const weight_type< value_type > & w, const_reference x);

   Insert sample x with weight w.

6.     weighted_mean & operator+=(const weighted_mean & rhs);

   Add another weighted_mean.

7.     weighted_mean & operator*=(const_reference s) noexcept;

   Scale by value.

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

8.     bool operator==(const weighted_mean & rhs) const noexcept;

9.     bool operator!=(const weighted_mean & rhs) const noexcept;

10.     const_reference sum_of_weights() const noexcept;

    Return sum of weights.

11.     const_reference sum_of_weights_squared() const noexcept;

    Return sum of weights squared (variance of weight distribution).

12.     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 whethervalue() andvariance() are defined, see documentation ofvalue() andvariance().count() can be used to compute the variance of the mean by dividingvariance() bycount().

13.     const_reference value() const noexcept;

    Return mean value of accumulated weighted samples.

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

14.     value_type variance() const;

    Return variance of accumulated weighted samples.

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

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