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 
00043 
00047   class Stat {
00048   public:
00050     Stat() {clear();}
00052     virtual ~Stat() {}
00053 
00055     virtual void clear()
00056     {
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     {
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     {
00093       double sigma2 = _sqr_sum/_n_samples - avg()*avg();
00094       return std::sqrt(sigma2 < 0 ? 0 : sigma2);
00095     }
00097     double sqr_sum() const {return _sqr_sum;}
00099     double sum() const {return _sum;}
00101     vec histogram() const {return vec(0);}
00102 
00103   protected:
00105     int _n_overflows;
00107     int _n_samples;
00109     int _n_zeros;
00111     double _max;
00113     double _min;
00115     double _sqr_sum;
00117     double _sum;
00118   };
00119 
00120 
00122   double mean(const vec &v);
00124   std::complex<double> mean(const cvec &v);
00126   double mean(const svec &v);
00128   double mean(const ivec &v);
00130   double mean(const mat &m);
00132   std::complex<double> mean(const cmat &m);
00134   double mean(const smat &m);
00136   double mean(const imat &m);
00137 
00139   template<class T>
00140   double geometric_mean(const Vec<T> &v)
00141   {
00142     return std::exp(std::log(static_cast<double>(prod(v))) / v.length());
00143   }
00144 
00146   template<class T>
00147   double geometric_mean(const Mat<T> &m)
00148   {
00149     return std::exp(std::log(static_cast<double>(prod(prod(m))))
00150                     / (m.rows() * m.cols()));
00151   }
00152 
00154   template<class T>
00155     double median(const Vec<T> &v)
00156   {
00157     Vec<T> invect(v);
00158     sort(invect);
00159     return (double)(invect[(invect.length()-1)/2]+invect[invect.length()/2])/2.0;
00160   }
00161 
00163   double norm(const cvec &v);
00164 
00166   template<class T>
00167   double norm(const Vec<T> &v)
00168   {
00169     double E = 0.0;
00170     for (int i = 0; i < v.size(); i++)
00171       E += sqr(static_cast<double>(v[i]));
00172 
00173     return std::sqrt(E);
00174   }
00175 
00177   double norm(const cvec &v, int p);
00178 
00180   template<class T>
00181   double norm(const Vec<T> &v, int p)
00182   {
00183     double E = 0.0;
00184     for (int i = 0; i < v.size(); i++)
00185       E += std::pow(fabs(static_cast<double>(v[i])), static_cast<double>(p));
00186 
00187     return std::pow(E, 1.0 / p);
00188   }
00189 
00191   double norm(const cvec &v, const std::string &s);
00192 
00194   template<class T>
00195   double norm(const Vec<T> &v, const std::string &s)
00196   {
00197     it_assert(s == "fro", "norm(): Unrecognised norm");
00198 
00199     double E = 0.0;
00200     for (int i = 0; i < v.size(); i++)
00201       E += sqr(static_cast<double>(v[i]));
00202 
00203     return std::sqrt(E);
00204   }
00205 
00214   double norm(const mat &m, int p = 2);
00215 
00224   double norm(const cmat &m, int p = 2);
00225 
00227   double norm(const mat &m, const std::string &s);
00228 
00230   double norm(const cmat &m, const std::string &s);
00231 
00232 
00234   double variance(const cvec &v);
00235 
00237   template<class T>
00238     double variance(const Vec<T> &v)
00239   {
00240     int len = v.size();
00241     const T *p=v._data();
00242     double sum=0.0, sq_sum=0.0;
00243 
00244     for (int i=0; i<len; i++, p++) {
00245       sum += *p;
00246       sq_sum += *p * *p;
00247     }
00248 
00249     return (double)(sq_sum - sum*sum/len) / (len-1);
00250   }
00251 
00253   template<class T>
00254     double energy(const Vec<T> &v)
00255   {
00256     return sqr(norm(v));
00257   }
00258 
00259 
00261   inline bool within_tolerance(double x, double xref, double tol = 1e-14)
00262   {
00263     return ( fabs(x-xref) <= tol ) ? true : false;
00264   }
00265 
00267   inline bool within_tolerance(std::complex<double> x, std::complex<double> xref, double tol = 1e-14)
00268   {
00269     return ( abs(x-xref) <= tol ) ? true : false;
00270   }
00271 
00273   inline bool within_tolerance(const vec &x, const vec &xref, double tol = 1e-14)
00274   {
00275     return ( max(abs(x-xref)) <= tol ) ? true : false;
00276   }
00277 
00279   inline bool within_tolerance(const cvec &x, const cvec &xref, double tol = 1e-14)
00280   {
00281     return ( max(abs(x-xref)) <= tol ) ? true : false;
00282   }
00283 
00285   inline bool within_tolerance(const mat &X, const mat &Xref, double tol = 1e-14)
00286   {
00287     return ( max(max(abs(X-Xref))) <= tol ) ? true : false;
00288   }
00289 
00291   inline bool within_tolerance(const cmat &X, const cmat &Xref, double tol = 1e-14)
00292   {
00293     return ( max(max(abs(X-Xref))) <= tol ) ? true : false;
00294   }
00295 
00307   double moment(const vec &x, const int r);
00308 
00337   double skewness(const vec &x);
00338 
00339 
00365   double kurtosisexcess(const vec &x);
00366 
00380   inline double kurtosis(const vec &x) {return kurtosisexcess(x)+3;}
00381 
00383 
00384 } // namespace itpp
00385 
00386 #endif // #ifndef MISC_STAT_H
SourceForge Logo

Generated on Mon Jan 7 22:28:59 2008 for IT++ by Doxygen 1.5.4