IT++ Logo
misc_stat.h
Go to the documentation of this file.
1 
29 #ifndef MISC_STAT_H
30 #define MISC_STAT_H
31 
32 #include <itpp/base/math/min_max.h>
33 #include <itpp/base/mat.h>
35 #include <itpp/base/matfunc.h>
36 #include <itpp/itexports.h>
37 
38 
39 namespace itpp
40 {
41 
44 
48 class ITPP_EXPORT Stat
49 {
50 public:
52  Stat() {clear();}
54  virtual ~Stat() {}
55 
57  virtual void clear() {
58  _n_overflows = 0;
59  _n_samples = 0;
60  _n_zeros = 0;
61  _max = 0.0;
62  _min = 0.0;
63  _sqr_sum = 0.0;
64  _sum = 0.0;
65  }
66 
68  virtual void sample(const double s, const bool overflow = false) {
69  _n_samples++;
70  _sum += s;
71  _sqr_sum += s * s;
72  if (s < _min) _min = s;
73  if (s > _max) _max = s;
74  if (overflow) _n_overflows++;
75  if (s == 0.0) _n_zeros++;
76  }
77 
79  int n_overflows() const {return _n_overflows;}
81  int n_samples() const {return _n_samples;}
83  int n_zeros() const {return _n_zeros;}
85  double avg() const {return _sum / _n_samples;}
87  double max() const {return _max;}
89  double min() const {return _min;}
91  double sigma() const {
92  double sigma2 = _sqr_sum / _n_samples - avg() * avg();
93  return std::sqrt(sigma2 < 0 ? 0 : sigma2);
94  }
96  double sqr_sum() const {return _sqr_sum;}
98  double sum() const {return _sum;}
100  vec histogram() const {return vec(0);}
101 
102 protected:
108  int _n_zeros;
110  double _max;
112  double _min;
114  double _sqr_sum;
116  double _sum;
117 };
118 
119 
121 ITPP_EXPORT double mean(const vec &v);
123 ITPP_EXPORT std::complex<double> mean(const cvec &v);
125 ITPP_EXPORT double mean(const svec &v);
127 ITPP_EXPORT double mean(const ivec &v);
129 ITPP_EXPORT double mean(const mat &m);
131 ITPP_EXPORT std::complex<double> mean(const cmat &m);
133 ITPP_EXPORT double mean(const smat &m);
135 ITPP_EXPORT double mean(const imat &m);
136 
138 template<class T>
139 double geometric_mean(const Vec<T> &v)
140 {
141  return std::exp(std::log(static_cast<double>(prod(v))) / v.length());
142 }
143 
145 template<class T>
146 double geometric_mean(const Mat<T> &m)
147 {
148  return std::exp(std::log(static_cast<double>(prod(prod(m))))
149  / (m.rows() * m.cols()));
150 }
151 
153 template<class T>
154 double median(const Vec<T> &v)
155 {
156  Vec<T> invect(v);
157  sort(invect);
158  return (double)(invect[(invect.length()-1)/2] + invect[invect.length()/2]) / 2.0;
159 }
160 
162 ITPP_EXPORT double norm(const cvec &v);
163 
165 template<class T>
166 double norm(const Vec<T> &v)
167 {
168  double E = 0.0;
169  for (int i = 0; i < v.size(); i++)
170  E += sqr(static_cast<double>(v[i]));
171 
172  return std::sqrt(E);
173 }
174 
176 ITPP_EXPORT double norm(const cvec &v, int p);
177 
179 template<class T>
180 double norm(const Vec<T> &v, int p)
181 {
182  double E = 0.0;
183  for (int i = 0; i < v.size(); i++)
184  E += std::pow(fabs(static_cast<double>(v[i])), static_cast<double>(p));
185 
186  return std::pow(E, 1.0 / p);
187 }
188 
190 ITPP_EXPORT double norm(const cvec &v, const std::string &s);
191 
193 template<class T>
194 double norm(const Vec<T> &v, const std::string &s)
195 {
196  it_assert(s == "fro", "norm(): Unrecognised norm");
197 
198  double E = 0.0;
199  for (int i = 0; i < v.size(); i++)
200  E += sqr(static_cast<double>(v[i]));
201 
202  return std::sqrt(E);
203 }
204 
213 ITPP_EXPORT double norm(const mat &m, int p = 2);
214 
223 ITPP_EXPORT double norm(const cmat &m, int p = 2);
224 
226 ITPP_EXPORT double norm(const mat &m, const std::string &s);
227 
229 ITPP_EXPORT double norm(const cmat &m, const std::string &s);
230 
231 
233 ITPP_EXPORT double variance(const cvec &v);
234 
236 template<class T>
237 double variance(const Vec<T> &v)
238 {
239  int len = v.size();
240  const T *p = v._data();
241  double sum = 0.0, sq_sum = 0.0;
242 
243  for (int i = 0; i < len; i++, p++) {
244  sum += *p;
245  sq_sum += *p * *p;
246  }
247 
248  return (double)(sq_sum - sum*sum / len) / (len - 1);
249 }
250 
252 template<class T>
253 double energy(const Vec<T> &v)
254 {
255  return sqr(norm(v));
256 }
257 
258 
260 inline bool within_tolerance(double x, double xref, double tol = 1e-14)
261 {
262  return (fabs(x -xref) <= tol) ? true : false;
263 }
264 
266 inline bool within_tolerance(std::complex<double> x, std::complex<double> xref, double tol = 1e-14)
267 {
268  return (abs(x -xref) <= tol) ? true : false;
269 }
270 
272 inline bool within_tolerance(const vec &x, const vec &xref, double tol = 1e-14)
273 {
274  return (max(abs(x -xref)) <= tol) ? true : false;
275 }
276 
278 inline bool within_tolerance(const cvec &x, const cvec &xref, double tol = 1e-14)
279 {
280  return (max(abs(x -xref)) <= tol) ? true : false;
281 }
282 
284 inline bool within_tolerance(const mat &X, const mat &Xref, double tol = 1e-14)
285 {
286  return (max(max(abs(X -Xref))) <= tol) ? true : false;
287 }
288 
290 inline bool within_tolerance(const cmat &X, const cmat &Xref, double tol = 1e-14)
291 {
292  return (max(max(abs(X -Xref))) <= tol) ? true : false;
293 }
294 
306 ITPP_EXPORT double moment(const vec &x, const int r);
307 
336 ITPP_EXPORT double skewness(const vec &x);
337 
338 
364 ITPP_EXPORT double kurtosisexcess(const vec &x);
365 
379 inline double kurtosis(const vec &x) {return kurtosisexcess(x) + 3;}
380 
382 
383 } // namespace itpp
384 
385 #endif // #ifndef MISC_STAT_H
SourceForge Logo

Generated on Sat Jul 6 2013 10:54:25 for IT++ by Doxygen 1.8.2