Ignore:
Timestamp:
Aug 6, 2012, 3:44:47 PM (11 years ago)
Author:
Peter
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
Added
Removed
  • trunk/yat/statistics/Averager.h

    r2790 r2809  
    2626*/
    2727
     28#include "averager_base.h"
     29
    2830#include "yat/utility/iterator_traits.h"
    2931
     
    4143  /// @see AveragerWeighted AveragerPair AveragerPairWeighted
    4244  ///
    43   class Averager
     45  class Averager : public averager_base2<Averager>
    4446  {
    4547  public:
     
    4951    ///
    5052    Averager(void);
    51    
     53
    5254    ///
    5355    /// Constructor taking sum of \a x, sum of squared x, \a xx, and
     
    6264
    6365    ///
    64     /// Adding \a n (default=1) number of data point(s) with value \a d.
    65     ///
    66     void add(double d, long n=1);
    67 
    68     /**
    69        @brief Coeffient of variation
    70 
    71        Coeffient of variation (cv) is defined as ratio between the
    72        standard deviation and the mean: \f$ \frac{\sigma}{\mu} \f$.
    73        
    74        @return standard deviation divided by mean.
    75     */
    76     double cv(void) const;
    77 
    78     ///
    79     /// @return %Mean of presented data, \f$ \frac{1}{n}\sum x_i \f$
    80     ///
    81     double mean(void) const;
    82 
    83     ///
    84     /// @return Number of data points
    85     ///
    86     long n(void) const;
    87 
    88     ///
    89     /// @brief Rescales the object
    90     ///
    91     /// \f$ \forall x_i \rightarrow a*x_i \f$,
    92     ///
    93     void rescale(double a);
    94 
    95     ///
    96     /// @return Standard error, i.e. standard deviation of the mean
    97     /// \f$ \sqrt{variance()/n} \f$
    98     ///
    99     double standard_error(void) const;
    100 
    101     ///
    102     /// @brief The standard deviation is defined as the square root of
    103     /// the variance.
    104     ///
    105     /// @return The standard deviation, root of the variance().
    106     ///
    107     double std(void) const;
    108 
    109     ///
    110     /// @brief The standard deviation is defined as the square root of
    111     /// the variance.
    112     ///
    113     /// @return Standard deviation around \a m, root of the variance(m).
    114     ///
    115     double std(double m) const;
    116 
    117     ///
    118     /// @return The sum of x
    119     ///
    120     double sum_x(void)  const;
    121 
    122     ///
    123     /// @return The sum of squares
    124     ///
    125     double sum_xx(void) const;
    126 
    127     ///
    128     /// @return \f$ \sum_i (x_i-m)^2 \f$
    129     ///
    130     double sum_xx_centered(void)  const;
    131 
    132     ///
    133     /// @brief The variance with know mean
    134     ///
    135     /// The variance is calculated as
    136     /// \f$ \frac{1}{n}\sum (x_i-m)^2 \f$.
    137     ///
    138     /// @return Variance when the mean is known to be \a m.
    139     ///
    140     double variance(double m) const;
    141 
    142     /**
    143        \brief The estimated variance
    144        
    145        The variance is calculated as \f$ \frac{1}{N}\sum_i
    146        (x_i-m)^2 \f$, where \f$ m \f$ is the mean.
    147        
    148        \return Estimation of variance
    149     */
    150     double variance(void) const;
    151 
    152     ///
    153     /// The variance is calculated using the \f$ (n-1) \f$ correction,
    154     /// which means it is the best unbiased estimator of the variance
    155     /// \f$ \frac{1}{N-1}\sum_i (x_i-m)^2 \f$, where \f$ m \f$ is the
    156     /// mean.
    157     ///
    158     /// @return unbiased estimation of variance
    159     ///
    160     double variance_unbiased(void) const;
    161 
    162     ///
    163     /// @brief Reset everything to zero
    164     ///
    165     void reset(void);
    166 
    167     ///
    16866    /// @brief The assignment operator
    16967    ///
    17068    const Averager& operator=(const Averager&);
    17169
    172     ///
    173     /// Operator to add another Averager
    174     ///
    175     const Averager& operator+=(const Averager&);
     70    /**
     71       \brief plus assignment operator
     72
     73       Add another Averager
     74     */
     75    template<class Derived>
     76    const Averager& operator+=(const averager_base2<Derived>& other);
    17677
    17778  private:
    178     long  n_;
    179     double  mean_, m2_;
     79    friend class averager_base<Averager>;
     80    void add_impl(double, long int);
     81    void rescale_impl(double);
    18082  };
    181  
     83
    18284
    18385  /**
     
    19597  }
    19698
     99  // template implementation
     100  template<class Derived>
     101  const Averager& Averager::operator+=(const averager_base2<Derived>& other)
     102  {
     103    if (other.n())
     104      add2(other.mean(), other.sum_xx_centered(), other.n());
     105    return *this;
     106  }
     107
     108
    197109}}} // of namespace statistics, yat, and theplu
    198110
Note: See TracChangeset for help on using the changeset viewer.