npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
ComparisonDistribution1D.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_COMPARISONDISTRIBUTION1D_HH_
2 #define NPSTAT_COMPARISONDISTRIBUTION1D_HH_
3 
4 /*!
5 // \file ComparisonDistribution1D.hh
6 //
7 // \brief Comparison distributions in one dimension
8 //
9 // Author: I. Volobouev
10 //
11 // September 2019
12 */
13 
14 #include <vector>
15 
17 
18 namespace npstat {
20  {
21  public:
24 
27 
28  inline virtual ~ComparisonDistribution1D() {cleanup();}
29 
30  /** Probability density */
31  double density(const double u) const;
32 
33  /**
34  // Coefficient of expansion of the density into
35  // shifted Legendre polynomials (that is, Legendre
36  // polynomials orthonormal on [0, 1]).
37  */
38  double legendreCoeff(unsigned degree) const;
39 
40  /**
41  // All coefficients of the Legendre polynomial expansion
42  // of the density, up to the degree requested. The array
43  // "coeffs" should have at least maxDeg+1 elements.
44  */
45  void legendreSeriesCoeffs(double* coeffs, unsigned maxDeg) const;
46 
47  /**
48  // Coefficient of expansion of this function into
49  // trigonometric functions, that is, sqrt(2)*sin(2 k Pi x)
50  // and sqrt(2)*cos(2 k Pi x). The integration is performed
51  // using the rectangle integration rule, with nInteg
52  // intervals. nInteg should be substantially larger than k,
53  // something like 32*k should work reasonably well.
54  */
55  double trigCoeff(unsigned k, bool isCosine, unsigned nInteg) const;
56 
57  /**
58  // Coefficient of expansion of this function into cosine
59  // basis, that is, sqrt(2)*cos(k Pi x). The integration
60  // is performed using the rectangle integration rule, with
61  // nInteg intervals. nInteg should be substantially larger
62  // than k, something like 16*k should work reasonably well.
63  */
64  double cosCoeff(unsigned k, unsigned nInteg) const;
65 
66  /** Cumulative distribution function */
67  inline double cdf(const double u) const
68  {
69  if (u <= 0.0)
70  return 0.0;
71  else if (u >= 1.0)
72  return 1.0;
73  else
74  return pF_->cdf(pH_->quantile(u));
75  }
76 
77  /** Exceedance (i.e., 1 - cdf) */
78  inline double exceedance(const double u) const
79  {
80  if (u <= 0.0)
81  return 1.0;
82  else if (u >= 1.0)
83  return 0.0;
84  else
85  return pF_->exceedance(pH_->quantile(u));
86  }
87 
88  /** Quantile function */
89  inline double quantile(const double x) const
90  {return pH_->cdf(pF_->quantile(x));}
91 
92  /** "Virtual copy constructor" */
93  inline virtual ComparisonDistribution1D* clone() const
94  {return new ComparisonDistribution1D(*this);}
95 
96  /** Fetch the compared distribution */
97  inline const AbsDistribution1D& compared() const {return *pF_;}
98 
99  /** Fetch the base line distribution */
100  inline const AbsDistribution1D& baseline() const {return *pH_;}
101 
102  //@{
103  /** Method needed for "geners" I/O */
104  virtual gs::ClassId classId() const {return gs::ClassId(*this);}
105  virtual bool write(std::ostream& os) const;
106  //@}
107 
108  static inline const char* classname()
109  {return "npstat::ComparisonDistribution1D";}
110  static inline unsigned version() {return 1;}
111  static ComparisonDistribution1D* read(
112  const gs::ClassId& id, std::istream& in);
113 
114  protected:
115  virtual bool isEqual(const AbsDistribution1D&) const;
116 
118 
119  const AbsDistribution1D* pF_;
120  const AbsDistribution1D* pH_;
121 
122  private:
123  void cleanup();
124 
125 #ifdef SWIG
126  public:
127  inline std::vector<double>
128  legendreSeriesCoeffs2(const unsigned maxDeg) const
129  {
130  std::vector<double> tmp(maxDeg+1U);
131  legendreSeriesCoeffs(&tmp[0], maxDeg);
132  return tmp;
133  }
134 #endif // SWIG
135  };
136 }
137 
138 #endif // NPSTAT_COMPARISONDISTRIBUTION1D_HH_
Interface definition for 1-d continuous statistical distributions.
Definition: ComparisonDistribution1D.hh:20
double cosCoeff(unsigned k, unsigned nInteg) const
const AbsDistribution1D & compared() const
Definition: ComparisonDistribution1D.hh:97
virtual bool isEqual(const AbsDistribution1D &) const
double quantile(const double x) const
Definition: ComparisonDistribution1D.hh:89
double trigCoeff(unsigned k, bool isCosine, unsigned nInteg) const
double density(const double u) const
const AbsDistribution1D & baseline() const
Definition: ComparisonDistribution1D.hh:100
virtual ComparisonDistribution1D * clone() const
Definition: ComparisonDistribution1D.hh:93
double exceedance(const double u) const
Definition: ComparisonDistribution1D.hh:78
void legendreSeriesCoeffs(double *coeffs, unsigned maxDeg) const
double cdf(const double u) const
Definition: ComparisonDistribution1D.hh:67
double legendreCoeff(unsigned degree) const
virtual gs::ClassId classId() const
Definition: ComparisonDistribution1D.hh:104
Definition: AbsArrayProjector.hh:14
Definition: AbsDistribution1D.hh:31
virtual double exceedance(double x) const =0
virtual double quantile(double x) const =0
virtual double cdf(double x) const =0