42 #include "../Io/FileTools.h"
43 #include "../Numeric/AutoParameter.h"
44 #include "../Numeric/Prob/BetaDiscreteDistribution.h"
45 #include "../Numeric/Prob/ConstantDistribution.h"
46 #include "../Numeric/Prob/DiscreteDistribution.h"
47 #include "../Numeric/Prob/ExponentialDiscreteDistribution.h"
48 #include "../Numeric/Prob/GammaDiscreteDistribution.h"
49 #include "../Numeric/Prob/GaussianDiscreteDistribution.h"
50 #include "../Numeric/Prob/InvariantMixedDiscreteDistribution.h"
51 #include "../Numeric/Prob/MixtureOfDiscreteDistributions.h"
52 #include "../Numeric/Prob/SimpleDiscreteDistribution.h"
53 #include "../Numeric/Prob/TruncatedExponentialDiscreteDistribution.h"
54 #include "../Numeric/Prob/UniformDiscreteDistribution.h"
55 #include "../Text/KeyvalTools.h"
56 #include "../Text/StringTokenizer.h"
57 #include "../Text/TextTools.h"
70 const std::string& distDescription,
73 unparsedArguments_.clear();
75 unique_ptr<DiscreteDistribution> rDist;
76 map<string, string> args;
79 if ((distName ==
"InvariantMixed") || (distName ==
"Invariant"))
82 string nestedDistDescription = args[
"dist"];
84 throw Exception(
"BppODiscreteDistributionFormat::read. Missing argument 'dist' for distribution 'Invariant'.");
95 for (map<string, string>::iterator it = unparsedArgumentsNested.begin();
96 it != unparsedArgumentsNested.end(); it++)
98 unparsedArguments_[
"Invariant." + it->first] = it->second;
101 if (args.find(
"p") != args.end())
102 unparsedArguments_[
"Invariant.p"] = args[
"p"];
104 else if (distName ==
"Constant")
106 if (args.find(
"value") == args.end())
107 throw Exception(
"Missing argument 'value' in Constant distribution");
109 unparsedArguments_[
"Constant.value"] = args[
"value"];
111 else if (distName ==
"Simple")
113 if (args.find(
"values") == args.end())
114 throw Exception(
"Missing argument 'values' in Simple distribution");
115 if (args.find(
"probas") == args.end())
116 throw Exception(
"Missing argument 'probas' in Simple distribution");
117 vector<double> probas, values;
119 string rf = args[
"values"];
129 std::map<size_t, std::vector<double> > ranges;
131 if (args.find(
"ranges") != args.end())
133 string rr = args[
"ranges"];
143 ppv = desc.find(
";");
154 if (ranges.size() == 0)
159 vector<string> v = rDist->getParameters().getParameterNames();
161 for (
unsigned int i = 0; i < v.size(); i++)
163 unparsedArguments_[v[i]] =
TextTools::toString(rDist->getParameterValue(rDist->getParameterNameWithoutNamespace(v[i])));
166 else if (distName ==
"Mixture")
168 if (args.find(
"probas") == args.end())
169 throw Exception(
"Missing argument 'probas' in Mixture distribution");
170 vector<double> probas;
171 vector<DiscreteDistribution*> v_pdd;
173 string rf = args[
"probas"];
178 vector<string> v_nestedDistrDescr;
180 unsigned int nbd = 0;
184 if (v_nestedDistrDescr.size() != probas.size())
185 throw Exception(
"Number of distributions (keyword 'dist" +
TextTools::toString(probas.size()) +
"') do not fit the number of probabilities");
189 for (
unsigned i = 0; i < v_nestedDistrDescr.size(); i++)
194 for (map<string, string>::iterator it = unparsedArgumentsNested.begin(); it != unparsedArgumentsNested.end(); it++)
196 unparsedArguments_[distName +
"." +
TextTools::toString(i + 1) +
"_" + it->first] = it->second;
198 v_pdd.push_back(pdd);
204 if (args.find(
"n") == args.end())
205 throw Exception(
"Missing argument 'n' (number of classes) in " + distName
207 unsigned int nbClasses = TextTools::to<unsigned int>(args[
"n"]);
209 if (distName ==
"Gamma")
213 if (args.find(
"offset") != args.end())
221 if (args.find(
"ParamOffset") != args.end())
226 if (args.find(
"alpha") != args.end())
227 unparsedArguments_[
"Gamma.alpha"] = args[
"alpha"];
228 if (args.find(
"beta") != args.end())
229 unparsedArguments_[
"Gamma.beta"] = args[
"beta"];
230 if (args.find(
"offset") != args.end())
231 unparsedArguments_[
"Gamma.offset"] = args[
"offset"];
233 else if (distName ==
"Gaussian")
236 if (args.find(
"mu") != args.end())
237 unparsedArguments_[
"Gaussian.mu"] = args[
"mu"];
238 if (args.find(
"sigma") != args.end())
239 unparsedArguments_[
"Gaussian.sigma"] = args[
"sigma"];
241 else if (distName ==
"Beta")
244 if (args.find(
"alpha") != args.end())
245 unparsedArguments_[
"Beta.alpha"] = args[
"alpha"];
246 if (args.find(
"beta") != args.end())
247 unparsedArguments_[
"Beta.beta"] = args[
"beta"];
249 else if (distName ==
"Exponential")
252 if (args.find(
"lambda") != args.end())
253 unparsedArguments_[
"Exponential.lambda"] = args[
"lambda"];
254 if (args.find(
"median") != args.end())
255 rDist->setMedian(
true);
257 else if (distName ==
"TruncExponential")
261 if (args.find(
"median") != args.end())
262 rDist->setMedian(
true);
263 if (args.find(
"lambda") != args.end())
264 unparsedArguments_[
"TruncExponential.lambda"] = args[
"lambda"];
265 if (args.find(
"tp") != args.end())
266 unparsedArguments_[
"TruncExponential.tp"] = args[
"tp"];
268 else if (distName ==
"Uniform")
270 if (args.find(
"begin") == args.end())
271 throw Exception(
"Missing argument 'begin' in Uniform distribution");
272 if (args.find(
"end") == args.end())
273 throw Exception(
"Missing argument 'end' in Uniform distribution");
276 TextTools::to<double>(args[
"begin"]),
277 TextTools::to<double>(args[
"end"])));
281 throw Exception(
"Unknown distribution: " + distName +
".");
293 return rDist.release();
300 std::map<std::string, std::string>& globalAliases,
301 std::vector<std::string>& writtenNames)
const
314 writeDiscreteDistribution(*pd, out, globalAliases, writtenNames);
323 for (
size_t i = 0; i < nd; i++)
328 writeDiscreteDistribution(*mix->
getNDistribution(i), out, globalAliases, writtenNames);
332 for (
size_t i = 0; i < nd; i++)
339 for (
size_t i = 1; i < nd; i++)
375 for (
size_t i = 0; i < nd; i++)
382 for (
size_t i = 0; i < nd; i++)
390 const std::map<size_t, std::vector<double> > range = ps->
getRanges();
391 if (range.size() != 0)
394 std::map<size_t, std::vector<double> >::const_iterator it(range.begin());
395 while (it != range.end())
400 if (it != range.end())
406 for (
size_t i = 1; i < nd; i++)
410 for (
size_t i = 1; i < nd + 1; i++)
428 for (
size_t i = 0; i < pl.
size(); ++i)
435 for (
size_t i = 0; i < pl.
size(); ++i)
437 const string pName = pl[i].getName();
439 pl[i].setValue(value);
double getCategory(size_t categoryIndex) const
double getProbability(size_t categoryIndex) const
size_t getNumberOfCategories() const
std::string getNamespace() const
virtual void setMessageHandler(OutputStream *mh)
Set the message handler for this AutoParameter.
Discretized Beta distribution with parameters alpha and beta, on a given interval....
Constant discrete distribution.
double getLowerBound() const
methods about the range of the definition
Interface for discrete distribution objects.
virtual double getProbability(size_t categoryIndex) const =0
virtual double getCategory(size_t categoryIndex) const =0
virtual std::string getName() const =0
Get the name of the distribution.
virtual size_t getNumberOfCategories() const =0
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
Discretized Exponential distribution.
Discretized Gamma distribution with an offset.
Discretized Gaussian distribution.
Discrete mixed distribution, with a one-category fixed value (called "invariant") and a user-specifie...
const DiscreteDistribution * getVariableSubDistribution() const
A Discrete distribution object defined by a vector of Discrete Distributions and a set of probabiliti...
double getNProbability(size_t n) const
Returns the probability of the n-th discrete distribution in the mixture.
size_t getNumberOfDistributions() const
Returns the number of discrete distributions in the mixture.
const DiscreteDistribution * getNDistribution(size_t n) const
Returns a pointer to the n-th discrete distribution in the mixture.
Extend the Parametrizable interface with support for parameter aliases.
virtual const ParameterList & getIndependentParameters() const =0
Get the minimal list of parameters to set the model.
The parameter list object.
virtual std::vector< std::string > getParameterNames() const
Get all parameter names in the list.
virtual void setParameter(size_t index, const Parameter ¶m)
Change given parameter.
virtual size_t getNumberOfParameters() const =0
Get the number of parameters.
virtual bool matchParametersValues(const ParameterList ¶meters)=0
Update the parameters from parameters.
A Discrete distribution object, where some specific probabilities are assigned to a finite set of val...
const std::map< size_t, std::vector< double > > getRanges() const
const std::string & nextToken()
Get the next available token. If no token is availbale, throw an Exception.
bool hasMoreToken() const
Tell if some tokens are still available.
Discretized Truncated (on the right) Exponential distribution, where the probabilities are given the ...
int toInt(const std::string &s, char scientificNotation)
Convert from string to int.
double toDouble(const std::string &s, char dec, char scientificNotation)
Convert from string to double.
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.