42 #include "../Io/FileTools.h" 43 #include "../Text/TextTools.h" 44 #include "../Text/StringTokenizer.h" 45 #include "../Text/KeyvalTools.h" 47 #include "../Numeric/Prob/DiscreteDistribution.h" 48 #include "../Numeric/Prob/InvariantMixedDiscreteDistribution.h" 49 #include "../Numeric/Prob/ConstantDistribution.h" 50 #include "../Numeric/Prob/SimpleDiscreteDistribution.h" 51 #include "../Numeric/Prob/MixtureOfDiscreteDistributions.h" 52 #include "../Numeric/Prob/GammaDiscreteDistribution.h" 53 #include "../Numeric/Prob/GaussianDiscreteDistribution.h" 54 #include "../Numeric/Prob/BetaDiscreteDistribution.h" 55 #include "../Numeric/Prob/ExponentialDiscreteDistribution.h" 56 #include "../Numeric/Prob/TruncatedExponentialDiscreteDistribution.h" 57 #include "../Numeric/Prob/UniformDiscreteDistribution.h" 58 #include "../Numeric/AutoParameter.h" 70 const std::string& distDescription,
73 unparsedArguments_.clear();
75 auto_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)
161 for (
unsigned int i = 0; i < v.size(); 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++)
191 pdd = nestedReader.
read(v_nestedDistrDescr[i],
true);
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")
212 if (args.find(
"alpha") != args.end())
213 unparsedArguments_[
"Gamma.alpha"] = args[
"alpha"];
214 if (args.find(
"beta") != args.end())
215 unparsedArguments_[
"Gamma.beta"] = args[
"beta"];
217 else if (distName ==
"Gaussian")
220 if (args.find(
"mu") != args.end())
221 unparsedArguments_[
"Gaussian.mu"] = args[
"mu"];
222 if (args.find(
"sigma") != args.end())
223 unparsedArguments_[
"Gaussian.sigma"] = args[
"sigma"];
225 else if (distName ==
"Beta")
228 if (args.find(
"alpha") != args.end())
229 unparsedArguments_[
"Beta.alpha"] = args[
"alpha"];
230 if (args.find(
"beta") != args.end())
231 unparsedArguments_[
"Beta.beta"] = args[
"beta"];
233 else if (distName ==
"Exponential")
236 if (args.find(
"lambda") != args.end())
237 unparsedArguments_[
"Exponential.lambda"] = args[
"lambda"];
238 if (args.find(
"median") != args.end())
241 else if (distName ==
"TruncExponential")
245 if (args.find(
"median") != args.end())
247 if (args.find(
"lambda") != args.end())
248 unparsedArguments_[
"TruncExponential.lambda"] = args[
"lambda"];
249 if (args.find(
"tp") != args.end())
250 unparsedArguments_[
"TruncExponential.tp"] = args[
"tp"];
252 else if (distName ==
"Uniform")
254 if (args.find(
"begin") == args.end())
255 throw Exception(
"Missing argument 'begin' in Uniform distribution");
256 if (args.find(
"end") == args.end())
257 throw Exception(
"Missing argument 'end' in Uniform distribution");
260 TextTools::to<double>(args[
"begin"]),
261 TextTools::to<double>(args[
"end"])));
265 throw Exception(
"Unknown distribution: " + distName +
".");
277 return rDist.release();
283 std::map<std::string, std::string>& globalAliases,
284 std::vector<std::string>& writtenNames)
const 297 write(*pd, out, globalAliases, writtenNames);
306 for (
unsigned int i = 0; i < nd; i++)
315 for (
unsigned int i = 0; i < nd; i++)
322 for (
unsigned int i = 1; i < nd; i++)
329 if (dynamic_cast<const BetaDiscreteDistribution*>(&dist) ||
331 dynamic_cast<const GammaDiscreteDistribution*>(&dist) ||
333 dynamic_cast<const TruncatedExponentialDiscreteDistribution*>(&dist) ||
358 for (
unsigned int i = 0; i < nd; i++)
365 for (
size_t i = 0; i < nd; i++)
373 const std::map<size_t, std::vector<double> > range = ps->
getRanges();
374 if (range.size() != 0)
377 std::map<size_t, std::vector<double> >::const_iterator it(range.begin());
378 while (it != range.end())
383 if (it != range.end())
389 for (
size_t i = 1; i < nd; i++)
393 for (
size_t i = 1; i < nd + 1; i++)
411 for (
size_t i = 0; i < pl.
size(); ++i)
418 for (
size_t i = 0; i < pl.
size(); ++i)
420 const string pName = pl[i].getName();
422 pl[i].setValue(value);
427 rDist.matchParametersValues(pl);
430 for (
size_t c = 0; c < rDist.getNumberOfCategories(); ++c)
double getNProbability(size_t n) const
Returns the probability of the n-th discrete distribution in the mixture.
double getCategory(size_t categoryIndex) const
virtual size_t getNumberOfCategories() const =0
const std::map< size_t, std::vector< double > > getRanges() const
This class allows to perform a correspondence analysis.
Interface for discrete distribution objects.
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.
const std::string & nextToken()
Get the next available token. If no token is availbale, throw an Exception.
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.
double getProbability(size_t categoryIndex) const
size_t getNumberOfCategories() const
Discretized Exponential distribution.
static std::string toString(T t)
General template method to convert to a string.
static int toInt(const std::string &s, char scientificNotation='e')
Convert from string to int.
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...
const DiscreteDistribution * getVariableSubDistribution() const
size_t getNumberOfDistributions() const
Returns the number of discrete distributions in the mixture.
static bool isEmpty(const std::string &s)
Tell if a string is empty.
virtual void setMessageHandler(OutputStream *mh)
Set the message handler for this AutoParameter.
virtual std::vector< std::string > getParameterNames() const
Get all parameter names in the list.
virtual std::string getName() const =0
Get the name of the distribution.
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.
Discretized Gamma distribution.
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...
Constant discrete distribution.
Discrete mixed distribution, with a one-category fixed value (called "invariant") and a user-specifie...
const DiscreteDistribution * getNDistribution(size_t n) const
Returns a pointer to the n-th discrete distribution in the mixture.
static double toDouble(const std::string &s, char dec='.', char scientificNotation='e')
Convert from string to double.
virtual size_t getNumberOfParameters() const =0
Get the number of parameters.
double getLowerBound() const
methods about the range of the definition
std::string getNamespace() const
Discretized Truncated (on the right) Exponential distribution, where the probabilities are given the ...