mlpack
epanechnikov_kernel_impl.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_CORE_KERNELS_EPANECHNIKOV_KERNEL_IMPL_HPP
13 #define MLPACK_CORE_KERNELS_EPANECHNIKOV_KERNEL_IMPL_HPP
14 
15 // In case it hasn't already been included.
16 #include "epanechnikov_kernel.hpp"
17 #include <mlpack/core/util/log.hpp>
18 
20 
21 namespace mlpack {
22 namespace kernel {
23 
24 template<typename VecTypeA, typename VecTypeB>
25 inline double EpanechnikovKernel::Evaluate(const VecTypeA& a, const VecTypeB& b)
26  const
27 {
28  return std::max(0.0, 1.0 - metric::SquaredEuclideanDistance::Evaluate(a, b)
29  * inverseBandwidthSquared);
30 }
31 
43 template<typename VecTypeA, typename VecTypeB>
44 double EpanechnikovKernel::ConvolutionIntegral(const VecTypeA& a,
45  const VecTypeB& b)
46 {
47  double distance = sqrt(metric::SquaredEuclideanDistance::Evaluate(a, b));
48  if (distance >= 2.0 * bandwidth)
49  return 0.0;
50 
51  double volumeSquared = std::pow(Normalizer(a.n_rows), 2.0);
52 
53  switch (a.n_rows)
54  {
55  case 1:
56  return 1.0 / volumeSquared *
57  (16.0 / 15.0 * bandwidth - 4.0 * distance * distance /
58  (3.0 * bandwidth) + 2.0 * distance * distance * distance /
59  (3.0 * bandwidth * bandwidth) -
60  std::pow(distance, 5.0) / (30.0 * std::pow(bandwidth, 4.0)));
61  case 2:
62  return 1.0 / volumeSquared *
63  ((2.0 / 3.0 * bandwidth * bandwidth - distance * distance) *
64  asin(sqrt(1.0 - std::pow(distance / (2.0 * bandwidth), 2.0))) +
65  sqrt(4.0 * bandwidth * bandwidth - distance * distance) *
66  (distance / 6.0 + 2.0 / 9.0 * distance *
67  std::pow(distance / bandwidth, 2.0) - distance / 72.0 *
68  std::pow(distance / bandwidth, 4.0)));
69  default:
70  Log::Fatal << "EpanechnikovKernel::ConvolutionIntegral(): dimension "
71  << a.n_rows << " not supported.";
72  return -1.0; // This line will not execute.
73  }
74 }
75 
77 template<typename Archive>
79  const uint32_t /* version */)
80 {
81  ar(CEREAL_NVP(bandwidth));
82  ar(CEREAL_NVP(inverseBandwidthSquared));
83 }
84 
85 } // namespace kernel
86 } // namespace mlpack
87 
88 #endif
static MLPACK_EXPORT util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.
Definition: log.hpp:90
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
void serialize(Archive &ar, const uint32_t version)
Serialize the kernel.
Definition: epanechnikov_kernel_impl.hpp:78
double ConvolutionIntegral(const VecTypeA &a, const VecTypeB &b)
Obtains the convolution integral [integral of K(||x-a||) K(||b-x||) dx] for the two vectors...
Definition: epanechnikov_kernel_impl.hpp:44
static VecTypeA::elem_type Evaluate(const VecTypeA &a, const VecTypeB &b)
Computes the distance between two points.
Definition: lmetric_impl.hpp:24
double Normalizer(const size_t dimension)
Compute the normalizer of this Epanechnikov kernel for the given dimension.
Definition: epanechnikov_kernel.cpp:22
double Evaluate(const VecTypeA &a, const VecTypeB &b) const
Evaluate the Epanechnikov kernel on the given two inputs.
Definition: epanechnikov_kernel_impl.hpp:25