00001 00029 #ifndef MISC_STAT_H 00030 #define MISC_STAT_H 00031 00032 #include <itpp/base/math/min_max.h> 00033 #include <itpp/base/mat.h> 00034 #include <itpp/base/math/elem_math.h> 00035 #include <itpp/base/matfunc.h> 00036 00037 00038 namespace itpp 00039 { 00040 00043 00047 class Stat 00048 { 00049 public: 00051 Stat() {clear();} 00053 virtual ~Stat() {} 00054 00056 virtual void clear() { 00057 _n_overflows = 0; 00058 _n_samples = 0; 00059 _n_zeros = 0; 00060 _max = 0.0; 00061 _min = 0.0; 00062 _sqr_sum = 0.0; 00063 _sum = 0.0; 00064 } 00065 00067 virtual void sample(const double s, const bool overflow = false) { 00068 _n_samples++; 00069 _sum += s; 00070 _sqr_sum += s * s; 00071 if (s < _min) _min = s; 00072 if (s > _max) _max = s; 00073 if (overflow) _n_overflows++; 00074 if (s == 0.0) _n_zeros++; 00075 } 00076 00078 int n_overflows() const {return _n_overflows;} 00080 int n_samples() const {return _n_samples;} 00082 int n_zeros() const {return _n_zeros;} 00084 double avg() const {return _sum / _n_samples;} 00086 double max() const {return _max;} 00088 double min() const {return _min;} 00090 double sigma() const { 00091 double sigma2 = _sqr_sum / _n_samples - avg() * avg(); 00092 return std::sqrt(sigma2 < 0 ? 0 : sigma2); 00093 } 00095 double sqr_sum() const {return _sqr_sum;} 00097 double sum() const {return _sum;} 00099 vec histogram() const {return vec(0);} 00100 00101 protected: 00103 int _n_overflows; 00105 int _n_samples; 00107 int _n_zeros; 00109 double _max; 00111 double _min; 00113 double _sqr_sum; 00115 double _sum; 00116 }; 00117 00118 00120 double mean(const vec &v); 00122 std::complex<double> mean(const cvec &v); 00124 double mean(const svec &v); 00126 double mean(const ivec &v); 00128 double mean(const mat &m); 00130 std::complex<double> mean(const cmat &m); 00132 double mean(const smat &m); 00134 double mean(const imat &m); 00135 00137 template<class T> 00138 double geometric_mean(const Vec<T> &v) 00139 { 00140 return std::exp(std::log(static_cast<double>(prod(v))) / v.length()); 00141 } 00142 00144 template<class T> 00145 double geometric_mean(const Mat<T> &m) 00146 { 00147 return std::exp(std::log(static_cast<double>(prod(prod(m)))) 00148 / (m.rows() * m.cols())); 00149 } 00150 00152 template<class T> 00153 double median(const Vec<T> &v) 00154 { 00155 Vec<T> invect(v); 00156 sort(invect); 00157 return (double)(invect[(invect.length()-1)/2] + invect[invect.length()/2]) / 2.0; 00158 } 00159 00161 double norm(const cvec &v); 00162 00164 template<class T> 00165 double norm(const Vec<T> &v) 00166 { 00167 double E = 0.0; 00168 for (int i = 0; i < v.size(); i++) 00169 E += sqr(static_cast<double>(v[i])); 00170 00171 return std::sqrt(E); 00172 } 00173 00175 double norm(const cvec &v, int p); 00176 00178 template<class T> 00179 double norm(const Vec<T> &v, int p) 00180 { 00181 double E = 0.0; 00182 for (int i = 0; i < v.size(); i++) 00183 E += std::pow(fabs(static_cast<double>(v[i])), static_cast<double>(p)); 00184 00185 return std::pow(E, 1.0 / p); 00186 } 00187 00189 double norm(const cvec &v, const std::string &s); 00190 00192 template<class T> 00193 double norm(const Vec<T> &v, const std::string &s) 00194 { 00195 it_assert(s == "fro", "norm(): Unrecognised norm"); 00196 00197 double E = 0.0; 00198 for (int i = 0; i < v.size(); i++) 00199 E += sqr(static_cast<double>(v[i])); 00200 00201 return std::sqrt(E); 00202 } 00203 00212 double norm(const mat &m, int p = 2); 00213 00222 double norm(const cmat &m, int p = 2); 00223 00225 double norm(const mat &m, const std::string &s); 00226 00228 double norm(const cmat &m, const std::string &s); 00229 00230 00232 double variance(const cvec &v); 00233 00235 template<class T> 00236 double variance(const Vec<T> &v) 00237 { 00238 int len = v.size(); 00239 const T *p = v._data(); 00240 double sum = 0.0, sq_sum = 0.0; 00241 00242 for (int i = 0; i < len; i++, p++) { 00243 sum += *p; 00244 sq_sum += *p * *p; 00245 } 00246 00247 return (double)(sq_sum - sum*sum / len) / (len - 1); 00248 } 00249 00251 template<class T> 00252 double energy(const Vec<T> &v) 00253 { 00254 return sqr(norm(v)); 00255 } 00256 00257 00259 inline bool within_tolerance(double x, double xref, double tol = 1e-14) 00260 { 00261 return (fabs(x -xref) <= tol) ? true : false; 00262 } 00263 00265 inline bool within_tolerance(std::complex<double> x, std::complex<double> xref, double tol = 1e-14) 00266 { 00267 return (abs(x -xref) <= tol) ? true : false; 00268 } 00269 00271 inline bool within_tolerance(const vec &x, const vec &xref, double tol = 1e-14) 00272 { 00273 return (max(abs(x -xref)) <= tol) ? true : false; 00274 } 00275 00277 inline bool within_tolerance(const cvec &x, const cvec &xref, double tol = 1e-14) 00278 { 00279 return (max(abs(x -xref)) <= tol) ? true : false; 00280 } 00281 00283 inline bool within_tolerance(const mat &X, const mat &Xref, double tol = 1e-14) 00284 { 00285 return (max(max(abs(X -Xref))) <= tol) ? true : false; 00286 } 00287 00289 inline bool within_tolerance(const cmat &X, const cmat &Xref, double tol = 1e-14) 00290 { 00291 return (max(max(abs(X -Xref))) <= tol) ? true : false; 00292 } 00293 00305 double moment(const vec &x, const int r); 00306 00335 double skewness(const vec &x); 00336 00337 00363 double kurtosisexcess(const vec &x); 00364 00378 inline double kurtosis(const vec &x) {return kurtosisexcess(x) + 3;} 00379 00381 00382 } // namespace itpp 00383 00384 #endif // #ifndef MISC_STAT_H
Generated on Wed Jul 27 2011 16:27:05 for IT++ by Doxygen 1.7.4