source: trunk/src/Averager.h @ 97

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

removed the weights

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.3 KB
Line 
1// $Id: Averager.h 97 2004-06-09 16:30:41Z peter $
2
3#ifndef _theplu_cpptools_averager_
4#define _theplu_cpptools_averager_
5
6#include <cmath>
7
8namespace theplu{
9namespace cpptools{
10  class ostream;
11  ///
12  /// Modified version of the Averager class written by Patrik Eden,
13  /// Dept. Theoretical Physic, Lund University, Sweden
14  ///
15  class Averager
16  {
17  public:
18
19    ///
20    /// Default constructor
21    ///
22    Averager(void);
23   
24    ///
25    /// Constructor taking the value of first data point.
26    ///
27    Averager(double);
28   
29    ///
30    /// Constructor taking sum of x, sum of squared x and number of
31    /// samples.
32    ///
33    Averager(double, double, long);
34   
35    ///
36    /// Copy constructor
37    ///
38    Averager(const Averager&);
39   
40    ///
41    /// adding n(default=1) number of data point(s) with value d.
42    ///
43    inline void add(const double d,const long n=1)
44    {n_+=n; x_+=n*d; xx_+=n*d*d;}
45
46    ///
47    /// @return average
48    ///
49    inline double average(void) const {return mean();}
50
51    ///
52    /// @return sum of squared values \f$ \sum x_i^2 \f$
53    ///
54    inline double average_sqr(void) const {return mean_sqr();}
55 
56    ///
57    /// @return average \f$ \frac{\sum x_i}{n} \f$
58    ///
59    inline double mean(void) const { return n_ ? x_/n_ : 0; }
60 
61    ///
62    /// @return @return sum of squared values \f$ \sum x_i^2 \f$.
63    ///
64    inline double mean_sqr(void) const { return n_ ? xx_/n_ : 0; }
65
66    ///
67    /// @return number of data points
68    ///
69    inline double n(void) const { return n_; }
70
71    ///
72    /// resets everything to zero
73    ///
74    inline void reset(void) { n_=0; x_=xx_=0.0;}
75
76    ///
77    /// @return standard deviation
78    ///
79    inline double standard_deviation(void) const {  return sqrt(variance()); }
80
81    ///
82    /// @return standard error, i.e. standard deviation of the mean
83    ///
84    inline double standard_error(void) const { return sqrt(variance()/n_); }
85
86    ///
87    /// @return the sum of x
88    ///
89    inline double sum_x(void) const { return x_; }
90
91    ///
92    /// @return the sum of squares
93    ///
94    inline double sum_xsqr(void) const { return xx_; }
95
96    ///
97    ///The variance is calculated using the (n-1) correction, so the
98    ///expectation value is unbiased @return variance
99    ///
100    inline double variance(void) const 
101    { return (n_>1) ? (xx_-x_*x_/n_)/(n_-1) : 0; }
102
103    ///
104    /// equality operator
105    ///
106    inline Averager operator=(const Averager& a)
107    { n_=a.n_; x_=a.x_; xx_=a.xx_; return *this; }
108
109    ///
110    /// operator to add a double to the object
111    ///
112    inline Averager operator+=(double d)
113    { add(d); return *this; }
114
115    ///
116    /// operator to add a "one"
117    ///
118    inline Averager operator++(void)
119    { add(1); return *this; }
120
121    ///
122    /// Operator to rescale the object
123    ///
124    inline Averager operator*=(double d)
125    { x_*=d; xx_*=d*d; return *this; }
126
127    ///
128    /// Operator to rescale the object
129    ///
130    inline Averager operator/=(double d)
131    { return (*this)*=(1/d); }
132
133    ///
134    /// operator to add another Averager
135    ///
136    const Averager& operator+=(const Averager&);
137
138    ///
139    /// operator to multiply with an Averager. See code for details.
140    ///
141    const Averager& operator*=(const Averager&);
142
143    ///
144    /// operator to multiply with an Averager. See code for details.
145    ///
146    const Averager& operator/=(const Averager&);
147
148  private:
149    long  n_;
150    double  x_, xx_;
151  };
152
153  Averager operator*(double,const Averager&);
154 
155  ///
156  /// The output operator
157  ///
158  std::ostream& operator<<( std::ostream&, const Averager&);
159
160
161}} // of namespace cpptools and namespace theplu
162
163#endif
Note: See TracBrowser for help on using the repository browser.