npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
TwoDistros1DFunctors.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_TWODISTROS1DFUNCTORS_HH_
2 #define NPSTAT_TWODISTROS1DFUNCTORS_HH_
3 
4 /*!
5 // \file TwoDistros1DFunctors.hh
6 //
7 // \brief A few functors used to study GoF tests
8 //
9 // Author: I. Volobouev
10 //
11 // October 2022
12 */
13 
14 #include <stdexcept>
15 
17 
18 namespace npstat {
20  {
21  public:
22  inline CdfDeltaFunctor1D(const AbsDistribution1D& nullHypothesis,
23  const AbsDistribution1D& alternative)
24  : AbsTwoDistros1DFunctor(nullHypothesis, alternative) {}
25 
26  inline virtual ~CdfDeltaFunctor1D() {}
27 
28  inline virtual double operator()(const double& x) const
29  {
30  if (x < 0.0 || x > 1.0)
31  return 0.0;
32  else
33  return altPtr_->cdf(nullPtr_->quantile(x)) - x;
34  }
35  };
36 
38  {
39  public:
41  const AbsDistribution1D& nullHypothesis,
42  const AbsDistribution1D& alternative)
43  : AbsTwoDistros1DFunctor(nullHypothesis, alternative) {}
44 
45  inline virtual ~ComparisonDensityFunctor1D() {}
46 
47  inline virtual double operator()(const double& x) const
48  {
49  if (x < 0.0 || x > 1.0)
50  return 0.0;
51  else
52  {
53  const double y = nullPtr_->quantile(x);
54  const double nulldens = nullPtr_->density(y);
55  if (nulldens <= 0.0) throw std::runtime_error(
56  "In npstat::ComparisonDensityFunctor1D::operator(): "
57  "baseline density is not positive");
58  return altPtr_->density(y)/nulldens;
59  }
60  }
61 
62  /**
63  // Coefficient of expansion of this function into
64  // shifted Legendre polynomials (that is, Legendre
65  // polynomials orthonormal on [0, 1]).
66  */
67  double legendreCoeff(unsigned degree) const;
68 
69  /**
70  // All coefficients of the Legendre polynomial expansion
71  // of the density, up to the degree requested. The array
72  // "coeffs" should have at least maxDeg+1 elements.
73  */
74  void legendreSeriesCoeffs(double* coeffs, unsigned maxDeg) const;
75 
76  /**
77  // Coefficient of expansion of this function into
78  // trigonometric functions, that is, sqrt(2)*sin(2 k Pi x)
79  // and sqrt(2)*cos(2 k Pi x). The integration is performed
80  // using the rectangle integration rule, with nInteg
81  // intervals. nInteg should be substantially larger than k,
82  // something like 32*k should work reasonably well.
83  */
84  double trigCoeff(unsigned k, bool isCosine, unsigned nInteg) const;
85 
86  /**
87  // Coefficient of expansion of this function into cosine
88  // basis, that is, sqrt(2)*cos(k Pi x). The integration
89  // is performed using the rectangle integration rule, with
90  // nInteg intervals. nInteg should be substantially larger
91  // than k, something like 16*k should work reasonably well.
92  */
93  double cosCoeff(unsigned k, unsigned nInteg) const;
94 
95 #ifdef SWIG
96  public:
97  inline std::vector<double>
98  legendreSeriesCoeffs2(const unsigned maxDeg) const
99  {
100  std::vector<double> tmp(maxDeg+1U);
101  legendreSeriesCoeffs(&tmp[0], maxDeg);
102  return tmp;
103  }
104 #endif // SWIG
105  };
106 }
107 
108 #endif // NPSTAT_TWODISTROS1DFUNCTORS_HH_
A base class for functors used to study GoF tests.
Definition: AbsTwoDistros1DFunctor.hh:21
Definition: TwoDistros1DFunctors.hh:20
Definition: TwoDistros1DFunctors.hh:38
double legendreCoeff(unsigned degree) const
double trigCoeff(unsigned k, bool isCosine, unsigned nInteg) const
void legendreSeriesCoeffs(double *coeffs, unsigned maxDeg) const
double cosCoeff(unsigned k, unsigned nInteg) const
Definition: AbsArrayProjector.hh:14
Definition: AbsDistribution1D.hh:31