contrib/mul/mbl/mbl_wt_histogram.h
Go to the documentation of this file.
00001 #ifndef mbl_wt_histogram_h_
00002 #define mbl_wt_histogram_h_
00003 
00004 //:
00005 // \file
00006 // \brief Simple object to build histogram from supplied data, with weights
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, with weights
00014 //  Each observation is supplied with a weight.  Record total weight
00015 //  in each bin of the histogram.
00016 class mbl_wt_histogram
00017 {
00018   //: Bin i is given by [xlo_+i*dx_,xlo_+(i+1)*dx_)
00019   double xlo_;
00020 
00021   //: Bin width. Bin i is given by [xlo_+i*dx_,xlo_+(i+1)*dx_)
00022   double dx_;
00023 
00024   //: Total weight in each bin
00025   vcl_vector<double> wt_sum_;
00026 
00027   //: Number below lowest bin
00028   double wt_below_;
00029 
00030   //: Number above highest bin
00031   double wt_above_;
00032 
00033   //: Total number of examples supplied
00034   int n_obs_;
00035 
00036   //: Total sum of weights supplied
00037   double total_wt_;
00038  public:
00039   //: Construct with no bins
00040   mbl_wt_histogram();
00041 
00042   //: Construct with given number of bins over given range
00043   mbl_wt_histogram(double x_lo, double x_hi, int n_bins);
00044 
00045   //: Define number and size of bins
00046   void set_bins(double x_lo, double x_hi, int n_bins);
00047 
00048   //: Remove all data
00049   void clear();
00050 
00051   //: Add given observation
00052   void obs(double v, double wt);
00053 
00054   //: Number of bins
00055   int n_bins() const { return wt_sum_.size(); }
00056 
00057   //: Bin width. Bin i is given by [bin_t0()+i*dx(),bin_t0()+(i+1)*dx())
00058   double xlo() const { return xlo_; }
00059 
00060   //: Bin width. Bin i is given by [bin_t0()+i*dx(),bin_t0()+(i+1)*dx())
00061   double dx() const { return dx_; }
00062 
00063   //: Number of observations
00064   int n_obs() const { return n_obs_;}
00065 
00066   //: Total sum of weights supplied
00067   double total_wt() const { return total_wt_; }
00068 
00069   //: Total weight in each bin
00070   const vcl_vector<double>& wt_sum() const { return wt_sum_; }
00071 
00072   //: Total weight below lowest bin (bin_t0())
00073   double wt_below() const { return wt_below_; }
00074 
00075   //: Total weight above highest bin (bin_t0()+(n_bins()-1) * dx())
00076   double wt_above() const { return wt_above_; }
00077 
00078   //: Write out probabilities (freq/wt) to a named file
00079   //  Can then be plotted by your favorite tool
00080   //
00081   //  Format: (bin-centre) prob     (one per line)
00082   // \return true if successful
00083   bool write_probabilities(const char* path);
00084 
00085   void print_summary(vcl_ostream& os) const;
00086   //: Version number for I/O
00087   short version_no() const;
00088   void b_write(vsl_b_ostream& bfs) const;
00089   void b_read(vsl_b_istream& bfs);
00090 
00091   //: Test for equality
00092   bool operator==(const mbl_wt_histogram& s) const;
00093 };
00094 
00095 //: Binary file stream output operator for class reference
00096 void vsl_b_write(vsl_b_ostream& bfs, const mbl_wt_histogram& histo);
00097 
00098 //: Binary file stream input operator for class reference
00099 void vsl_b_read(vsl_b_istream& bfs, mbl_wt_histogram& histo);
00100 
00101 //: Stream output operator for class reference
00102 vcl_ostream& operator<<(vcl_ostream& os, const mbl_wt_histogram& histo);
00103 
00104 //: Stream output operator for class reference
00105 void vsl_print_summary(vcl_ostream& os, const mbl_wt_histogram& histo);
00106 
00107 #endif