contrib/mul/pdf1d/pdf1d_mixture.h
Go to the documentation of this file.
00001 // This is mul/pdf1d/pdf1d_mixture.h
00002 #ifndef pdf1d_mixture_h_
00003 #define pdf1d_mixture_h_
00004 //:
00005 // \file
00006 // \brief Implements a mixture model (a set of individual pdfs + weights)
00007 // \author Tim Cootes and Ian Scott
00008 
00009 //=======================================================================
00010 
00011 #include <pdf1d/pdf1d_pdf.h>
00012 #include <vcl_vector.h>
00013 #include <vcl_iosfwd.h>
00014 
00015 //=======================================================================
00016 
00017 //: Represents a mixture model (a set of individual pdfs + weights)
00018 class pdf1d_mixture : public pdf1d_pdf
00019 {
00020   vcl_vector<pdf1d_pdf*> component_;
00021   vcl_vector<double>     weight_;
00022 
00023   void init();
00024   void delete_stuff();
00025  public:
00026 
00027   //: Dflt ctor
00028   pdf1d_mixture();
00029 
00030   //: Copy ctor
00031   pdf1d_mixture(const pdf1d_mixture&);
00032 
00033   //: Copy operator
00034   pdf1d_mixture& operator=(const pdf1d_mixture&);
00035 
00036   //: Destructor
00037   virtual ~pdf1d_mixture();
00038 
00039   //: Probability density at x
00040   virtual double operator()(double x) const;
00041 
00042   //: Log of probability density at x
00043   virtual double log_p(double x) const;
00044 
00045   //: Cumulative Probability (P(x'<x) for x' drawn from the distribution)
00046   virtual double cdf(double x) const;
00047 
00048   //: Return true if cdf() uses an analytic implementation
00049   virtual bool cdf_is_analytic() const;
00050 
00051   //: Gradient of PDF at x
00052   virtual double gradient(double x, double& p) const;
00053 
00054   //: Not Yet Implemented
00055   // Compute nearest point to x which has a density above a threshold
00056   //  If log_p(x)>log_p_min then x unchanged.  Otherwise x is moved
00057   //  (typically up the gradient) until log_p(x)>=log_p_min.
00058   // \param x This may be modified to the nearest plausible position.
00059   // \param log_p_min lower threshold for log_p(x)
00060   virtual double nearest_plausible(double x, double log_p_min) const;
00061 
00062   //: Initialise to use n components of type comp_type
00063   //  Clones taken by comp_type
00064   void init(const pdf1d_pdf& comp_type, int n);
00065 
00066   //: Return instance object for this PDF
00067   //  Object is created on heap. Caller responsible for deletion.
00068   virtual pdf1d_sampler * new_sampler() const;
00069 
00070   //: Number of components in mixture
00071   unsigned n_components() const { return component_.size(); }
00072 
00073   //: Get i<I>th</I> weight.
00074   double weight(unsigned i) const { return weight_[i]; }
00075 
00076   //: Array of weights
00077   // Use weight(i) where possible
00078   const vcl_vector<double>& weights() const { return weight_; }
00079 
00080   //: Array of weights
00081   // Warning care must be taken to ensure consistency when modifying weights
00082   // Warning. Use weight(i) where possible
00083   vcl_vector<double>& weights() { return weight_; }
00084 
00085   //: Return index of component nearest to x
00086   unsigned nearest_comp(double x) const;
00087 
00088   //: Add a component to current model
00089   //  Clone taken of comp
00090   void add_component(const pdf1d_pdf& comp);
00091 
00092   //: Remove all components cleanly
00093   void clear();
00094 
00095   //: Get i<I>th</I> component.
00096   const pdf1d_pdf & component(unsigned i) const { return *component_[i]; }
00097 
00098   //: Access to components - for use by builders
00099   //  Care must be taken to ensure consistency when modifying
00100   // Use component(i) where possible
00101   vcl_vector<pdf1d_pdf*>& components() { return component_; }
00102 
00103   //: Access to components - for use by builders
00104   // Use component(i) where possible
00105   const vcl_vector<pdf1d_pdf*>& components() const { return component_; }
00106 
00107   //: Set the whole pdf mean and variance values.
00108   // Components and Weights should already be correct so that
00109   // the error checking can work.
00110   // #define NDEBUG to turn off error checking.
00111   void set_mean_and_variance(double m, double v);
00112 
00113   //: Return true if the object represents a valid PDF.
00114   // This will return false, if n_dims() is 0, for example just ofter
00115   // default construction.
00116   virtual bool is_valid_pdf() const;
00117 
00118   //: Version number for I/O
00119   short version_no() const;
00120 
00121   //: Name of the class
00122   virtual vcl_string is_a() const;
00123 
00124   //: Does the name of the class match the argument?
00125   virtual bool is_class(vcl_string const& s) const;
00126 
00127   //: Create a copy on the heap and return base class pointer
00128   virtual pdf1d_pdf* clone() const;
00129 
00130   //: Print class to os
00131   virtual void print_summary(vcl_ostream& os) const;
00132 
00133   //: Save class to binary file stream
00134   virtual void b_write(vsl_b_ostream& bfs) const;
00135 
00136   //: Load class from binary file stream
00137   virtual void b_read(vsl_b_istream& bfs);
00138 };
00139 
00140 #endif // pdf1d_mixture_h_