# source:trunk/src/Averager.h@138

Last change on this file since 138 was 138, checked in by Peter, 18 years ago

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