IT++ Logo
misc_stat.h
Go to the documentation of this file.
1
29#ifndef MISC_STAT_H
30#define MISC_STAT_H
31
33#include <itpp/base/mat.h>
35#include <itpp/base/matfunc.h>
36#include <itpp/itexports.h>
37
38
39namespace itpp
40{
41
44
49{
50public:
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
102protected:
110 double _max;
112 double _min;
114 double _sqr_sum;
116 double _sum;
117};
118
119
121ITPP_EXPORT double mean(const vec &v);
123ITPP_EXPORT std::complex<double> mean(const cvec &v);
125ITPP_EXPORT double mean(const svec &v);
127ITPP_EXPORT double mean(const ivec &v);
129ITPP_EXPORT double mean(const mat &m);
131ITPP_EXPORT std::complex<double> mean(const cmat &m);
133ITPP_EXPORT double mean(const smat &m);
135ITPP_EXPORT double mean(const imat &m);
136
138template<class T>
139double geometric_mean(const Vec<T> &v)
140{
141 return std::exp(std::log(static_cast<double>(prod(v))) / v.length());
142}
143
145template<class T>
146double 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
153template<class T>
154double 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
162ITPP_EXPORT double norm(const cvec &v);
163
165template<class T>
166double 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
176ITPP_EXPORT double norm(const cvec &v, int p);
177
179template<class T>
180double 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
190ITPP_EXPORT double norm(const cvec &v, const std::string &s);
191
193template<class T>
194double 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
213ITPP_EXPORT double norm(const mat &m, int p = 2);
214
223ITPP_EXPORT double norm(const cmat &m, int p = 2);
224
226ITPP_EXPORT double norm(const mat &m, const std::string &s);
227
229ITPP_EXPORT double norm(const cmat &m, const std::string &s);
230
231
233ITPP_EXPORT double variance(const cvec &v);
234
236template<class T>
237double 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
252template<class T>
253double energy(const Vec<T> &v)
254{
255 return sqr(norm(v));
256}
257
258
260inline bool within_tolerance(double x, double xref, double tol = 1e-14)
261{
262 return (fabs(x -xref) <= tol) ? true : false;
263}
264
266inline 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
272inline 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
278inline 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
284inline 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
290inline 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
306ITPP_EXPORT double moment(const vec &x, const int r);
307
336ITPP_EXPORT double skewness(const vec &x);
337
338
364ITPP_EXPORT double kurtosisexcess(const vec &x);
365
379inline double kurtosis(const vec &x) {return kurtosisexcess(x) + 3;}
380
382
383} // namespace itpp
384
385#endif // #ifndef MISC_STAT_H
General array class.
Definition array.h:105
int size() const
Returns the number of data elements in the array object.
Definition array.h:155
int length() const
Returns the number of data elements in the array object.
Definition array.h:157
A class for sampling a signal and calculating statistics.
Definition misc_stat.h:49
int _n_zeros
Number of zero samples.
Definition misc_stat.h:108
int _n_overflows
Number of reported overflows.
Definition misc_stat.h:104
virtual void sample(const double s, const bool overflow=false)
Register a sample and flag for overflow.
Definition misc_stat.h:68
double _max
Maximum sample.
Definition misc_stat.h:110
double sqr_sum() const
Squared sum of all samples.
Definition misc_stat.h:96
double _sqr_sum
Squared sum of all samples.
Definition misc_stat.h:114
double avg() const
Average over all samples.
Definition misc_stat.h:85
vec histogram() const
Histogram over all samples (not implemented yet)
Definition misc_stat.h:100
int n_zeros() const
Number of zero samples.
Definition misc_stat.h:83
virtual ~Stat()
Destructor.
Definition misc_stat.h:54
int n_samples() const
Number of samples.
Definition misc_stat.h:81
double sigma() const
Standard deviation of all samples.
Definition misc_stat.h:91
int _n_samples
Number of samples.
Definition misc_stat.h:106
double _sum
Sum of all samples.
Definition misc_stat.h:116
double _min
Minimum sample.
Definition misc_stat.h:112
virtual void clear()
Clear statistics.
Definition misc_stat.h:57
Stat()
Default constructor.
Definition misc_stat.h:52
double min() const
Minimum sample.
Definition misc_stat.h:89
int n_overflows() const
Number of reported overflows.
Definition misc_stat.h:79
double sum() const
Sum of all samples.
Definition misc_stat.h:98
double max() const
Maximum sample.
Definition misc_stat.h:87
Elementary mathematical functions - header file.
#define it_assert(t, s)
Abort if t is not true.
Definition itassert.h:94
T sum(const Vec< T > &v)
Sum of all elements in the vector.
Definition matfunc.h:59
T prod(const Vec< T > &v)
The product of all elements in the vector.
Definition matfunc.h:195
T max(const Vec< T > &v)
Maximum value of vector.
Definition min_max.h:45
vec sqr(const cvec &data)
Absolute square of elements.
Definition elem_math.cpp:36
double moment(const vec &x, const int r)
Calculate the central moment of vector x.
double energy(const Vec< T > &v)
Calculate the energy: squared 2-norm. energy(v)=sum(abs(v).^2)
Definition misc_stat.h:253
bool within_tolerance(double x, double xref, double tol=1e-14)
Return true if the input value x is within the tolerance tol of the reference value xref.
Definition misc_stat.h:260
double kurtosis(const vec &x)
Calculate the kurtosis of the input vector x.
Definition misc_stat.h:379
double skewness(const vec &x)
Calculate the skewness excess of the input vector x.
double kurtosisexcess(const vec &x)
Calculate the kurtosis excess of the input vector x.
double geometric_mean(const Vec< T > &v)
The geometric mean of a vector.
Definition misc_stat.h:139
double norm(const cvec &v)
Calculate the 2-norm: norm(v)=sqrt(sum(abs(v).^2))
Definition misc_stat.cpp:77
double median(const Vec< T > &v)
The median.
Definition misc_stat.h:154
double variance(const cvec &v)
The variance of the elements in the vector. Normalized with N-1 to be unbiased.
double mean(const vec &v)
The mean value.
Definition misc_stat.cpp:36
Matrix Class Definitions.
Various functions on vectors and matrices - header file.
Minimum and maximum functions on vectors and matrices.
itpp namespace
Definition itmex.h:37
bin abs(const bin &inbin)
absolute value of bin
Definition binary.h:174

Generated on Tue Mar 26 2024 19:08:31 for IT++ by Doxygen 1.9.8