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.LGPL **
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 <ICLUtils/CompatMacros.h>
00034 #include <ICLMath/DynMatrix.h>
00035 
00036 namespace icl{
00037   namespace math{
00038     
00040 
00132     template<class T, class DataPoint>
00133     class LeastSquareModelFitting{
00134       public:
00136 
00137       typedef Function<void,const DataPoint&,T*> DesignMatrixGen;
00138       
00140       typedef std::vector<T> Model;
00141       
00142       private:
00144       int m_modelDim;
00145   
00147       DesignMatrixGen m_gen;
00148       
00150       DynMatrix<T> m_D, m_S, m_Evecs, m_Evals, m_svdU, m_svdS, m_svdVt;
00151       
00153       SmartPtr<DynMatrix<T> >m_C;
00154       
00156       Model m_model;
00157       
00158       public:
00159       
00161       LeastSquareModelFitting(){}
00162       
00164       LeastSquareModelFitting(int modelDim, DesignMatrixGen gen, 
00165                               DynMatrix<T> *constraintMatrix=0):
00166       m_modelDim(modelDim),m_gen(gen),m_S(modelDim,modelDim), 
00167       m_C(constraintMatrix),m_model(modelDim){
00168         
00169       }
00170       
00172 
00173       icl64f getError(const Model &model,const DataPoint &p){
00174         std::vector<T> d(m_modelDim);
00175         m_gen(p,d.data());
00176         icl64f e = 0;
00177         for(int i=0;i<m_modelDim;++i) e += d[i] * model[i];
00178         return fabs(e);
00179       }
00180       
00182 
00185       Model fit(const std::vector<DataPoint> &points){
00186         const int M = m_modelDim;
00187         const int N = (int)points.size();
00188         
00189         m_D.setBounds(M,N);
00190   
00192         for(int i=0;i<N;i++){
00193           m_gen(points[i],m_D.row_begin(i));
00194         }
00195         
00197         m_D.transp().mult(m_D,m_S);
00198         
00199         
00200         
00201         DynMatrix<T> Si;
00202         try{ 
00203           Si = m_C ? m_S.inv()* (*m_C) : m_S.inv();
00204         }catch(SingularMatrixException &ex){
00205           Si = m_C ? m_S.pinv(true)* (*m_C) : m_S.pinv(true); 
00206         }
00207         
00208         try{
00209           Si.eigen(m_Evecs, m_Evals);
00211           std::copy(m_Evecs.col_begin(0), m_Evecs.col_end(0), m_model.begin());
00212         }catch(ICLException &e){
00213           //Si.svd(m_svdU, m_svdS, m_svdVt);
00214           //SHOW(m_svdS);
00215           //
00216           //std::copy(m_svdU.col_begin(0), m_svdU.col_end(0), m_model.begin());
00217           //
00218           //for(int i=0;i<N;++i){
00219           //  SHOW(getError(m_model,points[i]));
00220           //}
00221 
00222           // this can happen if there is no noise in the input values
00223           // rather than solving Ax = lambda x, we have a 0 eigenvalue lambda
00224           // therefore we have to solve Ax = 0 ??
00225           DEBUG_LOG("LeastSquareModelFitting:fit: error in eigenvalue decomposition:\n" << e.what());
00226           std::fill(m_model.begin(),m_model.end(),Range<T>::limits().maxVal);
00227         }
00228         
00229         return m_model;
00230       }
00231     };
00232   } // namespace math
00233 }
00234 
00235 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines