Image Component Library (ICL)
LeastSquareModelFitting.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLMath/src/ICLMath/LeastSquareModelFitting.h **
10 ** Module : ICLMath **
11 ** Authors: Christof Elbrechter **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/CompatMacros.h>
34 #include <ICLMath/DynMatrix.h>
35 #include <ICLUtils/SmartPtr.h>
36 #include <ICLUtils/Function.h>
37 
38 namespace icl{
39  namespace math{
40 
42 
134  template<class T, class DataPoint>
136  public:
138 
140 
142  typedef std::vector<T> Model;
143 
144  private:
147 
150 
153 
156 
159 
160  public:
161 
164 
167  DynMatrix<T> *constraintMatrix=0):
168  m_modelDim(modelDim),m_gen(gen),m_S(modelDim,modelDim),
169  m_C(constraintMatrix),m_model(modelDim){
170 
171  }
172 
174 
175  icl64f getError(const Model &model,const DataPoint &p){
176  std::vector<T> d(m_modelDim);
177  m_gen(p,d.data());
178  icl64f e = 0;
179  for(int i=0;i<m_modelDim;++i) e += d[i] * model[i];
180  return fabs(e);
181  }
182 
184 
187  Model fit(const std::vector<DataPoint> &points) {
188  const int M = m_modelDim;
189  const int N = (int)points.size();
190 
191  m_D.setBounds(M,N);
192 
194  for(int i=0;i<N;i++){
195  m_gen(points[i],m_D.row_begin(i));
196  }
197 
199  m_D.transp().mult(m_D,m_S);
200 
201 
202 
203  DynMatrix<T> Si;
204  try{
205  Si = m_C ? m_S.inv()* (*m_C) : m_S.inv();
206  }catch(SingularMatrixException &){
207  Si = m_C ? m_S.pinv(true)* (*m_C) : m_S.pinv(true);
208  }
209 
210 
211  // might cause an exception
212  Si.eigen(m_Evecs, m_Evals);
214  std::copy(m_Evecs.col_begin(0), m_Evecs.col_end(0), m_model.begin());
215 
216  return m_model;
217  }
218  };
219  } // namespace math
220 }
221 
222 
LeastSquareModelFitting(int modelDim, DesignMatrixGen gen, DynMatrix< T > *constraintMatrix=0)
constructor with given parameters
Definition: LeastSquareModelFitting.h:166
DesignMatrixGen m_gen
design matrix row generator
Definition: LeastSquareModelFitting.h:149
DynMatrix< T > m_Evals
Definition: LeastSquareModelFitting.h:152
undocument this line if you encounter any issues!
Definition: Any.h:37
Special linear algebra exception type .
Definition: DynMatrix.h:71
icl64f getError(const Model &model, const DataPoint &p)
computes the error for a given data point
Definition: LeastSquareModelFitting.h:175
Highly flexible and optimized matrix class implementation.
Definition: DynMatrix.h:81
DynMatrix< T > m_svdVt
Definition: LeastSquareModelFitting.h:152
Direct Least Square Fitting Algorithm.
Definition: LeastSquareModelFitting.h:135
DynMatrix< T > m_D
utility valiables
Definition: LeastSquareModelFitting.h:152
LeastSquareModelFitting()
Empty constructor that creates a dummy instance.
Definition: LeastSquareModelFitting.h:163
Model fit(const std::vector< DataPoint > &points)
fits the model to the given data points and returns optimal parameter set
Definition: LeastSquareModelFitting.h:187
std::vector< T > Model
model type (defines the model parameters)
Definition: LeastSquareModelFitting.h:142
DynMatrix< T > m_svdS
Definition: LeastSquareModelFitting.h:152
Ipp64f icl64f
64Bit floating point type for the ICL
Definition: BasicTypes.h:52
DynMatrix< T > m_Evecs
Definition: LeastSquareModelFitting.h:152
Model m_model
the model parameters
Definition: LeastSquareModelFitting.h:158
DynMatrix< T > m_S
Definition: LeastSquareModelFitting.h:152
utils::Function< void, const DataPoint &, T * > DesignMatrixGen
fills the give float* with data from the given data point
Definition: LeastSquareModelFitting.h:139
void eigen(DynMatrix &eigenvectors, DynMatrix &eigenvalues) const
Extracts the matrix's eigenvalues and eigenvectors.
DynMatrix< T > m_svdU
Definition: LeastSquareModelFitting.h:152
void copy(const T *src, const T *srcEnd, T *dst)
moves data from source to destination array (no casting possible)
Definition: CoreFunctions.h:216
int m_modelDim
model dimension
Definition: LeastSquareModelFitting.h:146
utils::SmartPtr< DynMatrix< T > > m_C
constraint matrix
Definition: LeastSquareModelFitting.h:155
Specialization of the SmartPtrBase class for Pointers.
Definition: SmartPtr.h:75