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