pls.cpp
Go to the documentation of this file.
1 /* *********************
2  * @file pls.cpp
3  *
4  * @author Stefan Ulbrich
5  * @date 2013-2014
6  *
7  * @brief This file contains implementation of the PLS algorithm.
8  * *********************/
9 #include "kbm.h"
10 #include <vector>
11 
12 
13 namespace memoryx::KBM
14 {
15  /*
16  * In case that regression and reconstruction should be separated later,
17  * store the projection parameter in the data structure defined if
18  * the makro is activated. Will come in handy for an incremental version.
19  */
20  //#define SEPARATE_REGRESSION_RECONSTRUCTION
21 
22 #ifdef SEPARATE_REGRESSION_RECONSTRUCTION
23  struct Projection
24  {
25  Vector p;
26  Vector u;
27  Real q;
28  Projection(Vector p_, Vector u_, Real q_) : p(p_), u(u_), q(q_) {};
29  };
30 #endif
31 
32  Matrix PLS::solve(const Matrix& input, const Matrix output, Real threshold)
33  {
34 
35 #ifdef SEPARATE_REGRESSION_RECONSTRUCTION
36  std::vector<std::vector<Projection> > projections;
37 #endif
38 
39  // Results stored in the following matrix. It has to be initialized with zeros.
40  Matrix parameters = Matrix::Zero(output.rows(), input.rows());
41 
42  // PLS 1 optimizes all output dimensions separately
43  for (int d = 0; d < output.rows(); d++)
44  {
45 
46 #ifdef SEPARATE_REGRESSION_RECONSTRUCTION
47  projections.push_back(std::vector<Projection>());
48 #endif
49  // Make working copies
50  Matrix input_d = input;
51  Vector output_d = output.row(d);
52 
53  // In order to reconstruct the parameters, the identity matrix is transformed according to
54  // projections defined during the regression.
55  Matrix identity = Matrix::Identity(input.rows(), input.rows());
56 
57  for (int k = 0; k < input.rows(); k++) // maximal 3^{d_i} projections
58  {
59  // Step 1: Regression
60  Vector u = input_d * output_d;
61  Vector s = input_d.transpose() * u;
62  Real q = s.dot(output_d) / s.dot(s);
63  output_d = output_d - q * s;
64  Real mean_error = output_d.array().abs().mean();
65  Vector p = (input_d * s) / (s.dot(s));
66  // input_d' = input_d - p * s % minimal
67  input_d = input_d - p * s.transpose();
68 
69 #ifdef SEPARATE_REGRESSION_RECONSTRUCTION
70  projections[d].push_back(Projection(p, u, q));
71 #endif
72  // Step 2: Parameter reconstruction (uses u,p,q from first step)
73  // For separation these projection parameters can be stored in a std::vector
74  s = identity.transpose() * u;
75  parameters.row(d) += q * s.transpose();
76  identity -= p * s.transpose();
77 
78  // Step 3: Stopping condition
79  if (mean_error < threshold)
80  {
81  break;
82  }
83  } // for (int k)
84  } // for (int d)
85 
86  return parameters;
87  }
88 
89 
90 }
kbm.h
memoryx::KBM::Matrix
Eigen::MatrixXd Matrix
Definition: kbm.h:38
memoryx::KBM::Vector
Eigen::VectorXd Vector
Definition: kbm.h:39
memoryx::KBM
Definition: inverse.cpp:19
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:19
q
#define q
Eigen::Matrix
Definition: EigenForwardDeclarations.h:27
memoryx::KBM::Real
double Real
Type definition of the underlying Realing point type.
Definition: kbm.h:37
memoryx::KBM::PLS::solve
Matrix KBM_IMPORT_EXPORT solve(const Matrix &input, const Matrix output, Real threshold)
Solves a linear system of equations using the partial least squares algorithm.
Definition: pls.cpp:32
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33