IT++ Logo

misc_stat.h

Go to the documentation of this file.
00001 
00030 #ifndef MISC_STAT_H
00031 #define MISC_STAT_H
00032 
00033 #include <itpp/base/math/min_max.h>
00034 #include <itpp/base/mat.h>
00035 #include <itpp/base/math/elem_math.h>
00036 #include <itpp/base/matfunc.h>
00037 
00038 
00039 namespace itpp
00040 {
00041 
00044 
00048 class Stat
00049 {
00050 public:
00052   Stat() {clear();}
00054   virtual ~Stat() {}
00055 
00057   virtual void clear() {
00058     _n_overflows = 0;
00059     _n_samples = 0;
00060     _n_zeros = 0;
00061     _max = 0.0;
00062     _min = 0.0;
00063     _sqr_sum = 0.0;
00064     _sum = 0.0;
00065   }
00066 
00068   virtual void sample(const double s, const bool overflow = false) {
00069     _n_samples++;
00070     _sum += s;
00071     _sqr_sum += s * s;
00072     if (s < _min) _min = s;
00073     if (s > _max) _max = s;
00074     if (overflow) _n_overflows++;
00075     if (s == 0.0) _n_zeros++;
00076   }
00077 
00079   int n_overflows() const {return _n_overflows;}
00081   int n_samples() const {return _n_samples;}
00083   int n_zeros() const {return _n_zeros;}
00085   double avg() const {return _sum / _n_samples;}
00087   double max() const {return _max;}
00089   double min() const {return _min;}
00091   double sigma() const {
00092     double sigma2 = _sqr_sum / _n_samples - avg() * avg();
00093     return std::sqrt(sigma2 < 0 ? 0 : sigma2);
00094   }
00096   double sqr_sum() const {return _sqr_sum;}
00098   double sum() const {return _sum;}
00100   vec histogram() const {return vec(0);}
00101 
00102 protected:
00104   int _n_overflows;
00106   int _n_samples;
00108   int _n_zeros;
00110   double _max;
00112   double _min;
00114   double _sqr_sum;
00116   double _sum;
00117 };
00118 
00119 
00121 double mean(const vec &v);
00123 std::complex<double> mean(const cvec &v);
00125 double mean(const svec &v);
00127 double mean(const ivec &v);
00129 double mean(const mat &m);
00131 std::complex<double> mean(const cmat &m);
00133 double mean(const smat &m);
00135 double mean(const imat &m);
00136 
00138 template<class T>
00139 double geometric_mean(const Vec<T> &v)
00140 {
00141   return std::exp(std::log(static_cast<double>(prod(v))) / v.length());
00142 }
00143 
00145 template<class T>
00146 double geometric_mean(const Mat<T> &m)
00147 {
00148   return std::exp(std::log(static_cast<double>(prod(prod(m))))
00149                   / (m.rows() * m.cols()));
00150 }
00151 
00153 template<class T>
00154 double median(const Vec<T> &v)
00155 {
00156   Vec<T> invect(v);
00157   sort(invect);
00158   return (double)(invect[(invect.length()-1)/2] + invect[invect.length()/2]) / 2.0;
00159 }
00160 
00162 double norm(const cvec &v);
00163 
00165 template<class T>
00166 double norm(const Vec<T> &v)
00167 {
00168   double E = 0.0;
00169   for (int i = 0; i < v.size(); i++)
00170     E += sqr(static_cast<double>(v[i]));
00171 
00172   return std::sqrt(E);
00173 }
00174 
00176 double norm(const cvec &v, int p);
00177 
00179 template<class T>
00180 double norm(const Vec<T> &v, int p)
00181 {
00182   double E = 0.0;
00183   for (int i = 0; i < v.size(); i++)
00184     E += std::pow(fabs(static_cast<double>(v[i])), static_cast<double>(p));
00185 
00186   return std::pow(E, 1.0 / p);
00187 }
00188 
00190 double norm(const cvec &v, const std::string &s);
00191 
00193 template<class T>
00194 double norm(const Vec<T> &v, const std::string &s)
00195 {
00196   it_assert(s == "fro", "norm(): Unrecognised norm");
00197 
00198   double E = 0.0;
00199   for (int i = 0; i < v.size(); i++)
00200     E += sqr(static_cast<double>(v[i]));
00201 
00202   return std::sqrt(E);
00203 }
00204 
00213 double norm(const mat &m, int p = 2);
00214 
00223 double norm(const cmat &m, int p = 2);
00224 
00226 double norm(const mat &m, const std::string &s);
00227 
00229 double norm(const cmat &m, const std::string &s);
00230 
00231 
00233 double variance(const cvec &v);
00234 
00236 template<class T>
00237 double variance(const Vec<T> &v)
00238 {
00239   int len = v.size();
00240   const T *p = v._data();
00241   double sum = 0.0, sq_sum = 0.0;
00242 
00243   for (int i = 0; i < len; i++, p++) {
00244     sum += *p;
00245     sq_sum += *p * *p;
00246   }
00247 
00248   return (double)(sq_sum - sum*sum / len) / (len - 1);
00249 }
00250 
00252 template<class T>
00253 double energy(const Vec<T> &v)
00254 {
00255   return sqr(norm(v));
00256 }
00257 
00258 
00260 inline bool within_tolerance(double x, double xref, double tol = 1e-14)
00261 {
00262   return (fabs(x -xref) <= tol) ? true : false;
00263 }
00264 
00266 inline bool within_tolerance(std::complex<double> x, std::complex<double> xref, double tol = 1e-14)
00267 {
00268   return (abs(x -xref) <= tol) ? true : false;
00269 }
00270 
00272 inline bool within_tolerance(const vec &x, const vec &xref, double tol = 1e-14)
00273 {
00274   return (max(abs(x -xref)) <= tol) ? true : false;
00275 }
00276 
00278 inline bool within_tolerance(const cvec &x, const cvec &xref, double tol = 1e-14)
00279 {
00280   return (max(abs(x -xref)) <= tol) ? true : false;
00281 }
00282 
00284 inline bool within_tolerance(const mat &X, const mat &Xref, double tol = 1e-14)
00285 {
00286   return (max(max(abs(X -Xref))) <= tol) ? true : false;
00287 }
00288 
00290 inline bool within_tolerance(const cmat &X, const cmat &Xref, double tol = 1e-14)
00291 {
00292   return (max(max(abs(X -Xref))) <= tol) ? true : false;
00293 }
00294 
00306 double moment(const vec &x, const int r);
00307 
00336 double skewness(const vec &x);
00337 
00338 
00364 double kurtosisexcess(const vec &x);
00365 
00379 inline double kurtosis(const vec &x) {return kurtosisexcess(x) + 3;}
00380 
00382 
00383 } // namespace itpp
00384 
00385 #endif // #ifndef MISC_STAT_H
SourceForge Logo

Generated on Thu Apr 23 20:06:48 2009 for IT++ by Doxygen 1.5.8