npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
DeltaMixture1D.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_DELTAMIXTURE1D_HH_
2 #define NPSTAT_DELTAMIXTURE1D_HH_
3 
4 /*!
5 // \file DeltaMixture1D.hh
6 //
7 // \brief Weighted mixture of 1-d Dirac delta functions
8 //
9 // Author: I. Volobouev
10 //
11 // July 2019
12 */
13 
14 #include <vector>
15 #include <stdexcept>
16 
19 
20 namespace npstat {
22  {
23  public:
24  /**
25  // For this constructor, coordinates must be sorted
26  // in the increasing order
27  */
28  template <typename Real1, typename Real2>
29  inline DeltaMixture1D(const double location, const double scale,
30  const Real1* unscaledCoords,
31  const Real2* unscaledWeights,
32  const unsigned arrayLength)
34  {initialize(unscaledCoords, unscaledWeights, arrayLength);}
35 
36  inline virtual DeltaMixture1D* clone() const
37  {return new DeltaMixture1D(*this);}
38 
39  inline virtual ~DeltaMixture1D() {}
40 
41  inline unsigned nComponents() const {return locations_.size();}
42  inline double getLocation(const unsigned i) const {return locations_.at(i);}
43  inline double getWeight(const unsigned i) const {return weights_.at(i);}
44 
45  /** Density integral on the intervals (a, b), [a, b], etc */
46  double integral(double a, bool includePointAtA,
47  double b, bool includePointAtB) const;
48 
49  /** Distribution moment */
50  long double moment(long double center, unsigned order) const;
51 
52  /**
53  // A collection of distribution moments. The array "moments"
54  // should have at least "maxOrder+1" elements.
55  */
56  void moments(long double center, unsigned maxOrder,
57  long double* moments) const;
58 
59  /**
60  // A collection of distribution central moments. The array "moments"
61  // should have at least "maxOrder+1" elements. On exit, moments[1]
62  // will be set to the mean while all other moments will be central.
63  */
64  void centralMoments(unsigned maxOrder, long double* moments) const;
65 
66  // Methods needed for I/O
67  virtual gs::ClassId classId() const {return gs::ClassId(*this);}
68  virtual bool write(std::ostream&) const;
69 
70  static inline const char* classname() {return "npstat::DeltaMixture1D";}
71  static inline unsigned version() {return 1;}
72  static DeltaMixture1D* read(const gs::ClassId& id, std::istream& in);
73 
74  protected:
75  virtual bool isEqual(const AbsDistribution1D&) const;
76 
77  private:
79 
80  DeltaMixture1D(double location, double scale,
81  const std::vector<double>& params);
82  inline static int nParameters() {return -1;}
83 
84  template <typename Real1, typename Real2>
85  void initialize(const Real1* unscaledCoords,
86  const Real2* unscaledWeights,
87  const unsigned arrayLength);
88  void buildCdfTable();
89 
90  inline double unscaledDensity(double /* x */) const {return 0.0;}
91  double unscaledCdf(double x) const;
92  double unscaledExceedance(double x) const;
93  double unscaledQuantile(double x) const;
94 
95  double weightSum(unsigned imin, unsigned imax) const;
96 
97  // Find all locations in the range
98  void determineRange(double a, bool includeLeftBoundary,
99  double b, bool includeRightBoundary,
100  unsigned* imin, unsigned* imax) const;
101 
102  std::vector<double> locations_;
103  std::vector<double> weights_;
104  std::vector<double> cdf_;
105  std::vector<double> exceedance_;
106 
107 #ifdef SWIG
108  public:
109  inline DeltaMixture1D(const double location, const double scale,
110  const double* coords, unsigned lenCoords,
111  const double* weights, unsigned lenWeights)
112  : AbsScalableDistribution1D(location, scale)
113  {
114  if (lenCoords != lenWeights) throw std::invalid_argument(
115  "In npstat::DeltaMixture1D constructor: "
116  "lengths of arrays of coordinates and weights are incompatible");
117  initialize(coords, weights, lenCoords);
118  }
119 
120  inline DeltaMixture1D(const double location, const double scale,
121  const double* coords, unsigned lenCoords)
123  {
124  if (!lenCoords) throw std::invalid_argument(
125  "In npstat::DeltaMixture1D constructor: "
126  "empty array of coordinates");
127  std::vector<double> weights(lenCoords, 1.0);
128  initialize(coords, &weights[0], lenCoords);
129  }
130 
131  inline double moment_2(const double center, const unsigned order) const
132  {
133  return moment(center, order);
134  }
135 
136  inline std::vector<double> moments_2(const double center, const unsigned maxOrder) const
137  {
138  std::vector<long double> tmp(maxOrder+1U);
139  moments(center, maxOrder, &tmp[0]);
140  return std::vector<double>(tmp.begin(), tmp.end());
141  }
142 
143  inline std::vector<double> centralMoments_2(const unsigned maxOrder) const
144  {
145  std::vector<long double> tmp(maxOrder+1U);
146  centralMoments(maxOrder, &tmp[0]);
147  return std::vector<double>(tmp.begin(), tmp.end());
148  }
149 #endif
150  };
151 }
152 
153 #include "npstat/stat/DeltaMixture1D.icc"
154 
155 #endif // NPSTAT_DELTAMIXTURE1D_HH_
Interface definition for 1-d continuous statistical distributions.
Factories for 1-d distributions for use in interpretive language environments.
Definition: AbsDistribution1D.hh:165
double scale() const
Definition: AbsDistribution1D.hh:183
AbsScalableDistribution1D(const double location, const double scale)
Definition: AbsDistribution1D.hh:168
double location() const
Definition: AbsDistribution1D.hh:180
Definition: DeltaMixture1D.hh:22
virtual DeltaMixture1D * clone() const
Definition: DeltaMixture1D.hh:36
virtual gs::ClassId classId() const
Definition: DeltaMixture1D.hh:67
long double moment(long double center, unsigned order) const
double integral(double a, bool includePointAtA, double b, bool includePointAtB) const
void centralMoments(unsigned maxOrder, long double *moments) const
void moments(long double center, unsigned maxOrder, long double *moments) const
virtual bool isEqual(const AbsDistribution1D &) const
DeltaMixture1D(const double location, const double scale, const Real1 *unscaledCoords, const Real2 *unscaledWeights, const unsigned arrayLength)
Definition: DeltaMixture1D.hh:29
Definition: Distribution1DFactory.hh:35
Definition: AbsArrayProjector.hh:14
Definition: AbsDistribution1D.hh:31