File indexing completed on 2025-01-18 10:10:09
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
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
0033
0034
0035
0036
0037
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
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
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
0080
0081 virtual ~LogLikelihoodFCN () {}
0082
0083
0084
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
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
0112 virtual BaseFunction * Clone() const { return new LogLikelihoodFCN(*this); }
0113
0114
0115
0116
0117
0118 virtual unsigned int NFitPoints() const { return fNEffPoints; }
0119
0120
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
0127 virtual void Gradient(const double *x, double *g) const {
0128
0129 FitUtil::Evaluate<typename BaseFCN::T>::EvalLogLGradient(BaseFCN::ModelFunction(), BaseFCN::Data(), x, g,
0130 fNEffPoints, fExecutionPolicy);
0131 }
0132
0133
0134 virtual typename BaseObjFunction::Type_t Type() const { return BaseObjFunction::kLogLikelihood; }
0135
0136
0137
0138
0139 void UseSumOfWeightSquare(bool on = true) {
0140 if (fWeight == 0) return;
0141 if (on) fWeight = 2;
0142 else fWeight = 1;
0143 }
0144
0145
0146
0147 protected:
0148
0149
0150 private:
0151
0152
0153
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
0161 virtual double DoDerivative(const double * x, unsigned int icoord ) const {
0162 Gradient(x, &fGrad[0]);
0163 return fGrad[icoord];
0164 }
0165
0166
0167
0168 bool fIsExtended;
0169 int fWeight;
0170
0171
0172 mutable unsigned int fNEffPoints;
0173
0174 mutable std::vector<double> fGrad;
0175
0176 ::ROOT::EExecutionPolicy fExecutionPolicy;
0177 };
0178
0179
0180 typedef LogLikelihoodFCN<ROOT::Math::IMultiGenFunction, ROOT::Math::IParamMultiFunction> LogLikelihoodFunction;
0181 typedef LogLikelihoodFCN<ROOT::Math::IMultiGradFunction, ROOT::Math::IParamMultiFunction> LogLikelihoodGradFunction;
0182
0183 }
0184
0185 }
0186
0187
0188 #endif