contrib/mul/pdf1d/pdf1d_mixture_builder.h
Go to the documentation of this file.
00001 // This is mul/pdf1d/pdf1d_mixture_builder.h
00002 #ifndef pdf1d_mixture_builder_h_
00003 #define pdf1d_mixture_builder_h_
00004 //:
00005 // \file
00006 // \brief Implements builder for a mixture model PDF.
00007 // \author Tim Cootes and Ian Scott
00008 
00009 //=======================================================================
00010 
00011 #include <pdf1d/pdf1d_builder.h>
00012 #include <pdf1d/pdf1d_mixture_builder.h>
00013 #include <pdf1d/pdf1d_mixture.h>
00014 #include <vcl_iosfwd.h>
00015 
00016 //=======================================================================
00017 
00018 //: Uses the EM algorithm to build pdf1d_mixture objects.
00019 class pdf1d_mixture_builder : public pdf1d_builder
00020 {
00021  private:
00022   vcl_vector<pdf1d_builder*> builder_;
00023   double min_var_;
00024   int max_its_;
00025 
00026     //: Whether weights changed during iterations
00027   bool weights_fixed_;
00028 
00029   void initialise(pdf1d_mixture& model,
00030                   const double* data,
00031                   const vcl_vector<double>& wts) const;
00032 
00033   //: The Expectation part of the EM algorithm
00034   void e_step(pdf1d_mixture& model,
00035               vcl_vector<vnl_vector<double> >& probs,
00036               const double* data,
00037               const vcl_vector<double>& wts) const;
00038 
00039   //: The Maximisation part of the EM algorithm
00040   double m_step(pdf1d_mixture& model,
00041                 const vcl_vector<vnl_vector<double> >& probs,
00042                 const double* data,
00043                 const vcl_vector<double>& wts) const;
00044 
00045   void init();
00046   void delete_stuff();
00047  public:
00048 
00049   //: Dflt ctor
00050   pdf1d_mixture_builder();
00051 
00052   //: Copy ctor
00053   pdf1d_mixture_builder(const pdf1d_mixture_builder&);
00054 
00055   //: Copy operator
00056   pdf1d_mixture_builder& operator=(const pdf1d_mixture_builder&);
00057 
00058   //: Destructor
00059   virtual ~pdf1d_mixture_builder();
00060 
00061   //: Initialise n builders of type builder
00062   //  Clone taken of builder
00063   void init(pdf1d_builder& builder, int n);
00064 
00065   //: Define maximum number of EM iterations allowed
00066   void set_max_iterations(int n);
00067 
00068   //: Whether weights on components left unchanged during iterations
00069   bool weights_fixed() const { return weights_fixed_; }
00070 
00071   //: Whether weights on components left unchanged during iterations
00072   void set_weights_fixed(bool b);
00073 
00074   //: Create empty model
00075   virtual pdf1d_pdf* new_model() const;
00076 
00077   //: Name of the model class returned by new_model()
00078   virtual vcl_string new_model_type() const;
00079 
00080   //: Define lower threshold on variance for built models
00081   virtual void set_min_var(double min_var);
00082 
00083   //: Get lower threshold on variance for built models
00084   virtual double min_var() const;
00085 
00086   //: Calculate and set the mixture's mean and variance
00087   // This should be used after the weights or components of
00088   // the model have been altered.
00089   static void calc_mean_and_variance(pdf1d_mixture& model);
00090 
00091   //: Build default model with given mean
00092   virtual void build(pdf1d_pdf& model, double mean) const;
00093 
00094   //: Build model from data
00095   //  Use EM algorithm to best fit mixture model to given data
00096   //  If model does not have the correct number of components
00097   //  they will be initialised to means scattered through the
00098   //  data.  If they model has the correct number of components
00099   //  it is assumed that they have been set to sensible initial
00100   //  values.  The EM algorithm will be used to optimise their
00101   //  parameters.
00102   virtual void build(pdf1d_pdf& model,
00103                      mbl_data_wrapper<double>& data) const;
00104 
00105   //: Build model from weighted data
00106   //  Use EM algorithm to best fit mixture model to given data
00107   //  If model does not have the correct number of components
00108   //  they will be initialised to means scattered through the
00109   //  data.  If they model has the correct number of components
00110   //  it is assumed that they have been set to sensible initial
00111   //  values.  The EM algorithm will be used to optimise their
00112   //  parameters.
00113   virtual void weighted_build(pdf1d_pdf& model,
00114                               mbl_data_wrapper<double>& data,
00115                               const vcl_vector<double>& wts) const;
00116 
00117   //: Version number for I/O
00118   short version_no() const;
00119 
00120   //: Name of the class
00121   virtual vcl_string is_a() const;
00122 
00123   //: Does the name of the class match the argument?
00124   virtual bool is_class(vcl_string const& s) const;
00125 
00126   //: Create a copy on the heap and return base class pointer
00127   virtual pdf1d_builder* clone() const;
00128 
00129   //: Print class to os
00130   virtual void print_summary(vcl_ostream& os) const;
00131 
00132   //: Save class to binary file stream
00133   virtual void b_write(vsl_b_ostream& bfs) const;
00134 
00135   //: Load class from binary file stream
00136   virtual void b_read(vsl_b_istream& bfs);
00137 };
00138 
00139 #endif // pdf1d_mixture_builder_h_