Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:09

0001 // @(#)root/mathcore:$Id$
0002 // Author: L. Moneta Fri Aug 17 14:29:24 2007
0003 
0004 /**********************************************************************
0005  *                                                                    *
0006  * Copyright (c) 2007  LCG ROOT Math Team, CERN/PH-SFT                *
0007  *                                                                    *
0008  *                                                                    *
0009  **********************************************************************/
0010 
0011 // Header file for class LogLikelihoodFCN
0012 
0013 #ifndef ROOT_Fit_LogLikelihoodFCN
0014 #define ROOT_Fit_LogLikelihoodFCN
0015 
0016 #include "ROOT/EExecutionPolicy.hxx"
0017 #include "Fit/BasicFCN.h"
0018 #include "Fit/FitUtil.h"
0019 #include "Fit/UnBinData.h"
0020 #include "Math/IParamFunction.h"
0021 
0022 #include <memory>
0023 #include <vector>
0024 
0025 namespace ROOT {
0026 
0027    namespace Fit {
0028 
0029 
0030 //___________________________________________________________________________________
0031 /**
0032    LogLikelihoodFCN class
0033    for likelihood fits
0034 
0035    it is template to distinguish gradient and non-gradient case
0036 
0037    @ingroup  FitMethodFunc
0038 */
0039 template<class DerivFunType,class ModelFunType = ROOT::Math::IParamMultiFunction>
0040 class LogLikelihoodFCN : public BasicFCN<DerivFunType,ModelFunType,UnBinData>  {
0041 
0042 public:
0043 
0044    typedef typename ModelFunType::BackendType T;
0045    typedef  BasicFCN<DerivFunType,ModelFunType,UnBinData> BaseFCN;
0046 
0047    typedef  ::ROOT::Math::BasicFitMethodFunction<DerivFunType> BaseObjFunction;
0048    typedef typename  BaseObjFunction::BaseFunction BaseFunction;
0049 
0050    typedef  ::ROOT::Math::IParamMultiFunctionTempl<T> IModelFunction;
0051    typedef typename BaseObjFunction::Type_t Type_t;
0052 
0053 
0054    /**
0055       Constructor from unbin data set and model function (pdf)
0056    */
0057    LogLikelihoodFCN (const std::shared_ptr<UnBinData> & data, const std::shared_ptr<IModelFunction> & func, int weight = 0, bool extended = false, const ::ROOT::EExecutionPolicy &executionPolicy = ::ROOT::EExecutionPolicy::kSequential) :
0058       BaseFCN( data, func),
0059       fIsExtended(extended),
0060       fWeight(weight),
0061       fNEffPoints(0),
0062       fGrad ( std::vector<double> ( func->NPar() ) ),
0063       fExecutionPolicy(executionPolicy)
0064    {}
0065 
0066       /**
0067       Constructor from unbin data set and model function (pdf) for object managed by users
0068    */
0069    LogLikelihoodFCN (const UnBinData & data, const IModelFunction & func, int weight = 0, bool extended = false, const ::ROOT::EExecutionPolicy &executionPolicy = ::ROOT::EExecutionPolicy::kSequential) :
0070       BaseFCN(std::make_shared<UnBinData>(data), std::shared_ptr<IModelFunction>(dynamic_cast<IModelFunction*>(func.Clone() ) ) ),
0071       fIsExtended(extended),
0072       fWeight(weight),
0073       fNEffPoints(0),
0074       fGrad ( std::vector<double> ( func.NPar() ) ),
0075       fExecutionPolicy(executionPolicy)
0076    {}
0077 
0078    /**
0079       Destructor (no operations)
0080    */
0081    virtual ~LogLikelihoodFCN () {}
0082 
0083    /**
0084       Copy constructor
0085    */
0086    LogLikelihoodFCN(const LogLikelihoodFCN & f) :
0087       BaseFCN(f.DataPtr(), f.ModelFunctionPtr() ),
0088       fIsExtended(f.fIsExtended ),
0089       fWeight( f.fWeight ),
0090       fNEffPoints( f.fNEffPoints ),
0091       fGrad( f.fGrad),
0092       fExecutionPolicy(f.fExecutionPolicy)
0093    {  }
0094 
0095 
0096    /**
0097       Assignment operator
0098    */
0099    LogLikelihoodFCN & operator = (const LogLikelihoodFCN & rhs) {
0100       SetData(rhs.DataPtr() );
0101       SetModelFunction(rhs.ModelFunctionPtr() );
0102       fNEffPoints = rhs.fNEffPoints;
0103       fGrad = rhs.fGrad;
0104       fIsExtended = rhs.fIsExtended;
0105       fWeight = rhs.fWeight;
0106       fExecutionPolicy = rhs.fExecutionPolicy;
0107       return *this;
0108    }
0109 
0110 
0111    /// clone the function (need to return Base for Windows)
0112    virtual BaseFunction * Clone() const { return  new LogLikelihoodFCN(*this); }
0113 
0114 
0115    //using BaseObjFunction::operator();
0116 
0117    // effective points used in the fit
0118    virtual unsigned int NFitPoints() const { return fNEffPoints; }
0119 
0120    /// i-th likelihood contribution and its gradient
0121    virtual double DataElement(const double * x, unsigned int i, double * g, double *  h = nullptr, bool fullHessian = false) const {
0122       if (i==0) this->UpdateNCalls();
0123       return FitUtil::Evaluate<T>::EvalPdf(BaseFCN::ModelFunction(), BaseFCN::Data(), x, i, g, h, BaseFCN::IsAGradFCN(), fullHessian);
0124    }
0125 
0126    // need to be virtual to be instantiated
0127    virtual void Gradient(const double *x, double *g) const {
0128       // evaluate the chi2 gradient
0129       FitUtil::Evaluate<typename BaseFCN::T>::EvalLogLGradient(BaseFCN::ModelFunction(), BaseFCN::Data(), x, g,
0130                                                                fNEffPoints, fExecutionPolicy);
0131    }
0132 
0133    /// get type of fit method function
0134    virtual  typename BaseObjFunction::Type_t Type() const { return BaseObjFunction::kLogLikelihood; }
0135 
0136 
0137    // Use sum of the weight squared in evaluating the likelihood
0138    // (this is needed for calculating the errors)
0139    void UseSumOfWeightSquare(bool on = true) {
0140       if (fWeight == 0) return; // do nothing if it was not weighted
0141       if (on) fWeight = 2;
0142       else fWeight = 1;
0143    }
0144 
0145 
0146 
0147 protected:
0148 
0149 
0150 private:
0151 
0152    /**
0153       Evaluation of the  function (required by interface)
0154     */
0155    virtual double DoEval (const double * x) const {
0156       this->UpdateNCalls();
0157       return FitUtil::Evaluate<T>::EvalLogL(BaseFCN::ModelFunction(), BaseFCN::Data(), x, fWeight, fIsExtended, fNEffPoints, fExecutionPolicy);
0158    }
0159 
0160    // for derivatives
0161    virtual double  DoDerivative(const double * x, unsigned int icoord ) const {
0162       Gradient(x, &fGrad[0]);
0163       return fGrad[icoord];
0164    }
0165 
0166 
0167       //data member
0168    bool fIsExtended;  ///< flag for indicating if likelihood is extended
0169    int  fWeight;  ///< flag to indicate if needs to evaluate using weight or weight squared (default weight = 0)
0170 
0171 
0172    mutable unsigned int fNEffPoints;  ///< number of effective points used in the fit
0173 
0174    mutable std::vector<double> fGrad; ///< for derivatives
0175 
0176    ::ROOT::EExecutionPolicy fExecutionPolicy; ///< Execution policy
0177 };
0178       // define useful typedef's
0179       // using LogLikelihoodFunction_v = LogLikelihoodFCN<ROOT::Math::IMultiGenFunction, ROOT::Math::IParametricFunctionMultiDimTempl<T>>;
0180       typedef LogLikelihoodFCN<ROOT::Math::IMultiGenFunction, ROOT::Math::IParamMultiFunction>  LogLikelihoodFunction;
0181       typedef LogLikelihoodFCN<ROOT::Math::IMultiGradFunction, ROOT::Math::IParamMultiFunction> LogLikelihoodGradFunction;
0182 
0183    } // end namespace Fit
0184 
0185 } // end namespace ROOT
0186 
0187 
0188 #endif /* ROOT_Fit_LogLikelihoodFCN */