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

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

impl add function for AveragerWeighted?. The function distinguishes between normal iterators and weighted iterators, and call the member add function appropriately. refs #246

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.8 KB
Line 
1#ifndef _theplu_yat_statistics_averager_
2#define _theplu_yat_statistics_averager_
3
4// $Id: Averager.h 910 2007-09-29 00:20:10Z 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 <cmath>
30#include <sys/types.h>
31
32namespace theplu{
33namespace yat{
34namespace statistics{
35
36  class ostream;
37
38  ///
39  /// @brief Class to calculate simple (first and second moments) averages.
40  ///
41  /// @see AveragerWeighted AveragerPair AveragerPairWeighted
42  ///
43  class Averager
44  {
45  public:
46
47    ///
48    /// Default constructor
49    ///
50    Averager(void);
51   
52    ///
53    /// Constructor taking sum of \a x, sum of squared x, \a xx, and
54    /// number of samples \a n.
55    ///
56    Averager(double x, double xx, u_long n);
57
58    ///
59    /// Copy constructor
60    ///
61    Averager(const Averager& a);
62
63    ///
64    /// Adding \a n (default=1) number of data point(s) with value \a d.
65    ///
66    void add(double d, u_long n=1);
67
68    ///
69    /// Adding each value in an array \a v \a n (default=1)
70    /// number of times. The requirements for the type T of the
71    /// array \a v are: operator[] returning an element and function
72    /// size() returning the number of elements.
73    ///   
74    ///
75    template <typename T>
76    void add_values(const T& v, u_long n=1); 
77
78    /**
79       @brief Coeffient of variation
80
81       Coeffient of variation (cv) is defined as ratio between the
82       standard deviation and the mean: \f$ \frac{\sigma}{\mu} \f$.
83       
84       @return standard deviation divided by mean.
85    */
86    double cv(void) const;
87
88    ///
89    /// @return Mean of presented data, \f$ \frac{1}{n}\sum x_i \f$
90    ///
91    double mean(void) const;
92
93    ///
94    /// @return Number of data points
95    ///
96    u_long n(void) const;
97
98    ///
99    /// @brief Rescales the object
100    ///
101    /// \f$ \forall x_i \rightarrow a*x_i \f$,
102    /// \f$ \forall x_i^2 \rightarrow a^2*x_i^2 \f$
103    ///
104    void rescale(double a);
105
106    ///
107    /// @return Standard error, i.e. standard deviation of the mean
108    /// \f$ \sqrt{variance()/n} \f$
109    ///
110    double standard_error(void) const;
111
112    ///
113    /// @brief The standard deviation is defined as the square root of
114    /// the variance.
115    ///
116    /// @return The standard deviation, root of the variance().
117    ///
118    double std(void) const;
119
120    ///
121    /// @brief The standard deviation is defined as the square root of
122    /// the variance.
123    ///
124    /// @return Standard deviation around \a m, root of the variance(m).
125    ///
126    double std(double m) const;
127
128    ///
129    /// @return The sum of x
130    ///
131    double sum_x(void)  const;
132
133    ///
134    /// @return The sum of squares
135    ///
136    double sum_xx(void) const;
137
138    ///
139    /// @return \f$ \sum_i (x_i-m)^2 \f$
140    ///
141    double sum_xx_centered(void)  const;
142
143    ///
144    /// @brief The variance with know mean
145    ///
146    /// The variance is calculated as
147    /// \f$ \frac{1}{n}\sum (x_i-m)^2 \f$.
148    ///
149    /// @return Variance when the mean is known to be \a m.
150    ///
151    double variance(double m) const;
152
153    ///
154    /// @brief The estimated variance
155    ///
156    /// The variance is calculated as \f$ \frac{1}{N}\sum_i
157    /// (x_i-m)^2 \f$, where \f$ m \f$ is the mean.
158    ///
159    /// @return Estimation of variance
160    ///
161    double variance(void) const;
162
163    ///
164    /// The variance is calculated using the \f$ (n-1) \f$ correction,
165    /// which means it is the best unbiased estimator of the variance
166    /// \f$ \frac{1}{N-1}\sum_i (x_i-m)^2 \f$, where \f$ m \f$ is the
167    /// mean.
168    ///
169    /// @return unbiased estimation of variance
170    ///
171    double variance_unbiased(void) const;
172
173    ///
174    /// @brief Reset everything to zero
175    ///
176    void reset(void);
177
178    ///
179    /// @brief The assignment operator
180    ///
181    const Averager& operator=(const Averager&);
182
183    ///
184    /// Operator to add another Averager
185    ///
186    const Averager& operator+=(const Averager&);
187
188  private:
189    u_long  n_;
190    double  x_, xx_;
191  };
192 
193
194  /**
195     \brief adding a ranges of values to Averager \a a
196   */
197  template <typename Iter>
198  void add(Averager& a, Iter first, Iter last)
199  {
200    for ( ; first != last; ++first)
201      a.add(*first);
202  }
203
204
205  // Template implementations
206  template <typename T>
207  void  Averager::add_values(const T& v, u_long n)
208  { 
209    for (size_t i=0; i<v.size(); i++) 
210      add(v[i],n);
211  }
212
213}}} // of namespace statistics, yat, and theplu
214
215#endif
Note: See TracBrowser for help on using the repository browser.