contrib/mul/mbl/mbl_histogram.h
Go to the documentation of this file.
00001 #ifndef mbl_histogram_h_
00002 #define mbl_histogram_h_
00003 
00004 //:
00005 // \file
00006 // \brief Simple object to build histogram from supplied data.
00007 // \author Tim Cootes
00008 
00009 #include <vcl_iosfwd.h>
00010 #include <vsl/vsl_binary_io.h>
00011 #include <vcl_vector.h>
00012 
00013 //: Simple object to build histogram from supplied data.
00014 //  Assumes all 1D data can be cast to doubles
00015 //  One could perhaps re-write this, templated by object type, if really necessary.
00016 class mbl_histogram
00017 {
00018   //: Position of limits of each bin.
00019   //  Bin i is [bins_[i],bins_[i+1])
00020   vcl_vector<double> bins_;
00021 
00022   //: Number in each bin
00023   vcl_vector<int> freq_;
00024 
00025   //: Number below lowest bin
00026   int n_below_;
00027 
00028   //: Number above highest bin
00029   int n_above_;
00030 
00031   //: Total number of examples supplied
00032   int n_obs_;
00033  public:
00034   //: Construct with no bins
00035   mbl_histogram();
00036 
00037   //: Construct with given number of bins over given range
00038   mbl_histogram(double x_lo, double x_hi, int n_bins);
00039 
00040   //: Define number and size of bins
00041   void set_bins(double x_lo, double x_hi, int n_bins);
00042 
00043   //: Remove all data
00044   void clear();
00045 
00046   //: Add given observation
00047   void obs(double v);
00048 
00049   //: Number of bins
00050   int n_bins() const { return freq_.size(); }
00051 
00052   //: Position of limits of each bin.
00053   //  Bin i is [bins_[i],bins_[i+1])
00054   const vcl_vector<double>& bins() const { return bins_; }
00055 
00056   //: Number of observations
00057   int n_obs() const { return n_obs_;}
00058 
00059   //: Number in each bin
00060   const vcl_vector<int>& frequency() const { return freq_; }
00061 
00062   //: Number below lowest bin
00063   int n_below() const { return n_below_; }
00064 
00065   //: Number above highest bin
00066   int n_above() const { return n_above_; }
00067 
00068   //: Write out probabilities (freq/n_obs) to a named file
00069   //  Can then be plotted by your favorite tool
00070   //
00071   //  Format: (bin-centre) prob     (one per line)
00072   // \return true if successful
00073   bool write_probabilities(const char* path);
00074 
00075   //: Write out cumulative probability distribution to a named file
00076   //  Format: (bin-centre) sum_prob     (one per line)
00077   // \return true if successful
00078   bool write_cdf(const char* path);
00079 
00080   void print_summary(vcl_ostream& os) const;
00081   //: Version number for I/O
00082   short version_no() const;
00083   void b_write(vsl_b_ostream& bfs) const;
00084   void b_read(vsl_b_istream& bfs);
00085 
00086   //: Test for equality
00087   bool operator==(const mbl_histogram& s) const;
00088 };
00089 
00090 //: Binary file stream output operator for class reference
00091 void vsl_b_write(vsl_b_ostream& bfs, const mbl_histogram& histo);
00092 
00093 //: Binary file stream input operator for class reference
00094 void vsl_b_read(vsl_b_istream& bfs, mbl_histogram& histo);
00095 
00096 //: Stream output operator for class reference
00097 vcl_ostream& operator<<(vcl_ostream& os, const mbl_histogram& histo);
00098 
00099 //: Stream output operator for class reference
00100 void vsl_print_summary(vcl_ostream& os, const mbl_histogram& histo);
00101 
00102 #endif