contrib/brl/bbas/bsta/bsta_histogram.h
Go to the documentation of this file.
00001 // This is brl/bbas/bsta/bsta_histogram.h
00002 #ifndef bsta_histogram_h_
00003 #define bsta_histogram_h_
00004 //:
00005 // \file
00006 // \brief A simple histogram class
00007 // \author Joseph Mundy
00008 // \date May 19, 2004
00009 //
00010 // A templated histogram class.  Supports entropy calculations
00011 //
00012 // \verbatim
00013 //  J.L. Mundy added min,max, percentile methods
00014 //  B.A. Mayer add clear() method so a single instance can revert
00015 //             to the default constructor to be reused.
00016 // \endverbatim
00017 
00018 #include <vcl_vector.h>
00019 #include <vcl_cassert.h>
00020 #include <vcl_iostream.h>
00021 #include <bsta/bsta_histogram_base.h>
00022 template <class T> class bsta_histogram : public bsta_histogram_base
00023 {
00024  public:
00025   //: Default constructor
00026   bsta_histogram();
00027 
00028   //:Simple constructor that assumes all data values are positive
00029   bsta_histogram(const T range, const unsigned int nbins,
00030                  const T min_prob = 0.0);
00031 
00032   //:More general constructor defining a signed value range
00033   bsta_histogram(const T min, const T max, const unsigned int nbins,
00034                  const T min_prob = 0.0);
00035 
00036   //:More general constructor specifying bin interval, delta
00037   bsta_histogram(const unsigned int nbins, const T min, const T delta, 
00038                  const T min_prob = 0.0);
00039 
00040   //:construct from other histogram data
00041   bsta_histogram(const T min, const T max, vcl_vector<T> const& data,
00042                  const T min_prob = 0.0);
00043 
00044   ~bsta_histogram() {}
00045 
00046   // The number of bins in the histogram
00047   unsigned int nbins() const { return nbins_; }
00048 
00049   //: min,max of total range
00050   T min() const {return min_;}
00051   T max() const {return max_;}
00052 
00053   //: bin interval
00054   T delta() const {return delta_;}
00055 
00056   //: minimum probability
00057   T min_prob() const {return min_prob_;}
00058 
00059   //: The value range for a bin
00060   void value_range(const unsigned int bin, T& vmin, T& vmax) const
00061   { assert(bin<nbins_); vmin = bin*delta_+min_; vmax = (bin+1)*delta_+min_; }
00062 
00063   //: The average value for a bin
00064   T avg_bin_value(const unsigned int bin) const
00065   { assert(bin<nbins_); return min_ + bin*delta_ + delta_/2; }
00066 
00067   //: The counts in a given bin
00068   T counts(const unsigned int bin) const
00069   { assert(bin<nbins_); return counts_[bin]; }
00070 
00071   //: probability of a given bin
00072   T p(const unsigned int bin) const;
00073 
00074   //: probability of a value in the range
00075   T p(const T value) const;
00076 
00077   //: Total area under the histogram
00078   T area() const;
00079   
00080   //: The area under the histogram up to (excluding) the given bin
00081   T cumulative_area(unsigned bin) const;
00082 
00083   //: Mean of distribution
00084   T mean() const;
00085 
00086   //: Mean of distribution between bin indices
00087   T mean(const unsigned int lowbin, const unsigned int highbin) const;
00088 
00089   //: Mean of distribution between values
00090   T mean_vals(const T low, const T high) const;
00091 
00092   //: Variance of distribution
00093   T variance() const;
00094 
00095   //: Variance of distribution between bin indices
00096   T variance(const unsigned int lowbin, const unsigned int highbin) const;
00097 
00098   //: Variance of distribution between values
00099   T variance_vals(const T low, const T high) const;
00100 
00101   //: First non-zero bin from below
00102   unsigned low_bin();
00103 
00104   //: First non-zero bin from above
00105   unsigned high_bin();
00106 
00107   //: Fraction of area less than val
00108   T fraction_below(const T value) const;
00109 
00110   //: Fraction of area greater than val
00111   T fraction_above(const T value) const;
00112 
00113   //: Value for area fraction below value
00114   T value_with_area_below(const T area_fraction) const;
00115 
00116   //: Value for area fraction below value
00117   T value_with_area_above(const T area_fraction) const;
00118 
00119   //: Entropy of p(x)
00120   T entropy() const;
00121 
00122   //: Renyi alpha = 2 entropy of p(x)
00123   T renyi_entropy() const;
00124 
00125  //: Increase the count of the bin corresponding to val by mag
00126   void upcount(T val, T mag);
00127   
00128   //: Return the bin this element would fall on - it doesn't modify the current count
00129   int bin_at_val(T val);
00130 
00131   //: set the count for a given bin
00132   void set_count(const unsigned bin, const T count)
00133   { if (bin<nbins_){ counts_[bin]=count; area_valid_ = false;}}
00134 
00135   //: array of bin values
00136   vcl_vector<T> value_array() const {
00137     vcl_vector<T> v(nbins_);
00138     for (unsigned b = 0; b<nbins_; ++b) v[b]=avg_bin_value(b); return v; }
00139 
00140   //: array of bin counts
00141   vcl_vector<T> count_array() const {
00142     vcl_vector<T> v(nbins_);
00143     for (unsigned b = 0; b<nbins_; ++b) v[b]=counts(b); return v; }
00144 
00145  //: Smooth the histogram with a Parzen window of sigma
00146   void parzen(const T sigma);
00147 
00148   //: Write to stream
00149   vcl_ostream& write(vcl_ostream&) const;
00150 
00151   //: Read from stream
00152   vcl_istream& read(vcl_istream&);
00153 
00154   void pretty_print(vcl_ostream& os = vcl_cout) const;
00155   
00156   void print(vcl_ostream& os = vcl_cout) const;
00157 
00158   //: print as a matlab plot command
00159   void print_to_m(vcl_ostream& os = vcl_cout) const;
00160   
00161   //: print x and y arrays
00162   void print_to_arrays(vcl_ostream& os = vcl_cout) const;
00163 
00164   //: print values and bin probability in full (even if counts ==0)
00165   void print_vals_prob(vcl_ostream& os = vcl_cout) const;
00166 
00167   //:restore default constructor state.
00168   void clear();
00169 
00170  private:
00171   void compute_area() const; // mutable const
00172   mutable bool area_valid_;
00173   mutable T area_;
00174   unsigned int nbins_;
00175   T range_;
00176   T delta_;
00177   T min_prob_;
00178   T min_;
00179   T max_;
00180   vcl_vector<T> counts_;
00181 };
00182 
00183 //: Write histogram to stream
00184 // \relatesalso bsta_histogram
00185 template <class T>
00186 vcl_ostream&  operator<<(vcl_ostream& s, bsta_histogram<T> const& h);
00187 
00188 //: Read histogram from stream
00189 // \relatesalso bsta_histogram
00190 template <class T>
00191 vcl_istream&  operator>>(vcl_istream& is,  bsta_histogram<T>& h);
00192 
00193 //: Forward declaration of specialization
00194 template <>
00195 void bsta_histogram<char>::pretty_print(vcl_ostream& os) const;
00196 #include <bsta/bsta_histogram_sptr.h>
00197 #define BSTA_HISTOGRAM_INSTANTIATE(T) extern "Please #include <bsta/bsta_histogram.txx>"
00198 
00199 #endif // bsta_histogram_h_