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_PoissonLikelihoodFCN
0014 #define ROOT_Fit_PoissonLikelihoodFCN
0015
0016 #include "ROOT/EExecutionPolicy.hxx"
0017 #include "Fit/BasicFCN.h"
0018 #include "Fit/BinData.h"
0019 #include "Fit/FitUtil.h"
0020 #include "Math/IParamFunction.h"
0021
0022 #include <memory>
0023 #include <vector>
0024
0025
0026
0027
0028
0029
0030
0031
0032 namespace ROOT {
0033
0034 namespace Fit {
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045 template<class DerivFunType, class ModelFunType = ROOT::Math::IParamMultiFunction>
0046 class PoissonLikelihoodFCN : public BasicFCN<DerivFunType,ModelFunType,BinData> {
0047
0048 public:
0049 typedef typename ModelFunType::BackendType T;
0050 typedef BasicFCN<DerivFunType,ModelFunType,BinData> BaseFCN;
0051
0052 typedef ::ROOT::Math::BasicFitMethodFunction<DerivFunType> BaseObjFunction;
0053 typedef typename BaseObjFunction::BaseFunction BaseFunction;
0054
0055 typedef ::ROOT::Math::IParamMultiFunctionTempl<T> IModelFunction;
0056 typedef typename BaseObjFunction::Type_t Type_t;
0057
0058
0059
0060
0061 PoissonLikelihoodFCN (const std::shared_ptr<BinData> & data, const std::shared_ptr<IModelFunction> & func, int weight = 0, bool extended = true, const ::ROOT::EExecutionPolicy &executionPolicy = ::ROOT::EExecutionPolicy::kSequential ) :
0062 BaseFCN( data, func),
0063 fIsExtended(extended),
0064 fWeight(weight),
0065 fNEffPoints(0),
0066 fGrad ( std::vector<double> ( func->NPar() ) ),
0067 fExecutionPolicy(executionPolicy)
0068 { }
0069
0070
0071
0072
0073 PoissonLikelihoodFCN (const BinData & data, const IModelFunction & func, int weight = 0, bool extended = true, const ::ROOT::EExecutionPolicy &executionPolicy = ::ROOT::EExecutionPolicy::kSequential ) :
0074 BaseFCN(std::make_shared<BinData>(data), std::shared_ptr<IModelFunction>(dynamic_cast<IModelFunction*>(func.Clone() ) ) ),
0075 fIsExtended(extended),
0076 fWeight(weight),
0077 fNEffPoints(0),
0078 fGrad ( std::vector<double> ( func.NPar() ) ),
0079 fExecutionPolicy(executionPolicy)
0080 { }
0081
0082
0083
0084
0085
0086 virtual ~PoissonLikelihoodFCN () {}
0087
0088
0089
0090
0091 PoissonLikelihoodFCN(const PoissonLikelihoodFCN & f) :
0092 BaseFCN(f.DataPtr(), f.ModelFunctionPtr() ),
0093 fIsExtended(f.fIsExtended ),
0094 fWeight( f.fWeight ),
0095 fNEffPoints( f.fNEffPoints ),
0096 fGrad( f.fGrad),
0097 fExecutionPolicy(f.fExecutionPolicy)
0098 { }
0099
0100
0101
0102
0103 PoissonLikelihoodFCN & operator = (const PoissonLikelihoodFCN & rhs) {
0104 SetData(rhs.DataPtr() );
0105 SetModelFunction(rhs.ModelFunctionPtr() );
0106 fNEffPoints = rhs.fNEffPoints;
0107 fGrad = rhs.fGrad;
0108 fIsExtended = rhs.fIsExtended;
0109 fWeight = rhs.fWeight;
0110 fExecutionPolicy = rhs.fExecutionPolicy;
0111 }
0112
0113
0114
0115 virtual BaseFunction * Clone() const { return new PoissonLikelihoodFCN(*this); }
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, bool fullHessian) const {
0122 if (i==0) this->UpdateNCalls();
0123 return FitUtil::Evaluate<T>::EvalPoissonBinPdf(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
0130 FitUtil::Evaluate<typename BaseFCN::T>::EvalPoissonLogLGradient(BaseFCN::ModelFunction(), BaseFCN::Data(), x, g,
0131 fNEffPoints, fExecutionPolicy);
0132 }
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161 virtual typename BaseObjFunction::Type_t Type() const { return BaseObjFunction::kPoissonLikelihood; }
0162
0163 bool IsWeighted() const { return (fWeight != 0); }
0164
0165
0166 void UseSumOfWeights() {
0167 if (fWeight == 0) return;
0168 fWeight = 1;
0169 }
0170
0171
0172
0173 void UseSumOfWeightSquare(bool on = true) {
0174 if (fWeight == 0) return;
0175 if (on) fWeight = 2;
0176 else fWeight = 1;
0177 }
0178
0179
0180 protected:
0181
0182
0183 private:
0184
0185
0186
0187
0188 virtual double DoEval (const double * x) const {
0189 this->UpdateNCalls();
0190 return FitUtil::Evaluate<T>::EvalPoissonLogL(BaseFCN::ModelFunction(), BaseFCN::Data(), x, fWeight, fIsExtended,
0191 fNEffPoints, fExecutionPolicy);
0192 }
0193
0194
0195 virtual double DoDerivative(const double * x, unsigned int icoord ) const {
0196 Gradient(x, &fGrad[0]);
0197 return fGrad[icoord];
0198 }
0199
0200
0201
0202
0203 bool fIsExtended;
0204 int fWeight;
0205
0206 mutable unsigned int fNEffPoints;
0207
0208 mutable std::vector<double> fGrad;
0209
0210 ::ROOT::EExecutionPolicy fExecutionPolicy;
0211 };
0212
0213
0214 typedef PoissonLikelihoodFCN<ROOT::Math::IMultiGenFunction, ROOT::Math::IParamMultiFunction> PoissonLLFunction;
0215 typedef PoissonLikelihoodFCN<ROOT::Math::IMultiGradFunction, ROOT::Math::IParamMultiFunction> PoissonLLGradFunction;
0216
0217
0218 }
0219
0220 }
0221
0222
0223 #endif