npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
ConvolutionDensity1D.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_CONVOLUTIONDENSITY1D_HH_
2 #define NPSTAT_CONVOLUTIONDENSITY1D_HH_
3 
4 /*!
5 // \file ConvolutionDensity1D.hh
6 //
7 // \brief Calculates densities of the kind f(y) = Int K(x, y) f(x) dx,
8 // where f(x) can also be an empirical density (collection of points)
9 //
10 // Author: I. Volobouev
11 //
12 // December 2022
13 */
14 
15 #include <vector>
16 #include <utility>
17 
19 
20 namespace npstat {
21  template <class Kernel>
22  class ConvolutionDensity1D : public Functor1<long double,long double>
23  {
24  public:
25  typedef Kernel kernel_type;
26 
27  /**
28  // In the following constructor, the input functor "fcn" must represent
29  // a smooth 1-d density. If it is not normalized, it will be normalized
30  // internally. xmin and xmax represent the interval on which the integration
31  // is performed used Gauss-Legendre quadrature with nIntegPointsX points.
32  // Provide arguments nIntegPointsY, ymin, and ymax if you want automatic
33  // kernel normalization in the y direction (also with a Gauss-Legendre
34  // quadrature).
35  */
36  template<class Functor1D>
37  ConvolutionDensity1D(const Functor1D& fcn, const Kernel& kernel,
38  unsigned nIntegPointsX, double xmin, double xmax,
39  unsigned nIntegPointsY=0, double ymin=0., double ymax=0.);
40 
41  /**
42  // KDE. Provide arguments nIntegPointsY, ymin, and ymax if you
43  // want automatic kernel normalization in the y direction
44  // (that is, if you want to create a baloon estimator).
45  */
46  template<typename Numeric>
47  ConvolutionDensity1D(const Kernel& kernel,
48  const Numeric* coords, unsigned lenCoords,
49  unsigned nIntegPointsY=0, double ymin=0., double ymax=0.);
50 
51  /**
52  // KDE with weighted points. Provide arguments nIntegPointsY,
53  // ymin, and ymax if you want automatic kernel normalization
54  // in the y direction (that is, if you want to create a baloon estimator).
55  */
56  template<typename Numeric1, typename Numeric2>
57  ConvolutionDensity1D(const Kernel& kernel,
58  const Numeric1* coords, unsigned lenCoords,
59  const Numeric2* weights, unsigned lenWeights,
60  unsigned nIntegPointsY=0, double ymin=0., double ymax=0.);
61 
62  /**
63  // KDE with weighted points. The first argument of
64  // the pair is the coordinate and the second is the weight.
65  // Provide arguments nIntegPointsY, ymin, and ymax if you want
66  // automatic kernel normalization in the y direction (that is,
67  // if you want to create a baloon estimator).
68  */
69  template<typename Numeric1, typename Numeric2>
70  ConvolutionDensity1D(const Kernel& kernel,
71  const std::vector<std::pair<Numeric1,Numeric2> >& points,
72  unsigned nIntegPointsY=0, double ymin=0., double ymax=0.);
73 
74  inline virtual ~ConvolutionDensity1D() {}
75 
76  /** Convolution density at y */
77  long double operator()(const long double& y) const;
78 
79  /** Expected density variance at y for the unweighted sample of given size */
80  long double variance(long double y, long double sampleSize) const;
81 
82  /**
83  // Expected density covariance at (y, z)
84  // for the unweighted sample of given size
85  */
86  long double covariance(long double y, long double z,
87  long double sampleSize) const;
88  private:
89  void normalizeKernel(unsigned nIntegPointsY, double ymin, double ymax);
90 
91  Kernel kernel_;
92  std::vector<long double> xLocations_;
93  std::vector<long double> xDensity_;
94  std::vector<long double> norms_;
95  };
96 
97  // Helper class for calculating the variance part of the MISE.
98  // Do not use this class directly in the application code. Use
99  // "ConvolutionVarianceFunctor" instead.
100  template <class Kernel>
101  class ConvolutionVarianceFunctorHlp : public npstat::Functor1<long double,long double>
102  {
103  public:
105  const long double sampleSize)
106  : conv_(conv), sampleSize_(sampleSize) {}
107 
108  inline virtual ~ConvolutionVarianceFunctorHlp() {}
109 
110  inline virtual long double operator()(const long double& y) const
111  {return conv_.variance(y, sampleSize_);}
112 
113  private:
114  const ConvolutionDensity1D<Kernel>& conv_;
115  long double sampleSize_;
116  };
117 
118  // Functor for calculating the variance part of the MISE
119  template <class Kernel>
120  inline ConvolutionVarianceFunctorHlp<Kernel> ConvolutionVarianceFunctor(
121  const ConvolutionDensity1D<Kernel>& conv, const long double sampleSize)
122  {
123  return ConvolutionVarianceFunctorHlp<Kernel>(conv, sampleSize);
124  }
125 
126  // Do not use this class directly in the application code. Use
127  // "ConvolutionCovarianceFunctor" instead.
128  template <class Kernel>
129  class ConvolutionCovarianceFunctorHlp : public npstat::Functor2<long double,long double,long double>
130  {
131  public:
133  const long double sampleSize)
134  : conv_(conv), sampleSize_(sampleSize) {}
135 
136  inline virtual ~ConvolutionCovarianceFunctorHlp() {}
137 
138  inline virtual long double operator()(const long double& y, const long double& z) const
139  {return conv_.covariance(y, z, sampleSize_);}
140 
141  private:
142  const ConvolutionDensity1D<Kernel>& conv_;
143  long double sampleSize_;
144  };
145 
146  template <class Kernel>
147  inline ConvolutionCovarianceFunctorHlp<Kernel> ConvolutionCovarianceFunctor(
148  const ConvolutionDensity1D<Kernel>& conv, const long double sampleSize)
149  {
150  return ConvolutionCovarianceFunctorHlp<Kernel>(conv, sampleSize);
151  }
152 }
153 
154 #include "npstat/nm/ConvolutionDensity1D.icc"
155 
156 #endif // NPSTAT_CONVOLUTIONDENSITY1D_HH_
Interface definitions and concrete simple functors for a variety of functor-based calculations.
Definition: ConvolutionDensity1D.hh:130
Definition: ConvolutionDensity1D.hh:23
ConvolutionDensity1D(const Functor1D &fcn, const Kernel &kernel, unsigned nIntegPointsX, double xmin, double xmax, unsigned nIntegPointsY=0, double ymin=0., double ymax=0.)
long double variance(long double y, long double sampleSize) const
ConvolutionDensity1D(const Kernel &kernel, const Numeric1 *coords, unsigned lenCoords, const Numeric2 *weights, unsigned lenWeights, unsigned nIntegPointsY=0, double ymin=0., double ymax=0.)
ConvolutionDensity1D(const Kernel &kernel, const std::vector< std::pair< Numeric1, Numeric2 > > &points, unsigned nIntegPointsY=0, double ymin=0., double ymax=0.)
long double operator()(const long double &y) const
ConvolutionDensity1D(const Kernel &kernel, const Numeric *coords, unsigned lenCoords, unsigned nIntegPointsY=0, double ymin=0., double ymax=0.)
long double covariance(long double y, long double z, long double sampleSize) const
Definition: ConvolutionDensity1D.hh:102
Definition: AbsArrayProjector.hh:14
Definition: SimpleFunctors.hh:58
Definition: SimpleFunctors.hh:89