MLPACK  1.0.10
gaussian_kernel.hpp
Go to the documentation of this file.
1 
24 #ifndef __MLPACK_CORE_KERNELS_GAUSSIAN_KERNEL_HPP
25 #define __MLPACK_CORE_KERNELS_GAUSSIAN_KERNEL_HPP
26 
27 #include <mlpack/core.hpp>
29 
30 namespace mlpack {
31 namespace kernel {
32 
44 {
45  public:
49  GaussianKernel() : bandwidth(1.0), gamma(-0.5)
50  { }
51 
57  GaussianKernel(const double bandwidth) :
58  bandwidth(bandwidth),
59  gamma(-0.5 * pow(bandwidth, -2.0))
60  { }
61 
73  template<typename VecType>
74  double Evaluate(const VecType& a, const VecType& b) const
75  {
76  // The precalculation of gamma saves us a little computation time.
78  }
79 
87  double Evaluate(const double t) const
88  {
89  // The precalculation of gamma saves us a little computation time.
90  return exp(gamma * std::pow(t, 2.0));
91  }
92 
99  double Normalizer(const size_t dimension)
100  {
101  return pow(sqrt(2.0 * M_PI) * bandwidth, (double) dimension);
102  }
103 
111  template<typename VecType>
112  double ConvolutionIntegral(const VecType& a, const VecType& b)
113  {
114  return Evaluate(sqrt(metric::SquaredEuclideanDistance::Evaluate(a, b) / 2.0)) /
115  (Normalizer(a.n_rows) * pow(2.0, (double) a.n_rows / 2.0));
116  }
117 
118 
120  double Bandwidth() const { return bandwidth; }
121 
124  void Bandwidth(const double bandwidth)
125  {
126  this->bandwidth = bandwidth;
127  this->gamma = -0.5 * pow(bandwidth, -2.0);
128  }
129 
131  double Gamma() const { return gamma; }
132 
134  std::string ToString() const
135  {
136  std::ostringstream convert;
137  convert << "GaussianKernel [" << this << "]" << std::endl;
138  convert << " Bandwidth: " << bandwidth << std::endl;
139  return convert.str();
140  }
141 
142  private:
144  double bandwidth;
145 
148  double gamma;
149 };
150 
152 template<>
154 {
155  public:
157  static const bool IsNormalized = true;
158 };
159 
160 }; // namespace kernel
161 }; // namespace mlpack
162 
163 #endif
GaussianKernel()
Default constructor; sets bandwidth to 1.0.
This is a template class that can provide information about various kernels.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: load.hpp:31
void Bandwidth(const double bandwidth)
Modify the bandwidth.
double Evaluate(const double t) const
Evaluation of the Gaussian kernel given the distance between two points.
double gamma
Precalculated constant depending on the bandwidth; .
#define M_PI
Definition: prereqs.hpp:50
double ConvolutionIntegral(const VecType &a, const VecType &b)
Obtain a convolution integral of the Gaussian kernel.
GaussianKernel(const double bandwidth)
Construct the Gaussian kernel with a custom bandwidth.
static double Evaluate(const VecType1 &a, const VecType2 &b)
Computes the distance between two points.
double Normalizer(const size_t dimension)
Obtain the normalization constant of the Gaussian kernel.
double Bandwidth() const
Get the bandwidth.
double Evaluate(const VecType &a, const VecType &b) const
Evaluation of the Gaussian kernel.
The standard Gaussian kernel.
static const bool IsNormalized
If true, then the kernel is normalized: K(x, x) = K(y, y) = 1 for all x.
double Gamma() const
Get the precalculated constant.
double bandwidth
Kernel bandwidth.
std::string ToString() const
Convert object to string.