mlpack  1.0.12
gaussian_kernel.hpp
Go to the documentation of this file.
1 
16 #ifndef __MLPACK_CORE_KERNELS_GAUSSIAN_KERNEL_HPP
17 #define __MLPACK_CORE_KERNELS_GAUSSIAN_KERNEL_HPP
18 
19 #include <mlpack/core.hpp>
21 
22 namespace mlpack {
23 namespace kernel {
24 
36 {
37  public:
41  GaussianKernel() : bandwidth(1.0), gamma(-0.5)
42  { }
43 
49  GaussianKernel(const double bandwidth) :
50  bandwidth(bandwidth),
51  gamma(-0.5 * pow(bandwidth, -2.0))
52  { }
53 
65  template<typename VecType>
66  double Evaluate(const VecType& a, const VecType& b) const
67  {
68  // The precalculation of gamma saves us a little computation time.
70  }
71 
79  double Evaluate(const double t) const
80  {
81  // The precalculation of gamma saves us a little computation time.
82  return exp(gamma * std::pow(t, 2.0));
83  }
84 
91  double Normalizer(const size_t dimension)
92  {
93  return pow(sqrt(2.0 * M_PI) * bandwidth, (double) dimension);
94  }
95 
103  template<typename VecType>
104  double ConvolutionIntegral(const VecType& a, const VecType& b)
105  {
106  return Evaluate(sqrt(metric::SquaredEuclideanDistance::Evaluate(a, b) / 2.0)) /
107  (Normalizer(a.n_rows) * pow(2.0, (double) a.n_rows / 2.0));
108  }
109 
110 
112  double Bandwidth() const { return bandwidth; }
113 
116  void Bandwidth(const double bandwidth)
117  {
118  this->bandwidth = bandwidth;
119  this->gamma = -0.5 * pow(bandwidth, -2.0);
120  }
121 
123  double Gamma() const { return gamma; }
124 
126  std::string ToString() const
127  {
128  std::ostringstream convert;
129  convert << "GaussianKernel [" << this << "]" << std::endl;
130  convert << " Bandwidth: " << bandwidth << std::endl;
131  return convert.str();
132  }
133 
134  private:
136  double bandwidth;
137 
140  double gamma;
141 };
142 
144 template<>
146 {
147  public:
149  static const bool IsNormalized = true;
150 };
151 
152 }; // namespace kernel
153 }; // namespace mlpack
154 
155 #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:23
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:42
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.