5 #include "../Random/RandomTools.h" 6 #include "../VectorTools.h" 18 numberOfCategories_(nbClasses),
20 bounds_(nbClasses - 1),
23 discretizationScheme_(discretization)
69 cerr <<
"DEBUG: ERROR!!! Number of categories is <= 0 in AbstractDiscreteDistribution::setNumberOfCategories()." << endl;
83 map<double, double>::const_iterator it =
distribution_.begin();
84 for (
unsigned int i = 0; i < categoryIndex; i++)
95 map<double, double>::const_iterator it =
distribution_.begin();
96 for (
unsigned int i = 0; i < categoryIndex; i++)
116 for (map<double, double>::const_iterator it =
distribution_.begin();
120 result[i] = it->first;
132 for (map<double, double>::const_iterator it =
distribution_.begin();
136 result[i] = it->second;
171 for (map<double, double>::const_iterator i =
distribution_.begin();
175 cumprob += i->second;
188 map<double, double>::const_iterator it =
distribution_.find(category);
189 for (map<double, double>::const_iterator i =
distribution_.begin();
203 map<double, double>::const_iterator it =
distribution_.find(category);
206 for (map<double, double>::const_iterator i = ++it;
220 map<double, double>::const_iterator it =
distribution_.find(category);
223 for (map<double, double>::const_iterator i = ++it;
237 map<double, double>::const_iterator it =
distribution_.find(category);
238 for (map<double, double>::const_iterator i =
distribution_.begin();
253 (out <<
"Pr(" << (i->first) <<
") = " << (i->second)).endLine();
264 map<double, double>::const_iterator it =
distribution_.begin();
265 for (
unsigned int i = 1; i <
bounds_.size(); i++)
283 for (
unsigned int i = 1; i <
bounds_.size(); i++)
316 bounds_[i - 1] =
qProb(minX + static_cast<double>(i) * ec);
326 values[i] =
qProb(minX + (static_cast<double>(i) + 0.5) * ec);
338 values[i] *= mean / t / ec;
345 double firstBound =
intMinMax_->getLowerBound(), secondBound;
347 for (i = 0; i < numberOfCategories_ - 1; i++)
351 values[i] = (b - a) / ec;
352 if (values[i] < firstBound || values[i] > secondBound)
354 values[i] = (firstBound + secondBound) / 2.;
357 firstBound = secondBound;
360 values[numberOfCategories_ - 1] = (
Expectation(secondBound) - a) / ec;
361 if (values[numberOfCategories_ - 1] < firstBound || values[numberOfCategories_ - 1] > secondBound)
363 values[numberOfCategories_ - 1] = (firstBound + secondBound) / 2.;
378 for (i = 1; i < numberOfCategories_ - 1; i++)
383 values[numberOfCategories_ - 1] = (
intMinMax_->getUpperBound() +
bounds_[numberOfCategories_ - 1]) / 2;
422 if (values[i - 1] >
intMinMax_->getUpperBound())
463 double lowerBound =
intMinMax_->getLowerBound();
464 double upperBound =
intMinMax_->getUpperBound();
465 double condProb =
pProb(upperBound) -
pProb(lowerBound);
471 bounds_[i] = lowerBound + (
static_cast<double>(i) + 1.) * interval;
477 values[i] = lowerBound + (
static_cast<double>(i) + 0.5) * interval;
482 allBounds.push_front(lowerBound);
483 allBounds.push_back(upperBound);
509 allBounds.push_front(
intMinMax_->getLowerBound());
510 allBounds.push_back(
intMinMax_->getUpperBound());
514 if (allBounds[i] == allBounds[i - 1])
553 throw Exception(
"AbstractDiscreteDistribution::restrictToConstraint: the constraint is not an interval");
void set(double category, double probability)
Set the probability associated to a class.
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...
Vdouble getBounds() const
static short DISCRETIZATION_EQUAL_INTERVAL
DiscreteDistributionInterface * clone() const =0
Create a copy of this object and send a pointer to it.
Partial implementation of the DiscreteDistribution interface.
short discretizationScheme_
double getIInfCumulativeProbability(double category) const
this static class contains several useful constant values.
An interval, either bounded or not, which can also have infinite bounds.
The constraint interface.
double getSSupCumulativeProbability(double category) const
double getSupCumulativeProbability(double category) const
double getBound(size_t i) const
void add(double category, double probability)
Modify the probability associated to a class.
Vdouble getProbabilities() const
double getProbability(size_t categoryIndex) const
size_t getNumberOfCategories() const
AbstractDiscreteDistribution & operator=(const AbstractDiscreteDistribution &adde)
virtual double Expectation(double a) const =0
Return a primitive function used for the expectation of the continuous version of the distribution...
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.
static short DISCRETIZATION_EQUAL_PROB_WHEN_POSSIBLE
AbstractDiscreteDistribution(size_t nbClasses, const std::string &prefix="", short discretization=DISCRETIZATION_EQUAL_PROB)
double getLowerBound() const
methods about the range of the definition
static short DISCRETIZATION_EQUAL_PROB
std::vector< double > Vdouble
std::map< double, double, Order > distribution_
virtual void discretize()
Discretizes the distribution in equiprobable classes.
void discretizeEqualProportions()
AbstractParameterAliasable & operator=(const AbstractParameterAliasable &ap)
double getUpperBound() const
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
virtual double pProb(double x) const =0
Return the cumulative quantile of the continuous version of the distribution, ie .
void setNumberOfCategories(size_t nbClasses)
sets the number of categories and discretizes if there is a change in this number.
double getValueCategory(double value) const
virtual double qProb(double x) const =0
Return the quantile of the continuous version of the distribution, ie y such that ...
Vdouble getCategories() const
std::string toString(T t)
General template method to convert to a string.
void discretizeEqualIntervals()
size_t numberOfCategories_
size_t getCategoryIndex(double value) const
std::vector< double > bounds_