npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
KDE1D.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_KDE1D_HH_
2 #define NPSTAT_KDE1D_HH_
3 
4 /*!
5 // \file KDE1D.hh
6 //
7 // \brief Convenience class which aggregates the kernel and the data
8 // for brute-force 1-d KDE without boundary correction
9 //
10 // Author: I. Volobouev
11 //
12 // November 2018
13 */
14 
15 #include <cassert>
16 #include <stdexcept>
17 #include <vector>
18 #include <algorithm>
19 
21 #include "npstat/stat/KDE1DCV.hh"
22 
23 namespace npstat {
24  template <class Numeric>
25  class KDE1D
26  {
27  public:
28  inline KDE1D(const AbsKDE1DKernel& i_kernel,
29  const Numeric* i_coords, const unsigned long i_nCoords)
30  : kernel_(0), coords_(i_coords, i_coords + i_nCoords)
31  {
32  if (coords_.empty()) throw std::invalid_argument(
33  "In npstat::KDE1D constructor: empty point sample");
34  assert(i_coords);
35  std::sort(coords_.begin(), coords_.end());
36  kernel_ = i_kernel.clone();
37  }
38 
39  inline KDE1D(const AbsKDE1DKernel& i_kernel,
40  const std::vector<Numeric>& i_coords)
41  : kernel_(0), coords_(i_coords)
42  {
43  if (coords_.empty()) throw std::invalid_argument(
44  "In npstat::KDE1D constructor: empty point sample");
45  std::sort(coords_.begin(), coords_.end());
46  kernel_ = i_kernel.clone();
47  }
48 
49  inline KDE1D(const KDE1D& r)
50  : kernel_(r.kernel_->clone()), coords_(r.coords_) {}
51 
52  inline KDE1D& operator=(const KDE1D& r)
53  {
54  if (&r != this)
55  {
56  delete kernel_; kernel_ = 0;
57  coords_ = r.coords_;
58  kernel_ = r.kernel_->clone();
59  }
60  return *this;
61  }
62 
63  inline ~KDE1D() {delete kernel_;}
64 
65  inline void setNormFactor(const double d) {kernel_->setNormFactor(d);}
66 
67  inline void setSample(const Numeric* i_coords, const unsigned long i_nCoords)
68  {
69  if (!i_nCoords) throw std::invalid_argument(
70  "In npstat::KDE1D::setSample: empty point sample");
71  assert(i_coords);
72  coords_.clear();
73  coords_.reserve(i_nCoords);
74  std::copy(i_coords, i_coords+i_nCoords, std::back_inserter(coords_));
75  std::sort(coords_.begin(), coords_.end());
76  }
77 
78  inline void setSample(const std::vector<Numeric>& i_coords)
79  {
80  if (i_coords.empty()) throw std::invalid_argument(
81  "In npstat::KDE1D::setSample: empty point sample");
82  coords_ = i_coords;
83  std::sort(coords_.begin(), coords_.end());
84  }
85 
86  inline const AbsKDE1DKernel& kernel() const {return *kernel_;}
87  inline double normFactor() const {return kernel_->normFactor();}
88  inline const std::vector<Numeric>& coords() const {return coords_;}
89  inline unsigned long nCoords() const {return coords_.size();}
90  inline Numeric minCoordinate() const {return coords_[0];}
91  inline Numeric maxCoordinate() const {return coords_.back();}
92 
93  inline double density(const double x, const double bw) const
94  {return kernel_->kde(x, bw, &coords_[0], coords_.size(), true);}
95 
96  //@{
97  /**
98  // The lifetime of the lightweight functor returned by this method
99  // must not exceed the lifetime of this object
100  */
102  const double bandwidth) const
103  {
105  *kernel_, bandwidth, &coords_[0], coords_.size(), true, false);
106  }
107 
108  inline KDE1DRLCVFunctorHelper<Numeric> rlcvFunctor(
109  const double plcvAlpha) const
110  {
111  return KDE1DRLCVFunctor(*kernel_, plcvAlpha,
112  &coords_[0], coords_.size(), true);
113  }
114 
115  inline KDE1DLSCVFunctorHelper<Numeric> lscvFunctor(
116  const double xmin, const double xmax,
117  const unsigned nIntegIntervals, const unsigned nIntegPoints) const
118  {
119  return KDE1DLSCVFunctor(*kernel_, xmin, xmax,
120  nIntegIntervals, nIntegPoints,
121  &coords_[0], coords_.size(), true);
122  }
123  //@}
124 
125  inline double densityIntegral(
126  const double xmin, const double xmax,
127  const unsigned nIntegIntervals, const unsigned nIntegPoints,
128  const double bandwidth) const
129  {
130  GaussLegendreQuadrature glq(nIntegPoints);
131  return glq.integrate(this->densityFunctor(bandwidth),
132  xmin, xmax, nIntegIntervals);
133  }
134 
135  inline double integratedSquaredError(
136  const AbsDistribution1D& distro,
137  const unsigned nIntegIntervals, const unsigned nIntegPoints,
138  const double bandwidth) const
139  {
140  return kernel_->integratedSquaredError(
141  distro, nIntegIntervals, nIntegPoints, bandwidth,
142  false, &coords_[0], coords_.size(), true);
143  }
144 
145  inline double rlcv(const double bw, const double plcvAlpha) const
146  {
147  return KDE1DRLCVFunctor(*kernel_, plcvAlpha,
148  &coords_[0], coords_.size(), true)(bw);
149  }
150 
151  inline double lscv(const double bw,
152  const double xmin, const double xmax,
153  const unsigned nIntegIntervals,
154  const unsigned nIntegPoints) const
155  {
156  return KDE1DLSCVFunctor(*kernel_, xmin, xmax,
157  nIntegIntervals, nIntegPoints,
158  &coords_[0], coords_.size(), true)(bw);
159  }
160 
161  private:
162  AbsKDE1DKernel* kernel_;
163  std::vector<Numeric> coords_;
164  };
165 }
166 
167 #endif // NPSTAT_KDE1D_HH_
Gauss-Legendre quadratures in long double precision.
Cross-validation utilities for brute-force KDE in 1-d.
Definition: AbsKDE1DKernel.hh:23
double integratedSquaredError(const AbsDistribution1D &distro, unsigned nIntegIntervals, unsigned nIntegPoints, double bandwidth, bool useReverseKde, const Numeric *coords, unsigned long nCoords, bool coordinatesSorted=false) const
double kde(double x, double bandwidth, const Numeric *coords, unsigned long nCoords, bool coordinatesSorted=false) const
virtual AbsKDE1DKernel * clone() const =0
Definition: AbsKDE1DKernel.hh:234
Definition: KDE1DCV.hh:107
Definition: KDE1D.hh:26
KDE1DFunctorHelper< Numeric > densityFunctor(const double bandwidth) const
Definition: KDE1D.hh:101
Definition: AbsArrayProjector.hh:14
KDE1DRLCVFunctorHelper< Numeric > KDE1DRLCVFunctor(const AbsKDE1DKernel &kernel, const double plcvAlpha, const Numeric *coords, const unsigned long nCoords, const bool coordinatesSorted=false, const bool useReverseKde=false)
Definition: KDE1DCV.hh:160
KDE1DLSCVFunctorHelper< Numeric > KDE1DLSCVFunctor(const AbsKDE1DKernel &kernel, const double xmin, const double xmax, const unsigned nIntegIntervals, const unsigned nIntegPoints, const Numeric *coords, const unsigned long nCoords, const bool coordinatesSorted=false, const bool useReverseKde=false)
Definition: KDE1DCV.hh:82