npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
DistributionMix1D.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_DISTRIBUTIONMIX1D_HH_
2 #define NPSTAT_DISTRIBUTIONMIX1D_HH_
3 
4 /*!
5 // \file DistributionMix1D.hh
6 //
7 // \brief A mixture of one-dimensional statistical distributions
8 //
9 // Author: I. Volobouev
10 //
11 // June 2014
12 */
13 
15 
16 namespace npstat {
17  /** One-dimensional mixture of statistical distributions */
19  {
20  public:
22 
24  DistributionMix1D& operator=(const DistributionMix1D&);
25 
26  /** "Virtual copy constructor" */
27  inline virtual DistributionMix1D* clone() const
28  {return new DistributionMix1D(*this);}
29 
30  virtual ~DistributionMix1D();
31 
32  /**
33  // Add a component to the mixture. Weight must be non-negative.
34  // All weights will be normalized internally so that their
35  // sum is 1.
36  */
37  DistributionMix1D& add(const AbsDistribution1D& distro, double weight);
38 
39  /**
40  // Set all weights. Number of weights provided should be equal
41  // to the number of components. Weights will be normalized internally.
42  */
43  void setWeights(const double* weights, unsigned nWeights);
44 
45  /** Number of mixture components */
46  inline unsigned nComponents() const {return entries_.size();}
47 
48  /** Get the mixture component with the given number */
49  inline const AbsDistribution1D& getComponent(const unsigned n) const
50  {return *entries_.at(n);}
51 
52  /** Get the component weight */
53  double getWeight(unsigned n) const;
54 
55  /** Probability density */
56  double density(double x) const;
57 
58  /** Cumulative distribution function */
59  double cdf(double x) const;
60 
61  /** 1 - cdf, implementations should avoid subtractive cancellation */
62  double exceedance(double x) const;
63 
64  /** The quantile function */
65  double quantile(double x) const;
66 
67  /** Random number generator according to this distribution */
68  unsigned random(AbsRandomGenerator& g, double* r) const;
69 
70  //@{
71  /** Prototype needed for I/O */
72  virtual inline gs::ClassId classId() const {return gs::ClassId(*this);}
73  virtual bool write(std::ostream&) const;
74  //@}
75 
76  static inline const char* classname()
77  {return "npstat::DistributionMix1D";}
78  static inline unsigned version() {return 1;}
79  static DistributionMix1D* read(const gs::ClassId& id, std::istream& in);
80 
81  protected:
82  virtual bool isEqual(const AbsDistribution1D&) const;
83 
84  private:
85  void normalize();
86 
87  std::vector<const AbsDistribution1D*> entries_;
88  std::vector<double> weights_;
89  std::vector<double> weightCdf_;
90  long double wsum_;
91  bool isNormalized_;
92  };
93 }
94 
95 #endif // NPSTAT_DISTRIBUTIONMIX1D_HH_
Interface definition for 1-d continuous statistical distributions.
Definition: DistributionMix1D.hh:19
double getWeight(unsigned n) const
unsigned random(AbsRandomGenerator &g, double *r) const
DistributionMix1D & add(const AbsDistribution1D &distro, double weight)
double exceedance(double x) const
void setWeights(const double *weights, unsigned nWeights)
virtual DistributionMix1D * clone() const
Definition: DistributionMix1D.hh:27
unsigned nComponents() const
Definition: DistributionMix1D.hh:46
virtual bool isEqual(const AbsDistribution1D &) const
double cdf(double x) const
double density(double x) const
const AbsDistribution1D & getComponent(const unsigned n) const
Definition: DistributionMix1D.hh:49
virtual gs::ClassId classId() const
Definition: DistributionMix1D.hh:72
double quantile(double x) const
Definition: AbsArrayProjector.hh:14
Definition: AbsDistribution1D.hh:31
Definition: AbsRandomGenerator.hh:27