# source:trunk/lib/statistics/Averager.h@303

Last change on this file since 303 was 303, checked in by Peter, 17 years ago

docs

• Property svn:eol-style set to native
• Property svn:keywords set to Author Date Id Revision
File size: 2.8 KB
Line
1// $Id: Averager.h 303 2005-04-30 16:17:35Z peter$
2
3#ifndef _theplu_statistics_averager_
4#define _theplu_statistics_averager_
5
6#include <cmath>
7
8namespace theplu{
9namespace gslapi{
10  class vector;
11}
12
13namespace statistics{
14  class ostream;
15
16  ///
17  /// \defgroup Averagers
18  /// Class to calulate simple (first and second moments) averages.
19  ///
20  /// @see WeightedAverager
21  ///
22  class Averager
23  {
24  public:
25
26    ///
27    /// Default constructor
28    ///
29    Averager(void);
30
31    ///
32    /// Constructor taking sum of \a x, sum of squared x, \a xx, and
33    /// number of samples \a n.
34    ///
35    Averager(const double x, const double xx, const long n);
36
37    ///
38    /// Copy constructor
39    ///
40    Averager(const Averager&);
41
42    ///
43    /// Adding \a n (default=1) number of data point(s) with value \a d.
44    ///
45    inline void add(const double d,const long n=1)
46    {n_+=n; x_+=n*d; xx_+=n*d*d;}
47
48    ///
49    /// @return Mean of presented data, \f$\frac{1}{n}\sum x_i \f$
50    ///
51    inline double mean(void) const { return n_ ? x_/n_ : 0; }
52
53    ///
54    /// @return Mean of squared values \f$\frac{1}{n}\sum x_i^2 \f$.
55    ///
56    inline double mean_sqr(void) const { return n_ ? xx_/n_ : 0; }
57
58    ///
59    /// @return Number of data points
60    ///
61    inline long n(void) const { return n_; }
62
63    ///
64    /// Rescales the object, \f$\forall x_i \rightarrow a*x_i\f$, \f$65 /// \forall x_i^2 \rightarrow a^2*x_i^2 \f$
66    ///
67    inline void rescale(double a) { x_*=a; xx_*=a*a; }
68
69    ///
70    /// Resets everything to zero
71    ///
72    inline void reset(void) { n_=0; x_=xx_=0.0;}
73
74    ///
75    /// The standard deviation is defined as the square root of the
76    /// variance.
77    ///
78    /// @return The standard deviation, root of the variance().
79    ///
80    inline double std(void) const { return sqrt(variance()); }
81
82    ///
83    /// @return Standard error, i.e. standard deviation of the mean
84    /// \f$\sqrt{variance()/n} \f$
85    ///
86    inline double standard_error(void) const { return sqrt(variance()/n_); }
87
88    ///
89    /// @return The sum of x
90    ///
91    inline double sum_x(void) const { return x_; }
92
93    ///
94    /// @return The sum of squares
95    ///
96    inline double sum_xsqr(void) const { return xx_; }
97
98    ///
99    /// @return \f$\sum_i (x_i-m)^2\f$
100    ///
101    inline double sum_xsqr_centered(void) const { return xx_-x_*x_/n_; }
102
103    ///
104    /// The variance is calculated using the \f$(n-1) \f$ correction,
105    /// which means it is the best unbiased estimator of the variance
106    /// \f$\frac{1}{N-1}\sum_i (x_i-m)^2\f$, where \f$m\f$ is the
107    /// mean.
108    ///
109    /// @return The variance
110    ///
111    inline double variance(void) const
112    { return (n_>1) ? sum_xsqr_centered()/(n_-1) : 0; }
113
114    ///
115    /// The assignment operator
116    ///
117    inline const Averager& operator=(const Averager& a)
118      { n_=a.n_; x_=a.x_; xx_=a.xx_; return *this; }
119
120    ///
121    /// Operator to add another Averager
122    ///
123    const Averager& operator+=(const Averager&);
124
125  private:
126    long  n_;
127    double  x_, xx_;
128  };
129
130}} // of namespace statistics and namespace theplu
131
132#endif
Note: See TracBrowser for help on using the repository browser.