contrib/mul/clsfy/clsfy_rbf_parzen.h
Go to the documentation of this file.
00001 // This is mul/clsfy/clsfy_rbf_parzen.h
00002 // Copyright: (C) 2000 British Telecommunications plc.
00003 #ifndef clsfy_rbf_parzen_h_
00004 #define clsfy_rbf_parzen_h_
00005 //:
00006 // \file
00007 // \brief Describe a Parzen window classifier
00008 // \author Ian Scott
00009 // \date 2000-05-10
00010 // \verbatim
00011 //  Modifications
00012 //   2 May 2001 IMS Converted to VXL
00013 // \endverbatim
00014 
00015 #include <clsfy/clsfy_classifier_base.h>
00016 #include <vcl_iosfwd.h>
00017 
00018 //: A Parzen binary classifier using an RBF Window
00019 // A given vector, x0, is classified, by placing a window defined by w = exp(-1/(2*sigma^p) * |x-x0|^p),
00020 // and finding the majority prediction of the nearby training vectors, each weighted by the window function.
00021 class clsfy_rbf_parzen : public clsfy_classifier_base
00022 {
00023   //: Workspace value which is cached for speed.
00024   // Inversely proportional to width of the RBF window function.  Default value is -0.5.
00025   //
00026   // width_ = 1/sqrt(-2*gamma_)
00027   double gamma_;
00028 
00029   //: Width of the rbf window function.
00030   // Default value is 1.0.
00031   // width_ = 1/sqrt(-2*gamma_)
00032   double width_;
00033 
00034   //: The power, p, in the window function. Default value is 2.0.
00035   double power_;
00036 
00037   //: The set of training input values.  Size should equal number of outputs.
00038   vcl_vector<vnl_vector<double> > trainInputs_;
00039 
00040   //: The set of training output values.  Size should equal number of inputs.
00041   vcl_vector<unsigned> trainOutputs_;
00042 
00043  public:
00044   //: Construct a Parzen classifier.
00045   clsfy_rbf_parzen() { set_rbf_width(1.0); set_power(2.0);}
00046 
00047   //: Return the classification of the given probe vector.
00048   virtual unsigned classify(const vnl_vector<double> &input) const;
00049 
00050   //: Provides a probability-like value that the input being in each class.
00051   // output(i) i<nClasses, contains the probability that the input is in class i
00052   virtual void class_probabilities(vcl_vector<double> &outputs, const vnl_vector<double> &input) const;
00053 
00054   //: Return the number of proximate training vectors, weighted by the windowing function.
00055   double weightings(const vnl_vector<double> &input) const;
00056 
00057   //: This value has properties of a Log likelihood of being in class (binary classifiers only)
00058   // class probability = exp(logL) / (1+exp(logL))
00059   virtual double log_l(const vnl_vector<double> &input) const;
00060 
00061   //: Set the training data.
00062   void set(const vcl_vector<vnl_vector<double> > &inputs, const vcl_vector<unsigned> &outputs);
00063 
00064   //: The 1st standard deviation width of the RBF window.
00065   // The default value is 1.
00066   double rbf_width() const { return width_;}
00067 
00068   //: Set the 1st standard deviation width of the RBF window.
00069   // The default value in the constructor is 1.
00070   void set_rbf_width(double sigma);
00071 
00072   //: The value p in the window function $exp(-1/(2*sigma^p) * |x-y|^p)$.
00073   // The value p affects the kurtosis, or peakyness of the window. Towards 0 gives a more peaked central spike, and longer tail.
00074   // Toward +inf gives a broader peak, and shorter tail.
00075   // The default value is 2, giving a Gaussian distribution.
00076   double power() const { return power_;}
00077 
00078   //: The value p in the window function $exp(-1/(2*sigma^p) * |x-y|^p)$.
00079   // The value p affects the kurtosis, or peakyness of the window.
00080   // Towards 0 gives a more peaked central spike, and longer tail.
00081   // Toward +inf gives a broader peak, and shorter tail.
00082   // The default value is 2, giving a Gaussian distribution.
00083   void set_power(double p);
00084 
00085   //: Return a reference to the training vectors.
00086   // Vectors are ordered similarly to training_classes()
00087   const vcl_vector<vnl_vector<double> > & training_vectors() const
00088   {return trainInputs_;}
00089 
00090   //: Return a reference to the training classes
00091   // Vectors are ordered similarly to training_vectors()
00092   const vcl_vector<unsigned > & training_classes() const
00093   {return trainOutputs_;}
00094 
00095   //: The number of possible output classes.
00096   virtual unsigned n_classes() const {return 1;}
00097 
00098   //: The dimensionality of input vectors.
00099   virtual unsigned n_dims() const;
00100 
00101   //: Storage version number
00102   virtual short version_no() const;
00103 
00104   //: Name of the class
00105   virtual vcl_string is_a() const;
00106 
00107   //: Name of the class
00108   virtual bool is_class(vcl_string const& s) const;
00109 
00110   //: Create a copy on the heap and return base class pointer
00111   virtual clsfy_classifier_base* clone() const;
00112 
00113   //: Print class to os
00114   virtual void print_summary(vcl_ostream& os) const;
00115 
00116   //: Save class to binary file stream
00117   virtual void b_write(vsl_b_ostream& bfs) const;
00118 
00119   //: Load class from binary file stream
00120   virtual void b_read(vsl_b_istream& bfs);
00121 };
00122 
00123 #endif // clsfy_rbf_parzen_h_