LogLikelihoodFCN.h

Go to the documentation of this file.
00001 // @(#)root/mathcore:$Id: LogLikelihoodFCN.h 37232 2010-12-03 18:09:43Z moneta $
00002 // Author: L. Moneta Fri Aug 17 14:29:24 2007
00003 
00004 /**********************************************************************
00005  *                                                                    *
00006  * Copyright (c) 2007  LCG ROOT Math Team, CERN/PH-SFT                *
00007  *                                                                    *
00008  *                                                                    *
00009  **********************************************************************/
00010 
00011 // Header file for class LogLikelihoodFCN
00012 
00013 #ifndef ROOT_Fit_LogLikelihoodFCN
00014 #define ROOT_Fit_LogLikelihoodFCN
00015 
00016 #ifndef ROOT_Math_FitMethodFunction
00017 #include "Math/FitMethodFunction.h"
00018 #endif
00019 
00020 #ifndef ROOT_Math_IParamFunction
00021 #include "Math/IParamFunction.h"
00022 #endif
00023 
00024 #ifndef ROOT_Fit_UnBinData
00025 #include "Fit/UnBinData.h"
00026 #endif
00027 
00028 #ifndef ROOT_Fit_FitUtil
00029 #include "Fit/FitUtil.h"
00030 #endif
00031 
00032 #ifdef ROOT_FIT_PARALLEL
00033 #ifndef ROOT_Fit_FitUtilParallel
00034 #include "Fit/FitUtilParallel.h"
00035 #endif
00036 #endif
00037 
00038 namespace ROOT { 
00039 
00040    namespace Fit { 
00041 
00042 
00043 //___________________________________________________________________________________
00044 /** 
00045    LogLikelihoodFCN class 
00046    for likelihood fits 
00047 
00048    it is template to distinguish gradient and non-gradient case
00049 
00050    @ingroup  FitMethodFunc   
00051 */ 
00052 template<class FunType> 
00053 class LogLikelihoodFCN : public ::ROOT::Math::BasicFitMethodFunction<FunType>  {
00054 
00055 public: 
00056 
00057 
00058 
00059    typedef  ::ROOT::Math::BasicFitMethodFunction<FunType> BaseObjFunction; 
00060    typedef typename  BaseObjFunction::BaseFunction BaseFunction; 
00061 
00062    typedef  ::ROOT::Math::IParamMultiFunction IModelFunction;
00063 
00064 
00065    /** 
00066       Constructor from unbin data set and model function (pdf)
00067    */ 
00068    LogLikelihoodFCN (const UnBinData & data, const IModelFunction & func) : 
00069       BaseObjFunction(func.NPar(), data.Size() ),
00070       fData(data), 
00071       fFunc(func), 
00072       fNEffPoints(0),
00073       fGrad ( std::vector<double> ( func.NPar() ) )
00074    {}
00075   
00076 
00077    /** 
00078       Destructor (no operations)
00079    */ 
00080    virtual ~LogLikelihoodFCN () {}
00081 
00082 private:
00083    // usually copying is non trivial, so we make this unaccessible
00084 
00085    /** 
00086       Dummy Copy constructor (private)
00087    */ 
00088    LogLikelihoodFCN(const LogLikelihoodFCN &);
00089 
00090    /** 
00091       Dummy Assignment operator (private)
00092    */ 
00093    LogLikelihoodFCN & operator = (const LogLikelihoodFCN &);
00094 
00095 public: 
00096 
00097    /// clone the function (need to return Base for Windows)
00098    virtual BaseFunction * Clone() const { return  new LogLikelihoodFCN(fData,fFunc); }
00099 
00100 
00101    //using BaseObjFunction::operator();
00102 
00103    // effective points used in the fit
00104    virtual unsigned int NFitPoints() const { return fNEffPoints; }
00105 
00106    /// i-th likelihood contribution and its gradient
00107    virtual double DataElement(const double * x, unsigned int i, double * g) const { 
00108       if (i==0) this->UpdateNCalls();
00109       return FitUtil::EvaluatePdf(fFunc, fData, x, i, g); 
00110    }
00111 
00112 
00113    // need to be virtual to be instantited
00114    virtual void Gradient(const double *x, double *g) const { 
00115       // evaluate the chi2 gradient
00116       FitUtil::EvaluateLogLGradient(fFunc, fData, x, g, fNEffPoints);
00117    }
00118 
00119    /// get type of fit method function
00120    virtual  typename BaseObjFunction::Type_t Type() const { return BaseObjFunction::kLogLikelihood; }
00121 
00122    /// access to const reference to the data 
00123    virtual const UnBinData & Data() const { return fData; }
00124 
00125    /// access to const reference to the model function
00126    virtual const IModelFunction & ModelFunction() const { return fFunc; }
00127 
00128 
00129 protected: 
00130 
00131 
00132 private:
00133 
00134    /**
00135       Evaluation of the  function (required by interface)
00136     */
00137    virtual double DoEval (const double * x) const { 
00138       this->UpdateNCalls();
00139 
00140 #ifdef ROOT_FIT_PARALLEL
00141       return FitUtilParallel::EvaluateLogL(fFunc, fData, x, fNEffPoints); 
00142 #else 
00143       return FitUtil::EvaluateLogL(fFunc, fData, x, fNEffPoints); 
00144 #endif
00145    } 
00146 
00147    // for derivatives 
00148    virtual double  DoDerivative(const double * x, unsigned int icoord ) const { 
00149       Gradient(x, &fGrad[0]); 
00150       return fGrad[icoord]; 
00151    }
00152 
00153  
00154       //data member
00155 
00156    const UnBinData & fData; 
00157    const IModelFunction & fFunc; 
00158 
00159    mutable unsigned int fNEffPoints;  // number of effective points used in the fit 
00160 
00161    mutable std::vector<double> fGrad; // for derivatives
00162 
00163 
00164 }; 
00165 
00166       // define useful typedef's
00167       typedef LogLikelihoodFCN<ROOT::Math::IMultiGenFunction>  LogLikelihoodFunction; 
00168       typedef LogLikelihoodFCN<ROOT::Math::IMultiGradFunction> LogLikelihoodGradFunction; 
00169 
00170    } // end namespace Fit
00171 
00172 } // end namespace ROOT
00173 
00174 
00175 #endif /* ROOT_Fit_LogLikelihoodFCN */

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