bpp-core3  3.0.0
Simplex.cpp
Go to the documentation of this file.
1 //
2 // File: Simplex.cpp
3 // Authors:
4 // Laurent Guéguen
5 // Created: mardi 31 mai 2011, à 13h 16
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for numerical calculus.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 
42 #include "../NumConstants.h"
43 #include "../VectorTools.h"
44 #include "Simplex.h"
45 
46 using namespace bpp;
47 using namespace std;
48 
49 Simplex::Simplex(const std::vector<double>& probas, unsigned short method, bool allowNull, const std::string& name) : AbstractParameterAliasable(name),
50  dim_(probas.size()),
51  method_(method),
52  vProb_(),
53  valpha_()
54 {
55  if (dim_ == 0)
56  return;
57 
58  double sum = VectorTools::sum(probas);
59  if (fabs(1. - sum) > NumConstants::SMALL())
60  throw Exception("Simplex. Probabilities must equal 1 (sum =" + TextTools::toString(sum) + ").");
61 
62  std::shared_ptr<Constraint> pc = (allowNull ? Parameter::PROP_CONSTRAINT_IN : Parameter::PROP_CONSTRAINT_EX);
63 
64  for (unsigned int i = 0; i < dim_; i++)
65  {
66  vProb_.push_back(probas[i]);
67  }
68 
69  double y = 1;
70  switch (method_)
71  {
72  case 1:
73  for (unsigned int i = 0; i < dim_ - 1; i++)
74  {
75  addParameter_(new Parameter(name + "theta" + TextTools::toString(i + 1), vProb_[i] / y, pc));
76  y -= vProb_[i];
77  }
78  break;
79  case 2:
80  for (unsigned int i = 0; i < dim_ - 1; i++)
81  {
82  addParameter_(new Parameter(name + "theta" + TextTools::toString(i + 1), vProb_[i] / (vProb_[i] + vProb_[i + 1]), pc));
83  }
84  for (unsigned int i = 0; i < dim_ - 1; i++)
85  {
86  valpha_.push_back(vProb_[i + 1] / vProb_[i]);
87  }
88  break;
89  case 3:
90  for (size_t i = 1; i < dim_; i++)
91  {
92  size_t o = i;
93  size_t li2 = 0; // rank of the strongest bit
94  while (o)
95  {
96  li2++;
97  o = o >> 1;
98  }
99 
100  double i1 = 0, i0 = 0;
101  size_t j = 0;
102  size_t pi = i & ~(1 << (li2 - 1));
103  while (j < dim_)
104  {
105  size_t t = (j << li2) + pi;
106  if (t >= dim_)
107  break;
108  else
109  i0 += vProb_[t];
110  t += (1 << (li2 - 1));
111  if (t < dim_)
112  i1 += vProb_[t];
113  j++;
114  }
115  addParameter_(new Parameter(name + "theta" + TextTools::toString(i), i1 / (i0 + i1), pc));
116  }
117  break;
118  }
119 }
120 
121 Simplex::Simplex(size_t dim, unsigned short method, bool allowNull, const std::string& name) :
123  dim_(dim),
124  method_(method),
125  vProb_(),
126  valpha_()
127 {
128  if (dim_ == 0)
129  return;
130 
131  for (size_t i = 0; i < dim_; i++)
132  {
133  vProb_.push_back(1. / static_cast<double>(dim_));
134  }
135 
136  std::shared_ptr<Constraint> pc = (allowNull ? Parameter::PROP_CONSTRAINT_IN : Parameter::PROP_CONSTRAINT_EX);
137 
138  double y = 1;
139  switch (method_)
140  {
141  case 1:
142  for (unsigned int i = 0; i < dim_ - 1; i++)
143  {
144  addParameter_(new Parameter(name + "theta" + TextTools::toString(i + 1), vProb_[i] / y, pc));
145  y -= vProb_[i];
146  }
147  break;
148  case 2:
149  for (unsigned int i = 0; i < dim_ - 1; i++)
150  {
151  addParameter_(new Parameter(name + "theta" + TextTools::toString(i + 1), 0.5, pc));
152  }
153  for (unsigned int i = 0; i < dim_ - 1; i++)
154  {
155  valpha_.push_back(1.);
156  }
157  break;
158  case 3:
159  for (unsigned int i = 0; i < dim_ - 1; i++)
160  {
161  addParameter_(new Parameter(name + "theta" + TextTools::toString(i + 1), 0.5, pc));
162  }
164 
165  break;
166  }
167 }
168 
170 {
171  if (dim_ == 0)
172  return;
173 
174  double x = 1.0;
175  switch (method_)
176  {
177  case 1:
178  double th;
179  for (unsigned int i = 0; i < dim_ - 1; i++)
180  {
181  th = getParameterValue("theta" + TextTools::toString(i + 1));
182  vProb_[i] = th * x;
183  x *= 1 - th;
184  }
185  vProb_[dim_ - 1] = x;
186  break;
187  case 2:
188  for (unsigned int i = 0; i < dim_ - 1; i++)
189  {
190  th = getParameterValue("theta" + TextTools::toString(i + 1));
191  valpha_[i] = (1 - th) / th;
192  }
193  th = 1;
194  vProb_[0] = 1;
195  x = 1.0;
196  for (unsigned int i = 0; i < dim_ - 1; i++)
197  {
198  th *= valpha_[i];
199  vProb_[i + 1] = th;
200  x += vProb_[i + 1];
201  }
202 
203  if (x > NumConstants::TINY()) // avoid rounding pb
204  for (auto& vp : vProb_)
205  {
206  vp /= x;
207  }
208  else
209  for (auto& vp : vProb_)
210  {
211  vp = 1.0 / double(dim_);
212  }
213 
214  break;
215  case 3:
216  size_t o = dim_;
217  size_t ld2 = 0; // rank of the strongest bit
218  while (o)
219  {
220  ld2++;
221  o = o >> 1;
222  }
223  for (size_t i = 0; i < dim_; i++)
224  {
225  x = 1;
226  size_t ld = ld2;
227  size_t k = i;
228  while (ld)
229  {
230  if (k >> (ld - 1))
231  x *= getParameterValue("theta" + TextTools::toString(k));
232  else
233  {
234  if ((k + (1 << (ld - 1))) < dim_)
235  x *= 1 - getParameterValue("theta" + TextTools::toString(k + (1 << (ld - 1))));
236  }
237  k &= ~(1 << (--ld));
238  }
239  vProb_[i] = x;
240  }
241  break;
242  }
243 }
244 
245 
246 void Simplex::setFrequencies(const std::vector<double>& probas)
247 {
248  if (dim_ == 0)
249  return;
250 
251  double sum = VectorTools::sum(probas);
252  if (fabs(1. - sum) > NumConstants::SMALL())
253  throw Exception("Simplex::setFrequencies. Probabilities must equal 1 (sum =" + TextTools::toString(sum) + ").");
254 
255  double y = 1;
256 
257  ParameterList pl;
258  switch (method_)
259  {
260  case 1:
261  for (unsigned int i = 0; i < dim_ - 1; i++)
262  {
263  pl.addParameter(Parameter(getNamespace() + "theta" + TextTools::toString(i + 1), probas[i] / y));
264  y -= probas[i];
265  }
266  break;
267  case 2:
268  for (unsigned int i = 0; i < dim_ - 1; i++)
269  {
270  pl.addParameter(Parameter(getNamespace() + "theta" + TextTools::toString(i + 1), probas[i] / (probas[i] + probas[i + 1])));
271  valpha_[i] = probas[i + 1] / probas[i];
272  }
273  break;
274  case 3:
275  for (size_t i = 1; i < dim_; i++)
276  {
277  size_t o = i;
278  size_t li2 = 0; // rank of the strongest bit
279  while (o)
280  {
281  li2++;
282  o = o >> 1;
283  }
284 
285  double i1 = 0, i0 = 0;
286  size_t j = 0;
287  size_t pi = i & ~(1 << (li2 - 1));
288  while (j < dim_)
289  {
290  size_t t = (j << li2) + pi;
291  if (t >= dim_)
292  break;
293  else
294  i0 += probas[t];
295  t += (1 << (li2 - 1));
296  if (t < dim_)
297  i1 += probas[t];
298  j++;
299  }
300  pl.addParameter(Parameter(getNamespace() + "theta" + TextTools::toString(i), i1 / (i0 + i1)));
301  }
302  break;
303  }
304 
306 }
A partial implementation of the Parametrizable interface.
void addParameter_(Parameter *parameter)
double getParameterValue(const std::string &name) const
Get the value for parameter of name 'name'.
bool matchParametersValues(const ParameterList &parameters)
Update the parameters from parameters.
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
Definition: Exceptions.h:59
static double TINY()
Definition: NumConstants.h:82
static double SMALL()
Definition: NumConstants.h:81
The parameter list object.
Definition: ParameterList.h:65
virtual void addParameter(const Parameter &param)
Add a new parameter at the end of the list.
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:135
static const std::shared_ptr< IntervalConstraint > PROP_CONSTRAINT_IN
Definition: Parameter.h:329
static const std::shared_ptr< IntervalConstraint > PROP_CONSTRAINT_EX
Definition: Parameter.h:330
unsigned short method_
the method of parametrization.
Definition: Simplex.h:121
void fireParameterChanged(const ParameterList &parameters)
Notify the class when one or several parameters have changed.
Definition: Simplex.cpp:169
std::vector< double > vProb_
Definition: Simplex.h:123
size_t dim_
The dimension+1 of the space simplex (ie the number of probabilities).
Definition: Simplex.h:111
std::vector< double > valpha_
just used with local ratio (method 2)
Definition: Simplex.h:128
void setFrequencies(const std::vector< double > &)
Definition: Simplex.cpp:246
Simplex(size_t dim, unsigned short method=0, bool allowNull=false, const std::string &name="Simplex.")
Builds a new Simplex object from a number of probabilities. They are initialized equal.
Definition: Simplex.cpp:121
static T sum(const std::vector< T > &v1)
Definition: VectorTools.h:624
std::string toString(T t)
General template method to convert to a string.
Definition: TextTools.h:153