Tools/CIC/utl/Accumulator.h
Go to the documentation of this file.
1 // \author Darko Veberic
2 // \date 2014
3 
4 #ifndef _cic_utl_Accumulator_h_
5 #define _cic_utl_Accumulator_h_
6 
7 #include <utl/Math.h>
8 #include <vector>
9 #include <functional>
10 #include <algorithm>
11 
12 
13 namespace utl {
14 namespace Accumulator {
15 
16  class Count {
17  public:
18  Count(const unsigned int init = 0) : fCount(init) { }
19  void operator()() { ++fCount; }
20  unsigned int operator++() { operator()(); return fCount; }
21  unsigned int GetCount() const { return fCount; }
22  void Clear(const unsigned init = 0) { fCount = init; }
23  private:
24  unsigned int fCount;
25  };
26 
27 
28  template<typename T, class Container = std::vector<T>>
29  class Collect : public std::unary_function<T, void> {
30  public:
31  void operator()(const T& x) { fCollection.push_back(x); }
32 
33  Container& GetCollection() { return fCollection; }
34 
35  const Container& GetCollection() const { return fCollection; }
36 
37  template<class Order>
38  void Sort(Order ord)
39  { std::sort(fCollection.begin(), fCollection.end(), ord); }
40 
41  void Clear() { fCollection.clear(); }
42 
43  private:
44  Container fCollection;
45  };
46 
47 
48  template<typename T>
49  class Min : public std::unary_function<T, void> {
50  public:
51  typedef T Type;
52  Min(const T first) : fMin(first) { }
53  void operator()(const T x) { if (fMin > x) fMin = x; }
54  T GetMin() const { return fMin; }
55  void Clear(const T x) { fMin = x; }
56  protected:
57  Min() : fMin(T()) { }
58  void Clear() { fMin = T(); }
59  private:
60  T fMin;
61  };
62 
63 
64  template<typename T>
65  class Max : public std::unary_function<T, void> {
66  public:
67  typedef T Type;
68  Max(const T first) : fMax(first) { }
69  void operator()(const T x) { if (fMax < x) fMax = x; }
70  T GetMax() const { return fMax; }
71  void Clear(const T x) { fMax = x; }
72  protected:
73  Max() : fMax(T()) { }
74  void Clear() { fMax = T(); }
75  private:
76  T fMax;
77  };
78 
79 
80  template<typename T>
81  class MinMax : public Min<T>, public Max<T> {
82  public:
83  typedef T Type;
84  MinMax(const T first) : Min<T>(first), Max<T>(first) { }
85  MinMax(const T firstMin, const T firstMax) : Min<T>(firstMin), Max<T>(firstMax) { }
86  MinMax(const MinMax& mm) = default;
87  void operator()(const T x) { Min<T>::operator()(x); Max<T>::operator()(x); }
88  std::pair<T, T> GetMinMax() const { return std::make_pair(this->GetMin(), this->GetMax()); }
89  void Clear(const T x) { Clear(x, x); }
90  void Clear(const T min, const T max) { Min<T>::Clear(min); Max<T>::Clear(max); }
91  protected:
92  MinMax() : Min<T>(), Max<T>() { }
94  };
95 
96 
97  template<typename T>
98  class Covariance : public Count {
99  public:
100  explicit Covariance(const unsigned int nVar)
101  : Count(), fSumX(nVar), fSumXY(Sqr(nVar)) { }
102 
103  void
104  operator()(const std::vector<T>& values)
105  {
107  for (unsigned int i = 0, n = fSumX.size(); i < n; ++i) {
108  fSumX[i] += values[i];
109  for (unsigned int j = 0; j <= i; ++j)
110  fSumXY[n*i + j] += values[i] * values[j];
111  }
112  }
113 
114  std::vector<double>
115  GetMeans()
116  const
117  {
118  std::vector<double> m(fSumX.size());
119  for (unsigned int i = 0; i < fSumX.size(); ++i)
120  m[i] = double(fSumX[i]) / GetCount();
121  return m;
122  }
123 
124  std::vector<double>
125  GetVars()
126  const
127  {
128  const unsigned int n = fSumX.size();
129  std::vector<double> v(n);
130  for (unsigned int i = 0; i < n; ++i)
131  v[i] = (fSumXY[(n+1)*i] - double(Sqr(fSumX[i]))/GetCount()) / (GetCount() - 1);
132  return v;
133  }
134 
135  std::vector<double>
136  GetStds()
137  const
138  {
139  std::vector<double> s = GetVars();
140  for (double& x : s)
142  return s;
143  }
144 
145  double
146  GetCovariance(unsigned int i, unsigned int j)
147  const
148  {
149  if (j > i)
150  std::swap(i, j);
151  const unsigned int n = fSumX.size();
152  return (fSumXY[n*i + j] - double(fSumX[i]*fSumX[j])/GetCount()) / (GetCount() - 1);
153  }
154 
155  std::vector<std::vector<double>>
156  GetCovariance()
157  const
158  {
159  const unsigned int n = fSumX.size();
160  std::vector<std::vector<double>> c(n, std::vector<double>(n));
161  for (unsigned int i = 0; i < n; ++i)
162  for (unsigned int j = 0; j < n; ++j)
163  c[i][j] = GetCovariance(i, j);
164  return c;
165  }
166 
167  private:
168  std::vector<T> fSumX;
169  std::vector<T> fSumXY;
170  };
171 
172 
173  class Sum : public std::unary_function<double, void> {
174  public:
175  Sum() : fSumX(0) { }
176  void operator()(const double x) { fSumX += x; }
177  double GetMean(const int n) const { return fSumX / n; }
178  double GetSum() const { return fSumX; }
179  void Clear() { fSumX = 0; }
180  private:
181  double fSumX;
182  };
183 
184 
185  class Mean : public Count, public Sum {
186  public:
187  void operator()(const double x) { Count::operator()(); Sum::operator()(x); }
188  double GetMean() const { return Sum::GetMean(GetCount()); }
189  void Clear() { Count::Clear(); Sum::Clear(); }
190  };
191 
192 
193  class VarN : public Sum {
194  public:
195  void operator()(const double x) { Sum::operator()(x); fX2(Sqr(x)); }
196  double GetVar(const int n) const
197  { return (fX2.GetSum() - Sqr(GetSum())/n) / (n - 1); }
198  double GetSumOfSquares() const { return fX2.GetSum(); }
199  void Clear() { Sum::Clear(); fX2.Clear(); }
200  protected:
202  };
203 
204 
205  class StdN : public VarN {
206  public:
207  void operator()(const double x) { VarN::operator()(x); }
208  double GetStd(const int n) const { return std::sqrt(GetVar(n)); }
209  };
210 
211 
212  class Var : public Mean {
213  public:
214  void operator()(const double x) { Mean::operator()(x); fVar(x); }
215  double GetVar() const { return fVar.GetVar(GetCount()); }
216  double GetSumOfSquares() const { return fVar.GetSumOfSquares(); }
217  void Clear() { Mean::Clear(); fVar.Clear(); }
218  protected:
220  };
221 
222 
223  class Std : public Var {
224  public:
225  void operator()(const double x) { Var::operator()(x); }
226  double GetStd() const { return std::sqrt(GetVar()); }
227  };
228 
229 }
230 }
231 
232 
233 #endif
void operator()(const double x)
void swap(utl::Trace< T > &t1, utl::Trace< T > &t2)
Definition: Trace.h:363
constexpr double mm
Definition: AugerUnits.h:113
void operator()(const double x)
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)
double GetVar(const int n) const
void operator()(const std::vector< T > &values)
void operator()(const double x)
Count(const unsigned int init=0)
std::pair< T, T > GetMinMax() const
unsigned int GetCount() const
const Container & GetCollection() const
double GetMean(const int n) const
double GetStd(const int n) const
void Clear(const unsigned init=0)

, generated on Tue Sep 26 2023.