contrib/mul/vpdfl/vpdfl_pdf_base.h
Go to the documentation of this file.
00001 // This is mul/vpdfl/vpdfl_pdf_base.h
00002 #ifndef vpdfl_pdf_base_h
00003 #define vpdfl_pdf_base_h
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief Base class for Multi-Variate Probability Density Function classes.
00010 // \author Tim Cootes
00011 // \date 12-Apr-2001
00012 
00013 #include <vnl/io/vnl_io_vector.h>
00014 #include <vsl/vsl_binary_io.h>
00015 #include <vcl_string.h>
00016 #include <vcl_iosfwd.h>
00017 
00018 //=======================================================================
00019 
00020 class vpdfl_sampler_base;
00021 
00022 //: Base class for Multi-Variate Probability Density Function classes.
00023 // Functions are available to test the plausibility of a vector or
00024 // set of parameters, to modify a set of parameters so it is plausible
00025 // and to choose a threshold of plausibility.  Also, for cases where
00026 // the distributions of parameters are multi-modal, the number and
00027 // centres of each peak can be recorded.
00028 // This is particularly useful for non-linear and mixture model
00029 // representations of the parameter distributions.
00030 class vpdfl_pdf_base
00031 {
00032   vnl_vector<double> mean_;
00033   vnl_vector<double> var_;
00034  protected:
00035   void set_mean(const vnl_vector<double>& m) { mean_ = m; }
00036   void set_variance(const vnl_vector<double>& v) { var_ = v; }
00037  public:
00038 
00039   //: Dflt ctor
00040   vpdfl_pdf_base();
00041 
00042   //: Destructor
00043   virtual ~vpdfl_pdf_base();
00044 
00045   //: Mean of distribution
00046   const vnl_vector<double>& mean() const { return mean_; }
00047 
00048   //: Variance of each dimension
00049   const vnl_vector<double>& variance() const { return var_; }
00050 
00051   //: Number of dimensions
00052   int n_dims() const { return mean_.size(); }
00053 
00054   //: Number of peaks of distribution
00055   virtual int n_peaks() const { return 1; }
00056 
00057   //: Position of the i'th peak
00058   virtual const vnl_vector<double>& peak(int) const { return mean_; }
00059 
00060   //: Log of probability density at x
00061   virtual double log_p(const vnl_vector<double>& x) const =0;
00062 
00063   //: Probability density at x
00064   virtual double operator()(const vnl_vector<double>& x) const;
00065 
00066   //: Gradient and value of PDF at x
00067   //  Computes gradient of PDF at x, and returns the prob at x in p
00068   virtual void gradient(vnl_vector<double>& g,
00069                         const vnl_vector<double>& x, double& p) const =0;
00070 
00071   //: Gradient and value of log(p(x)) at x
00072   //  Computes gradient df/dx of f(x)=log(p(x)) at x.
00073   //  Result is vector of same dimensionality as x.
00074   //  Default baseclass implementation uses gradient() to compute grad/p
00075   virtual void gradient_logp(vnl_vector<double>& g,
00076                         const vnl_vector<double>& x) const;
00077 
00078   //: Create a sampler object on the heap
00079   // Caller is responsible for deletion.
00080   virtual vpdfl_sampler_base* new_sampler()const=0 ;
00081 
00082   //: Compute threshold for PDF to pass a given proportion
00083   virtual double log_prob_thresh(double pass_proportion)const;
00084 
00085   //: Compute nearest point to x which has a density above a threshold
00086   //  If log_p(x)>log_p_min then x unchanged.  Otherwise x is moved
00087   //  (typically up the gradient) until log_p(x)>=log_p_min.
00088   // \param x This may be modified to the nearest plausible position.
00089   // \param log_p_min lower threshold for log_p(x)
00090   virtual void nearest_plausible(vnl_vector<double>& x, double log_p_min)const =0;
00091 
00092   //: Return true if the object represents a valid PDF.
00093   // This will return false, if n_dims() is 0, for example just ofter
00094   // default construction.
00095   virtual bool is_valid_pdf() const;
00096 
00097   //: Version number for I/O
00098   short version_no() const;
00099 
00100   //: Name of the class
00101   virtual vcl_string is_a() const;
00102 
00103   //: Does the name of the class match the argument?
00104   virtual bool is_class(vcl_string const& s) const;
00105 
00106   //: Create a copy on the heap and return base class pointer
00107   virtual vpdfl_pdf_base* clone() const = 0;
00108 
00109   //: Print class to os
00110   virtual void print_summary(vcl_ostream& os) const = 0;
00111 
00112   //: Save class to binary file stream
00113   virtual void b_write(vsl_b_ostream& bfs) const = 0;
00114 
00115   //: Load class from binary file stream
00116   virtual void b_read(vsl_b_istream& bfs) = 0;
00117 };
00118 
00119 //: Allows derived class to be loaded by base-class pointer
00120 //  A loader object exists which is invoked by calls
00121 //  of the form "bfs>>base_ptr;".  This loads derived class
00122 //  objects from the disk, places them on the heap and
00123 //  returns a base class pointer.
00124 //  In order to work the loader object requires
00125 //  an instance of each derived class that might be
00126 //  found.  This function gives the model class to
00127 //  the appropriate loader.
00128 void vsl_add_to_binary_loader(const vpdfl_pdf_base& b);
00129 
00130 //: Binary file stream output operator for class reference
00131 void vsl_b_write(vsl_b_ostream& bfs, const vpdfl_pdf_base& b);
00132 
00133 //: Binary file stream input operator for class reference
00134 void vsl_b_read(vsl_b_istream& bfs, vpdfl_pdf_base& b);
00135 
00136 //: Stream output operator for class reference
00137 void vsl_print_summary(vcl_ostream& os,const vpdfl_pdf_base& b);
00138 
00139 //: Stream output operator for class pointer
00140 void vsl_print_summary(vcl_ostream& os,const vpdfl_pdf_base* b);
00141 
00142 //: Stream output operator for class reference
00143 vcl_ostream& operator<<(vcl_ostream& os,const vpdfl_pdf_base& b);
00144 
00145 //: Stream output operator for class pointer
00146 vcl_ostream& operator<<(vcl_ostream& os,const vpdfl_pdf_base* b);
00147 
00148 #endif // vpdfl_pdf_base_h