bpp-core3  3.0.0
AbstractDiscreteDistribution.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_NUMERIC_PROB_ABSTRACTDISCRETEDISTRIBUTION_H
6 #define BPP_NUMERIC_PROB_ABSTRACTDISCRETEDISTRIBUTION_H
7 
8 #include <map>
9 
10 #include "../AbstractParameterAliasable.h"
11 #include "../Constraints.h"
12 #include "DiscreteDistribution.h"
13 
14 namespace bpp
15 {
24  public virtual DiscreteDistributionInterface,
26 {
27 public:
31 
35  class Order
36  {
37 private:
38  double precision_;
39 
40 public:
41  Order(double prec = NumConstants::TINY()) :
42  precision_(prec)
43  {}
44 
45  Order(const Order& ord) :
46  precision_(ord.precision_)
47  {}
48 
49  Order& operator=(const Order& ord)
50  {
51  precision_ = ord.precision_;
52  return *this;
53  }
54 
55  double precision() const
56  {
57  return precision_;
58  }
59 
60  void setPrecision(double prec)
61  {
62  precision_ = prec;
63  }
64 
65  bool operator()(double l1, double l2) const
66  {
67  return l1 < l2 - precision_;
68  }
69  };
70 
71 protected:
72  /*
73  * The number of categories
74  */
75 
80  std::map<double, double, Order> distribution_;
81 
82  std::vector<double> bounds_;
83 
87  std::shared_ptr<IntervalConstraint> intMinMax_;
88 
92  bool median_;
93 
98 
99 public:
100  AbstractDiscreteDistribution(size_t nbClasses, const std::string& prefix = "", short discretization = DISCRETIZATION_EQUAL_PROB);
101 
105  AbstractDiscreteDistribution(size_t nbClasses, double precision, const std::string& prefix = "", short discretization = DISCRETIZATION_EQUAL_PROB);
106 
108 
110 
112 
113 public:
119  size_t getNumberOfCategories() const;
120  void setNumberOfCategories(size_t nbClasses);
121  double getCategory(size_t categoryIndex) const;
122  double getProbability(size_t categoryIndex) const;
123  double getProbability(double category) const;
124  Vdouble getCategories() const;
125  Vdouble getProbabilities() const;
126  double getValueCategory(double value) const;
127  size_t getCategoryIndex(double value) const;
128  void set(double category, double probability);
129  void add(double category, double probability);
130  double getInfCumulativeProbability(double category) const;
131  double getIInfCumulativeProbability(double category) const;
132  double getSupCumulativeProbability(double category) const;
133  double getSSupCumulativeProbability(double category) const;
134  double rand() const;
135  double randC() const { throw Exception("AbstractDiscreteDistribution::randC. No continuous version available for this distribution."); }
136 
137  /*
138  *@return value of the internal bound
139  *
140  */
141  double getBound(size_t i) const
142  {
143  if (i >= numberOfCategories_ - 1)
144  throw IndexOutOfBoundsException("AbstractDiscreteDistribution::getBound(i)", i, 0, numberOfCategories_ - 1);
145  return bounds_[i];
146  }
147 
148 
149  /*
150  *@brief Information about the range of the distribution
151  *
152  */
153  double getLowerBound() const
154  {
155  return intMinMax_->getLowerBound();
156  }
157 
158  double getUpperBound() const
159  {
160  return intMinMax_->getUpperBound();
161  }
162 
163  bool strictLowerBound() const
164  {
165  return intMinMax_->strictLowerBound();
166  }
167 
168  bool strictUpperBound() const
169  {
170  return intMinMax_->strictUpperBound();
171  }
172 
173  Vdouble getBounds() const;
174 
175  void print(OutputStream& out) const;
176 
177  double precision() const { return distribution_.key_comp().precision();}
178 
179  void setMedian(bool median)
180  {
181  if (median_ != median)
182  {
183  median_ = median;
184  discretize();
185  }
186  }
187 
188  virtual void discretize();
198  virtual void restrictToConstraint(const ConstraintInterface& c);
199 
200 protected:
203 };
204 } // end of namespace bpp.
205 #endif // BPP_NUMERIC_PROB_ABSTRACTDISCRETEDISTRIBUTION_H
double getInfCumulativeProbability(double category) const
double getCategory(size_t categoryIndex) const
Comparator class for AbstractDiscreteDistribution.
virtual void restrictToConstraint(const ConstraintInterface &c)
Restricts the distribution to the domain where the constraint is respected, in addition of other pred...
Partial implementation of the DiscreteDistribution interface.
double getIInfCumulativeProbability(double category) const
The constraint interface.
Definition: Constraints.h:28
double getSSupCumulativeProbability(double category) const
double getSupCumulativeProbability(double category) const
void add(double category, double probability)
Modify the probability associated to a class.
double randC() const
Draw a random number from the continuous version of this distribution, if it exists.
Interface for discrete distribution objects.
double getProbability(size_t categoryIndex) const
std::shared_ptr< IntervalConstraint > intMinMax_
the interval where the distribution is defined/restricted.
A partial implementation of the Parametrizable interface.
void print(OutputStream &out) const
Print the distribution (categories and corresponding probabilities) to a stream.
double rand() const
Draw a random number from this distribution.
void setMedian(bool median)
Sets the median value to true to say that the value in a class is proportional to the median value of...
static double TINY()
Definition: NumConstants.h:46
AbstractDiscreteDistribution(size_t nbClasses, const std::string &prefix="", short discretization=DISCRETIZATION_EQUAL_PROB)
double getLowerBound() const
methods about the range of the definition
std::vector< double > Vdouble
Definition: VectorTools.h:34
std::map< double, double, Order > distribution_
virtual void discretize()
Discretizes the distribution in equiprobable classes.
OutputStream interface.
Definition: OutputStream.h:29
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
Definition: Exceptions.h:20
void setNumberOfCategories(size_t nbClasses)
sets the number of categories and discretizes if there is a change in this number.
Index out of bounds exception class.
Definition: Exceptions.h:131