source: branches/better_matrix_class/lib/statistics/Averager.h @ 415

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

Removed some gslapi copy intensive operators.
Made gslapi assignment operators ignore views, and change them
into normal vectors if assigned.
Cleaning up of code aswell.

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