IT++ Logo

histogram.h

Go to the documentation of this file.
00001 
00030 #ifndef HISTOGRAM_H
00031 #define HISTOGRAM_H
00032 
00033 #include <itpp/base/mat.h>
00034 
00035 
00036 namespace itpp
00037 {
00038 
00041 
00075 template<typename Num_T>
00076 class Histogram
00077 {
00078 public:
00081   Histogram(Num_T from = Num_T(0), Num_T to = Num_T(99), int n_bins = 100);
00083   ~Histogram() {};
00084 
00086   void setup(Num_T from, Num_T to, int n_bins);
00087 
00089   void update(Num_T value);
00091   void update(Vec<Num_T> values);
00093   void update(Mat<Num_T> values);
00094 
00096   void reset() { trials_cnt = 0; bins.zeros(); };
00098   int get_bin(int ix) const { return bins(ix); };
00100   ivec get_bins() const { return bins; };
00102   Vec<Num_T> get_bin_centers() const { return center_vals; };
00104   Num_T get_bin_center(int ix) const { return center_vals(ix); };
00106   Vec<Num_T> get_bin_lefts() const { return lo_vals; };
00108   Num_T get_bin_left(int ix) const { return lo_vals(ix); };
00110   Vec<Num_T> get_bin_rights() const { return hi_vals; };
00112   Num_T get_bin_right(int ix) const { return hi_vals(ix); };
00113 
00115   vec get_pdf() const;
00117   vec get_cdf() const;
00118 
00120   int bins_num() const { return num_bins; };
00122   int trials_num() const {return trials_cnt;};
00123 
00124 private:
00126   int num_bins;
00128   Num_T step;
00130   Vec<Num_T> lo_vals;
00132   Vec<Num_T> hi_vals;
00134   Vec<Num_T> center_vals;
00136   ivec bins;
00138   int trials_cnt;
00139 };
00140 
00141 template<class Num_T>
00142 inline Histogram<Num_T>::Histogram(Num_T from, Num_T to, int n_bins)
00143 
00144 {
00145   setup(from, to, n_bins);
00146 }
00147 
00148 template<class Num_T>
00149 inline void Histogram<Num_T>::setup(Num_T from, Num_T to, int n_bins)
00150 {
00151   num_bins = n_bins;
00152   lo_vals.set_size(n_bins);
00153   hi_vals.set_size(n_bins);
00154   center_vals.set_size(n_bins);
00155   bins.set_size(n_bins);
00156   trials_cnt = 0;
00157   step = (to - from) / (num_bins - 1);
00158   center_vals = linspace(from, to, num_bins);
00159   lo_vals = center_vals - step / 2;
00160   hi_vals = center_vals + step / 2;
00161   reset();
00162 }
00163 
00164 template<class Num_T>
00165 inline void Histogram<Num_T>::update(Num_T value)
00166 {
00167   // search for the corresponding bin using dichotomy approach
00168   int start = 0;
00169   int end = num_bins - 1;
00170   int test = (start + end) / 2;
00171 
00172   while (start < end) {
00173     if (value < lo_vals(test))
00174       end = test - 1;
00175     else if (value >= hi_vals(test))
00176       start = test + 1;
00177     else
00178       break;
00179     test = (start + end) / 2;
00180   };
00181 
00182   bins(test)++;
00183   trials_cnt++;
00184 }
00185 
00186 template<class Num_T>
00187 inline void Histogram<Num_T>::update(Vec<Num_T> values)
00188 {
00189   for (int i = 0; i < values.length(); i++)
00190     update(values(i));
00191 }
00192 
00193 template<class Num_T>
00194 inline void Histogram<Num_T>::update(Mat<Num_T> values)
00195 {
00196   for (int i = 0; i < values.rows(); i++)
00197     for (int j = 0; j < values.cols(); j++)
00198       update(values(i, j));
00199 }
00200 
00201 template<class Num_T>
00202 inline vec Histogram<Num_T>::get_pdf() const
00203 {
00204   vec pdf(num_bins);
00205   for (int j = 0; j < num_bins; j++)
00206     pdf(j) = static_cast<double>(bins(j)) / trials_cnt;
00207   return pdf;
00208 }
00209 
00210 template<class Num_T>
00211 inline vec Histogram<Num_T>::get_cdf() const
00212 {
00213   ivec tmp = cumsum(bins);
00214   vec cdf(num_bins);
00215   for (int j = 0; j < num_bins; j++)
00216     cdf(j) = static_cast<double>(tmp(j)) / trials_cnt;
00217   return cdf;
00218 }
00219 
00221 
00222 } // namespace itpp
00223 
00224 #endif // #ifndef HISTOGRAM_H
SourceForge Logo

Generated on Fri Aug 14 15:28:29 2009 for IT++ by Doxygen 1.5.9