indii/ml/aux/GaussianKernel.hpp

00001 #ifndef INDII_ML_AUX_GAUSSIANKERNEL_HPP
00002 #define INDII_ML_AUX_GAUSSIANKERNEL_HPP
00003 
00004 #include "Kernel.hpp"
00005 
00006 namespace indii {
00007   namespace ml {
00008     namespace aux {
00009 /**
00010  * Gaussian kernel for density estimation.
00011  *
00012  * @author Lawrence Murray <lawrence@indii.org>
00013  * @version $Rev: 531 $
00014  * @date $Date: 2008-08-25 16:09:23 +0100 (Mon, 25 Aug 2008) $
00015  *
00016  * The Gaussian kernel takes the form:
00017  *
00018  * \f[
00019  *   K(x) = \frac{1}{\sqrt{2\pi}}e^{-\frac{1}{2}x^2}
00020  * \f]
00021  *
00022  * @see #hopt for guidance as to bandwidth selection.
00023  *
00024  * @section Serialization
00025  *
00026  * This class supports serialization through the Boost.Serialization
00027  * library.
00028  */
00029 class GaussianKernel : public Kernel {
00030 public:
00031   /**
00032    * Default constructor.
00033    *
00034    * This should generally only be used when the object is to be restored
00035    * from a serialization.
00036    */
00037   GaussianKernel();
00038 
00039   /**
00040    * Constructor.
00041    *
00042    * @param N \f$N\f$; dimensionality of the problem.
00043    * @param h \f$h\f$; the scaling parameter (bandwidth).
00044    *
00045    * Although the kernel itself is not intrinsically dependent on \f$N\f$
00046    * and \f$h\f$, its normalisation is. Supplying these allows substantial
00047    * performance increases through precalculations.
00048    */
00049   GaussianKernel(const unsigned int N, const double h);
00050 
00051   /**
00052    * Destructor.
00053    */
00054   virtual ~GaussianKernel();
00055 
00056   virtual double operator()(const double x) const;
00057 
00058   /**
00059    * Sample from the kernel.
00060    *
00061    * @return A sample from the kernel.
00062    */
00063   virtual double sample() const;
00064   
00065 private:
00066   /**
00067    * \f$(h\sqrt{2\pi})^{-1}\f$; the normalisation term.
00068    */
00069   double ZI;
00070   
00071   /**
00072    * \f$(-2h^2)^{-1}\f$; the exponent term.
00073    */
00074   double E;
00075 
00076   /**
00077    * Serialize.
00078    */
00079   template<class Archive>
00080   void serialize(Archive& ar, const unsigned int version);
00081 
00082   /*
00083    * Boost.Serialization requirements.
00084    */
00085   friend class boost::serialization::access;
00086 
00087 };
00088 
00089     }
00090   }
00091 }
00092 
00093 #include "Random.hpp"
00094 
00095 #include <math.h>
00096 
00097 inline double indii::ml::aux::GaussianKernel::operator()(const double x)
00098     const {
00099   return ZI * exp(E * pow(x,2));
00100 }
00101 
00102 inline double indii::ml::aux::GaussianKernel::sample() const {
00103   return fabs(Random::gaussian(0.0, getBandwidth()));
00104 }
00105 
00106 template<class Archive>
00107 void indii::ml::aux::GaussianKernel::serialize(Archive& ar,
00108     const unsigned int version) {  
00109   ar & boost::serialization::base_object<Kernel>(*this);
00110   ar & ZI;
00111   ar & E;
00112 }
00113 
00114 #endif
00115 

Generated on Wed Dec 17 15:11:57 2008 for dysii Dynamical Systems Library by  doxygen 1.5.3