TukeyBiweightLoss.h
Go to the documentation of this file.
1 /*!
2  *
3  * \brief Implements Tukey's Biweight-loss function for robust regression
4  *
5  *
6  * \author Oswin Krause
7  * \date 2014
8  *
9  *
10  * \par Copyright 1995-2017 Shark Development Team
11  *
12  * <BR><HR>
13  * This file is part of Shark.
14  * <http://shark-ml.org/>
15  *
16  * Shark is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Lesser General Public License as published
18  * by the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * Shark is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU Lesser General Public License for more details.
25  *
26  * You should have received a copy of the GNU Lesser General Public License
27  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30 
31 #ifndef SHARK_OBJECTIVEFUNCTIONS_LOSS_HUBERLOSS_H
32 #define SHARK_OBJECTIVEFUNCTIONS_LOSS_HUBERLOSS_H
33 
35 
36 namespace shark {
37 
38 /// \brief Tukey's Biweight-loss for robust regression
39 ///
40 /// Tukey's Biweight-loss is a robust regression function. For predictions close to the correct classification,
41 /// it is convex, but close to a value k, it approaches a constant value. for differences greater than k,
42 /// the function is constant and has gradient 0. This effectively ignores large outliers at the cost
43 /// of loosing the convexity of the loss function.
44 /// The 1-dimensional loss is defined as
45 ///\f[ f(x)= \frac {x^6}{6k^4} - \frac {x^4} {2k^2}+\frac {x^2} {2} \f]
46 /// for \f$ x \in [-k,k]\f$. outside it is the constant function \f$\frac {k^2}{6}\f$.
47 ///
48 /// For multidimensional problems we define it with x being the two-norm of the difference
49 /// between the label and predicted values.
50 class TukeyBiweightLoss : public AbstractLoss<RealVector, RealVector>
51 {
52 public:
53  /// constructor
54  TukeyBiweightLoss(double k = 1.0):m_k(k){
55  m_features |= base_type::HAS_FIRST_DERIVATIVE;
56  }
57 
58  /// \brief Returns class name "HuberLoss"
59  std::string name() const
60  { return "TukeyBiweightLoss"; }
61 
62 
63  ///\brief calculates the sum of all
64  double eval(BatchLabelType const& labels, BatchOutputType const& predictions) const{
65  SIZE_CHECK(labels.size1() == predictions.size1());
66  SIZE_CHECK(labels.size2() == predictions.size2());
67  std::size_t numInputs = labels.size1();
68 
69  double error = 0;
70  double k2 = sqr(m_k);
71  double k4 = sqr(k2);
72  double maxErr = k2/6;
73  for(std::size_t i = 0; i != numInputs;++i){
74  double norm2 = norm_sqr(row(predictions,i)-row(labels,i));
75 
76  //check whether we are in the quadratic area
77  if(norm2 <= sqr(m_k)){
78  error = norm2/2+sqr(norm2)/6*(norm2/k4-3/k2);
79  }
80  else{
81  error += maxErr;
82  }
83  }
84  return error;
85  }
86 
87  double evalDerivative(BatchLabelType const& labels, BatchOutputType const& predictions, BatchOutputType& gradient)const{
88  SIZE_CHECK(labels.size1() == predictions.size1());
89  SIZE_CHECK(labels.size2() == predictions.size2());
90  std::size_t numInputs = labels.size1();
91  std::size_t outputDim = predictions.size2();
92 
93  gradient.resize(numInputs,outputDim);
94  gradient.clear();
95  double error = 0;
96  double k2 = sqr(m_k);
97  double k4 = sqr(k2);
98  double maxErr = k2/6;
99  for(std::size_t i = 0; i != numInputs;++i){
100  double norm2 = norm_sqr(row(predictions,i)-row(labels,i));
101 
102  //check whether we are in the quadratic area
103  if(norm2 <= sqr(m_k)){
104  error = norm2/2+sqr(norm2)/6*(norm2/k4-3/k2);
105  noalias(row(gradient,i)) = (1+sqr(norm2)/k4-2*norm2/k2)*(row(predictions,i)-row(labels,i));
106  }
107  else{
108  error += maxErr;
109  //gradient is initialized to 0!
110  }
111  }
112  return error;
113  }
114 
115 private:
116  double m_k;
117 };
118 
119 }
120 #endif