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