ROL
ROL_RegressionError.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef ROL_REGRESSIONERROR_H
11 #define ROL_REGRESSIONERROR_H
12 
13 #include "ROL_StdVector.hpp"
14 #include "ROL_StdObjective.hpp"
15 
24 namespace ROL {
25 
26 template <class Real>
27 class RegressionError : public StdObjective<Real> {
28 private:
29  void checkSize(const std::vector<Real> &x) {
30  const std::vector<Real> data = Objective<Real>::getParameter();
31  if (data.size() != x.size()) {
32  throw Exception::NotImplemented("ROL::RegressionError : Data dimension does not match input dimension!");
33  }
34  }
35 
36 public:
37  RegressionError(void) {}
38 
39  Real value( const std::vector<Real> &x, Real &tol ) {
40  checkSize(x);
41  // Parse Input Vector
42  std::vector<Real> c; c.assign(x.begin()+1,x.end());
43  Real c0 = x[0];
44  // Parse Data
45  const std::vector<Real> data = Objective<Real>::getParameter();
46  std::vector<Real> X; X.assign(data.begin()+1,data.end());
47  Real Y = data[0];
48  // Build Error
49  int Xdim = X.size();
50  Real val = Y-c0;
51  for (int i = 0; i < Xdim; ++i) {
52  val -= c[i]*X[i];
53  }
54  return val;
55  }
56 
57  void gradient( std::vector<Real> &g, const std::vector<Real> &x, Real &tol ) {
58  checkSize(x); checkSize(g);
59  // Parse Data
60  const std::vector<Real> data = Objective<Real>::getParameter();
61  std::vector<Real> X; X.assign(data.begin()+1,data.end());
62  // Build Error
63  int Xdim = X.size();
64  g[0] = static_cast<Real>(-1);
65  for (int i = 0; i < Xdim; ++i) {
66  g[i+1] = -X[i];
67  }
68  }
69 
70  void hessVec( std::vector<Real> &hv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
71  hv.assign(hv.size(),static_cast<Real>(0));
72  }
73 }; // class RegressionError
74 
75 } // namespace ROL
76 
77 #endif
void gradient(std::vector< Real > &g, const std::vector< Real > &x, Real &tol)
Provides the interface to evaluate linear regression error.
void hessVec(std::vector< Real > &hv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Real value(const std::vector< Real > &x, Real &tol)
Specializes the ROL::Objective interface for objective functions that operate on ROL::StdVector&#39;s.
const std::vector< Real > getParameter(void) const
void checkSize(const std::vector< Real > &x)