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

Last change on this file since 616 was 616, checked in by Jari Häkkinen, 16 years ago

Removed gslapi namespace and put the code into utility namespace.
Removed unneccesary #includes, and added needed #includes.

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