contrib/mul/clsfy/clsfy_binary_pdf_classifier.h
Go to the documentation of this file.
00001 // This is mul/clsfy/clsfy_binary_pdf_classifier.h
00002 // Copyright: (C) 2000 British Telecommunications PLC
00003 #ifndef clsfy_binary_pdf_classifier_h_
00004 #define clsfy_binary_pdf_classifier_h_
00005 //:
00006 // \file
00007 // \brief Describe a classifier based on a single pdf.
00008 // \author Ian Scott
00009 
00010 #include <clsfy/clsfy_classifier_base.h>
00011 #include <vpdfl/vpdfl_pdf_base.h>
00012 #include <vcl_cassert.h>
00013 #include <vcl_iosfwd.h>
00014 
00015 //:  Decisions are based on an explicit multivariate probability distribution
00016 class clsfy_binary_pdf_classifier : public clsfy_classifier_base
00017 {
00018  protected:
00019   //: The current distribution model
00020   vpdfl_pdf_base *pdf_;
00021 
00022   //: The value of log probability density marking the class boundary
00023   // Above this value the answer is 1, and below it is 0
00024   double log_prob_limit_;
00025 
00026  public:
00027 
00028   // default constructor
00029   clsfy_binary_pdf_classifier(): pdf_(0), log_prob_limit_(0.0) {}
00030 
00031   //: A useful constructor
00032   // Specify the log probability density limit
00033   clsfy_binary_pdf_classifier(const vpdfl_pdf_base &pdf,
00034                               double log_prob_limit):
00035       pdf_(pdf.clone()), log_prob_limit_(log_prob_limit) {}
00036 
00037   // Destructor
00038   ~clsfy_binary_pdf_classifier() { deleteStuff(); }
00039 
00040   //: Classify the input vector
00041   // Returns either class1 (Inside PDF mode) or class 0 (Outside PDF mode).
00042   virtual unsigned classify(const vnl_vector<double> &input) const;
00043 
00044   //: Return the probability the input being in class 0.
00045   // output(0) contains the probability that the input is in class 1
00046   virtual void class_probabilities(vcl_vector<double> &outputs, const vnl_vector<double> &input) const;
00047 
00048   //: Log likelihood of being in class 0, i.e. const + log(P(class=0|data)).
00049   // The constant is chosen such that the decision boundary is at logL ==0;
00050   // This function is intended for binary classifiers only.
00051   // logL is related to class probability as P(class=0|data) = exp(logL) / (1+exp(logL))
00052   double log_l(const vnl_vector<double> &input) const;
00053 
00054   //: Set the log probability density limit,
00055   // above which the inputs are in class 1.
00056   void set_log_prob_limit(double limit)
00057   {
00058     log_prob_limit_ = limit;
00059   }
00060 
00061   //: The log probability density limit,
00062   // above which the inputs are in class 1.
00063   double log_prob_limit() const
00064   {
00065     return log_prob_limit_;
00066   }
00067 
00068   //: Set the PDF.
00069   // The class takes its own deep copy of the PDF.
00070   void set_pdf(const vpdfl_pdf_base &pdf)
00071   {
00072     deleteStuff();
00073     pdf_ = pdf.clone();
00074   }
00075 
00076   bool has_pdf() const
00077   {
00078     return pdf_!=0;
00079   }
00080   //: Get the internal PDFs
00081   // The object will return a reference to the internal data.
00082   const vpdfl_pdf_base & pdf() const
00083   {
00084     assert(pdf_!=0);
00085     return *pdf_;
00086   }
00087 
00088   //: The dimensionality of input vectors.
00089   virtual unsigned n_dims() const { assert(pdf_!=0); return pdf_->n_dims();}
00090 
00091   //: The number of possible output classes.
00092   virtual unsigned n_classes() const {return 1;}
00093 
00094   //: Produce a deep copy.
00095   // client has responsibility for deletion.
00096   clsfy_classifier_base* clone() const;
00097 
00098   //: Version number for I/O
00099   short version_no() const;
00100 
00101   //: Name of the class
00102   virtual vcl_string is_a() const;
00103 
00104   //: Name of the class
00105   virtual bool is_class(vcl_string const& s) const;
00106 
00107   //: Print class to os
00108   virtual void print_summary(vcl_ostream& os) const;
00109 
00110   //: Save class to a binary File Stream
00111   virtual void b_write(vsl_b_ostream& bfs) const;
00112 
00113   //: Load the class from a Binary File Stream
00114   virtual void b_read(vsl_b_istream& bfs);
00115 
00116  public:
00117 
00118   // Copy constructor
00119   clsfy_binary_pdf_classifier( const clsfy_binary_pdf_classifier& b ):
00120     clsfy_classifier_base(), pdf_(0), log_prob_limit_(0.0) { *this = b; }
00121 
00122   //:Assignment operator
00123   clsfy_binary_pdf_classifier& operator=(const clsfy_binary_pdf_classifier& classifier);
00124 
00125  protected:
00126 
00127   // Delete members
00128   void deleteStuff();
00129 };
00130 
00131 #endif // clsfy_binary_pdf_classifier_h_