IT++ Logo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
37 
38 namespace itpp
39 {
40 
43 
47 class Stat
48 {
49 public:
51  Stat() {clear();}
53  virtual ~Stat() {}
54 
56  virtual void clear() {
57  _n_overflows = 0;
58  _n_samples = 0;
59  _n_zeros = 0;
60  _max = 0.0;
61  _min = 0.0;
62  _sqr_sum = 0.0;
63  _sum = 0.0;
64  }
65 
67  virtual void sample(const double s, const bool overflow = false) {
68  _n_samples++;
69  _sum += s;
70  _sqr_sum += s * s;
71  if (s < _min) _min = s;
72  if (s > _max) _max = s;
73  if (overflow) _n_overflows++;
74  if (s == 0.0) _n_zeros++;
75  }
76 
78  int n_overflows() const {return _n_overflows;}
80  int n_samples() const {return _n_samples;}
82  int n_zeros() const {return _n_zeros;}
84  double avg() const {return _sum / _n_samples;}
86  double max() const {return _max;}
88  double min() const {return _min;}
90  double sigma() const {
91  double sigma2 = _sqr_sum / _n_samples - avg() * avg();
92  return std::sqrt(sigma2 < 0 ? 0 : sigma2);
93  }
95  double sqr_sum() const {return _sqr_sum;}
97  double sum() const {return _sum;}
99  vec histogram() const {return vec(0);}
100 
101 protected:
107  int _n_zeros;
109  double _max;
111  double _min;
113  double _sqr_sum;
115  double _sum;
116 };
117 
118 
120 double mean(const vec &v);
122 std::complex<double> mean(const cvec &v);
124 double mean(const svec &v);
126 double mean(const ivec &v);
128 double mean(const mat &m);
130 std::complex<double> mean(const cmat &m);
132 double mean(const smat &m);
134 double mean(const imat &m);
135 
137 template<class T>
138 double geometric_mean(const Vec<T> &v)
139 {
140  return std::exp(std::log(static_cast<double>(prod(v))) / v.length());
141 }
142 
144 template<class T>
145 double geometric_mean(const Mat<T> &m)
146 {
147  return std::exp(std::log(static_cast<double>(prod(prod(m))))
148  / (m.rows() * m.cols()));
149 }
150 
152 template<class T>
153 double median(const Vec<T> &v)
154 {
155  Vec<T> invect(v);
156  sort(invect);
157  return (double)(invect[(invect.length()-1)/2] + invect[invect.length()/2]) / 2.0;
158 }
159 
161 double norm(const cvec &v);
162 
164 template<class T>
165 double norm(const Vec<T> &v)
166 {
167  double E = 0.0;
168  for (int i = 0; i < v.size(); i++)
169  E += sqr(static_cast<double>(v[i]));
170 
171  return std::sqrt(E);
172 }
173 
175 double norm(const cvec &v, int p);
176 
178 template<class T>
179 double norm(const Vec<T> &v, int p)
180 {
181  double E = 0.0;
182  for (int i = 0; i < v.size(); i++)
183  E += std::pow(fabs(static_cast<double>(v[i])), static_cast<double>(p));
184 
185  return std::pow(E, 1.0 / p);
186 }
187 
189 double norm(const cvec &v, const std::string &s);
190 
192 template<class T>
193 double norm(const Vec<T> &v, const std::string &s)
194 {
195  it_assert(s == "fro", "norm(): Unrecognised norm");
196 
197  double E = 0.0;
198  for (int i = 0; i < v.size(); i++)
199  E += sqr(static_cast<double>(v[i]));
200 
201  return std::sqrt(E);
202 }
203 
212 double norm(const mat &m, int p = 2);
213 
222 double norm(const cmat &m, int p = 2);
223 
225 double norm(const mat &m, const std::string &s);
226 
228 double norm(const cmat &m, const std::string &s);
229 
230 
232 double variance(const cvec &v);
233 
235 template<class T>
236 double variance(const Vec<T> &v)
237 {
238  int len = v.size();
239  const T *p = v._data();
240  double sum = 0.0, sq_sum = 0.0;
241 
242  for (int i = 0; i < len; i++, p++) {
243  sum += *p;
244  sq_sum += *p * *p;
245  }
246 
247  return (double)(sq_sum - sum*sum / len) / (len - 1);
248 }
249 
251 template<class T>
252 double energy(const Vec<T> &v)
253 {
254  return sqr(norm(v));
255 }
256 
257 
259 inline bool within_tolerance(double x, double xref, double tol = 1e-14)
260 {
261  return (fabs(x -xref) <= tol) ? true : false;
262 }
263 
265 inline bool within_tolerance(std::complex<double> x, std::complex<double> xref, double tol = 1e-14)
266 {
267  return (abs(x -xref) <= tol) ? true : false;
268 }
269 
271 inline bool within_tolerance(const vec &x, const vec &xref, double tol = 1e-14)
272 {
273  return (max(abs(x -xref)) <= tol) ? true : false;
274 }
275 
277 inline bool within_tolerance(const cvec &x, const cvec &xref, double tol = 1e-14)
278 {
279  return (max(abs(x -xref)) <= tol) ? true : false;
280 }
281 
283 inline bool within_tolerance(const mat &X, const mat &Xref, double tol = 1e-14)
284 {
285  return (max(max(abs(X -Xref))) <= tol) ? true : false;
286 }
287 
289 inline bool within_tolerance(const cmat &X, const cmat &Xref, double tol = 1e-14)
290 {
291  return (max(max(abs(X -Xref))) <= tol) ? true : false;
292 }
293 
305 double moment(const vec &x, const int r);
306 
335 double skewness(const vec &x);
336 
337 
363 double kurtosisexcess(const vec &x);
364 
378 inline double kurtosis(const vec &x) {return kurtosisexcess(x) + 3;}
379 
381 
382 } // namespace itpp
383 
384 #endif // #ifndef MISC_STAT_H
SourceForge Logo

Generated on Fri Mar 21 2014 17:14:14 for IT++ by Doxygen 1.8.1.2