bpp-core  2.2.0
DirichletDiscreteDistribution.cpp
Go to the documentation of this file.
1 //
2 // File: BetaDiscreteDistribution.cpp
3 // Created by: Laurent Guéguen
4 // Created on: jeudi 2 septembre 2010, à 17h 03
5 //
6 
7 /*
8  Copyright or © or Copr. CNRS, (November 17, 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 
42 #include "../Random/RandomTools.h"
43 #include "../NumConstants.h"
44 #include "../../Text/TextTools.h"
45 
46 using namespace bpp;
47 
48 // From the STL:
49 #include <cmath>
50 
51 using namespace std;
52 
56  AbstractParameterAliasable("Dirichlet."),
57  vpBDD_()
58 {
59  if (vn.size() <= 0 || vn.size() != valpha.size() - 1)
60  throw Exception("Wrong number of categories for Dirichlet distribution: " + TextTools::toString(vn.size()));
61 
62  for (size_t j = 0; j < valpha.size(); j++)
63  {
64  addParameter_(new Parameter("Dirichlet.alpha_" + TextTools::toString(j + 1), valpha[j], new IntervalConstraint(1, 0.0001, true), true));
65  }
66 
67  for (size_t j = 0; j < vn.size(); j++)
68  {
69  if (vn[j] <= 0)
70  throw Exception("Wrong number of categories in Dirichlet distribution constructor: " + TextTools::toString(vn[j]));
71  }
72 
73  for (size_t j = 0; j < vn.size(); j++)
74  {
75  vpBDD_.push_back(new BetaDiscreteDistribution(vn[j], 1, 1));
76  }
77 
78  discretize(valpha);
79 }
80 
82 {
83  for (unsigned int i = 0; i < vpBDD_.size(); i++)
84  {
85  delete vpBDD_[i];
86  }
87 }
88 
89 /******************************************************************************/
90 
92 {
95 }
96 
97 /******************************************************************************/
98 
100 {
101  Vdouble valpha;
102 
103  for (unsigned int j = 0; j < vpBDD_.size() + 1; j++)
104  {
105  valpha.push_back(getParameterValue("alpha_" + TextTools::toString(j + 1)));
106  }
107 
108  discretize(valpha);
109 }
110 
111 /******************************************************************************/
112 
114 {
115  /* discretization of Dirichlet distribution with equal proportions in
116  each category */
117 
118  double x;
119  ParameterList pL;
120  pL.addParameter(Parameter("Beta.alpha", 1));
121  pL.addParameter(Parameter("Beta.beta", 1));
122  for (unsigned int j = 0; j < vpBDD_.size(); j++)
123  {
124  x = 0;
125  for (unsigned int i = j + 1; i < valpha.size(); i++)
126  {
127  x += valpha[i];
128  }
129 
130  pL.setParameterValue("Beta.alpha", valpha[j]);
131  pL.setParameterValue("Beta.beta", x);
132 
133  vpBDD_[j]->matchParametersValues(pL);
134  }
135 }
136 
137 
138 /******************************************************************************/
139 
141 {
142  size_t n = 1;
143 
144  for (size_t j = 0; j < vpBDD_.size(); j++)
145  {
146  n *= vpBDD_[j]->getNumberOfCategories();
147  }
148 
149  return n;
150 }
151 
152 /******************************************************************************/
153 
155 {
156  if (value.size() != vpBDD_.size() + 1)
157  throw Exception("Bad Vdouble parameter in DirichletDiscreteDistribution::getValueCategory");
158 
159  Vdouble vd;
160  double y, sumc = 0;
161 
162  for (size_t j = 0; j < vpBDD_.size(); j++)
163  {
164  if (1 - sumc < NumConstants::VERY_TINY())
166  else
167  y = vpBDD_[j]->getValueCategory(value[j] / (1 - sumc)) * (1 - sumc);
168  sumc += y;
169  vd.push_back(y);
170  }
171  vd.push_back(1 - sumc);
172  return vd;
173 }
174 
175 /******************************************************************************/
176 
178 {
179  if (category.size() != vpBDD_.size() + 1)
180  throw Exception("Bad Vdouble parameter in DirichletDiscreteDistribution::getProbability");
181 
182  double sumc = 0;
183  double p = 1;
184 
185  for (unsigned int j = 0; j < vpBDD_.size(); j++)
186  {
187  p *= vpBDD_[j]->getProbability(category[j] / (1 - sumc));
188  sumc += category[j];
189  }
190  return p;
191 }
192 
193 /******************************************************************************/
194 
196 {
197  VVdouble vvd1, vvd2;
198  Vdouble vdj, vd;
199  double sumc = 0;
200 
201  vdj = vpBDD_[0]->getCategories();
202  for (unsigned int k = 0; k < vdj.size(); k++)
203  {
204  vd.push_back(vdj[k]);
205  vvd1.push_back(vd);
206  vd.pop_back();
207  }
208 
209  for (unsigned int j = 1; j < vpBDD_.size(); j++)
210  {
211  vdj = vpBDD_[j]->getCategories();
212  vvd2.clear();
213  for (unsigned int i = 0; i < vvd1.size(); i++)
214  {
215  vd = vvd1[i];
216  sumc = 0;
217  for (unsigned int k = 0; k < vd.size(); k++)
218  {
219  sumc += vd[k];
220  }
221  for (unsigned int k = 0; k < vdj.size(); k++)
222  {
223  vd.push_back(vdj[k] * (1 - sumc));
224  vvd2.push_back(vd);
225  vd.pop_back();
226  }
227  }
228  vvd1 = vvd2;
229  }
230 
231  vvd2.clear();
232  for (unsigned int i = 0; i < vvd1.size(); i++)
233  {
234  vd = vvd1[i];
235  sumc = 0;
236  for (unsigned int k = 0; k < vd.size(); k++)
237  {
238  sumc += vd[k];
239  }
240  vd.push_back(1 - sumc);
241  vvd2.push_back(vd);
242  }
243 
244  return vvd2;
245 }
246 
247 /******************************************************************************/
248 
250 {
251  Vdouble vd;
252  double x, sumc = 0;
253  for (unsigned int j = 0; j < vpBDD_.size(); j++)
254  {
255  x = vpBDD_[j]->rand() * (1 - sumc);
256  sumc += x;
257  vd.push_back(x);
258  }
259 
260  vd.push_back(1 - sumc);
261  return vd;
262 }
263 
264 /******************************************************************************/
265 
267 {
268  Vdouble vd;
269  double x, sumc = 0;
270  for (unsigned int j = 0; j < vpBDD_.size(); j++)
271  {
272  x = vpBDD_[j]->randC() * (1 - sumc);
273  sumc += x;
274  vd.push_back(x);
275  }
276 
277  vd.push_back(1 - sumc);
278  return vd;
279 }
280 
281 /******************************************************************************/
282 
void fireParameterChanged(const ParameterList &parameters)
Notify the class when one or several parameters have changed.
An interval, either bounded or not, which can also have infinite bounds.
Definition: Constraints.h:135
This class allows to perform a correspondence analysis.
Vdouble rand() const
Draw a random vector from this distribution.
STL namespace.
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:135
void addParameter_(Parameter *parameter)
Discretized Beta distribution with parameters alpha and beta, on a given interval. On default, the interval is , but it can be restricted.
static double VERY_TINY()
Definition: NumConstants.h:82
virtual void setParameterValue(const std::string &name, double value)
Set the value of parameter with name name to be equal to value.
static std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:189
The parameter list object.
Definition: ParameterList.h:61
A partial implementation of the Parametrizable interface.
std::vector< double > Vdouble
Definition: VectorTools.h:67
DirichletDiscreteDistribution(std::vector< size_t > vn, Vdouble valpha)
Build a new discretized Dirichlet distribution.
virtual double getProbability(Vdouble &category) const
void fireParameterChanged(const ParameterList &parameters)
Notify the class when one or several parameters have changed.
virtual void addParameter(const Parameter &param)
Add a new parameter at the end of the list.
Exception base class.
Definition: Exceptions.h:57
Vdouble randC() const
Draw a random vector from the continuous version of this distribution.
std::vector< BetaDiscreteDistribution *> vpBDD_
std::vector< Vdouble > VVdouble
Definition: VectorTools.h:68
double getParameterValue(const std::string &name) const
Get the value for parameter of name &#39;name&#39;.