npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
KernelSensitivityCalculator.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_KERNELSENSITIVITYCALCULATOR_HH_
2 #define NPSTAT_KERNELSENSITIVITYCALCULATOR_HH_
3 
4 /*!
5 // \file KernelSensitivityCalculator.hh
6 //
7 // \brief Helper class for calculating kernel sensitivity matrices
8 //
9 // Author: I. Volobouev
10 //
11 // March 2023
12 */
13 
14 #include <utility>
15 
16 #include "npstat/nm/Matrix.hh"
18 
20 
21 namespace npstat {
23  {
24  public:
25  /**
26  // Oracle constructor. Arguments are as follows:
27  //
28  // kernel The kernel functor, K(x,y). Must have a method
29  // with a signature similar to
30  // "double operator()(double x, double y) const".
31  //
32  // distro The oracle distribution.
33  //
34  // maxdegInputPoly Maximum degree of P_k(x) polynomials.
35  //
36  // maxdegOutPoly Maximum degree of Q_j(y) polynomials.
37  //
38  // ymin, ymax The interval on which the KDE of the oracle
39  // density is supported.
40  //
41  // xIntegrator Numerical quadrature object for calculating
42  // integrals in x.
43  //
44  // yIntegrator Numerical quadrature object for calculating
45  // integrals in y.
46  //
47  // normalizeKernel Set this to "true" in order to normalize the
48  // kernel internally so that Integral K(x,y) dy = 1
49  // for every x. Set this to "false" if the kernel
50  // is already normalized.
51  */
52  template <class Kernel>
54  const Kernel& kernel, const AbsDistribution1D& distro,
55  unsigned maxdegInputPoly, unsigned maxdegOutPoly,
56  long double ymin, long double ymax,
57  const AbsIntervalQuadrature1D& xIntegrator,
58  const AbsIntervalQuadrature1D& yIntegrator,
59  bool normalizeKernel = true, bool validateCDF = true);
60 
61  /**
62  // Non-oracle constructor. Arguments are as follows:
63  //
64  // kernel The kernel functor, K(x,y). Must have a method
65  // with a signature similar to
66  // "double operator()(double x, double y) const".
67  //
68  // sample The sample of points on which to perform KDE.
69  //
70  // maxdegInputPoly Maximum degree of P_k(x) polynomials.
71  //
72  // maxdegOutPoly Maximum degree of Q_j(y) polynomials.
73  //
74  // ymin, ymax The interval on which the KDE is supported.
75  //
76  // yIntegrator Numerical quadrature object for calculating
77  // integrals in y.
78  //
79  // normalizeKernel Set this to "true" in order to normalize the
80  // kernel internally so that Integral K(x,y) dy = 1
81  // for every x. Set this to "false" if the kernel
82  // is already normalized.
83  //
84  // validateCDF If "true", the code will check that all values
85  // of the KDE cumulative distribution function
86  // belong to the [0, 1] interval and will throw
87  // an exception if this is not the case. If "false",
88  // this check will not be performed.
89  */
90  template <class Kernel>
92  const Kernel& kernel, const std::vector<double>& sample,
93  unsigned maxdegInputPoly, unsigned maxdegOutPoly,
94  long double ymin, long double ymax,
95  const AbsIntervalQuadrature1D& yIntegrator,
96  bool normalizeKernel = true, bool validateCDF = true);
97 
98  inline bool isOracle() const {return oracle_;}
99  inline unsigned maxdegInputPoly() const {return maxdegInputPoly_;}
100  inline unsigned maxdegOutPoly() const {return maxdegOutPoly_;}
101 
102  /**
103  // Return the "standard" kernel sensitivity matrix.
104  //
105  // "inputPoly" is the OPS orthonormal with the weight
106  // "distro" (oracle case) or with the discrete measure
107  // generated by the sample (non-oracle case).
108  //
109  // "outPoly" is the OPS orthonormal with the weight
110  // given by the KDE density.
111  */
112  template<class InPoly, class OutPoly>
114  const InPoly& inputPoly, const OutPoly& outPoly) const;
115 
116  /**
117  // Return the kernel sensitivity matrix that uses
118  // comparison density in y.
119  //
120  // "inputPoly" is the OPS orthonormal with the weight
121  // "distro" (oracle case) or with the discrete measure
122  // generated by the sample (non-oracle case).
123  //
124  // "cdf" is the functor that returns the cumulative
125  // distribution function for the KDE density.
126  */
127  template<class InPoly, class KDECdf>
129  const InPoly& inputPoly, const KDECdf& cdf) const;
130 
131  /**
132  // Return the kernel sensitivity matrix that uses
133  // comparison density in x.
134  //
135  // "outPoly" is the OPS orthonormal with the weight
136  // given by the KDE density.
137  */
138  template<class OutPoly>
139  Matrix<double> sMatrix2(const OutPoly& outPoly) const;
140 
141  /**
142  // Return the kernel sensitivity matrix that uses
143  // comparison density in both x and y.
144  //
145  // "cdf" is the functor that returns the cumulative
146  // distribution function for the KDE density.
147  */
148  template<class KDECdf>
149  Matrix<double> sMatrix3(const KDECdf& cdf) const;
150 
151  private:
152  typedef std::vector<std::pair<long double,long double> > WeightedCoords;
153 
154  template <class Kernel>
155  void calcKValues(const Kernel& kernel, bool normalize);
156 
157  void makeLocalLegendreMatrix() const;
158 
159  template<class Poly>
160  Matrix<long double> standardPolyMatrix(
161  const Poly& poly, unsigned maxdeg, const WeightedCoords& wcoords) const;
162 
163  template<class CdfFcn>
164  Matrix<long double> legendrePolyMatrix(
165  const CdfFcn& cdf, unsigned maxdeg, const WeightedCoords& wcoords) const;
166 
167  std::shared_ptr<const AbsDistribution1D> distro_;
168  unsigned maxdegInputPoly_;
169  unsigned maxdegOutPoly_;
170  Matrix<long double> kValues_;
171  WeightedCoords xw_;
172  WeightedCoords yw_;
173  mutable Matrix<long double> localLegendreMatrix_;
174  long double densityIntegral_;
175  bool oracle_;
176  bool validateCdf_;
177  };
178 }
179 
180 #include "npstat/stat/KernelSensitivityCalculator.icc"
181 
182 #endif // NPSTAT_KERNELSENSITIVITYCALCULATOR_HH_
Interface definition for 1-d continuous statistical distributions.
Base class for quadratures on the [-1, 1] interval.
Template matrix class.
Definition: AbsIntervalQuadrature1D.hh:19
Definition: KernelSensitivityCalculator.hh:23
Matrix< double > sMatrix2(const OutPoly &outPoly) const
KernelSensitivityCalculator(const Kernel &kernel, const AbsDistribution1D &distro, unsigned maxdegInputPoly, unsigned maxdegOutPoly, long double ymin, long double ymax, const AbsIntervalQuadrature1D &xIntegrator, const AbsIntervalQuadrature1D &yIntegrator, bool normalizeKernel=true, bool validateCDF=true)
Matrix< double > sMatrix0(const InPoly &inputPoly, const OutPoly &outPoly) const
Matrix< double > sMatrix1(const InPoly &inputPoly, const KDECdf &cdf) const
KernelSensitivityCalculator(const Kernel &kernel, const std::vector< double > &sample, unsigned maxdegInputPoly, unsigned maxdegOutPoly, long double ymin, long double ymax, const AbsIntervalQuadrature1D &yIntegrator, bool normalizeKernel=true, bool validateCDF=true)
Matrix< double > sMatrix3(const KDECdf &cdf) const
Definition: AbsArrayProjector.hh:14
Definition: AbsDistribution1D.hh:31