npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
AbsSmoothGOFTest1D.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_ABSSMOOTHGOFTEST1D_HH_
2 #define NPSTAT_ABSSMOOTHGOFTEST1D_HH_
3 
4 /*!
5 // \file AbsSmoothGOFTest1D.hh
6 //
7 // \brief Base class for smooth tests for goodness-of-fit
8 //
9 // Author: I. Volobouev
10 //
11 // October 2022
12 */
13 
14 #include <vector>
15 
17 
18 namespace npstat {
20  {
21  public:
22  // mask and lenMask arguments can both be 0
24  unsigned maxdeg,
25  const unsigned char* mask, unsigned lenMask);
26 
28  const std::vector<int>& mask);
29 
30  inline virtual ~AbsSmoothGOFTest1D() {}
31 
32  inline unsigned maxDegree() const {return maxdeg_;}
33  inline unsigned nDegreesUsed() const {return nUnmasked_;}
34  inline bool isDegreeUsed(const unsigned n) const
35  {
36  if (!n) return false;
37  const unsigned i = n - 1U;
38  return i >= maxdeg_ ? false : !mask_[i];
39  }
40 
41  virtual double testStatistic(
42  const double* data, unsigned long sz,
43  bool isDataSorted) const;
44 
45  virtual double testStatistic(
46  const float* data, unsigned long sz,
47  bool isDataSorted) const;
48 
49  inline virtual bool hasAnalyticPValue() const {return true;}
50 
51  /**
52  // p-value calculated by this method is valid asymptotically
53  // for the statistic returned by the "testStatistic" method,
54  // not "directedStatistic".
55  */
56  virtual double analyticPValue(double stat, unsigned long sz) const;
57 
58  virtual double inverseExceedance(double pvalue, unsigned long sz,
59  double statMin, double statMax) const;
60 
61  //@{
62  /**
63  // Get individual normalized deviations from which
64  // the test statistics are constructed. Only unmasked
65  // deviations are returned. The return value of this
66  // function is the number of such deviations.
67  */
68  virtual unsigned normalizedDeviations(
69  const double* data, unsigned long sz,
70  bool isDataSorted,
71  double* deviations, unsigned lenDeviations) const=0;
72 
73  virtual unsigned normalizedDeviations(
74  const float* data, unsigned long sz,
75  bool isDataSorted,
76  double* deviations, unsigned lenDeviations) const=0;
77  //@}
78 
79  //@{
80  /**
81  // It is expected that the asymptotic distribution of
82  // the directed test statistic for the null hypothesis
83  // is the standard normal.
84  */
85  inline virtual double directedStatistic(
86  const double* direction, unsigned lenDirection,
87  const double* data, unsigned long sz,
88  bool isDataSorted) const
89  {
90  return directedStat(direction, lenDirection, data, sz, isDataSorted);
91  }
92 
93  inline virtual double directedStatistic(
94  const double* direction, unsigned lenDirection,
95  const float* data, unsigned long sz,
96  bool isDataSorted) const
97  {
98  return directedStat(direction, lenDirection, data, sz, isDataSorted);
99  }
100  //@}
101 
102  inline void simulateDirectedStatistic(
103  const double* direction, unsigned lenDirection,
104  AbsRandomGenerator& g, unsigned long sz,
105  unsigned nPseudo, std::vector<double>* stats) const
106  {
107  simulateAltDirectedStatistic(direction, lenDirection,
108  *distro_, g, sz, nPseudo, stats);
109  }
110 
111  // The following method should sort the returned statistic values
112  // in the increasing order
113  virtual void simulateAltDirectedStatistic(
114  const double* direction, unsigned lenDirection,
115  const AbsDistribution1D& alternative,
116  AbsRandomGenerator& g, unsigned long sz,
117  unsigned nPseudo, std::vector<double>* stats) const;
118 
119  protected:
120  std::string testNameWithMask(const char* prefix) const;
121 
122  std::vector<unsigned char> mask_;
123  unsigned maxdeg_;
124  unsigned nUnmasked_;
125 
126  private:
127  template<typename Numeric>
128  double directedStat(const double* direction, unsigned lenDirection,
129  const Numeric* data, unsigned long sz,
130  bool isDataSorted) const;
131 
132  template<typename Numeric>
133  void initMask(const Numeric* mask, unsigned lenMask);
134 
135  mutable std::vector<double> devBuf_;
136 
137 #ifdef SWIG
138  public:
139  inline std::vector<double> normalizedDeviations2(
140  const double* data, unsigned long sz,
141  const bool dataIsSorted) const
142  {
143  std::vector<double> tmp(nUnmasked_);
144  this->normalizedDeviations(data, sz, dataIsSorted,
145  &tmp[0], nUnmasked_);
146  return tmp;
147  }
148 
149  inline std::vector<double> simulateDirectedStatistic2(
150  const double* direction, unsigned lenDirection,
151  AbsRandomGenerator& g, const unsigned long sz,
152  const unsigned nPseudo) const
153  {
154  std::vector<double> tmp;
155  simulateDirectedStatistic(direction, lenDirection,
156  g, sz, nPseudo, &tmp);
157  return tmp;
158  }
159 
160  inline std::vector<double> simulateAltDirectedStatistic2(
161  const double* direction, unsigned lenDirection,
162  const AbsDistribution1D& alternative,
163  AbsRandomGenerator& g, const unsigned long sz,
164  const unsigned nPseudo) const
165  {
166  std::vector<double> tmp;
167  simulateAltDirectedStatistic(direction, lenDirection,
168  alternative, g, sz, nPseudo, &tmp);
169  return tmp;
170  }
171 #endif
172  };
173 }
174 
175 #include "npstat/stat/AbsSmoothGOFTest1D.icc"
176 
177 #endif // NPSTAT_ABSSMOOTHGOFTEST1D_HH_
Interface definition for goodness-of-fit tests for 1-d distributions.
Definition: AbsSmoothGOFTest1D.hh:20
virtual double inverseExceedance(double pvalue, unsigned long sz, double statMin, double statMax) const
virtual unsigned normalizedDeviations(const double *data, unsigned long sz, bool isDataSorted, double *deviations, unsigned lenDeviations) const =0
virtual double directedStatistic(const double *direction, unsigned lenDirection, const double *data, unsigned long sz, bool isDataSorted) const
Definition: AbsSmoothGOFTest1D.hh:85
virtual double analyticPValue(double stat, unsigned long sz) const
Definition: AbsUnbinnedGOFTest1D.hh:24
Definition: AbsArrayProjector.hh:14
Definition: AbsDistribution1D.hh:31