contrib/mul/pdf1d/pdf1d_weighted_kernel_pdf.h
Go to the documentation of this file.
00001 // This is mul/pdf1d/pdf1d_weighted_kernel_pdf.h
00002 #ifndef pdf1d_weighted_kernel_pdf_h
00003 #define pdf1d_weighted_kernel_pdf_h
00004 
00005 //:
00006 // \file
00007 // \brief Univariate weighted kernel_pdf PDF
00008 // \author Ian Scott
00009 
00010 #include <pdf1d/pdf1d_kernel_pdf.h>
00011 #include <vcl_iosfwd.h>
00012 
00013 //: Class for univariate kernel based PDFs.
00014 //  Distribution is the sum of a set of kernel functions placed on the training
00015 //  samples.
00016 //
00017 //  Essentially a special case of a mixture model.  It differs from
00018 //  the former in that a) every component has the same shape, though
00019 //  possibly a different width
00020 //  b) Usually one uses different training algorithms.
00021 //  Mixture models are more general.
00022 //
00023 //  Use a normal kernel pdf builder to build this, and then
00024 //  set the weights separately.
00025 class pdf1d_weighted_kernel_pdf : public pdf1d_kernel_pdf
00026 {
00027  protected:
00028   //: Weight of each kernel
00029   vnl_vector<double> weight_;
00030   double sum_weights_;
00031  public:
00032 
00033   //:calc the weighted mean and var of kernels.
00034   // w is expected to sum to n.
00035   static void pdf1d_weighted_kernel_mean_var(double& mean, double& var,
00036                                              const vnl_vector<double>& centres,
00037                                              const vnl_vector<double>& widths,
00038                                              const vnl_vector<double>& weights);
00039 
00040   //: Set the weights.
00041   // The weights will be scaled to sum to 1.0
00042   void set_weight(const vnl_vector<double>& weights);
00043 
00044   //: Swap in the weights values.
00045   // This function is fast when you are changing the weights regularly
00046   // The weights will be scaled to sum to 1.0
00047   void swap_weight(vnl_vector<double>& weights);
00048 
00049   //: Weight of each kernel
00050   const vnl_vector<double>& weight() const { return weight_; }
00051 
00052   //: Initialise so all kernels have the same width
00053   // This function resets the weights.
00054   virtual void set_centres(const vnl_vector<double>& x, double width);
00055 
00056   //: Initialise so all kernels have given width
00057   // This function resets the weights.
00058   virtual void set_centres(const vnl_vector<double>& x,
00059                            const vnl_vector<double>& width);
00060 
00061   //: Version number for I/O
00062   short version_no() const;
00063 
00064   //: Name of the class
00065   virtual vcl_string is_a() const;
00066 
00067   //: Does the name of the class match the argument?
00068   virtual bool is_class(vcl_string const& s) const;
00069 
00070   //: Print class to os
00071   virtual void print_summary(vcl_ostream& os) const;
00072 
00073   //: Save class to binary file stream
00074   virtual void b_write(vsl_b_ostream& bfs) const;
00075 
00076   //: Load class from binary file stream
00077   virtual void b_read(vsl_b_istream& bfs);
00078 };
00079 
00080 #endif // pdf1d_weighted_kernel_pdf_h