contrib/brl/bbas/bsta/bsta_joint_histogram.h
Go to the documentation of this file.
00001 // This is brl/bbas/bsta/bsta_joint_histogram.h
00002 #ifndef bsta_joint_histogram_h_
00003 #define bsta_joint_histogram_h_
00004 //:
00005 // \file
00006 // \brief A simple joint_histogram class
00007 // \author Joseph L. Mundy
00008 // \date   May 19, 2004
00009 //
00010 // A templated joint_histogram class.  Supports entropy calculations
00011 //
00012 // \verbatim
00013 //  Modifications
00014 //   06/01/2010  Brandon A. Mayer. Added clear() function so that a single joint histogram 
00015 //               instance may revert to the default constructor and be reused.
00016 //   06/01/2010  Brandon A. Mayer. Added mutual_information() function
00017 // \endverbatim
00018 
00019 #include <vbl/vbl_array_2d.h>
00020 #include <vcl_vector.h>
00021 #include <vcl_iostream.h>
00022 #include <bsta/bsta_joint_histogram_base.h>
00023 
00024 template <class T> class bsta_joint_histogram : public bsta_joint_histogram_base
00025 {
00026  public:
00027   bsta_joint_histogram();
00028 
00029   bsta_joint_histogram(const T range, const unsigned int nbins,
00030                        const T min_prob = 0.0);
00031 
00032   bsta_joint_histogram(const T range_a, const unsigned int nbins_a,
00033                        const T range_b, const unsigned int nbins_b, 
00034                        const T min_prob = 0.0);
00035   //:More general constructor defining a signed value range
00036   bsta_joint_histogram(const T min_a, const T max_a,
00037                        const unsigned int nbins_a,
00038                        const T min_b, const T max_b,
00039                        const unsigned int nbins_b,
00040                        const T min_prob = 0.0);
00041 
00042   ~bsta_joint_histogram() {}
00043 
00044   //: legacy use where a and b have the same bin granularity
00045   unsigned int nbins() const { return nbins_a_; }
00046 
00047   //: number of bins for variable a
00048   unsigned int nbins_a() const {return nbins_a_;}
00049   //: number of bins for variable b
00050   unsigned int nbins_b() const {return nbins_b_;}
00051 
00052   //: legacy use where a and b have the same range
00053   T range() const {return range_a_;}
00054 
00055   //: range for variable a
00056   T range_a() const {return range_a_;}  
00057   //: range for variable b
00058   T range_b() const {return range_b_;}
00059   //: min value for variable a
00060   T min_a() const {return min_a_;}
00061   //: max value for variable a
00062   T max_a() const {return max_a_;}
00063   //: min value for variable b
00064   T min_b() const {return min_b_;}
00065   //: max value for variable b
00066   T max_b() const {return max_b_;}
00067   //: delta value for variable a
00068   T delta_a() const { return delta_a_; }
00069   //: delta value for variable b
00070   T delta_b() const { return delta_b_; }
00071 
00072   T min_prob() const {return min_prob_;}
00073 
00074   vbl_array_2d<T> counts() const {return counts_;}
00075 
00076   void upcount(T a, T mag_a,
00077                T b, T mag_b);
00078   void parzen(const T sigma);
00079 
00080   //: access by bin index
00081   T p(unsigned int a, unsigned int b) const;
00082 
00083   //: access by value
00084   T p(T a, T b) const;
00085 
00086   T volume() const;
00087   T entropy() const;
00088   T mutual_information() const;
00089   T renyi_entropy() const;
00090   T entropy_marginal_a() const;
00091   
00092   void print(vcl_ostream& os = vcl_cout) const;
00093 
00094   //: The average and variance bin value for row a using counts to compute probs
00095   bool avg_and_variance_bin_for_row_a(const unsigned int a, T & avg, T & var) const;
00096 
00097   void set_count(unsigned r, unsigned c, T cnt)
00098     { if (r<static_cast<unsigned>(counts_.rows())&&
00099           c<static_cast<unsigned>(counts_.cols()))
00100       counts_[r][c]=cnt;
00101     }
00102 
00103   //:access by index
00104   T get_count(unsigned r, unsigned c) const
00105     { if (r<static_cast<unsigned>(counts_.rows())&&
00106           c<static_cast<unsigned>(counts_.cols()))
00107       return counts_[r][c];
00108     else
00109       return T(0);
00110     }
00111   //:access by value
00112   T get_count(T a, T b) const;
00113 
00114   void print_to_vrml(vcl_ostream& os) const;
00115   void print_to_m(vcl_ostream& os) const;
00116   void print_to_text(vcl_ostream& os) const;
00117 
00118   //:restore to default constructor state
00119   void clear();
00120 
00121  private:
00122   void compute_volume() const; // mutable const
00123   mutable bool volume_valid_;
00124   mutable T volume_;
00125   unsigned int nbins_a_, nbins_b_;
00126   T range_a_, range_b_;
00127   T delta_a_, delta_b_;
00128   T min_a_, max_a_;
00129   T min_b_, max_b_;
00130   T min_prob_;
00131   vbl_array_2d<T> counts_;
00132 };
00133 #include <bsta/bsta_joint_histogram_sptr.h>
00134 #define BSTA_JOINT_HISTOGRAM_INSTANTIATE(T) extern "Please #include <bsta/bsta_joint_histogram.txx>"
00135 
00136 #endif // bsta_joint_histogram_h_