source: trunk/src/WeightedAverager.h @ 94

Last change on this file since 94 was 94, checked in by Peter, 19 years ago

Weighted averager

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 1.0 KB
Line 
1// $Id: WeightedAverager.h 94 2004-06-09 10:24:53Z peter $
2
3#ifndef _theplu_cpptools_weighted_averager_
4#define _theplu_cpptools_weighted_averager_
5
6//#include <cmath>
7#include "Averager.h"
8
9namespace theplu{
10namespace cpptools{
11  ///
12  /// Averager class for weights.
13  ///
14  class WeightedAverager
15  {
16  public:
17
18    ///
19    /// Default constructor
20    ///
21    WeightedAverager(void);
22   
23    ///
24    /// adding a data point d, with weight w (default is 1)
25    ///
26    inline void add(const double d,const double w=1)
27    {w_.add(w); wx_.add(w*d); wwx_.add(w*w*d);}
28
29    ///
30    /// @return average
31    ///
32    inline double average(void) const {return mean();}
33
34    ///
35    /// @return average
36    ///
37    inline double mean(void) const { return w_.sum_x() ? 
38                                       wx_.sum_x()/w_.sum_x() : 0; }
39 
40    ///
41    /// @return squarred error
42    ///
43    inline double squarred_error(void)  const 
44    { return (wx_.sum_xsqr()-2*mean()*wwx_.sum_x()+
45              mean()*mean()*w_.sum_xsqr()) / (w_.sum_x()*w_.sum_x()); }
46
47  private:
48    Averager w_;
49    Averager wx_;
50    Averager wwx_;
51   
52
53  };
54
55
56}} // of namespace cpptools and namespace theplu
57
58#endif
Note: See TracBrowser for help on using the repository browser.