contrib/mul/clsfy/clsfy_mean_square_1d.cxx
Go to the documentation of this file.
00001 #include "clsfy_mean_square_1d.h"
00002 //:
00003 // \file
00004 // \brief Simplest possible 1D classifier: A single thresholding function
00005 // \author Tim Cootes
00006 
00007 //=======================================================================
00008 
00009 #include <vcl_string.h>
00010 #include <vcl_iostream.h>
00011 #include <vcl_vector.h>
00012 #include <vcl_cassert.h>
00013 #include <vcl_cmath.h>
00014 #include <vsl/vsl_binary_io.h>
00015 #include <vsl/vsl_indent.h>
00016 #include <vnl/vnl_double_2.h>
00017 #include <vnl/io/vnl_io_vector.h>
00018 #include <vnl/io/vnl_io_matrix.h>
00019 
00020 //=======================================================================
00021 
00022 //: Find the posterior probability of the input being in the positive class.
00023 // The result is outputs(0)
00024 void clsfy_mean_square_1d::class_probabilities(vcl_vector<double> &outputs,
00025                                                double input) const
00026 {
00027   outputs.resize(1);
00028   outputs[0] = 1.0 / (1.0 + vcl_exp(-log_l(input)));
00029 }
00030 
00031 
00032 //=======================================================================
00033 
00034 //: Log likelihood of being in the positive class.
00035 // Class probability = 1 / (1+exp(-log_l))
00036 double clsfy_mean_square_1d::log_l(double input) const
00037 {
00038   double k= mean_-input;
00039   return k*k; // - threshold_ ;  //ie distance above threshold!
00040                                  // this isn't a loglikelihood, as lower value
00041                                  // indicates more likely to be pos example
00042 }
00043 
00044 //: Return parameters defining classifier in a vector (format depends on classifier)
00045 vnl_vector<double> clsfy_mean_square_1d::params() const
00046 {
00047   return vnl_double_2(mean_,threshold_).as_vector();
00048 }
00049 
00050 //: Set parameters defining classifier with a vector (format depends on classifier)
00051 void clsfy_mean_square_1d::set_params(const vnl_vector<double>& p)
00052 {
00053   assert(p.size()==2);
00054   mean_=p[0];
00055   threshold_=p[1];
00056 }
00057 
00058 //=======================================================================
00059 
00060 //: Equality operator for 1d classifiers
00061 bool clsfy_mean_square_1d::operator==(const clsfy_classifier_1d& x) const
00062 {
00063   assert( x.is_class("clsfy_mean_square_1d"));
00064   clsfy_mean_square_1d& x2= (clsfy_mean_square_1d&) x;
00065   return vcl_fabs(x2.mean_ - mean_) < 0.001 &&
00066          vcl_fabs(x2.threshold_ - threshold_) < 0.001;
00067 }
00068 
00069 
00070 vcl_string clsfy_mean_square_1d::is_a() const
00071 {
00072   return vcl_string("clsfy_mean_square_1d");
00073 }
00074 
00075 bool clsfy_mean_square_1d::is_class(vcl_string const& s) const
00076 {
00077   return s == clsfy_mean_square_1d::is_a() || clsfy_classifier_1d::is_class(s);
00078 }
00079 
00080 //=======================================================================
00081 
00082 // required if data is present in this class
00083 void clsfy_mean_square_1d::print_summary(vcl_ostream& os) const
00084 {
00085   os << "mean: " << mean_ << "   threshold: "<<threshold_<<'\n';
00086 }
00087 
00088 //=======================================================================
00089 
00090 short clsfy_mean_square_1d::version_no() const
00091 {
00092   return 1;
00093 }
00094 
00095 //=======================================================================
00096 
00097 void clsfy_mean_square_1d::b_write(vsl_b_ostream& bfs) const
00098 {
00099   vsl_b_write(bfs,version_no());
00100   vsl_b_write(bfs,mean_);
00101   vsl_b_write(bfs,threshold_);
00102 }
00103 
00104 //=======================================================================
00105 
00106 void clsfy_mean_square_1d::b_read(vsl_b_istream& bfs)
00107 {
00108   if (!bfs) return;
00109 
00110   short version;
00111   vsl_b_read(bfs,version);
00112   switch (version)
00113   {
00114     case (1):
00115       vsl_b_read(bfs,mean_);
00116       vsl_b_read(bfs,threshold_);
00117       break;
00118     default:
00119       vcl_cerr << "I/O ERROR: clsfy_mean_square_1d::b_read(vsl_b_istream&)\n"
00120                << "           Unknown version number "<< version << '\n';
00121       bfs.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00122   }
00123 }
00124 
00125 //=======================================================================
00126 
00127 void vsl_b_write(vsl_b_ostream& bfs, const clsfy_mean_square_1d& b)
00128 {
00129   b.b_write(bfs);
00130 }
00131 
00132 //=======================================================================
00133 
00134 void vsl_b_read(vsl_b_istream& bfs, clsfy_mean_square_1d& b)
00135 {
00136   b.b_read(bfs);
00137 }
00138 
00139 //=======================================================================
00140 
00141 void vsl_print_summary(vcl_ostream& os,const clsfy_mean_square_1d& b)
00142 {
00143   os << b.is_a() << ": ";
00144   vsl_indent_inc(os);
00145   b.print_summary(os);
00146   vsl_indent_dec(os);
00147 }
00148 
00149 //=======================================================================
00150 
00151 vcl_ostream& operator<<(vcl_ostream& os,const clsfy_mean_square_1d& b)
00152 {
00153   vsl_print_summary(os,b);
00154   return os;
00155 }