bpp-core  2.2.0
TransformedParameter.h
Go to the documentation of this file.
1 //
2 // File: TransformedParameter.h
3 // Created by: Julien Dutheil
4 // Created on: Fri Jan 30 09:42 2009
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team, (November 19, 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 #ifndef _TRANSFORMEDPARAMETER_H_
41 #define _TRANSFORMEDPARAMETER_H_
42 
43 #include "Parameter.h"
44 #include "NumConstants.h"
45 
46 #include <cmath>
47 
48 namespace bpp
49 {
50 
60  public Parameter
61 {
62  public:
63  TransformedParameter(const std::string& name, double value):
64  Parameter(name, value) {}
65 
66  TransformedParameter* clone() const = 0;
67 
68  public:
75  virtual void setOriginalValue(double value) throw (ConstraintException) = 0;
76 
80  virtual double getOriginalValue() const = 0;
81 
87  virtual double getFirstOrderDerivative() const throw (NotImplementedException) = 0;
88 
94  virtual double getSecondOrderDerivative() const throw (NotImplementedException) = 0;
95 };
96 
118  public TransformedParameter
119 {
120  private:
121  double scale_;
122  double bound_;
123  bool positive_;
124 
125  public:
135  RTransformedParameter(const std::string& name, double value, double bound = 0, bool positive = true, double scale = 1):
136  TransformedParameter(name, 1.),
137  scale_(scale),
138  bound_(bound),
139  positive_(positive)
140  {
141  setOriginalValue(value);
142  }
143 
144  RTransformedParameter* clone() const { return new RTransformedParameter(*this); }
145 
146  public:
147  void setOriginalValue(double value) throw (ConstraintException)
148  {
149  if (positive_ ? value <= bound_ : value >= bound_) throw ConstraintException("RTransformedParameter::setValue", this, value);
150  if (positive_ & (value < 1 + bound_)) setValue(log(scale_ * (value - bound_)));
151  if (positive_ & (value >= 1 + bound_)) setValue(scale_ * (value - 1. - bound_));
152  if (!positive_ & (value > -1 + bound_)) setValue(log(-scale_ * (value - bound_)));
153  if (!positive_ & (value <= -1 + bound_)) setValue(-scale_ * (value - 1. - bound_));
154  }
155 
156  double getOriginalValue() const
157  {
158  double x = getValue();
159  if (positive_)
160  if(x < 0) return exp(x) / scale_ + bound_;
161  else return x / scale_ + 1. + bound_;
162  else
163  if(x < 0) return - exp(-x) / scale_ + bound_;
164  else return - x / scale_ - 1. + bound_;
165  }
166 
168  {
169  double x = getValue();
170  if (positive_)
171  if(x < 0) return exp(x) / scale_;
172  else return 1. / scale_;
173  else
174  if(x < 0) return exp(-x) / scale_;
175  else return - 1. / scale_;
176  }
177 
179  {
180  double x = getValue();
181  if (positive_)
182  if(x < 0) return exp(x) / scale_;
183  else return 0;
184  else
185  if(x < 0) return - exp(-x) / scale_;
186  else return 0;
187  }
188 
189 };
190 
206  public TransformedParameter
207 {
208  private:
209  double scale_;
210  double lowerBound_;
211  double upperBound_;
212  bool hyper_;
213  double tiny_;
214 
215  public:
226  IntervalTransformedParameter(const std::string& name, double value, double lowerBound = 0, double upperBound = 1, double scale = 1, bool hyper = true):
227  TransformedParameter(name, hyper ?
228  scale * atanh(2. * (value - lowerBound) / (upperBound - lowerBound) - 1.) :
229  scale * tan(NumConstants::PI() * (value - lowerBound)/(upperBound - lowerBound) - NumConstants::PI() / 2.)),
230  scale_(scale),
231  lowerBound_(lowerBound),
232  upperBound_(upperBound),
233  hyper_(hyper),
234  tiny_(NumConstants::TINY())
235  {}
236 
238 
239  public:
240  void setOriginalValue(double value) throw (ConstraintException)
241  {
242  if (value <= lowerBound_ || value >= upperBound_) throw ConstraintException("IntervalTransformedParameter::setValue", this, value);
243  setValue(hyper_ ?
244  scale_ * atanh(2. * (value - lowerBound_) / (upperBound_ - lowerBound_) - 1.) :
245  scale_ * std::tan(NumConstants::PI() * (value - lowerBound_)/(upperBound_ - lowerBound_) - NumConstants::PI() / 2.));
246  }
247 
248  double getOriginalValue() const
249  {
250  double x = getValue();
251  double x2 = hyper_ ?
252  (tanh(x / scale_) + 1.) * (upperBound_ - lowerBound_) / 2. + lowerBound_ :
253  (atan(x / scale_) + NumConstants::PI() / 2.) * (upperBound_ - lowerBound_) / NumConstants::PI() + lowerBound_;
254  return x2;
255  }
256 
257 
259  {
260  double x = getValue();
261  double x2 = hyper_ ?
262  1. / (pow(cosh(x / scale_), 2)) * (upperBound_ - lowerBound_) / (2. * scale_) :
263  (upperBound_ - lowerBound_) / (NumConstants::PI() * scale_ * (pow(x / scale_, 2) + 1.));
264  return x2;
265  }
267  {
268  double x = getValue();
269  double x2 = hyper_ ?
270  - 1. / (pow(cosh(x / scale_), 2)) * tanh(x / scale_) * (upperBound_ - lowerBound_) / (scale_ * scale_) :
271  -2. * x * (upperBound_ - lowerBound_) / (NumConstants::PI() * pow(scale_, 3) * pow((pow(x / scale_, 2) + 1.), 2));
272  return x2;
273  }
274 };
275 
283  public TransformedParameter
284 {
285  public:
286  PlaceboTransformedParameter(const std::string& name, double value):
287  TransformedParameter(name, value)
288  {}
289 
291 
292  public:
293  void setOriginalValue(double value) throw (ConstraintException)
294  {
295  setValue(value);
296  }
297 
298  double getOriginalValue() const
299  {
300  return getValue();
301  }
302 
303  double getFirstOrderDerivative() const throw (NotImplementedException) { return 1.; }
304 
305  double getSecondOrderDerivative() const throw (NotImplementedException) { return 0.; }
306 };
307 
308 
309 } //end of namespace bpp.
310 
311 #endif //_TRANSFORMEDPARAMETER_H_
312 
this static class contains several useful constant values.
Definition: NumConstants.h:54
TransformedParameter(const std::string &name, double value)
This class allows to perform a correspondence analysis.
virtual void setOriginalValue(double value)=0
Set the value of the parameter using the orignal coordinate system.
IntervalTransformedParameter * clone() const
Create a copy of this object and send a pointer to it.
virtual double getOriginalValue() const =0
void setOriginalValue(double value)
Set the value of the parameter using the orignal coordinate system.
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:135
virtual double getSecondOrderDerivative() const =0
TransformedParameter * clone() const =0
Create a copy of this object and send a pointer to it.
&#39;Placebo&#39; parameter transformation from ] b, +inf [ or ] -inf, b [ to ]-inf, + inf [...
PlaceboTransformedParameter(const std::string &name, double value)
virtual void setValue(double value)
Set the value of this parameter.
Definition: Parameter.cpp:123
This expeption is sent when a given method is not implemented.
Definition: Exceptions.h:400
PlaceboTransformedParameter * clone() const
Create a copy of this object and send a pointer to it.
virtual double getFirstOrderDerivative() const =0
Parameter transformation from ] b, +inf [ or ] -inf, b [ to ]-inf, + inf [.
RTransformedParameter(const std::string &name, double value, double bound=0, bool positive=true, double scale=1)
Build a new RTransformedParameter, with given bound and scale.
IntervalTransformedParameter(const std::string &name, double value, double lowerBound=0, double upperBound=1, double scale=1, bool hyper=true)
Build a new IntervalTransformedParameter, with given bounds and scale.
void setOriginalValue(double value)
Set the value of the parameter using the orignal coordinate system.
void setOriginalValue(double value)
Set the value of the parameter using the orignal coordinate system.
virtual double getValue() const
Get the value of this parameter.
Definition: Parameter.h:241
Exception thrown when a value do not match a given constraint.
static double PI()
Definition: NumConstants.h:96
The TransformedParameter abstract class.
RTransformedParameter * clone() const
Create a copy of this object and send a pointer to it.
Parameter transformation from ] a, b [ to ]-inf, + inf [.