npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
QuantileTable1D.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_QUANTILETABLE1D_HH_
2 #define NPSTAT_QUANTILETABLE1D_HH_
3 
4 /*!
5 // \file QuantileTable1D.hh
6 //
7 // \brief 1-d statistical distribution defined by its quantile table
8 //
9 // Author: I. Volobouev
10 //
11 // March 2013
12 */
13 
14 #include <vector>
15 #include <stdexcept>
16 
19 
20 namespace npstat {
22  {
23  public:
24  /**
25  // The "data" array must provide quantile values at
26  // equidistant intervals. It is assumed that the first
27  // value corresponds to the 0.5/dataLen quantile and
28  // the last to the 1.0 - 0.5/dataLen quantile. All data
29  // values must be between 0 and 1 (the unscaled quantile
30  // function value will be set to 0 at 0 and to 1 at 1).
31  //
32  // The represented density looks essentially like
33  // a variable-bin histogram. Calculation of the quantile
34  // function will be very fast, but calculation of the density
35  // and cdf will be O(log(N)), where N is the number of entries
36  // in the table.
37  */
38  template <typename Real>
39  inline QuantileTable1D(const double location, const double scale,
40  const Real* data, const unsigned long dataLen)
42  qtable_(data, data+dataLen)
43  {
44  if (!(data && dataLen))
45  throw std::invalid_argument(
46  "In npstat::QuantileTable1D constructor: no data");
47  if (dataLen > 1U && !isNonDecreasing(qtable_.begin(), qtable_.end()))
48  throw std::invalid_argument(
49  "In npstat::QuantileTable1D constructor: invalid table");
50  for (unsigned long i=0; i<dataLen; ++i)
51  if (qtable_[i] < 0.0 || qtable_[i] > 1.0)
52  throw std::invalid_argument(
53  "In npstat::QuantileTable1D constructor: "
54  "out of range table value");
55  }
56 
57  inline virtual QuantileTable1D* clone() const
58  {return new QuantileTable1D(*this);}
59 
60  inline virtual ~QuantileTable1D() {}
61 
62  /** Return properly scaled and shifted table of quantiles */
63  std::vector<double> quantileTable() const;
64 
65  // Methods needed for I/O
66  virtual gs::ClassId classId() const {return gs::ClassId(*this);}
67  virtual bool write(std::ostream& os) const;
68 
69  static inline const char* classname() {return "npstat::QuantileTable1D";}
70  static inline unsigned version() {return 1;}
71  static QuantileTable1D* read(const gs::ClassId& id, std::istream& in);
72 
73  protected:
74  virtual bool isEqual(const AbsDistribution1D& r) const;
75 
76  private:
78 
79  QuantileTable1D(double location, double scale,
80  const std::vector<double>& params);
81  inline static int nParameters() {return -1;}
82 
83  double unscaledDensity(double x) const;
84  double unscaledCdf(double x) const;
85  double unscaledQuantile(double x) const;
86 
87  inline double unscaledExceedance(const double x) const
88  {return 1.0 - unscaledCdf(x);}
89 
90  std::vector<double> qtable_;
91  };
92 }
93 
94 #endif // NPSTAT_QUANTILETABLE1D_HH_
Factories for 1-d distributions for use in interpretive language environments.
Definition: AbsDistribution1D.hh:165
double scale() const
Definition: AbsDistribution1D.hh:183
double location() const
Definition: AbsDistribution1D.hh:180
Definition: QuantileTable1D.hh:22
QuantileTable1D(const double location, const double scale, const Real *data, const unsigned long dataLen)
Definition: QuantileTable1D.hh:39
std::vector< double > quantileTable() const
virtual bool isEqual(const AbsDistribution1D &r) const
virtual gs::ClassId classId() const
Definition: QuantileTable1D.hh:66
virtual QuantileTable1D * clone() const
Definition: QuantileTable1D.hh:57
Definition: Distribution1DFactory.hh:35
A few simple template functions for checking monotonicity of container values.
Definition: AbsArrayProjector.hh:14
bool isNonDecreasing(Iter begin, Iter const end)
Definition: isMonotonous.hh:54
Definition: AbsDistribution1D.hh:31