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

Generated on Tue Dec 2 05:11:22 2008 for contrib/mul/vpdfl by  doxygen 1.5.1