Advertisement
Guest User

KDE implementation

a guest
Jul 3rd, 2014
702
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. /**
  2.  * @brief Creates KDE function
  3.  * @param bandwidth
  4.  */
  5. KernelDensityEstimator::KernelDensityEstimator(double bandwidth) {
  6.   params.append(bandwidth);
  7.   kernel = NULL;
  8. }
  9. //---------------------------------------------------------------------------
  10.  
  11. /**
  12.  * @brief Evaluates function in x
  13.  * @param x
  14.  * @return y
  15.  */
  16. double KernelDensityEstimator::evalPrivate(const Point1D &x) const {
  17.   double h = getBandwidth();
  18.   double f = 1.0 / (data.size() * h);
  19.   double sum = 0;
  20.   for (int i = 0; i < data.size(); i++) {
  21.     sum += kernel->eval((x - data[i]) / h);
  22.   }
  23.   return f * sum;
  24. }
  25. //---------------------------------------------------------------------------
  26.  
  27. void KernelDensityEstimator::setKernel(IFunction1D* kernel) {
  28.   this->kernel = kernel;
  29. }
  30. //---------------------------------------------------------------------------
  31.  
  32. void KernelDensityEstimator::setData(const Points1D &data) {
  33.   this->data = data;
  34. }
  35. //---------------------------------------------------------------------------
  36. void KernelDensityEstimator::setBandwidth(double bandwidth) {
  37.   this->params[0] = bandwidth;
  38. }
  39. //---------------------------------------------------------------------------
  40.  
  41. double KernelDensityEstimator::getBandwidth() const {
  42.   return params.at(0);
  43. }
  44. //---------------------------------------------------------------------------
  45.  
  46. IFunction1D* KernelDensityEstimator::getKernel() const {
  47.   return kernel;
  48. }
  49. //---------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement