npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
EdgeworthSeries1D.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_EDGEWORTHSERIES1D_HH_
2 #define NPSTAT_EDGEWORTHSERIES1D_HH_
3 
4 /*!
5 // \file EdgeworthSeries1D.hh
6 //
7 // \brief Distribution defined by Edgeworth series w.r.t. normal
8 //
9 // Author: I. Volobouev
10 //
11 // June 2019
12 */
13 
14 #include <vector>
15 
17 
20 
21 namespace npstat {
23  {
24  public:
25  /**
26  // Constructor arguments are as follows:
27  //
28  // cumulants -- The vector of cumulants. The first element
29  // of the vector (with index 0) is the first
30  // cumulant, the second element is the second, etc.
31  //
32  // m -- The method used to construct the distribition.
33  // See header EdgeworthSeriesMethod.hh for further
34  // explanations.
35  //
36  // order -- The expansion order in powers of 1/sqrt(n).
37  // O(1/sqrt(n)) is the 0th order, O(1/n) is the first
38  // order, O(1/(n sqrt(n)) is the second order, etc.
39  // Currently, the highest supported order is 14 for
40  // EDGEWORTH_CLASSICAL method in a non-SLR mode, and
41  // 4 in all other situations.
42  //
43  // slrMode -- If true, we will assume that the kth cumulant
44  // vanishes (for k > 2) to O(n^{-k/2}) instead of
45  // the usual O(n^{-(k-2)/2}). This is the case for
46  // the SLR statistic.
47  */
48  EdgeworthSeries1D(const std::vector<double>& cumulants,
49  EdgeworthSeriesMethod m, unsigned order,
50  bool slrMode = false);
51 
52  inline virtual EdgeworthSeries1D* clone() const
53  {return new EdgeworthSeries1D(*this);}
54 
55  inline virtual ~EdgeworthSeries1D() {}
56 
57  virtual double density(double x) const;
58  virtual double cdf(double x) const;
59  virtual double exceedance(double x) const;
60  virtual double quantile(double x) const;
61 
62  /**
63  // Inverse exceedance function. More precise than "quantile(1 - x)"
64  // for small x.
65  */
66  double inverseExceedance(double x) const;
67 
68  //@{
69  /** Inspector */
70  inline EdgeworthSeriesMethod method() const {return m_;}
71  inline bool slrMode() const {return slrMode_;}
72  inline unsigned order() const {return order_;}
73  //@}
74 
75  /**
76  // This function returns the constructor argument, not the
77  // real cumulant which depends on the expansion order used.
78  // Use the "empiricalCentralMoment" function and then convert
79  // central moments to cumulants if you need real cumulants.
80  // The real mean is 0 if order == 0 and cum(0) if order > 0.
81  */
82  inline double cum(const unsigned k) const {return cumulants_.at(k);};
83 
84  /** Mean of the function used to build the expansion */
85  double edgMean() const;
86 
87  /** Standard deviation of the function used to build the expansion */
88  double edgStdev() const;
89 
90  /** Edgeworth expansion factor for the density */
91  double densityFactor(double x) const;
92 
93  /** Edgeworth expansion factor for the CDF */
94  double cdfFactor(double x) const;
95 
96  /**
97  // Actual central moment of the density (evaluated numerically
98  // for degree > 1 using Gauss-Hermite quadrature)
99  */
100  double empiricalCentralMoment(unsigned degree) const;
101 
102  //@{
103  /** Prototype needed for I/O */
104  inline virtual gs::ClassId classId() const {return gs::ClassId(*this);}
105  virtual bool write(std::ostream&) const;
106  //@}
107 
108  static inline const char* classname()
109  {return "npstat::EdgeworthSeries1D";}
110  static inline unsigned version() {return 1;}
111  static EdgeworthSeries1D* read(
112  const gs::ClassId& id, std::istream& is);
113 
114  protected:
115  virtual bool isEqual(const AbsDistribution1D&) const;
116 
117  private:
118  void hermiteCoeffsClassical(long double* buf, unsigned* maxdeg) const;
119  void hermiteCoeffsNormal(long double* buf, unsigned* maxdeg) const;
120  void hermiteCoeffsSLR(long double* buf, unsigned* maxdeg) const;
121  double normalizedCoord(double x) const;
122  double normalizedSigma() const;
123  unsigned minNCumulants() const;
124 
125  std::vector<double> cumulants_;
126  EdgeworthSeriesMethod m_;
127  unsigned order_;
128  bool slrMode_;
129  };
130 
131  /** This class is mostly useful for testing purposes */
132  class EdgeworthSeriesMomentFcn : public Functor1<double,double>
133  {
134  public:
135  inline EdgeworthSeriesMomentFcn(const EdgeworthSeries1D& series,
136  const unsigned degree)
137  : series_(series), mean_(0.0), degree_(degree)
138  {if (series_.order()) {mean_ = series_.cum(0);}}
139 
140  inline virtual ~EdgeworthSeriesMomentFcn() {}
141 
142  virtual double operator()(const double&) const;
143 
144  private:
145  const EdgeworthSeries1D& series_;
146  double mean_;
147  unsigned degree_;
148  };
149 }
150 
151 #endif // NPSTAT_EDGEWORTHSERIES1D_HH_
Interface definition for 1-d continuous statistical distributions.
Enumeration of methods used to create Edgeworth series.
Interface definitions and concrete simple functors for a variety of functor-based calculations.
Definition: EdgeworthSeries1D.hh:23
virtual double exceedance(double x) const
double cum(const unsigned k) const
Definition: EdgeworthSeries1D.hh:82
EdgeworthSeries1D(const std::vector< double > &cumulants, EdgeworthSeriesMethod m, unsigned order, bool slrMode=false)
virtual bool isEqual(const AbsDistribution1D &) const
virtual gs::ClassId classId() const
Definition: EdgeworthSeries1D.hh:104
double inverseExceedance(double x) const
virtual double cdf(double x) const
double cdfFactor(double x) const
EdgeworthSeriesMethod method() const
Definition: EdgeworthSeries1D.hh:70
double empiricalCentralMoment(unsigned degree) const
virtual double quantile(double x) const
virtual double density(double x) const
virtual EdgeworthSeries1D * clone() const
Definition: EdgeworthSeries1D.hh:52
double densityFactor(double x) const
Definition: EdgeworthSeries1D.hh:133
Definition: AbsArrayProjector.hh:14
Definition: AbsDistribution1D.hh:31
Definition: SimpleFunctors.hh:58