bpp-core  2.2.0
Simplex.cpp
Go to the documentation of this file.
1 //
2 // File: Simplex.cpp
3 // Created by: Laurent Guéguen
4 // Created on: mardi 31 mai 2011, à 13h 16
5 //
6 
7 /*
8  Copyright or © or Copr. Bio++ Development Team, (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 
40 #include "Simplex.h"
41 #include "../NumConstants.h"
42 
43 #include "../VectorTools.h"
44 
45 using namespace bpp;
46 using namespace std;
47 
48 Simplex::Simplex(const std::vector<double>& probas, unsigned short method, bool allowNull, const std::string& name) : AbstractParameterAliasable(name),
49  dim_(probas.size()),
50  method_(method),
51  vProb_(),
52  valpha_()
53 {
54  if (dim_==0)
55  return;
56 
57  double sum = VectorTools::sum(probas);
58  if (fabs(1. - sum) > NumConstants::SMALL())
59  throw Exception("Simplex. Probabilities must equal 1 (sum =" + TextTools::toString(sum) + ").");
60 
62 
63  for (unsigned int i = 0; i < dim_; i++)
64  {
65  vProb_.push_back(probas[i]);
66  }
67 
68  double y = 1;
69  switch (method_)
70  {
71  case 1:
72  for (unsigned int i = 0; i < dim_ - 1; i++)
73  {
74  addParameter_(new Parameter(name + "theta" + TextTools::toString(i + 1), vProb_[i] / y, pc));
75  y -= vProb_[i];
76  }
77  break;
78  case 2:
79  for (unsigned int i = 0; i < dim_ - 1; i++)
80  {
81  addParameter_(new Parameter(name + "theta" + TextTools::toString(i + 1), vProb_[i] / (vProb_[i] + vProb_[i + 1]), pc));
82  }
83  for (unsigned int i = 0; i < dim_ - 1; i++)
84  {
85  valpha_.push_back(vProb_[i + 1] / vProb_[i]);
86  }
87  break;
88  case 3:
89  for (size_t i = 1; i < dim_; i++)
90  {
91  size_t o = i;
92  size_t li2 = 0; // rank of the strongest bit
93  while (o)
94  {
95  li2++;
96  o = o >> 1;
97  }
98 
99  double i1 = 0, i0 = 0;
100  size_t j = 0;
101  size_t pi = i & ~(1 << (li2 - 1));
102  while (j < dim_)
103  {
104  size_t t = (j << li2) + pi;
105  if (t >= dim_)
106  break;
107  else
108  i0 += vProb_[t];
109  t += (1 << (li2 - 1));
110  if (t < dim_)
111  i1 += vProb_[t];
112  j++;
113  }
114  addParameter_(new Parameter(name + "theta" + TextTools::toString(i), i1 / (i0 + i1), pc));
115  }
116  break;
117  }
118 }
119 
120 Simplex::Simplex(size_t dim, unsigned short method, bool allowNull, const std::string& name) :
122  dim_(dim),
123  method_(method),
124  vProb_(),
125  valpha_()
126 {
127  if (dim_==0)
128  return;
129 
130  for (size_t i = 0; i < dim_; i++)
131  {
132  vProb_.push_back(1. / static_cast<double>(dim_));
133  }
134 
136 
137  double y = 1;
138  switch (method_)
139  {
140  case 1:
141  for (unsigned int i = 0; i < dim_ - 1; i++)
142  {
143  addParameter_(new Parameter(name + "theta" + TextTools::toString(i + 1), vProb_[i] / y, pc));
144  y -= vProb_[i];
145  }
146  break;
147  case 2:
148  for (unsigned int i = 0; i < dim_ - 1; i++)
149  {
150  addParameter_(new Parameter(name + "theta" + TextTools::toString(i + 1), 0.5, pc));
151  }
152  for (unsigned int i = 0; i < dim_ - 1; i++)
153  {
154  valpha_.push_back(1.);
155  }
156  break;
157  case 3:
158  for (unsigned int i = 0; i < dim_ - 1; i++)
159  {
160  addParameter_(new Parameter(name + "theta" + TextTools::toString(i + 1), 0.5, pc));
161  }
163 
164  break;
165  }
166 }
167 
169 {
170  if (dim_==0)
171  return;
172 
174 
175  double x = 1.0;
176  switch (method_)
177  {
178  case 1:
179  double th;
180  for (unsigned int i = 0; i < dim_ - 1; i++)
181  {
182  th = getParameterValue("theta" + TextTools::toString(i + 1));
183  vProb_[i] = th * x;
184  x *= 1 - th;
185  }
186  vProb_[dim_ - 1] = x;
187  break;
188  case 2:
189  for (unsigned int i = 0; i < dim_ - 1; i++)
190  {
191  th = getParameterValue("theta" + TextTools::toString(i + 1));
192  valpha_[i] = (1 - th) / th;
193  }
194  th = 1;
195  vProb_[0] = 1;
196  x = 1.0;
197  for (unsigned int i = 0; i < dim_ - 1; i++)
198  {
199  th *= valpha_[i];
200  vProb_[i + 1] = th;
201  x += vProb_[i + 1];
202  }
203  for (unsigned int i = 0; i < dim_; i++)
204  {
205  vProb_[i] /= x;
206  }
207 
208  break;
209  case 3:
210  size_t o = dim_;
211  size_t ld2 = 0; // rank of the strongest bit
212  while (o)
213  {
214  ld2++;
215  o = o >> 1;
216  }
217  for (size_t i = 0; i < dim_; i++)
218  {
219  x = 1;
220  size_t ld = ld2;
221  size_t k = i;
222  while (ld)
223  {
224  if (k >> (ld - 1))
225  x *= getParameterValue("theta" + TextTools::toString(k));
226  else
227  {
228  if ((k + (1 << (ld - 1))) < dim_)
229  x *= 1 - getParameterValue("theta" + TextTools::toString(k + (1 << (ld - 1))));
230  }
231  k &= ~(1 << (--ld));
232  }
233  vProb_[i] = x;
234  }
235  break;
236  }
237 }
238 
239 
240 void Simplex::setFrequencies(const std::vector<double>& probas)
241 {
242  if (dim_==0)
243  return;
244 
245  double sum = VectorTools::sum(probas);
246  if (fabs(1. - sum) > NumConstants::SMALL())
247  throw Exception("Simplex::setFrequencies. Probabilities must equal 1 (sum =" + TextTools::toString(sum) + ").");
248 
249  double y = 1;
250 
251  ParameterList pl;
252  switch (method_)
253  {
254  case 1:
255  for (unsigned int i = 0; i < dim_ - 1; i++)
256  {
257  pl.addParameter(Parameter(getNamespace() + "theta" + TextTools::toString(i + 1), probas[i] / y));
258  y -= probas[i];
259  }
260  break;
261  case 2:
262  for (unsigned int i = 0; i < dim_ - 1; i++)
263  {
264  pl.addParameter(Parameter(getNamespace() + "theta" + TextTools::toString(i + 1), probas[i] / (probas[i] + probas[i + 1])));
265  valpha_[i] = probas[i + 1] / probas[i];
266  }
267  break;
268  case 3:
269  for (size_t i = 1; i < dim_; i++)
270  {
271  size_t o = i;
272  size_t li2 = 0; // rank of the strongest bit
273  while (o)
274  {
275  li2++;
276  o = o >> 1;
277  }
278 
279  double i1 = 0, i0 = 0;
280  size_t j = 0;
281  size_t pi = i & ~(1 << (li2 - 1));
282  while (j < dim_)
283  {
284  size_t t = (j << li2) + pi;
285  if (t >= dim_)
286  break;
287  else
288  i0 += vProb_[t];
289  t += (1 << (li2 - 1));
290  if (t < dim_)
291  i1 += vProb_[t];
292  j++;
293  }
294  pl.addParameter(Parameter(getNamespace() + "theta" + TextTools::toString(i), i1 / (i0 + i1)));
295  }
296  break;
297  }
298 
300 }
301 
void fireParameterChanged(const ParameterList &parameters)
Notify the class when one or several parameters have changed.
void setFrequencies(const std::vector< double > &)
Definition: Simplex.cpp:240
void fireParameterChanged(const ParameterList &parameters)
Notify the class when one or several parameters have changed.
Definition: Simplex.cpp:168
This class allows to perform a correspondence analysis.
std::vector< double > vProb_
Definition: Simplex.h:121
static T sum(const std::vector< T > &v1)
Definition: VectorTools.h:614
static const IntervalConstraint PROP_CONSTRAINT_EX
Definition: Parameter.h:337
STL namespace.
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:135
void addParameter_(Parameter *parameter)
size_t dim_
The dimension+1 of the space simplex (ie the number of probabilities).
Definition: Simplex.h:109
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 > valpha_
just used with local ratio (method 2)
Definition: Simplex.h:126
The constraint interface.
Definition: Constraints.h:62
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:120
bool matchParametersValues(const ParameterList &parameters)
Update the parameters from parameters.
static double SMALL()
Definition: NumConstants.h:80
virtual void addParameter(const Parameter &param)
Add a new parameter at the end of the list.
Exception base class.
Definition: Exceptions.h:57
static const IntervalConstraint PROP_CONSTRAINT_IN
Definition: Parameter.h:336
unsigned short method_
the method of parametrization.
Definition: Simplex.h:119
double getParameterValue(const std::string &name) const
Get the value for parameter of name &#39;name&#39;.