Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
LeastSquareModelFitting.h
Go to the documentation of this file.
00001 /********************************************************************
00002 **                Image Component Library (ICL)                    **
00003 **                                                                 **
00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld          **
00005 **                         Neuroinformatics Group                  **
00006 ** Website: www.iclcv.org and                                      **
00007 **          http://opensource.cit-ec.de/projects/icl               **
00008 **                                                                 **
00009 ** File   : ICLMath/src/ICLMath/LeastSquareModelFitting.h          **
00010 ** Module : ICLMath                                                **
00011 ** Authors: Christof Elbrechter                                    **
00012 **                                                                 **
00013 **                                                                 **
00014 ** GNU LESSER GENERAL PUBLIC LICENSE                               **
00015 ** This file may be used under the terms of the GNU Lesser General **
00016 ** Public License version 3.0 as published by the                  **
00017 **                                                                 **
00018 ** Free Software Foundation and appearing in the file LICENSE.GPL  **
00019 ** included in the packaging of this file.  Please review the      **
00020 ** following information to ensure the license requirements will   **
00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt                **
00022 **                                                                 **
00023 ** The development of this software was supported by the           **
00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology.    **
00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche       **
00026 ** Forschungsgemeinschaft (DFG) in the context of the German       **
00027 ** Excellence Initiative.                                          **
00028 **                                                                 **
00029 ********************************************************************/
00030 
00031 #pragma once
00032 
00033 #include <ICLMath/DynMatrix.h>
00034 
00035 namespace icl{
00036   namespace math{
00037     
00039 
00131     template<class T, class DataPoint>
00132     class LeastSquareModelFitting{
00133       public:
00135 
00136       typedef Function<void,const DataPoint&,T*> DesignMatrixGen;
00137       
00139       typedef std::vector<T> Model;
00140       
00141       private:
00143       int m_modelDim;
00144   
00146       DesignMatrixGen m_gen;
00147       
00149       DynMatrix<T> m_D, m_S, m_Evecs, m_Evals;
00150       
00152       SmartPtr<DynMatrix<T> >m_C;
00153       
00155       Model m_model;
00156       
00157       public:
00158       
00160       LeastSquareModelFitting(){}
00161       
00163       LeastSquareModelFitting(int modelDim, DesignMatrixGen gen, 
00164                               DynMatrix<T> *constraintMatrix=0):
00165       m_modelDim(modelDim),m_gen(gen),m_S(modelDim,modelDim), 
00166       m_C(constraintMatrix),m_model(modelDim){
00167         
00168       }
00169       
00171 
00172       icl64f getError(const Model &model,const DataPoint &p){
00173         std::vector<T> d(m_modelDim);
00174         m_gen(p,d.data());
00175         icl64f e = 0;
00176         for(int i=0;i<m_modelDim;++i) e += d[i] * model[i];
00177         return fabs(e);
00178       }
00179       
00181 
00184       Model fit(const std::vector<DataPoint> &points){
00185         const int M = m_modelDim;
00186         const int N = (int)points.size();
00187         
00188         m_D.setBounds(M,N);
00189   
00191         for(int i=0;i<N;i++){
00192           m_gen(points[i],m_D.row_begin(i));
00193         }
00194         
00196         m_D.transp().mult(m_D,m_S);
00197         
00198         
00199         
00200         DynMatrix<T> Si;
00201         try{ 
00202           Si = m_C ? m_S.inv()* (*m_C) : m_S.inv();
00203         }catch(SingularMatrixException &ex){
00204           Si = m_C ? m_S.pinv(true)* (*m_C) : m_S.pinv(true); 
00205         }
00206         
00207         try{
00208           Si.eigen(m_Evecs, m_Evals);
00210           std::copy(m_Evecs.col_begin(0), m_Evecs.col_end(0), m_model.begin());
00211         }catch(ICLException &e){
00212           std::fill(m_model.begin(),m_model.end(),Range<T>::limits().maxVal);
00213         }
00214         
00215         return m_model;
00216       }
00217     };
00218   } // namespace math
00219 }
00220 
00221 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines