kde_rules.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_KDE_RULES_HPP
14 #define MLPACK_METHODS_KDE_RULES_HPP
15 
17 
18 namespace mlpack {
19 namespace kde {
20 
25 template<typename MetricType, typename KernelType, typename TreeType>
26 class KDERules
27 {
28  public:
49  KDERules(const arma::mat& referenceSet,
50  const arma::mat& querySet,
51  arma::vec& densities,
52  const double relError,
53  const double absError,
54  const double mcProb,
55  const size_t initialSampleSize,
56  const double mcAccessCoef,
57  const double mcBreakCoef,
58  MetricType& metric,
59  KernelType& kernel,
60  const bool monteCarlo,
61  const bool sameSet);
62 
64  double BaseCase(const size_t queryIndex, const size_t referenceIndex);
65 
67  double Score(const size_t queryIndex, TreeType& referenceNode);
68 
70  double Rescore(const size_t queryIndex,
71  TreeType& referenceNode,
72  const double oldScore) const;
73 
75  double Score(TreeType& queryNode, TreeType& referenceNode);
76 
78  double Rescore(TreeType& queryNode,
79  TreeType& referenceNode,
80  const double oldScore) const;
81 
83 
85  const TraversalInfoType& TraversalInfo() const { return traversalInfo; }
86 
88  TraversalInfoType& TraversalInfo() { return traversalInfo; }
89 
91  size_t BaseCases() const { return baseCases; }
92 
94  size_t Scores() const { return scores; }
95 
98  size_t MinimumBaseCases() const { return 0; }
99 
100  private:
102  double EvaluateKernel(const size_t queryIndex,
103  const size_t referenceIndex) const;
104 
106  double EvaluateKernel(const arma::vec& query,
107  const arma::vec& reference) const;
108 
110  double CalculateAlpha(TreeType* node);
111 
113  const arma::mat& referenceSet;
114 
116  const arma::mat& querySet;
117 
119  arma::vec& densities;
120 
122  const double absError;
123 
125  const double relError;
126 
129  const double mcBeta;
130 
132  const size_t initialSampleSize;
133 
137  const double mcAccessCoef;
138 
141  const double mcBreakCoef;
142 
144  MetricType& metric;
145 
147  KernelType& kernel;
148 
150  const bool monteCarlo;
151 
153  arma::vec accumMCAlpha;
154 
156  arma::vec accumError;
157 
159  const bool sameSet;
160 
162  constexpr static bool kernelIsGaussian =
163  std::is_same<KernelType, kernel::GaussianKernel>::value;
164 
166  const double absErrorTol;
167 
169  size_t lastQueryIndex;
170 
172  size_t lastReferenceIndex;
173 
175  TraversalInfoType traversalInfo;
176 
178  size_t baseCases;
179 
181  size_t scores;
182 };
183 
188 template<typename TreeType>
190 {
191  public:
193  KDECleanRules() { /* Nothing to do. */ }
194 
196  double BaseCase(const size_t /* queryIndex */, const size_t /* refIndex */);
197 
199  double Score(const size_t /* queryIndex */, TreeType& referenceNode);
200 
202  double Rescore(const size_t /* queryIndex */,
203  TreeType& /* referenceNode */,
204  const double oldScore) const { return oldScore; }
205 
207  double Score(TreeType& queryNode, TreeType& referenceNode);
208 
210  double Rescore(TreeType& /* queryNode */,
211  TreeType& /* referenceNode*/ ,
212  const double oldScore) const { return oldScore; }
213 
215 
217  const TraversalInfoType& TraversalInfo() const { return traversalInfo; }
218 
220  TraversalInfoType& TraversalInfo() { return traversalInfo; }
221 
222  private:
224  TraversalInfoType traversalInfo;
225 };
226 
227 } // namespace kde
228 } // namespace mlpack
229 
230 // Include implementation.
231 #include "kde_rules_impl.hpp"
232 
233 #endif
double Score(const size_t queryIndex, TreeType &referenceNode)
SingleTree Rescore.
const TraversalInfoType & TraversalInfo() const
Get traversal information.
Definition: kde_rules.hpp:85
The TraversalInfo class holds traversal information which is used in dual-tree (and single-tree) trav...
Linear algebra utility functions, generally performed on matrices or vectors.
tree::TraversalInfo< TreeType > TraversalInfoType
Definition: kde_rules.hpp:82
A dual-tree traversal Rules class for cleaning used trees before performing kernel density estimation...
Definition: kde_rules.hpp:189
TraversalInfoType & TraversalInfo()
Modify traversal information.
Definition: kde_rules.hpp:220
const TraversalInfoType & TraversalInfo() const
Get traversal information.
Definition: kde_rules.hpp:217
double Rescore(const size_t queryIndex, TreeType &referenceNode, const double oldScore) const
SingleTree Score.
double Rescore(const size_t, TreeType &, const double oldScore) const
SingleTree Rescore.
Definition: kde_rules.hpp:202
size_t MinimumBaseCases() const
Get the minimum number of base cases we need to perform to have acceptable results.
Definition: kde_rules.hpp:98
double BaseCase(const size_t queryIndex, const size_t referenceIndex)
Base Case.
double Rescore(TreeType &, TreeType &, const double oldScore) const
Dual-Tree Rescore.
Definition: kde_rules.hpp:210
TraversalInfoType & TraversalInfo()
Modify traversal information.
Definition: kde_rules.hpp:88
size_t BaseCases() const
Get the number of base cases.
Definition: kde_rules.hpp:91
A dual-tree traversal Rules class for kernel density estimation.
Definition: kde_rules.hpp:26
tree::TraversalInfo< TreeType > TraversalInfoType
Definition: kde_rules.hpp:214
size_t Scores() const
Get the number of scores.
Definition: kde_rules.hpp:94
KDECleanRules()
Construct KDECleanRules.
Definition: kde_rules.hpp:193
KDERules(const arma::mat &referenceSet, const arma::mat &querySet, arma::vec &densities, const double relError, const double absError, const double mcProb, const size_t initialSampleSize, const double mcAccessCoef, const double mcBreakCoef, MetricType &metric, KernelType &kernel, const bool monteCarlo, const bool sameSet)
Construct KDERules.