bpp-core3  3.0.0
TransformedParameter.h
Go to the documentation of this file.
1 //
2 // File: TransformedParameter.h
3 // Authors:
4 // Julien Dutheil
5 // Created: 2009-01-30 09:42:00
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 19, 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 #ifndef BPP_NUMERIC_TRANSFORMEDPARAMETER_H
42 #define BPP_NUMERIC_TRANSFORMEDPARAMETER_H
43 
44 #include <cmath>
45 
46 #include "NumConstants.h"
47 #include "Parameter.h"
48 
49 namespace bpp
50 {
60  public Parameter
61 {
62 public:
63  TransformedParameter(const std::string& name, double value) :
64  Parameter(name, value) {}
65 
67 
68 public:
75  virtual void setOriginalValue(double value) = 0;
76 
80  virtual double getOriginalValue() const = 0;
81 
87  virtual double getFirstOrderDerivative() const = 0;
88 
94  virtual double getSecondOrderDerivative() const = 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)
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 if (x < 0) return -exp(-x) / scale_ + bound_;
163  else return -x / scale_ - 1. + bound_;
164  }
165 
166  double getFirstOrderDerivative() const
167  {
168  double x = getValue();
169  if (positive_)
170  if (x < 0) return exp(x) / scale_;
171  else return 1. / scale_;
172  else if (x < 0) return exp(-x) / scale_;
173  else return -1. / scale_;
174  }
175 
177  {
178  double x = getValue();
179  if (positive_)
180  if (x < 0) return exp(x) / scale_;
181  else return 0;
182  else if (x < 0) return -exp(-x) / scale_;
183  else return 0;
184  }
185 };
186 
202  public TransformedParameter
203 {
204 private:
205  double scale_;
206  double lowerBound_;
207  double upperBound_;
208  bool hyper_;
209  double tiny_;
210 
211 public:
222  IntervalTransformedParameter(const std::string& name, double value, double lowerBound = 0, double upperBound = 1, double scale = 1, bool hyper = true) :
223  TransformedParameter(name, hyper ?
224  scale * atanh(2. * (value - lowerBound) / (upperBound - lowerBound) - 1.) :
225  scale* tan(NumConstants::PI() * (value - lowerBound) / (upperBound - lowerBound) - NumConstants::PI() / 2.)),
226  scale_(scale),
227  lowerBound_(lowerBound),
228  upperBound_(upperBound),
229  hyper_(hyper),
230  tiny_(NumConstants::TINY())
231  {}
232 
234 
235 public:
236  void setOriginalValue(double value)
237  {
238  if (value <= lowerBound_ || value >= upperBound_) throw ConstraintException("IntervalTransformedParameter::setValue", this, value);
239  setValue(hyper_ ?
240  scale_ * atanh(2. * (value - lowerBound_) / (upperBound_ - lowerBound_) - 1.) :
241  scale_* std::tan(NumConstants::PI() * (value - lowerBound_) / (upperBound_ - lowerBound_) - NumConstants::PI() / 2.));
242  }
243 
244  double getOriginalValue() const
245  {
246  double x = getValue();
247  double x2 = hyper_ ?
248  (tanh(x / scale_) + 1.) * (upperBound_ - lowerBound_) / 2. + lowerBound_ :
250  return x2;
251  }
252 
253 
254  double getFirstOrderDerivative() const
255  {
256  double x = getValue();
257  double x2 = hyper_ ?
258  1. / (std::pow(cosh(x / scale_), 2)) * (upperBound_ - lowerBound_) / (2. * scale_) :
259  (upperBound_ - lowerBound_) / (NumConstants::PI() * scale_ * (std::pow(x / scale_, 2) + 1.));
260  return x2;
261  }
263  {
264  double x = getValue();
265  double x2 = hyper_ ?
266  -1. / (std::pow(cosh(x / scale_), 2)) * tanh(x / scale_) * (upperBound_ - lowerBound_) / (scale_ * scale_) :
267  -2. * x * (upperBound_ - lowerBound_) / (NumConstants::PI() * std::pow(scale_, 3) * std::pow((std::pow(x / scale_, 2) + 1.), 2));
268  return x2;
269  }
270 };
271 
279  public TransformedParameter
280 {
281 public:
282  PlaceboTransformedParameter(const std::string& name, double value) :
283  TransformedParameter(name, value)
284  {}
285 
287 
288 public:
289  void setOriginalValue(double value)
290  {
291  setValue(value);
292  }
293 
294  double getOriginalValue() const
295  {
296  return getValue();
297  }
298 
299  double getFirstOrderDerivative() const { return 1.; }
300 
301  double getSecondOrderDerivative() const { return 0.; }
302 };
303 } // end of namespace bpp.
304 #endif // BPP_NUMERIC_TRANSFORMEDPARAMETER_H
Exception thrown when a value do not match a given constraint.
Parameter transformation from ] a, b [ to ]-inf, + inf [.
IntervalTransformedParameter * clone() const
Create a copy of this object and send a pointer to it.
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.
this static class contains several useful constant values.
Definition: NumConstants.h:57
static double PI()
Definition: NumConstants.h:97
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:135
virtual void setValue(double value)
Set the value of this parameter.
Definition: Parameter.cpp:110
virtual double getValue() const
Get the value of this parameter.
Definition: Parameter.h:218
'Placebo' parameter transformation from ] b, +inf [ or ] -inf, b [ to ]-inf, + inf [.
void setOriginalValue(double value)
Set the value of the parameter using the orignal coordinate system.
PlaceboTransformedParameter(const std::string &name, double value)
PlaceboTransformedParameter * clone() const
Create a copy of this object and send a pointer to it.
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.
RTransformedParameter * clone() const
Create a copy of this object and send a pointer to it.
void setOriginalValue(double value)
Set the value of the parameter using the orignal coordinate system.
The TransformedParameter abstract class.
virtual double getOriginalValue() const =0
virtual double getSecondOrderDerivative() const =0
TransformedParameter * clone() const =0
Create a copy of this object and send a pointer to it.
virtual double getFirstOrderDerivative() const =0
virtual void setOriginalValue(double value)=0
Set the value of the parameter using the orignal coordinate system.
TransformedParameter(const std::string &name, double value)