Utilities/Statistics/Accumulator.h
Go to the documentation of this file.
1 #ifndef _utl_Accumulator_h_
2 #define _utl_Accumulator_h_
3 
4 #include <vector>
5 #include <deque>
6 #include <utl/Math.h>
7 
8 #include <boost/shared_ptr.hpp>
9 
10 #include <algorithm>
11 
12 
13 namespace utl {
14  namespace Accumulator {
15 
24  template<typename T>
25  class Min {
26  public:
27  typedef T Type;
28  Min(const T first) : fMin(first) { }
29  void operator()(const T x) { if (fMin > x) fMin = x; }
30  T GetMin() const { return fMin; }
31  void Clear(const T x) { fMin = x; }
32  protected:
33  Min() : fMin(T()) { }
34  void SetFirst(const T x) { fMin = x; }
35  void Clear() { fMin = T(); }
36  private:
37  T fMin;
38  };
39 
40 
41  template<typename T>
42  class Max {
43  public:
44  typedef T Type;
45  Max(const T first) : fMax(first) { }
46  void operator()(const T x) { if (fMax < x) fMax = x; }
47  T GetMax() const { return fMax; }
48  void Clear(const T x) { fMax = x; }
49  protected:
50  Max() : fMax(T()) { }
51  void Clear() { fMax = T(); }
52  private:
53  T fMax;
54  };
55 
56 
57  template<typename T>
58  class MinMax : public Min<T>, public Max<T> {
59  public:
60  typedef T Type;
61  MinMax(const T first) : Min<T>(first), Max<T>(first) { }
62  MinMax(const T firstMin, const T firstMax) : Min<T>(firstMin), Max<T>(firstMax) { }
63  void operator()(const T x) { Min<T>::operator()(x); Max<T>::operator()(x); }
64  void Clear(const T x) { Clear(x, x); }
65  void Clear(const T min, const T max) { Min<T>::Clear(min); Max<T>::Clear(max); }
66  protected:
67  MinMax() { }
69  };
70 
71 
72  class AverageN {
73  public:
74  void operator()(const double x) { fX += x; }
75  double GetAverage(const int n) const { return fX / n; }
76  double GetSum() const { return fX; }
77  void Clear() { fX = 0; }
78  protected:
79  double fX = 0;
80  };
81 
82 
83  class Average {
84  public:
85  void operator()(const double x) { fAvg(x); ++fN; }
86  double GetAverage() const { return fAvg.GetAverage(fN); }
87  int GetN() const { return fN; }
88  void Clear() { fAvg.Clear(); fN = 0; }
89  explicit operator bool() const { return fN; }
90  private:
92  int fN = 0;
93  };
94 
95 
96  template<typename T>
97  class MinMaxAverage : public Min<T>, public Max<T>, public Average {
98  public:
99  typedef T Type;
100  MinMaxAverage(const T first) : Min<T>(first), Max<T>(first) { Average::operator()(first); }
103  protected:
106  };
107 
108 
109  //
110 
111 
112  class Validator {
113  public:
114  bool IsValid() const { return fIsValid; }
115  void Validate(const bool state = true) { fIsValid = state; }
116  void Clear() { fIsValid = false; }
117  private:
118  bool fIsValid = false;
119  };
120 
152  template<typename AccumulatorT>
153  class Safe : public AccumulatorT, public Validator {
154  public:
155  void
156  operator()(const typename AccumulatorT::Type x)
157  {
158  if (*this)
159  AccumulatorT::operator()(x);
160  else {
161  AccumulatorT::Clear(x);
162  Validate();
163  }
164  }
165 
166  explicit operator bool() const { return IsValid(); }
167 
168  void Clear() { AccumulatorT::Clear(); Validator::Clear(); }
169  };
170 
171 
172  //
173 
174 
175  class SampleVarianceN : public AverageN {
176  public:
177  void operator()(const double x) { AverageN::operator()(x); fX2 += Sqr(x); }
178  double GetVariance(const int n) const { return (fX2 - Sqr(fX)/n) / (n - 1); }
179  double GetAverageError(const int n) const { return std::sqrt(GetVariance(n)/n); }
180  double GetSumOfSquares() const { return fX2; }
181  void Clear() { AverageN::Clear(); fX2 = 0; }
182  protected:
183  double fX2 = 0;
184  };
185 
186 
187  //
188 
189 
191  public:
192  void operator()(const double x) { SampleVarianceN::operator()(x); }
193  double GetStandardDeviation(const int n) const { return std::sqrt(GetVariance(n)); }
194  };
195 
196 
197  //
198 
199 
200  class SampleVariance : public Average {
201  public:
202  void operator()(const double x) { Average::operator()(x); fVariance(x); }
203  double GetVariance() const { return fVariance.GetVariance(Average::GetN()); }
204  double GetAverageError() const { return fVariance.GetAverageError(GetN()); }
205  double GetSumOfSquares() const { return fVariance.GetSumOfSquares(); }
207  protected:
209  };
210 
211 
236  public:
237  void operator()(const double x) { SampleVariance::operator()(x); }
238  double GetStandardDeviation() const { return std::sqrt(GetVariance()); }
239  };
240 
241 
242  //
243 
244 
246  public:
247  unsigned int GetN() const { return fX.GetN(); }
248  double GetAverageX() const { return fX.GetAverage(); }
249  double GetAverageY() const { return fY.GetAverage(GetN()); }
250  double GetAverageXY() const { return fXY.GetAverage(GetN()); }
251  double GetStandardDeviationX() const { return fX.GetStandardDeviation(); }
252  double GetStandardDeviationY() const { return fY.GetStandardDeviation(GetN()); }
254  { return GetN() * (GetAverageXY() - GetAverageX()*GetAverageY()) /
256  void Clear() { fX.Clear(); fY.Clear(); fXY.Clear(); }
257  void operator()(const double x, const double y) { fX(x); fY(y); fXY(x*y); }
258  private:
262  };
263 
264  }
265 
266 }
267 
268 
269 #endif
constexpr T Sqr(const T &x)
MinMax(const T firstMin, const T firstMax)
void Clear(const T min, const T max)
#define max(a, b)
void operator()(const typename AccumulatorT::Type x)
Accumulates and calculates standard deviation.
Type
The type of file that we are acutally opening.
Definition: IoCodes.h:33

, generated on Tue Sep 26 2023.