indii/ml/aux/AlmostGaussianKernel.hpp

00001 #ifndef INDII_ML_AUX_ALMOSTGAUSSIANKERNEL_HPP
00002 #define INDII_ML_AUX_ALMOSTGAUSSIANKERNEL_HPP
00003 
00004 #include "Kernel.hpp"
00005 
00006 namespace indii {
00007   namespace ml {
00008     namespace aux {
00009 /**
00010  * Gaussian kernel for density estimation without squared exponent.
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 kernel takes the form:
00017  *
00018  * \f[
00019  *   K(x) = \frac{1}{\sqrt{2\pi}}e^{-\frac{1}{2}x}
00020  * \f]
00021  *
00022  * Note that the \f$x\f$ in the exponent is not squared as per the usual
00023  * Gaussian. This means that the kernel is actually a scaled Laplacian
00024  * (i.e. does not integrate to 1). Combining with Almost2Norm, however,
00025  * produces the same result as using PNorm<2> and GaussianKernel, but is
00026  * much more efficient, as the square root in the norm and square in the 
00027  * exponent of the Gaussian are cancelled.
00028  *
00029  * @see #hopt for guidance as to bandwidth selection.
00030  *
00031  * @section Serialization
00032  *
00033  * This class supports serialization through the Boost.Serialization
00034  * library.
00035  */
00036 class AlmostGaussianKernel : public Kernel {
00037 public:
00038   /**
00039    * Default constructor.
00040    *
00041    * This should generally only be used when the object is to be restored
00042    * from a serialization.
00043    */
00044   AlmostGaussianKernel();
00045 
00046   /**
00047    * Constructor.
00048    *
00049    * @param N \f$N\f$; dimensionality of the problem.
00050    * @param h \f$h\f$; the scaling parameter (bandwidth).
00051    *
00052    * Although the kernel itself is not intrinsically dependent on \f$N\f$
00053    * and \f$h\f$, its normalisation is. Supplying these allows substantial
00054    * performance increases through precalculationa.
00055    */
00056   AlmostGaussianKernel(const unsigned int N, const double h);
00057 
00058   /**
00059    * Destructor.
00060    */
00061   virtual ~AlmostGaussianKernel();
00062 
00063   virtual double operator()(const double x) const;
00064 
00065   /**
00066    * Sample from the kernel.
00067    *
00068    * @return A sample from the kernel.
00069    */
00070   virtual double sample() const;
00071   
00072 private:
00073   /**
00074    * \f$(h\sqrt{2\pi})^{-1}\f$; the normalisation term.
00075    */
00076   double ZI;
00077   
00078   /**
00079    * \f$(-2h^2)^{-1}\f$; the exponent term.
00080    */
00081   double E;
00082 
00083   /**
00084    * Serialize.
00085    */
00086   template<class Archive>
00087   void serialize(Archive& ar, const unsigned int version);
00088 
00089   /*
00090    * Boost.Serialization requirements.
00091    */
00092   friend class boost::serialization::access;
00093 
00094 };
00095 
00096     }
00097   }
00098 }
00099 
00100 #include "Random.hpp"
00101 
00102 #include <math.h>
00103 
00104 inline double indii::ml::aux::AlmostGaussianKernel::operator()(const double x)
00105     const {
00106   return ZI * exp(E * x);
00107 }
00108 
00109 inline double indii::ml::aux::AlmostGaussianKernel::sample() const {
00110   return fabs(Random::gaussian(0.0, getBandwidth()));
00111 }
00112 
00113 template<class Archive>
00114 void indii::ml::aux::AlmostGaussianKernel::serialize(Archive& ar,
00115     const unsigned int version) {  
00116   ar & boost::serialization::base_object<Kernel>(*this);
00117   ar & ZI;
00118   ar & E;
00119 }
00120 
00121 #endif
00122 

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