source: trunk/src/Averager.h @ 218

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

sum_xsqr_centered(void) added

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