npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
GaussianMixture1D.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_GAUSSIANMIXTURE1D_HH_
2 #define NPSTAT_GAUSSIANMIXTURE1D_HH_
3 
4 /*!
5 // \file GaussianMixture1D.hh
6 //
7 // \brief One-dimensional Gaussian mixtures
8 //
9 // Author: I. Volobouev
10 //
11 // October 2011
12 */
13 
14 #include <vector>
15 #include <utility>
16 
19 
20 namespace npstat {
21  /** One-dimensional Gaussian mixture distribution */
23  {
24  public:
25  /**
26  // This constructor takes an array of GaussianMixtureEntry
27  // objects together with an overall location and scale parameters
28  // which are used to shift and scale the whole distribution.
29  // There must be at least one component with positive weight
30  // in the array of entries. The sum of weights will be normalized
31  // to 1 internally.
32  */
35  unsigned nEntries);
36 
37  GaussianMixture1D(double location, double scale,
38  const std::vector<GaussianMixtureEntry>& entries);
39 
40  /** Constructor from a single Gaussian distribution */
41  explicit GaussianMixture1D(const Gauss1D& gauss);
42 
43  inline virtual ~GaussianMixture1D() {}
44 
45  inline virtual GaussianMixture1D* clone() const
46  {return new GaussianMixture1D(*this);}
47 
48  /** Number of mixture components */
49  inline unsigned nentries() const {return entries_.size();}
50 
51  /** Get the mixture component with the given number */
52  inline const GaussianMixtureEntry& entry(const unsigned n) const
53  {return entries_.at(n);}
54 
55  /** Get all mixture components */
56  inline const std::vector<GaussianMixtureEntry>& entries() const
57  {return entries_;}
58 
59  /** Generate random numbers asscording to this distribution */
60  virtual unsigned random(AbsRandomGenerator& g, double* r) const;
61 
62  /** Mean of the whole mixture */
63  double mean() const;
64 
65  /** Standard deviation of the whole mixture */
66  double stdev() const;
67 
68  /**
69  // Mean Integrated Squared Error expected for KDE of a sample from
70  // this mixture using Hermite polynomial kernel. The formula is from
71  // the article "Exact Mean Integrated Squared Error" by J.S. Marron
72  // and M.P. Wand, The Annals Of Statistics, Vol 20, pp. 712-736 (1992).
73  // Kernel order is maxPolyDegree + 2 if maxPolyDegree is even, and
74  // maxPolyDegree + 1 if maxPolyDegree is odd.
75  //
76  // Due to accumulation of round-off errors, the calculation becomes
77  // unreliable for maxPolyDegree above 16 or so.
78  */
79  double gaussianMISE(unsigned maxPolyDegree, double bandwidth,
80  unsigned long npoints) const;
81 
82  /** Bandwidth which minimizes the KDE MISE */
83  double miseOptimalBw(unsigned maxPolyDegree, unsigned long npoints,
84  double* mise = 0) const;
85 
86  //@{
87  /** Methods needed for "geners" I/O */
88  virtual inline gs::ClassId classId() const {return gs::ClassId(*this);}
89  virtual bool write(std::ostream& os) const;
90  //@}
91 
92  static inline const char* classname() {return "npstat::GaussianMixture1D";}
93  static inline unsigned version() {return 1;}
94  static GaussianMixture1D* read(const gs::ClassId& id, std::istream& in);
95 
96  protected:
97  virtual bool isEqual(const AbsDistribution1D& r) const;
98 
99  private:
101 
102  class GaussianMISEFunctor;
103  friend class GaussianMISEFunctor;
104 
105  class GaussianMISEFunctor : public Functor1<long double,double>
106  {
107  public:
108  inline GaussianMISEFunctor(const GaussianMixture1D& mix,
109  const unsigned maxPolyDegree,
110  const unsigned long npoints)
111  : mix_(mix), npt_(npoints), polyDeg_(maxPolyDegree) {}
112  inline virtual ~GaussianMISEFunctor() {}
113 
114  inline long double operator()(const double& bandwidth) const
115  {return mix_.gmise(bandwidth, polyDeg_, npt_);}
116 
117  private:
118  const GaussianMixture1D& mix_;
119  const unsigned long npt_;
120  const unsigned polyDeg_;
121  };
122 
123  inline GaussianMixture1D(double location, double scale)
125  GaussianMixture1D(const double location, const double scale,
126  const std::vector<double>& params);
127  inline static int nParameters() {return -1;}
128 
129  void initialize();
130  void copyNonzeroEntries(const GaussianMixtureEntry* ientries,
131  const unsigned nentries);
132  double unscaledDensity(double x) const;
133  double unscaledCdf(double x) const;
134  double unscaledExceedance(double x) const;
135  double unscaledQuantile(double x) const;
136  double unscaledMean() const;
137  double unscaledStdev() const;
138 
139  long double U(long double h, unsigned s, unsigned q) const;
140  long double gmise(long double h, unsigned deg, unsigned long n) const;
141 
142  std::vector<GaussianMixtureEntry> entries_;
143  std::vector<double> weightCdf_;
144  std::vector<Gauss1D> distros_;
145  mutable std::vector<long double> polyterms_;
146  mutable std::vector<std::pair<long double,long double> > uterms_;
147  };
148 }
149 
150 #endif // NPSTAT_GAUSSIANMIXTURE1D_HH_
A number of useful 1-d continuous statistical distributions.
A helper class for constructing one-dimensional Gaussian mixtures.
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: Distributions1D.hh:316
Definition: GaussianMixture1D.hh:23
virtual unsigned random(AbsRandomGenerator &g, double *r) const
double gaussianMISE(unsigned maxPolyDegree, double bandwidth, unsigned long npoints) const
virtual GaussianMixture1D * clone() const
Definition: GaussianMixture1D.hh:45
unsigned nentries() const
Definition: GaussianMixture1D.hh:49
double miseOptimalBw(unsigned maxPolyDegree, unsigned long npoints, double *mise=0) const
const GaussianMixtureEntry & entry(const unsigned n) const
Definition: GaussianMixture1D.hh:52
GaussianMixture1D(const Gauss1D &gauss)
virtual gs::ClassId classId() const
Definition: GaussianMixture1D.hh:88
virtual bool isEqual(const AbsDistribution1D &r) const
GaussianMixture1D(double location, double scale, const GaussianMixtureEntry *entries, unsigned nEntries)
const std::vector< GaussianMixtureEntry > & entries() const
Definition: GaussianMixture1D.hh:56
Definition: GaussianMixtureEntry.hh:23
Definition: Distribution1DFactory.hh:35
Definition: AbsArrayProjector.hh:14
Definition: AbsDistribution1D.hh:31
Definition: AbsRandomGenerator.hh:27
Definition: SimpleFunctors.hh:58