# Changeset 2809 for trunk/yat/statistics/Averager.h

Ignore:
Timestamp:
Aug 6, 2012, 3:44:47 PM (11 years ago)
Message:

closes #705. Create a common interface class for average classes (averager_base) that is extended by additional base classes, averager_base2, averager_base3, and averager_base4. Averager is an implementation of averager_base2, and Averager4 is an implementation of averager_base4. For completeness added also classes Averager1 and Averager3 that implement averager_base and averager_base3, respectively. Using curiously recurrent template pattern to avoid vtables which has the downside that runtime polymorphism is not allowed (but also not really wanted here).

File:
1 edited

### Legend:

Unmodified
 r2790 */ #include "averager_base.h" #include "yat/utility/iterator_traits.h" /// @see AveragerWeighted AveragerPair AveragerPairWeighted /// class Averager class Averager : public averager_base2 { public: /// Averager(void); /// /// Constructor taking sum of \a x, sum of squared x, \a xx, and /// /// Adding \a n (default=1) number of data point(s) with value \a d. /// void add(double d, long n=1); /** @brief Coeffient of variation Coeffient of variation (cv) is defined as ratio between the standard deviation and the mean: \f$\frac{\sigma}{\mu} \f$. @return standard deviation divided by mean. */ double cv(void) const; /// /// @return %Mean of presented data, \f$\frac{1}{n}\sum x_i \f$ /// double mean(void) const; /// /// @return Number of data points /// long n(void) const; /// /// @brief Rescales the object /// /// \f$\forall x_i \rightarrow a*x_i \f$, /// void rescale(double a); /// /// @return Standard error, i.e. standard deviation of the mean /// \f$\sqrt{variance()/n} \f$ /// double standard_error(void) const; /// /// @brief The standard deviation is defined as the square root of /// the variance. /// /// @return The standard deviation, root of the variance(). /// double std(void) const; /// /// @brief The standard deviation is defined as the square root of /// the variance. /// /// @return Standard deviation around \a m, root of the variance(m). /// double std(double m) const; /// /// @return The sum of x /// double sum_x(void)  const; /// /// @return The sum of squares /// double sum_xx(void) const; /// /// @return \f$\sum_i (x_i-m)^2 \f$ /// double sum_xx_centered(void)  const; /// /// @brief The variance with know mean /// /// The variance is calculated as /// \f$\frac{1}{n}\sum (x_i-m)^2 \f$. /// /// @return Variance when the mean is known to be \a m. /// double variance(double m) const; /** \brief The estimated variance The variance is calculated as \f$\frac{1}{N}\sum_i (x_i-m)^2 \f$, where \f$m \f$ is the mean. \return Estimation of variance */ double variance(void) const; /// /// The variance is calculated using the \f$(n-1) \f$ correction, /// which means it is the best unbiased estimator of the variance /// \f$\frac{1}{N-1}\sum_i (x_i-m)^2 \f$, where \f$m \f$ is the /// mean. /// /// @return unbiased estimation of variance /// double variance_unbiased(void) const; /// /// @brief Reset everything to zero /// void reset(void); /// /// @brief The assignment operator /// const Averager& operator=(const Averager&); /// /// Operator to add another Averager /// const Averager& operator+=(const Averager&); /** \brief plus assignment operator Add another Averager */ template const Averager& operator+=(const averager_base2& other); private: long  n_; double  mean_, m2_; friend class averager_base; void add_impl(double, long int); void rescale_impl(double); }; /** } // template implementation template const Averager& Averager::operator+=(const averager_base2& other) { if (other.n()) add2(other.mean(), other.sum_xx_centered(), other.n()); return *this; } }}} // of namespace statistics, yat, and theplu