00001 // This is mul/pdf1d/pdf1d_sampler.h 00002 #ifndef pdf1d_sampler_h 00003 #define pdf1d_sampler_h 00004 00005 //: 00006 // \file 00007 // \author Tim Cootes 00008 // \brief Base class for Univariate random sampler classes. 00009 00010 #include <vnl/vnl_vector.h> 00011 #include <vcl_string.h> 00012 #include <vcl_iosfwd.h> 00013 class pdf1d_pdf; 00014 00015 //======================================================================= 00016 00017 //: Base class for Univariate Random Sampler classes. 00018 // This is really a random number generator whose 00019 // outputs have the PDF of the relevant pdf1d_pdf. 00020 // Sampler objects should only exist as long as their pdf object, 00021 // and are not meant to be persistent. 00022 00023 class pdf1d_sampler 00024 { 00025 protected: 00026 const pdf1d_pdf *pdf_model_; 00027 public: 00028 00029 //: Dflt ctor 00030 pdf1d_sampler(); 00031 00032 //: Destructor 00033 virtual ~pdf1d_sampler(); 00034 00035 //: PDF of which this is an instance 00036 const pdf1d_pdf& model() const; 00037 00038 //: Set model for which this is an instance 00039 virtual void set_model(const pdf1d_pdf&); 00040 00041 //: Draw random sample from distribution 00042 virtual double sample()=0; 00043 00044 //: Fill x with samples drawn from distribution 00045 virtual void get_samples(vnl_vector<double>& x); 00046 00047 //: Fill x with samples possibly chosen so as to represent the distribution 00048 // Generate a set of pseudo-random samples, chosen so as to be suitable 00049 // to represent the distribution. This is meant to be used for estimating 00050 // continuous integrals with sampled approximations. Where there are 00051 // multiple peaks (e.g. kernel or mixture models), it is preferred that 00052 // the number of samples from each component is roughly proportional 00053 // to the weight for the component. When small numbers are requested, 00054 // this can be done explicitly. 00055 // The default is simply to call sample() for each element of x 00056 virtual void regular_samples(vnl_vector<double>& x); 00057 00058 //: Fill x with samples possibly chosen so as to represent the distribution 00059 // As regular_samples(x), but p[i] is set to p(x[i]) 00060 virtual void regular_samples_and_prob(vnl_vector<double>& x, vnl_vector<double>& p); 00061 00062 //: Reseeds the internal random number generator 00063 // To achieve quasi-random initialisation use; 00064 // \code 00065 // #include <vcl_ctime.h> 00066 // .. 00067 // sampler.reseed(vcl_time(0)); 00068 // \endcode 00069 virtual void reseed(unsigned long)=0; 00070 00071 00072 //: Name of the class 00073 virtual vcl_string is_a() const; 00074 00075 //: Does the name of the class match the argument? 00076 virtual bool is_class(vcl_string const& s) const; 00077 00078 //: Create a copy on the heap and return base class pointer 00079 virtual pdf1d_sampler* clone() const = 0; 00080 00081 //: Print class to os 00082 virtual void print_summary(vcl_ostream& os) const; 00083 }; 00084 00085 00086 //: Stream output operator for class reference 00087 void vsl_print_summary(vcl_ostream& os,const pdf1d_sampler& b); 00088 00089 //: Stream output operator for class pointer 00090 void vsl_print_summary(vcl_ostream& os,const pdf1d_sampler* b); 00091 00092 00093 #endif // pdf1d_sampler_h