contrib/mul/clsfy/clsfy_random_classifier.h
Go to the documentation of this file.
00001 // This is mul/clsfy/clsfy_random_classifier.h
00002 #ifndef clsfy_random_classifier_h_
00003 #define clsfy_random_classifier_h_
00004 // Copyright: (C) 2001 British Telecommunications plc
00005 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00006 #pragma interface
00007 #endif
00008 //:
00009 // \file
00010 // \brief Describe a random classifier
00011 // \author Ian Scott
00012 // \date 2000-05-10
00013 // \verbatim
00014 //  Modifications
00015 //   2 May 2001 IMS Converted to VXL
00016 // \endverbatim
00017 
00018 #include "clsfy_classifier_base.h"
00019 #include <vnl/vnl_vector.h>
00020 #include <vnl/vnl_random.h>
00021 #include <vsl/vsl_binary_io.h>
00022 #include <vcl_vector.h>
00023 #include <vcl_string.h>
00024 #include <vcl_iosfwd.h>
00025 
00026 //:  A common interface for 1-out-of-N classifiers
00027 // This class takes a vector and classifies into one of
00028 // N classes.
00029 //
00030 // And derived classes with binary in the name indicates that
00031 // the classifier works with only two classes, 0 and 1.
00032 
00033 class clsfy_random_classifier : public clsfy_classifier_base
00034 {
00035  public:
00036   // Dflt constructor
00037   clsfy_random_classifier();
00038 
00039   // Destructor
00040   virtual ~clsfy_random_classifier() {}
00041 
00042   //: Return the probability the input being in each class.
00043   // output(i) i<nClasses, contains the probability that the input is in class i
00044   virtual void class_probabilities(vcl_vector<double> &outputs, const vnl_vector<double> &input) const;
00045 
00046   //: Log likelihood of being in class (binary classifiers only)
00047   // class probability = 1 / (1+exp(-log_l))
00048   // Operation of this method is undefined for multiclass classifiers
00049   virtual double log_l(const vnl_vector<double> &input) const;
00050 
00051   //: The number of possible output classes.
00052   virtual unsigned n_classes() const;
00053 
00054   //: The dimensionality of input vectors.
00055   virtual unsigned n_dims() const;
00056 
00057   //: Name of the class
00058   virtual vcl_string is_a() const;
00059 
00060   //: Name of the class
00061   virtual bool is_class(vcl_string const& s) const;
00062 
00063   //: Create a copy on the heap and return base class pointer
00064   virtual clsfy_classifier_base* clone() const;
00065 
00066   //: Print class to os
00067   virtual void print_summary(vcl_ostream& os) const;
00068 
00069   //: Save class to binary file stream
00070   virtual void b_write(vsl_b_ostream& bfs) const;
00071 
00072   //: Load class from binary file stream
00073   virtual void b_read(vsl_b_istream& bfs);
00074 
00075   //: The probabilities of returning a value in each class.
00076   const vcl_vector<double> & probs() const;
00077 
00078   //: Set the prior probabilities of each class
00079   // The values are normalised to sum to 1.
00080   void set_probs(const vcl_vector<double> &);
00081 
00082   //: Set the number of dimensions the classifier reports that it uses.
00083   // The classifier itself pays no attention to this value, but it
00084   // may be useful for other error checking code which calls n_dims()
00085   void set_n_dims(unsigned);
00086 
00087   //: The mean confidence noise added to class probabilities.
00088   double confidence() const;
00089 
00090   //: Set the mean of confidence noise added to class probabilities.
00091   // If the value is 0.0, then the probability of the winning class, will
00092   // only be just large enough to guarantee the win. Larger values will allow
00093   // more confident winning class probabilities - however each actual
00094   // confidence increase is random.
00095   void set_confidence(double);
00096 
00097   //: Reseeds the internal random number generator
00098   // To achieve quasi-random initialisation use
00099   // \code
00100   // #include <vcl_ctime.h>
00101   // ..
00102   // sampler.reseed(vcl_time(0));
00103   // \endcode
00104   virtual void reseed(unsigned long);
00105 
00106  private:
00107   //: Calculate the minimum value each class probability needs to be biased by to win
00108   void calc_min_to_win();
00109 
00110   //: The probabilities of each class.
00111   // The values will always sum to 1.
00112   // If the vector is empty then the builder will use the prior probability
00113   vcl_vector<double> probs_;
00114 
00115   //: The mean confidence noise added to class probabilities
00116   double confidence_;
00117 
00118   //: The classifier may get asked this
00119   unsigned n_dims_;
00120 
00121   //: Give the same answers if the same vector is presented twice in a row
00122   mutable vnl_vector<double> last_inputs_;
00123 
00124   //: The last class probabilities calculated.
00125   mutable vcl_vector<double> last_outputs_;
00126 
00127   //: The random number generator used to sample classes.
00128   mutable vnl_random rng_;
00129 
00130   //: The minimum value each class probability needs to be biased by to win.
00131   vcl_vector<double> min_to_win_;
00132 };
00133 
00134 #endif // clsfy_random_classifier_h_