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

Last change on this file since 911 was 911, checked in by Peter, 14 years ago

Made add function for Averager no longer accept weighted iterators. refs #246

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 KB
Line 
1#ifndef _theplu_yat_statistics_averager_
2#define _theplu_yat_statistics_averager_
3
4// $Id: Averager.h 911 2007-09-29 00:41:40Z peter $
5
6/*
7  Copyright (C) 2004 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2005, 2006 Jari Häkkinen, Markus Ringnér, Peter Johansson
9  Copyright (C) 2007 Peter Johansson
10
11  This file is part of the yat library, http://trac.thep.lu.se/trac/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/IteratorTraits.h"
30
31#include <cmath>
32#include <sys/types.h>
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, u_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, u_long n=1);
69
70    ///
71    /// Adding each value in an array \a v \a n (default=1)
72    /// number of times. The requirements for the type T of the
73    /// array \a v are: operator[] returning an element and function
74    /// size() returning the number of elements.
75    ///   
76    ///
77    template <typename T>
78    void add_values(const T& v, u_long n=1); 
79
80    /**
81       @brief Coeffient of variation
82
83       Coeffient of variation (cv) is defined as ratio between the
84       standard deviation and the mean: \f$ \frac{\sigma}{\mu} \f$.
85       
86       @return standard deviation divided by mean.
87    */
88    double cv(void) const;
89
90    ///
91    /// @return Mean of presented data, \f$ \frac{1}{n}\sum x_i \f$
92    ///
93    double mean(void) const;
94
95    ///
96    /// @return Number of data points
97    ///
98    u_long n(void) const;
99
100    ///
101    /// @brief Rescales the object
102    ///
103    /// \f$ \forall x_i \rightarrow a*x_i \f$,
104    /// \f$ \forall x_i^2 \rightarrow a^2*x_i^2 \f$
105    ///
106    void rescale(double a);
107
108    ///
109    /// @return Standard error, i.e. standard deviation of the mean
110    /// \f$ \sqrt{variance()/n} \f$
111    ///
112    double standard_error(void) const;
113
114    ///
115    /// @brief The standard deviation is defined as the square root of
116    /// the variance.
117    ///
118    /// @return The standard deviation, root of the variance().
119    ///
120    double std(void) const;
121
122    ///
123    /// @brief The standard deviation is defined as the square root of
124    /// the variance.
125    ///
126    /// @return Standard deviation around \a m, root of the variance(m).
127    ///
128    double std(double m) const;
129
130    ///
131    /// @return The sum of x
132    ///
133    double sum_x(void)  const;
134
135    ///
136    /// @return The sum of squares
137    ///
138    double sum_xx(void) const;
139
140    ///
141    /// @return \f$ \sum_i (x_i-m)^2 \f$
142    ///
143    double sum_xx_centered(void)  const;
144
145    ///
146    /// @brief The variance with know mean
147    ///
148    /// The variance is calculated as
149    /// \f$ \frac{1}{n}\sum (x_i-m)^2 \f$.
150    ///
151    /// @return Variance when the mean is known to be \a m.
152    ///
153    double variance(double m) const;
154
155    ///
156    /// @brief The estimated variance
157    ///
158    /// The variance is calculated as \f$ \frac{1}{N}\sum_i
159    /// (x_i-m)^2 \f$, where \f$ m \f$ is the mean.
160    ///
161    /// @return Estimation of variance
162    ///
163    double variance(void) const;
164
165    ///
166    /// The variance is calculated using the \f$ (n-1) \f$ correction,
167    /// which means it is the best unbiased estimator of the variance
168    /// \f$ \frac{1}{N-1}\sum_i (x_i-m)^2 \f$, where \f$ m \f$ is the
169    /// mean.
170    ///
171    /// @return unbiased estimation of variance
172    ///
173    double variance_unbiased(void) const;
174
175    ///
176    /// @brief Reset everything to zero
177    ///
178    void reset(void);
179
180    ///
181    /// @brief The assignment operator
182    ///
183    const Averager& operator=(const Averager&);
184
185    ///
186    /// Operator to add another Averager
187    ///
188    const Averager& operator+=(const Averager&);
189
190  private:
191    u_long  n_;
192    double  x_, xx_;
193  };
194 
195
196  /**
197     \brief adding a ranges of values to Averager \a a
198   */
199  template <typename Iter>
200  void add(Averager& a, Iter first, Iter last)
201  {
202    add(a, first, last, 
203        typename utility::weighted_iterator_traits<Iter>::type());
204  }
205
206  // unweighted impl. (weighted version is not implemented and should
207  // not compile)
208  template <typename Iter>
209  void add(Averager& a, Iter first, Iter last, 
210           utility::unweighted_type type)
211  {
212    for ( ; first != last; ++first)
213      a.add(*first);
214  }
215
216
217  // Template implementations
218  template <typename T>
219  void  Averager::add_values(const T& v, u_long n)
220  { 
221    for (size_t i=0; i<v.size(); i++) 
222      add(v[i],n);
223  }
224
225}}} // of namespace statistics, yat, and theplu
226
227#endif
Note: See TracBrowser for help on using the repository browser.