contrib/mul/vpdfl/vpdfl_kernel_pdf_builder.h
Go to the documentation of this file.
00001 // This is mul/vpdfl/vpdfl_kernel_pdf_builder.h
00002 #ifndef vpdfl_kernel_pdf_builder_h
00003 #define vpdfl_kernel_pdf_builder_h
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \author Tim Cootes
00010 // \brief Initialises kernel pdfs
00011 
00012 #include <vpdfl/vpdfl_builder_base.h>
00013 #include <vcl_iosfwd.h>
00014 
00015 //=======================================================================
00016 
00017 class vpdfl_kernel_pdf;
00018 
00019 //: Build kernel pdf objects.
00020 //  Contains algorithms for selecting kernel widths.
00021 //
00022 //  Simplest is to use equal widths (set_use_equal_width()).
00023 //
00024 //  A fixed width can be supplied (set_use_fixed_width(w))
00025 //
00026 //  The widths can be estimated from the proximity of neighbours
00027 //  set_use_width_from_separation()
00028 //
00029 //  More interesting is an adaptive kernel estimate (set_use_adaptive()).
00030 //  This tends to get results comparable with the equal width method for
00031 //  simple cases, but can match to more complex distributions more easily.
00032 //  In particular, it tends to approximate the tails more accurately.
00033 //
00034 //  See book on Density Estimation by B.W.Silverman (Pub. Chapman and Hall, 1986)
00035 //  for details.
00036 class vpdfl_kernel_pdf_builder : public vpdfl_builder_base
00037 {
00038  public:
00039   enum build_type { fixed_width, select_equal, width_from_sep, adaptive };
00040  private:
00041   //: Minimum variance of whole model
00042   double min_var_;
00043 
00044   //: Type of building to be performed
00045   build_type build_type_;
00046 
00047   //: Width set if fixed_width option on build used
00048   double fixed_width_;
00049 
00050   vpdfl_kernel_pdf& kernel_pdf(vpdfl_pdf_base& model) const;
00051  public:
00052 
00053   //: Dflt ctor
00054   vpdfl_kernel_pdf_builder();
00055 
00056   //: Destructor
00057   virtual ~vpdfl_kernel_pdf_builder();
00058 
00059   //: Use fixed width kernels of given width when building.
00060   void set_use_fixed_width(double width);
00061 
00062   //: Use equal width kernels of width depending on number of samples.
00063   void set_use_equal_width();
00064 
00065   //: Kernel width proportional to distance to nearby samples.
00066   void set_use_width_from_separation();
00067 
00068   //: Build adaptive kernel estimate.
00069   void set_use_adaptive();
00070 
00071   //: Define lower threshold on variance for built models
00072   virtual void set_min_var(double min_var);
00073 
00074   //: Get lower threshold on variance for built models
00075   virtual double min_var() const;
00076 
00077   //: Build default model with given mean
00078   virtual void build(vpdfl_pdf_base& model, const vnl_vector<double>& mean) const;
00079 
00080   //: Build model from data
00081   virtual void build(vpdfl_pdf_base& model,
00082                      mbl_data_wrapper<vnl_vector<double> >& data) const;
00083 
00084   //: Build kernel_pdf from n elements in data[i]
00085   void build_from_array(vpdfl_pdf_base& model,
00086                         const vnl_vector<double>* data, int n) const;
00087 
00088   //: Build model from weighted data
00089   virtual void weighted_build(vpdfl_pdf_base& model,
00090                               mbl_data_wrapper<vnl_vector<double> >& data,
00091                               const vcl_vector<double>& wts) const;
00092 
00093   //: Build from n elements in data[i].  Fixed kernel width.
00094   void build_fixed_width(vpdfl_kernel_pdf& kpdf,
00095                          const vnl_vector<double>* data, int n, double width) const;
00096 
00097   //: Build from n elements in data[i].  Chooses width.
00098   //  Same width selected for all points, using
00099   //  $w=(4/(2n+d.n)^{1/(d+4)}\sigma$, as suggested by Silverman
00100   //  Note: This value only suitable for gaussian kernels!
00101   void build_select_equal_width(vpdfl_kernel_pdf& kpdf,
00102                                  const vnl_vector<double>* data, int n) const;
00103 
00104   //: Kernel width proportional to distance to nearby samples.
00105   void build_width_from_separation(vpdfl_kernel_pdf& kpdf,
00106                                    const vnl_vector<double>* data, int n) const;
00107 
00108   //: Build adaptive kernel estimate.
00109   //  Use equal widths to create a pilot estimate, then use the prob at each
00110   //  data point to modify the widths
00111   void build_adaptive(vpdfl_kernel_pdf& kpdf,
00112                       const vnl_vector<double>* data, int n) const;
00113 
00114   //: Version number for I/O
00115   short version_no() const;
00116 
00117   //: Name of the class
00118   virtual vcl_string is_a() const;
00119 
00120   //: Does the name of the class match the argument?
00121   virtual bool is_class(vcl_string const& s) const;
00122 
00123   //: Print class to os
00124   virtual void print_summary(vcl_ostream& os) const;
00125 
00126   //: Save class to binary file stream
00127   virtual void b_write(vsl_b_ostream& bfs) const;
00128 
00129   //: Load class from binary file stream
00130   virtual void b_read(vsl_b_istream& bfs);
00131 
00132   //: Read initialisation settings from a stream.
00133   // Parameters:
00134   // \verbatim
00135   // {
00136   //   min_var: 1.0e-6
00137   //   // kernel_widths can be fixed_width,select_equal,width_from_sep,adaptive
00138   //   kernel_widths: fixed_width
00139   //   // Width to be used when it is fixed_width
00140   //   fixed_width: 1.0
00141   // }
00142   // \endverbatim
00143   // \throw mbl_exception_parse_error if the parse fails.
00144   virtual void config_from_stream(vcl_istream & is);
00145 
00146 };
00147 
00148 #endif // vpdfl_kernel_pdf_builder_h