kde_model.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_KDE_MODEL_HPP
13 #define MLPACK_METHODS_KDE_MODEL_HPP
14 
15 // Include trees.
20 
21 // Include core.
22 #include <mlpack/core.hpp>
23 
24 // Remaining includes.
25 #include "kde.hpp"
26 
27 namespace mlpack {
28 namespace kde {
29 
35 {
36  private:
37  // SFINAE check if Normalizer function is present.
38  HAS_MEM_FUNC(Normalizer, HasNormalizer);
39 
40  public:
42  template<typename KernelType>
43  static void ApplyNormalizer(
44  KernelType& /* kernel */,
45  const size_t /* dimension */,
46  arma::vec& /* estimations */,
47  const typename std::enable_if<
48  !HasNormalizer<KernelType, double(KernelType::*)(size_t)>::value>::
49  type* = 0)
50  { return; }
51 
53  template<typename KernelType>
54  static void ApplyNormalizer(
55  KernelType& kernel,
56  const size_t dimension,
57  arma::vec& estimations,
58  const typename std::enable_if<
59  HasNormalizer<KernelType, double(KernelType::*)(size_t)>::value>::
60  type* = 0)
61  {
62  estimations /= kernel.Normalizer(dimension);
63  }
64 };
65 
72 {
73  public:
77 
80  virtual KDEWrapperBase* Clone() const = 0;
81 
83  virtual ~KDEWrapperBase() { }
84 
86  virtual void Bandwidth(const double bw) = 0;
87 
89  virtual void RelativeError(const double relError) = 0;
90 
92  virtual void AbsoluteError(const double absError) = 0;
93 
95  virtual bool MonteCarlo() const = 0;
97  virtual bool& MonteCarlo() = 0;
98 
100  virtual void MCProb(const double mcProb) = 0;
101 
103  virtual size_t MCInitialSampleSize() const = 0;
105  virtual size_t& MCInitialSampleSize() = 0;
106 
108  virtual void MCEntryCoef(const double entryCoef) = 0;
109 
111  virtual void MCBreakCoef(const double breakCoef) = 0;
112 
114  virtual KDEMode Mode() const = 0;
116  virtual KDEMode& Mode() = 0;
117 
119  virtual void Train(util::Timers& timers, arma::mat&& referenceSet) = 0;
120 
122  virtual void Evaluate(util::Timers& timers,
123  arma::mat&& querySet,
124  arma::vec& estimates) = 0;
125 
127  virtual void Evaluate(util::Timers& timers, arma::vec& estimates) = 0;
128 };
129 
135 template<typename KernelType,
136  template<typename TreeMetricType,
137  typename TreeStatType,
138  typename TreeMatType> class TreeType>
140 {
141  public:
143  KDEWrapper(const double relError,
144  const double absError,
145  const KernelType& kernel) :
146  kde(relError, absError, kernel)
147  {
148  // Nothing left to do.
149  }
150 
153  virtual KDEWrapper* Clone() const { return new KDEWrapper(*this); }
154 
156  virtual ~KDEWrapper() { }
157 
159  virtual void Bandwidth(const double bw) { kde.Kernel() = KernelType(bw); }
160 
162  virtual void RelativeError(const double eps) { kde.RelativeError(eps); }
163 
165  virtual void AbsoluteError(const double eps) { kde.AbsoluteError(eps); }
166 
168  virtual bool MonteCarlo() const { return kde.MonteCarlo(); }
170  virtual bool& MonteCarlo() { return kde.MonteCarlo(); }
171 
173  virtual void MCProb(const double mcProb) { kde.MCProb(mcProb); }
174 
176  virtual size_t MCInitialSampleSize() const
177  {
178  return kde.MCInitialSampleSize();
179  }
181  virtual size_t& MCInitialSampleSize()
182  {
183  return kde.MCInitialSampleSize();
184  }
185 
187  virtual void MCEntryCoef(const double e) { kde.MCEntryCoef(e); }
188 
190  virtual void MCBreakCoef(const double b) { kde.MCBreakCoef(b); }
191 
193  virtual KDEMode Mode() const { return kde.Mode(); }
195  virtual KDEMode& Mode() { return kde.Mode(); }
196 
198  virtual void Train(util::Timers& timers, arma::mat&& referenceSet);
199 
201  virtual void Evaluate(util::Timers& timers,
202  arma::mat&& querySet,
203  arma::vec& estimates);
204 
206  virtual void Evaluate(util::Timers& timers, arma::vec& estimates);
207 
209  template<typename Archive>
210  void serialize(Archive& ar, const uint32_t /* version */)
211  {
212  ar(CEREAL_NVP(kde));
213  }
214 
215  protected:
216  typedef KDE<KernelType,
218  arma::mat,
219  TreeType> KDEType;
220 
222  KDEType kde;
223 };
224 
231 class KDEModel
232 {
233  public:
235  {
240  R_TREE
241  };
242 
244  {
249  TRIANGULAR_KERNEL
250  };
251 
252  private:
254  double bandwidth;
255 
257  double relError;
258 
260  double absError;
261 
263  KernelTypes kernelType;
264 
266  TreeTypes treeType;
267 
269  bool monteCarlo;
270 
273  double mcProb;
274 
276  size_t initialSampleSize;
277 
279  double mcEntryCoef;
280 
282  double mcBreakCoef;
283 
288  KDEWrapperBase* kdeModel;
289 
290  public:
315  KDEModel(const double bandwidth = 1.0,
316  const double relError = KDEDefaultParams::relError,
317  const double absError = KDEDefaultParams::absError,
318  const KernelTypes kernelType = KernelTypes::GAUSSIAN_KERNEL,
319  const TreeTypes treeType = TreeTypes::KD_TREE,
320  const bool monteCarlo = KDEDefaultParams::mode,
321  const double mcProb = KDEDefaultParams::mcProb,
322  const size_t initialSampleSize = KDEDefaultParams::initialSampleSize,
323  const double mcEntryCoef = KDEDefaultParams::mcEntryCoef,
324  const double mcBreakCoef = KDEDefaultParams::mcBreakCoef);
325 
327  KDEModel(const KDEModel& other);
328 
330  KDEModel(KDEModel&& other);
331 
337  KDEModel& operator=(const KDEModel& other);
338 
344  KDEModel& operator=(KDEModel&& other);
345 
347  ~KDEModel();
348 
350  template<typename Archive>
351  void serialize(Archive& ar, const uint32_t version);
352 
354  double Bandwidth() const { return bandwidth; }
355 
357  void Bandwidth(const double newBandwidth);
358 
360  double RelativeError() const { return relError; }
361 
363  void RelativeError(const double newRelError);
364 
366  double AbsoluteError() const { return absError; }
367 
369  void AbsoluteError(const double newAbsError);
370 
372  TreeTypes TreeType() const { return treeType; }
373 
375  TreeTypes& TreeType() { return treeType; }
376 
378  KernelTypes KernelType() const { return kernelType; }
379 
381  KernelTypes& KernelType() { return kernelType; }
382 
384  bool MonteCarlo() const { return monteCarlo; }
385 
387  void MonteCarlo(const bool newMonteCarlo);
388 
390  double MCProbability() const { return mcProb; }
391 
393  void MCProbability(const double newMCProb);
394 
396  size_t MCInitialSampleSize() const { return initialSampleSize; }
397 
399  void MCInitialSampleSize(const size_t newSampleSize);
400 
402  double MCEntryCoefficient() const { return mcEntryCoef; }
403 
405  void MCEntryCoefficient(const double newEntryCoef);
406 
408  double MCBreakCoefficient() const { return mcBreakCoef; }
409 
411  void MCBreakCoefficient(const double newBreakCoef);
412 
414  KDEMode Mode() const { return kdeModel->Mode(); }
415 
417  KDEMode& Mode() { return kdeModel->Mode(); }
418 
422  void InitializeModel();
423 
433  void BuildModel(util::Timers& timers, arma::mat&& referenceSet);
434 
447  void Evaluate(util::Timers& timers,
448  arma::mat&& querySet,
449  arma::vec& estimations);
450 
460  void Evaluate(util::Timers& timers, arma::vec& estimations);
461 
462 
463  private:
465  void CleanMemory();
466 };
467 
468 } // namespace kde
469 } // namespace mlpack
470 
471 #include "kde_model_impl.hpp"
472 
473 #endif
virtual void MCProb(const double mcProb)
Modify the Monte Carlo probability.
Definition: kde_model.hpp:173
double Bandwidth() const
Get the bandwidth of the kernel.
Definition: kde_model.hpp:354
static constexpr double relError
Relative error tolerance.
Definition: kde.hpp:35
virtual void MCEntryCoef(const double e)
Modify the Monte Carlo entry coefficient.
Definition: kde_model.hpp:187
double RelativeError() const
Get the relative error tolerance.
Definition: kde_model.hpp:360
Linear algebra utility functions, generally performed on matrices or vectors.
KDEMode
KDEMode represents the ways in which KDE algorithm can be executed.
Definition: kde.hpp:25
KDEType kde
The instantiated KDE object that we are wrapping.
Definition: kde_model.hpp:222
double MCEntryCoefficient() const
Get Monte Carlo entry coefficient.
Definition: kde_model.hpp:402
static constexpr double mcBreakCoef
Monte Carlo break coefficient.
Definition: kde.hpp:57
virtual KDEWrapper * Clone() const
Create a new KDEWrapper that is the same as this one.
Definition: kde_model.hpp:153
virtual size_t MCInitialSampleSize() const
Get the Monte Carlo sample size.
Definition: kde_model.hpp:176
virtual KDEMode Mode() const =0
Get the search mode.
virtual ~KDEWrapper()
Destruct the KDEWrapper (nothing to do).
Definition: kde_model.hpp:156
void serialize(Archive &ar, const uint32_t)
Serialize the KDE model.
Definition: kde_model.hpp:210
virtual KDEMode Mode() const
Get the search mode.
Definition: kde_model.hpp:193
virtual size_t & MCInitialSampleSize()
Modify the Monte Carlo sample size.
Definition: kde_model.hpp:181
KernelTypes & KernelType()
Modify the kernel type of the model.
Definition: kde_model.hpp:381
TreeTypes TreeType() const
Get the tree type of the model.
Definition: kde_model.hpp:372
virtual void Bandwidth(const double bw)
Modify the bandwidth of the kernel.
Definition: kde_model.hpp:159
static constexpr KDEMode mode
KDE algorithm mode.
Definition: kde.hpp:41
bool MonteCarlo() const
Get whether the model is using Monte Carlo estimations or not.
Definition: kde_model.hpp:384
KDEWrapper is a wrapper class for all KDE types supported by KDEModel.
Definition: kde_model.hpp:139
static constexpr double absError
Absolute error tolerance.
Definition: kde.hpp:38
virtual KDEMode & Mode()
Modify the search mode.
Definition: kde_model.hpp:195
KDEMode & Mode()
Modify the mode of the model.
Definition: kde_model.hpp:417
The KDE class is a template class for performing Kernel Density Estimations.
Definition: kde.hpp:88
double MCBreakCoefficient() const
Get Monte Carlo break coefficient.
Definition: kde_model.hpp:408
KDEMode Mode() const
Get the mode of the model.
Definition: kde_model.hpp:414
Include all of the base components required to write mlpack methods, and the main mlpack Doxygen docu...
static void ApplyNormalizer(KernelType &kernel, const size_t dimension, arma::vec &estimations, const typename std::enable_if< HasNormalizer< KernelType, double(KernelType::*)(size_t)>::value >::type *=0)
Normalize kernels that have normalizer.
Definition: kde_model.hpp:54
KDEWrapperBase is a base wrapper class for holding all KDE types supported by KDEModel.
Definition: kde_model.hpp:71
static void ApplyNormalizer(KernelType &, const size_t, arma::vec &, const typename std::enable_if< !HasNormalizer< KernelType, double(KernelType::*)(size_t)>::value >::type *=0)
Normalization not needed.
Definition: kde_model.hpp:43
virtual bool MonteCarlo() const
Get whether Monte Carlo search is being used.
Definition: kde_model.hpp:168
The KDEModel provides an abstraction for the KDE class, abstracting away the KernelType and TreeType ...
Definition: kde_model.hpp:231
double AbsoluteError() const
Get the absolute error tolerance.
Definition: kde_model.hpp:366
static constexpr size_t initialSampleSize
Initial sample size for Monte Carlo estimations.
Definition: kde.hpp:51
KDE< KernelType, metric::EuclideanDistance, arma::mat, TreeType > KDEType
Definition: kde_model.hpp:219
static constexpr double mcEntryCoef
Monte Carlo entry coefficient.
Definition: kde.hpp:54
TreeTypes & TreeType()
Modify the tree type of the model.
Definition: kde_model.hpp:375
virtual void AbsoluteError(const double eps)
Modify the absolute error tolerance.
Definition: kde_model.hpp:165
static constexpr double mcProb
Probability of a Monte Carlo estimation to be bounded by the relative error tolerance.
Definition: kde.hpp:48
virtual ~KDEWrapperBase()
Destruct the KDEWrapperBase (nothing to do).
Definition: kde_model.hpp:83
KDEWrapperBase()
Create the KDEWrapperBase object.
Definition: kde_model.hpp:76
void CleanMemory(util::Params &params)
Delete any unique pointers that are held by the IO object.
KernelNormalizer holds a set of methods to normalize estimations applying in each case the appropiate...
Definition: kde_model.hpp:34
LMetric< 2, true > EuclideanDistance
The Euclidean (L2) distance.
Definition: lmetric.hpp:112
KernelTypes KernelType() const
Get the kernel type of the model.
Definition: kde_model.hpp:378
KDEWrapper(const double relError, const double absError, const KernelType &kernel)
Create the KDEWrapper object, initializing the internally-held KDE object.
Definition: kde_model.hpp:143
virtual bool & MonteCarlo()
Modify whether Monte Carlo search is being used.
Definition: kde_model.hpp:170
double MCProbability() const
Get Monte Carlo probability of error being bounded by relative error.
Definition: kde_model.hpp:390
virtual void RelativeError(const double eps)
Modify the relative error tolerance.
Definition: kde_model.hpp:162
size_t MCInitialSampleSize() const
Get the initial sample size for Monte Carlo estimations.
Definition: kde_model.hpp:396
virtual void MCBreakCoef(const double b)
Modify the Monte Carlo break coefficient.
Definition: kde_model.hpp:190