GSLNLSMinimizer.h

Go to the documentation of this file.
00001 // @(#)root/mathmore:$Id: GSLNLSMinimizer.h 37427 2010-12-09 08:57:45Z moneta $
00002 // Author: L. Moneta Wed Dec 20 17:16:32 2006
00003 
00004 /**********************************************************************
00005  *                                                                    *
00006  * Copyright (c) 2006  LCG ROOT Math Team, CERN/PH-SFT                *
00007  *                                                                    *
00008  * This library is free software; you can redistribute it and/or      *
00009  * modify it under the terms of the GNU General Public License        *
00010  * as published by the Free Software Foundation; either version 2     *
00011  * of the License, or (at your option) any later version.             *
00012  *                                                                    *
00013  * This library is distributed in the hope that it will be useful,    *
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   *
00016  * General Public License for more details.                           *
00017  *                                                                    *
00018  * You should have received a copy of the GNU General Public License  *
00019  * along with this library (see file COPYING); if not, write          *
00020  * to the Free Software Foundation, Inc., 59 Temple Place, Suite      *
00021  * 330, Boston, MA 02111-1307 USA, or contact the author.             *
00022  *                                                                    *
00023  **********************************************************************/
00024 
00025 // Header file for class GSLNLSMinimizer
00026 
00027 #ifndef ROOT_Math_GSLNLSMinimizer
00028 #define ROOT_Math_GSLNLSMinimizer
00029 
00030 
00031 
00032 #ifndef ROOT_Math_Minimizer
00033 #include "Math/Minimizer.h"
00034 #endif
00035 
00036 
00037 #ifndef ROOT_Math_IFunctionfwd
00038 #include "Math/IFunctionfwd.h"
00039 #endif
00040 
00041 #ifndef ROOT_Math_IParamFunctionfwd
00042 #include "Math/IParamFunctionfwd.h"
00043 #endif
00044 
00045 #ifndef ROOT_Math_FitMethodFunction
00046 #include "Math/FitMethodFunction.h"
00047 #endif
00048 
00049 #ifndef ROOT_Math_MinimizerVariable
00050 #include "Math/MinimizerVariable.h"
00051 #endif
00052 
00053 
00054 #include <vector>
00055 #include <map>
00056 #include <string>
00057 
00058 namespace ROOT { 
00059 
00060    namespace Math { 
00061 
00062       class GSLMultiFit; 
00063 
00064 
00065 //________________________________________________________________________________
00066 /** 
00067     LSResidualFunc class description. 
00068     Internal class used for accessing the residuals of the Least Square function
00069     and their derivates which are estimated numerically using GSL numerical derivation. 
00070     The class contains a pointer to the fit method function and an index specifying 
00071     the i-th residual and wraps it in a multi-dim gradient function interface
00072     ROOT::Math::IGradientFunctionMultiDim. 
00073     The class is used by ROOT::Math::GSLNLSMinimizer (GSL non linear least square fitter)
00074 
00075     @ingroup MultiMin
00076 */
00077 class LSResidualFunc : public IMultiGradFunction { 
00078 public: 
00079 
00080    //default ctor (required by CINT) 
00081    LSResidualFunc() : fIndex(0), fChi2(0)
00082    {}
00083 
00084 
00085    LSResidualFunc(const ROOT::Math::FitMethodFunction & func, unsigned int i) : 
00086       fIndex(i), 
00087       fChi2(&func), 
00088       fX2(std::vector<double>(func.NDim() ) )
00089    {}
00090 
00091 
00092    // copy ctor
00093    LSResidualFunc(const LSResidualFunc & rhs) :
00094       IMultiGenFunction(), 
00095       IMultiGradFunction() 
00096    { 
00097       operator=(rhs);
00098    } 
00099 
00100    // assignment
00101    LSResidualFunc & operator= (const LSResidualFunc & rhs) 
00102    { 
00103       fIndex = rhs.fIndex;
00104       fChi2 = rhs.fChi2; 
00105       fX2 = rhs.fX2;
00106       return *this;
00107    } 
00108 
00109    IMultiGenFunction * Clone() const { 
00110       return new LSResidualFunc(*fChi2,fIndex); 
00111    }
00112 
00113    unsigned int NDim() const { return fChi2->NDim(); }
00114 
00115    void Gradient( const double * x, double * g) const { 
00116       double f0 = 0; 
00117       FdF(x,f0,g);
00118    }
00119 
00120    void FdF (const double * x, double & f, double * g) const { 
00121       unsigned int n = NDim(); 
00122       std::copy(x,x+n,fX2.begin());
00123       const double kEps = 1.0E-4;
00124       f = DoEval(x); 
00125       for (unsigned int i = 0; i < n; ++i) { 
00126          fX2[i] += kEps;
00127          g[i] =  ( DoEval(&fX2.front()) - f )/kEps;
00128          fX2[i] = x[i];
00129       }
00130    } 
00131    
00132 
00133 private: 
00134 
00135    double DoEval (const double * x) const { 
00136       return fChi2->DataElement(x, fIndex);
00137    }
00138    
00139    double DoDerivative(const double * x, unsigned int icoord) const { 
00140       //return  ROOT::Math::Derivator::Eval(*this, x, icoord, 1E-8);
00141       std::copy(x,x+NDim(),fX2.begin());
00142       const double kEps = 1.0E-4;
00143       fX2[icoord] += kEps;
00144       return ( DoEval(&fX2.front()) - DoEval(x) )/kEps;
00145    }
00146 
00147    unsigned int fIndex; 
00148    const ROOT::Math::FitMethodFunction * fChi2; 
00149    mutable std::vector<double> fX2;  // cached vector
00150 };
00151 
00152 
00153 //_____________________________________________________________________________________________________
00154 /** 
00155    GSLNLSMinimizer class for Non Linear Least Square fitting
00156    It Uses the Levemberg-Marquardt algorithm from 
00157    <A HREF="http://www.gnu.org/software/gsl/manual/html_node/Nonlinear-Least_002dSquares-Fitting.html">
00158    GSL Non Linear Least Square fitting</A>.
00159 
00160    @ingroup MultiMin
00161 */ 
00162 class GSLNLSMinimizer : public  ROOT::Math::Minimizer {
00163 
00164 public: 
00165 
00166    /** 
00167       Default constructor
00168    */ 
00169    GSLNLSMinimizer (int type = 0); 
00170 
00171    /** 
00172       Destructor (no operations)
00173    */ 
00174    ~GSLNLSMinimizer ();  
00175 
00176 private:
00177    // usually copying is non trivial, so we make this unaccessible
00178 
00179    /** 
00180       Copy constructor
00181    */ 
00182    GSLNLSMinimizer(const GSLNLSMinimizer &) : ROOT::Math::Minimizer() {} 
00183 
00184    /** 
00185       Assignment operator
00186    */ 
00187    GSLNLSMinimizer & operator = (const GSLNLSMinimizer & rhs)  {
00188       if (this == &rhs) return *this;  // time saving self-test
00189       return *this;
00190    }
00191 
00192 public: 
00193 
00194    /// set the function to minimize
00195    virtual void SetFunction(const ROOT::Math::IMultiGenFunction & func); 
00196 
00197    /// set gradient the function to minimize
00198    virtual void SetFunction(const ROOT::Math::IMultiGradFunction & func); 
00199 
00200    /// set free variable 
00201    virtual bool SetVariable(unsigned int ivar, const std::string & name, double val, double step); 
00202 
00203    /// set lower limited variable 
00204    virtual bool SetLowerLimitedVariable(unsigned int  ivar , const std::string & name , double val , double step , double lower );
00205    /// set upper limited variable
00206    virtual bool SetUpperLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double upper ); 
00207    /// set upper/lower limited variable 
00208    virtual bool SetLimitedVariable(unsigned int ivar , const std::string & name , double val , double step , double lower , double upper ); 
00209    /// set fixed variable
00210    virtual bool SetFixedVariable(unsigned int ivar , const std::string & name , double val );  
00211 
00212    /// set the value of an existing variable 
00213    virtual bool SetVariableValue(unsigned int ivar, double val );
00214    /// set the values of all existing variables (array must be dimensioned to the size of existing parameters)
00215    virtual bool SetVariableValues(const double * x);
00216 
00217    /// method to perform the minimization
00218    virtual  bool Minimize(); 
00219 
00220    /// return minimum function value
00221    virtual double MinValue() const { return fMinVal; } 
00222 
00223    /// return expected distance reached from the minimum
00224    virtual double Edm() const { return fEdm; } // not impl. }
00225 
00226    /// return  pointer to X values at the minimum 
00227    virtual const double *  X() const { return &fValues.front(); } 
00228 
00229    /// return pointer to gradient values at the minimum 
00230    virtual const double *  MinGradient() const; 
00231 
00232    /// number of function calls to reach the minimum 
00233    virtual unsigned int NCalls() const { return (fObjFunc) ? fObjFunc->NCalls() : 0; } 
00234 
00235    /// this is <= Function().NDim() which is the total 
00236    /// number of variables (free+ constrained ones) 
00237    virtual unsigned int NDim() const { return fDim; }   
00238 
00239    /// number of free variables (real dimension of the problem) 
00240    /// this is <= Function().NDim() which is the total 
00241    virtual unsigned int NFree() const { return fNFree; }  
00242 
00243    /// minimizer provides error and error matrix
00244    virtual bool ProvidesError() const { return true; } 
00245 
00246    /// return errors at the minimum 
00247    virtual const double * Errors() const { return (fErrors.size() > 0) ? &fErrors.front() : 0; }
00248 //  { 
00249 //       static std::vector<double> err; 
00250 //       err.resize(fDim);
00251 //       return &err.front(); 
00252 //    }
00253 
00254    /** return covariance matrices elements 
00255        if the variable is fixed the matrix is zero
00256        The ordering of the variables is the same as in errors
00257    */ 
00258    virtual double CovMatrix(unsigned int , unsigned int ) const;
00259 
00260    /// return covariance matrix status
00261    virtual int CovMatrixStatus() const;
00262 
00263 protected: 
00264 
00265 private: 
00266    
00267 
00268    unsigned int fDim;        // dimension of the function to be minimized 
00269    unsigned int fNFree;      // dimension of the internal function to be minimized 
00270    unsigned int fSize;        // number of fit points (residuals)
00271  
00272 
00273    ROOT::Math::GSLMultiFit * fGSLMultiFit;        // pointer to GSL multi fit solver 
00274    const ROOT::Math::FitMethodFunction * fObjFunc;      // pointer to Least square function
00275    
00276    double fMinVal;                                // minimum function value
00277    double fEdm;                                   // edm value
00278    double fLSTolerance;                           // Line Search Tolerance
00279    std::vector<double> fValues;           
00280    std::vector<double> fErrors;
00281    std::vector<double> fCovMatrix;              //  cov matrix (stored as cov[ i * dim + j] 
00282    std::vector<double> fSteps;
00283    std::vector<std::string> fNames;
00284    std::vector<LSResidualFunc> fResiduals;   //! transient Vector of the residual functions
00285 
00286    std::vector<ROOT::Math::EMinimVariableType> fVarTypes;  // vector specifyng the type of variables
00287    std::map< unsigned int, std::pair<double, double> > fBounds; // map specifying the bound using as key the parameter index
00288 
00289 
00290 }; 
00291 
00292    } // end namespace Math
00293 
00294 } // end namespace ROOT
00295 
00296 
00297 #endif /* ROOT_Math_GSLNLSMinimizer */

Generated on Tue Jul 5 14:25:25 2011 for ROOT_528-00b_version by  doxygen 1.5.1