bpp-core3  3.0.0
AbstractDiscreteDistribution.h
Go to the documentation of this file.
1 //
2 // File: AbstractDiscreteDistribution.h
3 // Authors:
4 // Julien Dutheil
5 // Created: ?
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 19, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for numerical calculus.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 #ifndef BPP_NUMERIC_PROB_ABSTRACTDISCRETEDISTRIBUTION_H
42 #define BPP_NUMERIC_PROB_ABSTRACTDISCRETEDISTRIBUTION_H
43 
44 #include <map>
45 
46 #include "../AbstractParameterAliasable.h"
47 #include "../Constraints.h"
48 #include "DiscreteDistribution.h"
49 
50 namespace bpp
51 {
60  public virtual DiscreteDistribution,
62 {
63 public:
67  class Order
68  {
69 private:
70  double precision_;
71 
72 public:
73  Order(double prec = NumConstants::TINY()) :
74  precision_(prec)
75  {}
76 
77  Order(const Order& ord) :
79  {}
80 
81  Order& operator=(const Order& ord)
82  {
83  precision_ = ord.precision_;
84  return *this;
85  }
86 
87  double precision() const
88  {
89  return precision_;
90  }
91 
92  void setPrecision(double prec)
93  {
94  precision_ = prec;
95  }
96 
97  bool operator()(double l1, double l2) const
98  {
99  return l1 < l2 - precision_;
100  }
101  };
102 
103 protected:
104  /*
105  * The number of categories
106  */
107 
112  std::map<double, double, Order> distribution_;
113 
114  std::vector<double> bounds_;
115 
121  std::shared_ptr<IntervalConstraint> intMinMax_;
122 
128  bool median_;
129 
130 public:
131  AbstractDiscreteDistribution(size_t nbClasses, const std::string& prefix = "");
132 
137  AbstractDiscreteDistribution(size_t nbClasses, double precision, const std::string& prefix = "");
138 
140 
142 
144 
145 public:
151  size_t getNumberOfCategories() const;
152  void setNumberOfCategories(size_t nbClasses);
153  double getCategory(size_t categoryIndex) const;
154  double getProbability(size_t categoryIndex) const;
155  double getProbability(double category) const;
156  Vdouble getCategories() const;
157  Vdouble getProbabilities() const;
158  double getValueCategory(double value) const;
159  size_t getCategoryIndex(double value) const;
160  void set(double category, double probability);
161  void add(double category, double probability);
162  double getInfCumulativeProbability(double category) const;
163  double getIInfCumulativeProbability(double category) const;
164  double getSupCumulativeProbability(double category) const;
165  double getSSupCumulativeProbability(double category) const;
166  double rand() const;
167  double randC() const { throw Exception("AbstractDiscreteDistribution::randC. No continuous version available for this distribution."); }
168 
169  /*
170  *@return value of the internal bound
171  *
172  */
173  double getBound(size_t i) const
174  {
175  if (i >= numberOfCategories_ - 1)
176  throw IndexOutOfBoundsException("AbstractDiscreteDistribution::getBound(i)", i, 0, numberOfCategories_ - 1);
177  return bounds_[i];
178  }
179 
180 
181  /*
182  *@brief Information about the range of the distribution
183  *
184  */
185  double getLowerBound() const
186  {
187  return intMinMax_->getLowerBound();
188  }
189 
190  double getUpperBound() const
191  {
192  return intMinMax_->getUpperBound();
193  }
194 
195  bool strictLowerBound() const
196  {
197  return intMinMax_->strictLowerBound();
198  }
199 
200  bool strictUpperBound() const
201  {
202  return intMinMax_->strictUpperBound();
203  }
204 
205  Vdouble getBounds() const;
206 
207  void print(OutputStream& out) const;
208 
209  double precision() const { return distribution_.key_comp().precision();}
210 
211  void setMedian(bool median)
212  {
213  if (median_ != median)
214  {
215  median_ = median;
216  discretize();
217  }
218  }
219 
220  virtual void discretize();
221 
233  virtual void restrictToConstraint(const Constraint& c);
234 };
235 } // end of namespace bpp.
236 #endif // BPP_NUMERIC_PROB_ABSTRACTDISCRETEDISTRIBUTION_H
Comparator class for AbstractDiscreteDistribution.
Partial implementation of the DiscreteDistribution interface.
double getCategory(size_t categoryIndex) const
double randC() const
Draw a random number from the continuous version of this distribution, if it exists.
double getIInfCumulativeProbability(double category) const
void set(double category, double probability)
Set the probability associated to a class.
void add(double category, double probability)
Modify the probability associated to a class.
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...
double getSSupCumulativeProbability(double category) const
double rand() const
Draw a random number from this distribution.
double getProbability(size_t categoryIndex) const
virtual void restrictToConstraint(const Constraint &c)
Restricts the distribution to the domain where the constraint is respected, in addition of other pred...
std::map< double, double, Order > distribution_
double getLowerBound() const
methods about the range of the definition
double getInfCumulativeProbability(double category) const
void setNumberOfCategories(size_t nbClasses)
sets the number of categories and discretizes if there is a change in this number.
double getSupCumulativeProbability(double category) const
AbstractDiscreteDistribution(size_t nbClasses, const std::string &prefix="")
void print(OutputStream &out) const
Print the distribution (categories and corresponding probabilities) to a stream.
AbstractDiscreteDistribution & operator=(const AbstractDiscreteDistribution &adde)
virtual void discretize()
Discretizes the distribution in equiprobable classes.
std::shared_ptr< IntervalConstraint > intMinMax_
the interval where the distribution is defined/restricted.
A partial implementation of the Parametrizable interface.
The constraint interface.
Definition: Constraints.h:66
Interface for discrete distribution objects.
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
Definition: Exceptions.h:59
Index out of bounds exception class.
Definition: Exceptions.h:170
static double TINY()
Definition: NumConstants.h:82
OutputStream interface.
Definition: OutputStream.h:67
std::vector< double > Vdouble
Definition: VectorTools.h:70