source: trunk/yat/statistics/Averager.h @ 1295

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

Fixes #361. Averager and AveragerPair? accepts negative n for data removal.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 KB
Line 
1#ifndef _theplu_yat_statistics_averager_
2#define _theplu_yat_statistics_averager_
3
4// $Id: Averager.h 1295 2008-05-12 08:35:10Z jari $
5
6/*
7  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2005, 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér
9  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
10
11  This file is part of the yat library, http://trac.thep.lu.se/yat
12
13  The yat library is free software; you can redistribute it and/or
14  modify it under the terms of the GNU General Public License as
15  published by the Free Software Foundation; either version 2 of the
16  License, or (at your option) any later version.
17
18  The yat library is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21  General Public License for more details.
22
23  You should have received a copy of the GNU General Public License
24  along with this program; if not, write to the Free Software
25  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26  02111-1307, USA.
27*/
28
29#include "yat/utility/iterator_traits.h"
30
31#include <cmath>
32
33namespace theplu{
34namespace yat{
35namespace statistics{
36
37  class ostream;
38
39  ///
40  /// @brief Class to calculate simple (first and second moments) averages.
41  ///
42  /// @see AveragerWeighted AveragerPair AveragerPairWeighted
43  ///
44  class Averager
45  {
46  public:
47
48    ///
49    /// Default constructor
50    ///
51    Averager(void);
52   
53    ///
54    /// Constructor taking sum of \a x, sum of squared x, \a xx, and
55    /// number of samples \a n.
56    ///
57    Averager(double x, double xx, long n);
58
59    ///
60    /// Copy constructor
61    ///
62    Averager(const Averager& a);
63
64    ///
65    /// Adding \a n (default=1) number of data point(s) with value \a d.
66    ///
67    void add(double d, long n=1);
68
69    /**
70       @brief Coeffient of variation
71
72       Coeffient of variation (cv) is defined as ratio between the
73       standard deviation and the mean: \f$ \frac{\sigma}{\mu} \f$.
74       
75       @return standard deviation divided by mean.
76    */
77    double cv(void) const;
78
79    ///
80    /// @return %Mean of presented data, \f$ \frac{1}{n}\sum x_i \f$
81    ///
82    double mean(void) const;
83
84    ///
85    /// @return Number of data points
86    ///
87    long n(void) const;
88
89    ///
90    /// @brief Rescales the object
91    ///
92    /// \f$ \forall x_i \rightarrow a*x_i \f$,
93    /// \f$ \forall x_i^2 \rightarrow a^2*x_i^2 \f$
94    ///
95    void rescale(double a);
96
97    ///
98    /// @return Standard error, i.e. standard deviation of the mean
99    /// \f$ \sqrt{variance()/n} \f$
100    ///
101    double standard_error(void) const;
102
103    ///
104    /// @brief The standard deviation is defined as the square root of
105    /// the variance.
106    ///
107    /// @return The standard deviation, root of the variance().
108    ///
109    double std(void) const;
110
111    ///
112    /// @brief The standard deviation is defined as the square root of
113    /// the variance.
114    ///
115    /// @return Standard deviation around \a m, root of the variance(m).
116    ///
117    double std(double m) const;
118
119    ///
120    /// @return The sum of x
121    ///
122    double sum_x(void)  const;
123
124    ///
125    /// @return The sum of squares
126    ///
127    double sum_xx(void) const;
128
129    ///
130    /// @return \f$ \sum_i (x_i-m)^2 \f$
131    ///
132    double sum_xx_centered(void)  const;
133
134    ///
135    /// @brief The variance with know mean
136    ///
137    /// The variance is calculated as
138    /// \f$ \frac{1}{n}\sum (x_i-m)^2 \f$.
139    ///
140    /// @return Variance when the mean is known to be \a m.
141    ///
142    double variance(double m) const;
143
144    /**
145       \brief The estimated variance
146       
147       The variance is calculated as \f$ \frac{1}{N}\sum_i
148       (x_i-m)^2 \f$, where \f$ m \f$ is the mean.
149       
150       \return Estimation of variance
151    */
152    double variance(void) const;
153
154    ///
155    /// The variance is calculated using the \f$ (n-1) \f$ correction,
156    /// which means it is the best unbiased estimator of the variance
157    /// \f$ \frac{1}{N-1}\sum_i (x_i-m)^2 \f$, where \f$ m \f$ is the
158    /// mean.
159    ///
160    /// @return unbiased estimation of variance
161    ///
162    double variance_unbiased(void) const;
163
164    ///
165    /// @brief Reset everything to zero
166    ///
167    void reset(void);
168
169    ///
170    /// @brief The assignment operator
171    ///
172    const Averager& operator=(const Averager&);
173
174    ///
175    /// Operator to add another Averager
176    ///
177    const Averager& operator+=(const Averager&);
178
179  private:
180    long  n_;
181    double  x_, xx_;
182  };
183 
184
185  /**
186     \brief adding a ranges of values to Averager \a a
187   */
188  template <typename Iter>
189  void add(Averager& a, Iter first, Iter last)
190  {
191    utility::check_iterator_is_unweighted(first);
192    for ( ; first != last; ++first)
193      a.add(*first);
194  }
195
196}}} // of namespace statistics, yat, and theplu
197
198#endif
Note: See TracBrowser for help on using the repository browser.