source: trunk/c++_tools/statistics/Averager.h @ 620

Last change on this file since 620 was 620, checked in by Markus Ringnér, 16 years ago

Fixed problems with Averager that sometimes appeared as failures in averager_test when optimization is turned on

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.8 KB
Line 
1#ifndef _theplu_statistics_averager_
2#define _theplu_statistics_averager_
3
4// $Id: Averager.h 620 2006-09-04 15:16:57Z markus $
5
6#include <cmath>
7#include <sys/types.h>
8
9namespace theplu{
10
11namespace statistics{
12  class ostream;
13
14  ///
15  /// Class to calculate simple (first and second moments) averages.
16  ///
17  /// @see AveragerWeighted AveragerPair AveragerPairWeighted
18  ///
19  class Averager
20  {
21  public:
22
23    ///
24    /// Default constructor
25    ///
26    Averager(void);
27   
28    ///
29    /// Constructor taking sum of \a x, sum of squared x, \a xx, and
30    /// number of samples \a n.
31    ///
32    Averager(double x, double xx, u_long n);
33
34    ///
35    /// Copy constructor
36    ///
37    Averager(const Averager& a);
38
39    ///
40    /// Adding \a n (default=1) number of data point(s) with value \a d.
41    ///
42    inline void add(double d, u_long n=1) { n_+=n; x_+=n*d; xx_+=n*d*d;}
43
44    ///
45    /// Adding each value in an array \a v \a n (default=1)
46    /// number of times. The requirements for the type T of the
47    /// array \a v are: operator[] returning an element and function
48    /// size() returning the number of elements.
49    ///   
50    ///
51    template <typename T>
52    void  add_values(const T& v, u_long n=1); 
53
54    ///
55    /// @return Mean of presented data, \f$ \frac{1}{n}\sum x_i \f$
56    ///
57    inline double mean(void) const { return n_ ? x_/n_ : 0; }
58 
59    ///
60    /// @return Number of data points
61    ///
62    inline u_long n(void) const { return n_; }
63
64    ///
65    /// Rescales the object, \f$ \forall x_i \rightarrow a*x_i \f$,
66    /// \f$ \forall x_i^2 \rightarrow a^2*x_i^2 \f$
67    ///
68    inline void rescale(double a) { x_*=a; xx_*=a*a; }
69
70    ///
71    /// @return Standard error, i.e. standard deviation of the mean
72    /// \f$ \sqrt{variance()/n} \f$
73    ///
74    inline double standard_error(void) const { return sqrt(variance()/n_); }
75
76    ///
77    /// The standard deviation is defined as the square root of the
78    /// variance.
79    ///
80    /// @return The standard deviation, root of the variance().
81    ///
82    inline double std(void) const { return sqrt(variance()); }
83
84    ///
85    /// The standard deviation is defined as the square root of the
86    /// variance.
87    ///
88    /// @return Standard deviation around \a m, root of the variance(m).
89    ///
90    inline double std(double m) const { return sqrt(variance(m)); }
91
92    ///
93    /// @return The sum of x
94    ///
95    inline double sum_x(void) const { return x_; }
96
97    ///
98    /// @return The sum of squares
99    ///
100    inline double sum_xx(void) const { return xx_; }
101
102    ///
103    /// @return \f$ \sum_i (x_i-m)^2 \f$
104    ///
105    inline double sum_xx_centered(void) const { return xx_-x_*x_/n_; }
106
107    ///
108    /// \f$ \frac{1}{n}\sum (x_i-m)^2 \f$.
109    ///
110    /// @return Variance when the mean is known to be \a m.
111    ///
112    inline double variance(double m) const 
113    { return n_ ? (xx_ - 2*m*x_ + m*m*n()) /n_ : 0; }
114
115    ///
116    /// The variance is calculated as \f$ \frac{1}{N}\sum_i
117    /// (x_i-m)^2 \f$, where \f$ m \f$ is the mean.
118    ///
119    /// @return estimation of variance
120    ///
121    inline double variance(void) const 
122    { return n_>1 ? sum_xx_centered()/n_ : 0; }
123
124    ///
125    /// The variance is calculated using the \f$ (n-1) \f$ correction,
126    /// which means it is the best unbiased estimator of the variance
127    /// \f$ \frac{1}{N-1}\sum_i (x_i-m)^2 \f$, where \f$ m \f$ is the
128    /// mean.
129    ///
130    /// @return unbiased estimation of variance
131    ///
132    inline double variance_unbiased(void) const 
133    { return (n_>1) ? sum_xx_centered()/(n_-1) : 0; }
134
135    ///
136    /// Resets everything to zero
137    ///
138    inline void reset(void) { n_=0; x_=xx_=0.0;}
139
140    ///
141    /// The assignment operator
142    ///
143    inline const Averager& operator=(const Averager& a)
144      { n_=a.n_; x_=a.x_; xx_=a.xx_; return *this; }
145
146    ///
147    /// Operator to add another Averager
148    ///
149    const Averager& operator+=(const Averager&);
150
151  private:
152    u_long  n_;
153    double  x_, xx_;
154  };
155 
156  // Template implementations
157  template <typename T>
158  void  Averager::add_values(const T& v, u_long n)
159  { 
160    for (size_t i=0; i<v.size(); i++) 
161      add(v[i],n);
162  }
163
164
165}} // of namespace statistics and namespace theplu
166
167#endif
Note: See TracBrowser for help on using the repository browser.