1 //                                               -*- C++ -*-
2 /**
3  *  @brief Class for a Box cox implementation
4  *
5  *  Copyright 2005-2021 Airbus-EDF-IMACS-ONERA-Phimeca
6  *
7  *  This library is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU Lesser General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public License
18  *  along with this library.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #include "openturns/BoxCoxGradient.hxx"
23 #include "openturns/PersistentObjectFactory.hxx"
24 #include "openturns/TBB.hxx"
25 
26 BEGIN_NAMESPACE_OPENTURNS
27 
28 CLASSNAMEINIT(BoxCoxGradient)
29 
30 static const Factory<BoxCoxGradient> Factory_BoxCoxGradient;
31 
32 /* Default constructor */
BoxCoxGradient()33 BoxCoxGradient::BoxCoxGradient()
34   : GradientImplementation()
35   , p_evaluation_()
36 {
37   // Nothing to do
38 }
39 
40 /* Parameter constructor */
BoxCoxGradient(const BoxCoxEvaluation & evaluation)41 BoxCoxGradient::BoxCoxGradient(const BoxCoxEvaluation & evaluation)
42   : GradientImplementation()
43   , p_evaluation_(evaluation.clone())
44 {
45   // Nothing to do
46 }
47 
48 /* Parameters constructor */
BoxCoxGradient(const Pointer<BoxCoxEvaluation> & p_evaluation)49 BoxCoxGradient::BoxCoxGradient(const Pointer<BoxCoxEvaluation> & p_evaluation)
50   : GradientImplementation()
51   , p_evaluation_(p_evaluation)
52 {
53   // Nothing to do
54 }
55 
56 /* Clone constructor */
clone() const57 BoxCoxGradient * BoxCoxGradient::clone() const
58 {
59   return new BoxCoxGradient(*this);
60 }
61 
62 /* Comparison operator */
operator ==(const BoxCoxGradient & other) const63 Bool BoxCoxGradient::operator ==(const BoxCoxGradient & other) const
64 {
65   if (this == &other) return true;
66   return (*p_evaluation_ == *other.p_evaluation_);
67 }
68 
69 /* String converter */
__repr__() const70 String BoxCoxGradient::__repr__() const
71 {
72   OSS oss(true);
73   oss << "class=" << BoxCoxGradient::GetClassName()
74       << " name=" << getName()
75       << " evaluation=" << *p_evaluation_;
76   return oss;
77 }
78 
79 /* String converter __str__ */
__str__(const String &) const80 String BoxCoxGradient::__str__(const String & ) const
81 {
82   OSS oss (false);
83   oss << "BoxCoxGradient(lambda=" << getLambda()
84       << ", shift=" << getShift()
85       << ")";
86   return oss;
87 }
88 
89 /* Gradient evaluation method */
gradient(const Point & inP) const90 Matrix BoxCoxGradient::gradient(const Point & inP) const
91 {
92   const UnsignedInteger dimension = getInputDimension();
93   if (inP.getDimension() != dimension) throw InvalidArgumentException(HERE) << "Error: the given point has an invalid dimension. Expect a dimension " << dimension << ", got " << inP.getDimension();
94   Matrix result(1, dimension);
95 
96   // There is no check of positive variables
97   // This last one must be done by user or, as the gradient is used in a stochastic context, in the BoxCoxTransform class
98   for (UnsignedInteger index = 0; index < dimension; ++index)
99   {
100     const Scalar x = inP[index] + getShift()[index];
101     if (!(x > 0.0))
102       throw InvalidArgumentException(HERE) << "Can not apply the Box Cox gradient function to a nonpositive shifted value x=" << x;
103 
104     // Applying the Box-Cox function
105     const Scalar lambda_i = getLambda()[index];
106     const Scalar logX = log(x);
107     if (std::abs(lambda_i * logX) < 1e-8) result(0, index) = (1.0 + lambda_i * logX) / x;
108     else result(0, index) = exp((lambda_i - 1.0) * logX);
109   }
110   return result;
111 }
112 
113 /* Accessor for input point dimension */
getInputDimension() const114 UnsignedInteger BoxCoxGradient::getInputDimension() const
115 {
116   return p_evaluation_->getInputDimension();
117 }
118 
119 /* Accessor for output point dimension */
getOutputDimension() const120 UnsignedInteger BoxCoxGradient::getOutputDimension() const
121 {
122   return p_evaluation_->getOutputDimension();
123 }
124 
125 /* Accessor for the lambda */
getLambda() const126 Point BoxCoxGradient::getLambda() const
127 {
128   return p_evaluation_->getLambda();
129 }
130 
131 /* Accessor for the shift */
getShift() const132 Point BoxCoxGradient::getShift() const
133 {
134   return p_evaluation_->getShift();
135 }
136 
137 /* Method save() stores the object through the StorageManager */
save(Advocate & adv) const138 void BoxCoxGradient::save(Advocate & adv) const
139 {
140   GradientImplementation::save(adv);
141   adv.saveAttribute( "evaluation_", *p_evaluation_ );
142 }
143 
144 /* Method load() reloads the object from the StorageManager */
load(Advocate & adv)145 void BoxCoxGradient::load(Advocate & adv)
146 {
147   GradientImplementation::load(adv);
148   TypedInterfaceObject<BoxCoxEvaluation> evaluation;
149   adv.loadAttribute( "evaluation_", evaluation );
150   p_evaluation_ = evaluation.getImplementation();
151 }
152 
153 END_NAMESPACE_OPENTURNS
154