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

Last change on this file since 2161 was 2161, checked in by Peter, 12 years ago

updating copyright years

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.5 KB
Line 
1#ifndef _theplu_yat_statistics_averager_
2#define _theplu_yat_statistics_averager_
3
4// $Id: Averager.h 2161 2010-01-19 23:59:48Z peter $
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  Copyright (C) 2009, 2010 Peter Johansson
11
12  This file is part of the yat library, http://dev.thep.lu.se/yat
13
14  The yat library is free software; you can redistribute it and/or
15  modify it under the terms of the GNU General Public License as
16  published by the Free Software Foundation; either version 3 of the
17  License, or (at your option) any later version.
18
19  The yat library is distributed in the hope that it will be useful,
20  but WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23
24  You should have received a copy of the GNU General Public License
25  along with yat. If not, see <http://www.gnu.org/licenses/>.
26*/
27
28#include "yat/utility/iterator_traits.h"
29
30#include <boost/concept_check.hpp>
31
32#include <cmath>
33
34namespace theplu{
35namespace yat{
36namespace statistics{
37
38  class ostream;
39
40  ///
41  /// @brief Class to calculate simple (first and second moments) averages.
42  ///
43  /// @see AveragerWeighted AveragerPair AveragerPairWeighted
44  ///
45  class Averager
46  {
47  public:
48
49    ///
50    /// Default constructor
51    ///
52    Averager(void);
53   
54    ///
55    /// Constructor taking sum of \a x, sum of squared x, \a xx, and
56    /// number of samples \a n.
57    ///
58    Averager(double x, double xx, long n);
59
60    ///
61    /// Copy constructor
62    ///
63    Averager(const Averager& a);
64
65    ///
66    /// Adding \a n (default=1) number of data point(s) with value \a d.
67    ///
68    void add(double d, long n=1);
69
70    /**
71       @brief Coeffient of variation
72
73       Coeffient of variation (cv) is defined as ratio between the
74       standard deviation and the mean: \f$ \frac{\sigma}{\mu} \f$.
75       
76       @return standard deviation divided by mean.
77    */
78    double cv(void) const;
79
80    ///
81    /// @return %Mean of presented data, \f$ \frac{1}{n}\sum x_i \f$
82    ///
83    double mean(void) const;
84
85    ///
86    /// @return Number of data points
87    ///
88    long n(void) const;
89
90    ///
91    /// @brief Rescales the object
92    ///
93    /// \f$ \forall x_i \rightarrow a*x_i \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 range of values to Averager \a a
187
188     \relates Averager
189   */
190  template <typename InputIterator>
191  void add(Averager& a, InputIterator first, InputIterator last)
192  {
193    BOOST_CONCEPT_ASSERT((boost::InputIterator<InputIterator>));
194    utility::check_iterator_is_unweighted(first);
195    for ( ; first != last; ++first)
196      a.add(*first);
197  }
198
199}}} // of namespace statistics, yat, and theplu
200
201#endif
Note: See TracBrowser for help on using the repository browser.