5 #include "../Io/FileTools.h" 6 #include "../Numeric/AutoParameter.h" 7 #include "../Numeric/Prob/BetaDiscreteDistribution.h" 8 #include "../Numeric/Prob/ConstantDistribution.h" 9 #include "../Numeric/Prob/DiscreteDistribution.h" 10 #include "../Numeric/Prob/ExponentialDiscreteDistribution.h" 11 #include "../Numeric/Prob/GammaDiscreteDistribution.h" 12 #include "../Numeric/Prob/GaussianDiscreteDistribution.h" 13 #include "../Numeric/Prob/InvariantMixedDiscreteDistribution.h" 14 #include "../Numeric/Prob/MixtureOfDiscreteDistributions.h" 15 #include "../Numeric/Prob/SimpleDiscreteDistribution.h" 16 #include "../Numeric/Prob/TruncatedExponentialDiscreteDistribution.h" 17 #include "../Numeric/Prob/UniformDiscreteDistribution.h" 18 #include "../Text/KeyvalTools.h" 19 #include "../Text/StringTokenizer.h" 20 #include "../Text/TextTools.h" 33 const std::string& distDescription,
36 unparsedArguments_.clear();
38 unique_ptr<DiscreteDistributionInterface> rDist;
39 map<string, string> args;
42 if ((distName ==
"InvariantMixed") || (distName ==
"Invariant"))
45 string nestedDistDescription = args[
"dist"];
47 throw Exception(
"BppODiscreteDistributionFormat::read. Missing argument 'dist' for distribution 'Invariant'.");
55 rDist = make_unique<InvariantMixedDiscreteDistribution>(std::move(nestedDistribution), 0.1, 0.000001);
58 for (
auto& it : unparsedArgumentsNested)
60 unparsedArguments_[
"Invariant." + it.first] = it.second;
63 if (args.find(
"p") != args.end())
64 unparsedArguments_[
"Invariant.p"] = args[
"p"];
66 else if (distName ==
"Constant")
68 if (args.find(
"value") == args.end())
69 throw Exception(
"Missing argument 'value' in Constant distribution");
70 rDist = make_unique<ConstantDistribution>(TextTools::to<double>(args[
"value"]));
71 unparsedArguments_[
"Constant.value"] = args[
"value"];
73 else if (distName ==
"Simple")
75 if (args.find(
"values") == args.end())
76 throw Exception(
"Missing argument 'values' in Simple distribution");
77 if (args.find(
"probas") == args.end())
78 throw Exception(
"Missing argument 'probas' in Simple distribution");
79 vector<double> probas, values;
81 string rf = args[
"values"];
91 std::map<size_t, std::vector<double>> ranges;
93 if (args.find(
"ranges") != args.end())
95 string rr = args[
"ranges"];
105 ppv = desc.find(
";");
116 if (ranges.size() == 0)
117 rDist = make_unique<SimpleDiscreteDistribution>(values, probas);
119 rDist = make_unique<SimpleDiscreteDistribution>(values, ranges, probas);
128 else if (distName ==
"Mixture")
130 if (args.find(
"probas") == args.end())
131 throw Exception(
"Missing argument 'probas' in Mixture distribution");
132 vector<double> probas;
133 vector<unique_ptr<DiscreteDistributionInterface>> v_pdd;
134 unique_ptr<DiscreteDistributionInterface> pdd;
135 string rf = args[
"probas"];
140 vector<string> v_nestedDistrDescr;
142 unsigned int nbd = 0;
146 if (v_nestedDistrDescr.size() != probas.size())
147 throw Exception(
"Number of distributions (keyword 'dist" +
TextTools::toString(probas.size()) +
"') do not fit the number of probabilities");
151 for (
unsigned i = 0; i < v_nestedDistrDescr.size(); ++i)
156 for (
auto& it : unparsedArgumentsNested)
158 unparsedArguments_[distName +
"." +
TextTools::toString(i + 1) +
"_" + it.first] = it.second;
160 v_pdd.push_back(std::move(pdd));
162 rDist = make_unique<MixtureOfDiscreteDistributions>(v_pdd, probas);
166 if (args.find(
"n") == args.end())
167 throw Exception(
"Missing argument 'n' (number of classes) in " + distName
169 unsigned int nbClasses = TextTools::to<unsigned int>(args[
"n"]);
171 if (distName ==
"Gamma")
175 if (args.find(
"offset") != args.end())
183 if (args.find(
"ParamOffset") != args.end())
188 if (args.find(
"alpha") != args.end())
189 unparsedArguments_[
"Gamma.alpha"] = args[
"alpha"];
190 if (args.find(
"beta") != args.end())
191 unparsedArguments_[
"Gamma.beta"] = args[
"beta"];
192 if (args.find(
"offset") != args.end())
193 unparsedArguments_[
"Gamma.offset"] = args[
"offset"];
195 else if (distName ==
"Gaussian")
198 if (args.find(
"mu") != args.end())
199 unparsedArguments_[
"Gaussian.mu"] = args[
"mu"];
200 if (args.find(
"sigma") != args.end())
201 unparsedArguments_[
"Gaussian.sigma"] = args[
"sigma"];
203 else if (distName ==
"Beta")
206 if (args.find(
"alpha") != args.end())
207 unparsedArguments_[
"Beta.alpha"] = args[
"alpha"];
208 if (args.find(
"beta") != args.end())
209 unparsedArguments_[
"Beta.beta"] = args[
"beta"];
211 else if (distName ==
"Exponential")
214 if (args.find(
"lambda") != args.end())
215 unparsedArguments_[
"Exponential.lambda"] = args[
"lambda"];
216 if (args.find(
"median") != args.end())
219 else if (distName ==
"TruncExponential")
223 if (args.find(
"median") != args.end())
225 if (args.find(
"lambda") != args.end())
226 unparsedArguments_[
"TruncExponential.lambda"] = args[
"lambda"];
227 if (args.find(
"tp") != args.end())
228 unparsedArguments_[
"TruncExponential.tp"] = args[
"tp"];
230 else if (distName ==
"Uniform")
232 if (args.find(
"begin") == args.end())
233 throw Exception(
"Missing argument 'begin' in Uniform distribution");
234 if (args.find(
"end") == args.end())
235 throw Exception(
"Missing argument 'end' in Uniform distribution");
238 TextTools::to<double>(args[
"begin"]),
239 TextTools::to<double>(args[
"end"])));
243 throw Exception(
"Unknown distribution: " + distName +
".");
262 std::map<std::string, std::string>& globalAliases,
263 std::vector<std::string>& writtenNames)
const 274 writeDiscreteDistribution(pd, out, globalAliases, writtenNames);
283 for (
size_t i = 0; i < nd; ++i)
288 writeDiscreteDistribution(mix.nDistribution(i), out, globalAliases, writtenNames);
292 for (
size_t i = 0; i < nd; ++i)
294 out << mix.getNProbability(i);
299 for (
size_t i = 1; i < nd; ++i)
308 if (dynamic_cast<const BetaDiscreteDistribution*>(&dist) ||
310 dynamic_cast<const GammaDiscreteDistribution*>(&dist) ||
312 dynamic_cast<const TruncatedExponentialDiscreteDistribution*>(&dist) ||
328 out <<
"value=" << pc.getLowerBound();
342 for (
size_t i = 0; i < nd; ++i)
344 out << ps.getCategory(i);
349 for (
size_t i = 0; i < nd; ++i)
351 out << ps.getProbability(i);
357 auto range = ps.getRanges();
358 if (range.size() != 0)
361 auto it(range.begin());
362 while (it != range.end())
367 if (it != range.end())
373 for (
size_t i = 1; i < nd; ++i)
377 for (
size_t i = 1; i < nd + 1; ++i)
397 for (
size_t i = 0; i < pl.
size(); ++i)
404 for (
size_t i = 0; i < pl.
size(); ++i)
406 const string pName = pl[i].getName();
408 pl[i].setValue(value);
virtual bool matchParametersValues(const ParameterList ¶meters)=0
Update the parameters from parameters.
const DiscreteDistributionInterface & variableSubDistribution() const
virtual std::string getName() const =0
Get the name of the distribution.
double toDouble(const std::string &s, char dec, char scientificNotation)
Convert from string to double.
virtual void setMedian(bool median)=0
Sets the median value to true to say that the value in a class is proportional to the median value of...
const std::string & nextToken()
Get the next available token. If no token is availbale, throw an Exception.
virtual double getParameterValue(const std::string &name) const =0
Get the value for parameter of name 'name'.
bool hasMoreToken() const
Tell if some tokens are still available.
virtual const ParameterList & getParameters() const =0
Get all parameters available.
Interface for discrete distribution objects.
Discretized Gaussian distribution.
Discretized Beta distribution with parameters alpha and beta, on a given interval. On default, the interval is , but it can be restricted.
size_t getNumberOfCategories() const
virtual double getProbability(size_t categoryIndex) const =0
Discretized Exponential distribution.
A Discrete distribution object defined by a vector of Discrete Distributions and a set of probabiliti...
The parameter list object.
A Discrete distribution object, where some specific probabilities are assigned to a finite set of val...
virtual void setMessageHandler(std::shared_ptr< OutputStream > mh)
Set the message handler for this AutoParameter.
size_t getNumberOfDistributions() const
Returns the number of discrete distributions in the mixture.
virtual std::vector< std::string > getParameterNames() const
Get all parameter names in the list.
virtual size_t getNumberOfCategories() const =0
virtual const ParameterList & getIndependentParameters() const =0
Get the minimal list of parameters to set the model.
virtual void setParameter(size_t index, const Parameter ¶m)
Change given parameter.
virtual std::string getParameterNameWithoutNamespace(const std::string &name) const =0
Resolves a parameter name according to the current namespace.
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
Discretized Gamma distribution with an offset.
int toInt(const std::string &s, char scientificNotation)
Convert from string to int.
Constant discrete distribution.
Discrete mixed distribution, with a one-category fixed value (called "invariant") and a user-specifie...
virtual double getCategory(size_t categoryIndex) const =0
bool isEmpty(const std::string &s)
Tell if a string is empty. A string is considered to be 'empty' if it is only made of white spaces...
std::string toString(T t)
General template method to convert to a string.
virtual size_t getNumberOfParameters() const =0
Get the number of parameters.
Discretized Truncated (on the right) Exponential distribution, where the probabilities are given the ...