npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
KDE1DCV.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_KDE1DCV_HH_
2 #define NPSTAT_KDE1DCV_HH_
3 
4 /*!
5 // \file KDE1DCV.hh
6 //
7 // \brief Cross-validation utilities for brute-force KDE in 1-d
8 //
9 // Author: I. Volobouev
10 //
11 // November 2018
12 */
13 
15 
16 namespace npstat {
17  /**
18  // A lightweight functor for getting negative LSCV values (that is,
19  // quantity to be maximized). It will not copy the kernel and will
20  // not own the data. It is the responsibility of the user of this
21  // class to ensure that the lifetimes of the kernel and the data
22  // exceed the lifetime of the functor.
23  //
24  // Intended usage is via the "KDE1DLSCVFunctor" utility function.
25  */
26  template <typename Numeric>
27  class KDE1DLSCVFunctorHelper : public Functor1<double, double>
28  {
29  public:
30  inline KDE1DLSCVFunctorHelper(const AbsKDE1DKernel& kernel,
31  const double xmin, const double xmax,
32  const unsigned nIntegIntervals,
33  const unsigned nIntegPoints,
34  const Numeric* coords,
35  const unsigned long nCoords,
36  const bool coordinatesSorted,
37  const bool useReverseKde)
38  : kernel_(kernel), xmin_(xmin), xmax_(xmax),
39  nIntegIntervals_(nIntegIntervals), nIntegPoints_(nIntegPoints),
40  coords_(coords), nCoords_(nCoords),
41  coordinatesSorted_(coordinatesSorted),
42  useReverseKde_(useReverseKde) {}
43 
44  inline virtual ~KDE1DLSCVFunctorHelper() {}
45 
46  inline double operator()(const double& bw) const
47  {
48  long double msum = 0.0L;
49  for (unsigned long i=0; i<nCoords_; ++i)
50  {
51  const double x = coords_[i];
52  double kde;
53  if (useReverseKde_)
54  kde = kernel_.reverseKde(x, bw, coords_, nCoords_,
55  coordinatesSorted_);
56  else
57  kde = kernel_.kde(x, bw, coords_, nCoords_,
58  coordinatesSorted_);
59  const double loo = kernel_.looKde(bw, nCoords_, kde);
60  msum += loo;
61  }
62  const double integ = kernel_.integratedKdeSquared(
63  xmin_, xmax_, nIntegIntervals_, nIntegPoints_,
64  bw, useReverseKde_, coords_, nCoords_, coordinatesSorted_);
65  return -(integ - msum*2.0/nCoords_);
66  }
67 
68  private:
69  const AbsKDE1DKernel& kernel_;
70  double xmin_;
71  double xmax_;
72  unsigned nIntegIntervals_;
73  unsigned nIntegPoints_;
74  const Numeric* coords_;
75  unsigned long nCoords_;
76  bool coordinatesSorted_;
77  bool useReverseKde_;
78  };
79 
80  /** A convenience function for creating KDE1DLSCVFunctorHelper objects */
81  template <typename Numeric>
83  const AbsKDE1DKernel& kernel,
84  const double xmin, const double xmax,
85  const unsigned nIntegIntervals,
86  const unsigned nIntegPoints,
87  const Numeric* coords,
88  const unsigned long nCoords,
89  const bool coordinatesSorted = false,
90  const bool useReverseKde = false)
91  {
93  kernel, xmin, xmax, nIntegIntervals, nIntegPoints,
94  coords, nCoords, coordinatesSorted, useReverseKde);
95  }
96 
97  /**
98  // A lightweight functor for getting log RLCV values. It will not
99  // copy the kernel and will not own the data. It is the responsibility
100  // of the user of this class to ensure that the lifetimes of the kernel
101  // and the data exceed the lifetime of the functor.
102  //
103  // Intended usage is via the "KDE1DRLCVFunctor" utility function.
104  */
105  template <typename Numeric>
106  class KDE1DRLCVFunctorHelper : public Functor1<double, double>
107  {
108  public:
109  inline KDE1DRLCVFunctorHelper(const AbsKDE1DKernel& kernel,
110  const double plcvAlpha,
111  const Numeric* coords,
112  const unsigned long nCoords,
113  const bool coordinatesSorted,
114  const bool useReverseKde)
115  : kernel_(kernel), plcvAlpha_(plcvAlpha),
116  coords_(coords), nCoords_(nCoords),
117  coordinatesSorted_(coordinatesSorted),
118  useReverseKde_(useReverseKde) {}
119 
120  inline virtual ~KDE1DRLCVFunctorHelper() {}
121 
122  inline double operator()(const double& bw) const
123  {
124  const double nPt = nCoords_;
125  const double selfC = kernel_(0.0)/bw/nPt;
126  const double minDens = selfC/pow(nPt, plcvAlpha_);
127  const double logm = log(minDens);
128 
129  long double msum = 0.0L;
130  for (unsigned long i=0; i<nCoords_; ++i)
131  {
132  const double x = coords_[i];
133  double kde;
134  if (useReverseKde_)
135  kde = kernel_.reverseKde(x, bw, coords_, nCoords_,
136  coordinatesSorted_);
137  else
138  kde = kernel_.kde(x, bw, coords_, nCoords_,
139  coordinatesSorted_);
140  const double loo = kernel_.looKde(bw, nCoords_, kde);
141  if (loo > minDens)
142  msum += log(loo);
143  else
144  msum += logm;
145  }
146  return msum;
147  }
148 
149  private:
150  const AbsKDE1DKernel& kernel_;
151  double plcvAlpha_;
152  const Numeric* coords_;
153  unsigned long nCoords_;
154  bool coordinatesSorted_;
155  bool useReverseKde_;
156  };
157 
158  /** A convenience function for creating KDE1DRLCVFunctorHelper objects */
159  template <typename Numeric>
161  const AbsKDE1DKernel& kernel,
162  const double plcvAlpha,
163  const Numeric* coords,
164  const unsigned long nCoords,
165  const bool coordinatesSorted = false,
166  const bool useReverseKde = false)
167  {
169  kernel, plcvAlpha, coords, nCoords,
170  coordinatesSorted, useReverseKde);
171  }
172 }
173 
174 #endif // NPSTAT_KDE1DCV_HH_
Brute-force non-discretized KDE in 1-d. No boundary correction.
Definition: AbsKDE1DKernel.hh:23
double reverseKde(double x, double bandwidth, 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
double looKde(double bandwidth, unsigned long nCoords, double originalDensityEstimate) const
double integratedKdeSquared(double xmin, double xmax, unsigned nIntegIntervals, unsigned nIntegPoints, double bandwidth, bool useReverseKde, const Numeric *coords, unsigned long nCoords, bool coordinatesSorted=false) const
Definition: KDE1DCV.hh:28
Definition: KDE1DCV.hh:107
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
Definition: SimpleFunctors.hh:58