Logo ROOT   6.12/07
Reference Guide
PoissonLikelihoodFCN.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Author: L. Moneta Fri Aug 17 14:29:24 2007
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2007 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Header file for class PoissonLikelihoodFCN
12 
13 #ifndef ROOT_Fit_PoissonLikelihoodFCN
14 #define ROOT_Fit_PoissonLikelihoodFCN
15 
16 #include "Fit/BasicFCN.h"
17 
18 #include "Math/IParamFunction.h"
19 
20 #include "Fit/BinData.h"
21 
22 #include "Fit/FitUtil.h"
23 
24 
25 #include <memory>
26 
27 //#define PARALLEL
28 // #ifdef PARALLEL
29 // #ifndef ROOT_Fit_FitUtilParallel
30 // #include "Fit/FitUtilParallel.h"
31 // #endif
32 // #endif
33 
34 namespace ROOT {
35 
36  namespace Fit {
37 
38 
39 //___________________________________________________________________________________
40 /**
41  class evaluating the log likelihood
42  for binned Poisson likelihood fits
43  it is template to distinguish gradient and non-gradient case
44 
45  @ingroup FitMethodFunc
46 */
47 template<class DerivFunType, class ModelFunType = ROOT::Math::IParamMultiFunction>
48 class PoissonLikelihoodFCN : public BasicFCN<DerivFunType,ModelFunType,BinData> {
49 
50 public:
51  typedef typename ModelFunType::BackendType T;
53 
54  typedef ::ROOT::Math::BasicFitMethodFunction<DerivFunType> BaseObjFunction;
56 
57  typedef ::ROOT::Math::IParamMultiFunctionTempl<T> IModelFunction;
58  typedef typename BaseObjFunction::Type_t Type_t;
59 
60  /**
61  Constructor from unbin data set and model function (pdf)
62  */
63  PoissonLikelihoodFCN (const std::shared_ptr<BinData> & data, const std::shared_ptr<IModelFunction> & func, int weight = 0, bool extended = true, const ROOT::Fit::ExecutionPolicy &executionPolicy = ROOT::Fit::ExecutionPolicy::kSerial ) :
64  BaseFCN( data, func),
65  fIsExtended(extended),
66  fWeight(weight),
67  fNEffPoints(0),
68  fGrad ( std::vector<double> ( func->NPar() ) ),
69  fExecutionPolicy(executionPolicy)
70  { }
71 
72  /**
73  Constructor from unbin data set and model function (pdf) managed by the users
74  */
75  PoissonLikelihoodFCN (const BinData & data, const IModelFunction & func, int weight = 0, bool extended = true, const ROOT::Fit::ExecutionPolicy &executionPolicy = ROOT::Fit::ExecutionPolicy::kSerial ) :
76  BaseFCN(std::shared_ptr<BinData>(const_cast<BinData*>(&data), DummyDeleter<BinData>()), std::shared_ptr<IModelFunction>(dynamic_cast<IModelFunction*>(func.Clone() ) ) ),
77  fIsExtended(extended),
78  fWeight(weight),
79  fNEffPoints(0),
80  fGrad ( std::vector<double> ( func.NPar() ) ),
81  fExecutionPolicy(executionPolicy)
82  { }
83 
84 
85  /**
86  Destructor (no operations)
87  */
88  virtual ~PoissonLikelihoodFCN () {}
89 
90  /**
91  Copy constructor
92  */
94  BaseFCN(f.DataPtr(), f.ModelFunctionPtr() ),
96  fWeight( f.fWeight ),
98  fGrad( f.fGrad),
100  { }
101 
102  /**
103  Assignment operator
104  */
106  SetData(rhs.DataPtr() );
108  fNEffPoints = rhs.fNEffPoints;
109  fGrad = rhs.fGrad;
110  fIsExtended = rhs.fIsExtended;
111  fWeight = rhs.fWeight;
113  }
114 
115 
116  /// clone the function (need to return Base for Windows)
117  virtual BaseFunction * Clone() const { return new PoissonLikelihoodFCN(*this); }
118 
119  // effective points used in the fit
120  virtual unsigned int NFitPoints() const { return fNEffPoints; }
121 
122  /// i-th likelihood element and its gradient
123  virtual double DataElement(const double * x, unsigned int i, double * g) const {
124  if (i==0) this->UpdateNCalls();
126  }
127 
128  /// evaluate gradient
129  virtual void Gradient(const double *x, double *g) const
130  {
131  // evaluate the Poisson gradient
134  }
135 
136  /// get type of fit method function
137  virtual typename BaseObjFunction::Type_t Type() const { return BaseObjFunction::kLogLikelihood; }
138 
139  bool IsWeighted() const { return (fWeight != 0); }
140 
141  // Use the weights in evaluating the likelihood
143  if (fWeight == 0) return; // do nothing if it was not weighted
144  fWeight = 1;
145  }
146 
147  // Use sum of the weight squared in evaluating the likelihood
148  // (this is needed for calculating the errors)
149  void UseSumOfWeightSquare(bool on = true) {
150  if (fWeight == 0) return; // do nothing if it was not weighted
151  if (on) fWeight = 2;
152  else fWeight = 1;
153  }
154 
155 
156 protected:
157 
158 
159 private:
160 
161  /**
162  Evaluation of the function (required by interface)
163  */
164  virtual double DoEval (const double * x) const {
165  this->UpdateNCalls();
168  }
169 
170  // for derivatives
171  virtual double DoDerivative(const double * x, unsigned int icoord ) const {
172  Gradient(x, &fGrad[0]);
173  return fGrad[icoord];
174  }
175 
176 
177  //data member
178 
179  bool fIsExtended; // flag to indicate if is extended (when false is a Multinomial lieklihood), default is true
180  int fWeight; // flag to indicate if needs to evaluate using weight or weight squared (default weight = 0)
181 
182  mutable unsigned int fNEffPoints; // number of effective points used in the fit
183 
184  mutable std::vector<double> fGrad; // for derivatives
185 
187 };
188 
189  // define useful typedef's
192 
193 
194  } // end namespace Fit
195 
196 } // end namespace ROOT
197 
198 
199 #endif /* ROOT_Fit_PoissonLikelihoodFCN */
PoissonLikelihoodFCN(const BinData &data, const IModelFunction &func, int weight=0, bool extended=true, const ROOT::Fit::ExecutionPolicy &executionPolicy=ROOT::Fit::ExecutionPolicy::kSerial)
Constructor from unbin data set and model function (pdf) managed by the users.
ROOT::Fit::ExecutionPolicy fExecutionPolicy
virtual void UpdateNCalls() const
update number of calls
Type_t
enumeration specyfing the possible fit method types
BaseObjFunction::BaseFunction BaseFunction
virtual const IModelFunction & ModelFunction() const
access to const reference to the model function
Definition: BasicFCN.h:77
void SetData(const std::shared_ptr< BinData > &data)
Set the data pointer.
Definition: BasicFCN.h:88
void SetModelFunction(const std::shared_ptr< IModelFunction > &func)
Set the function pointer.
Definition: BasicFCN.h:91
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
virtual unsigned int NFitPoints() const
std::shared_ptr< BinData > DataPtr() const
access to data pointer
Definition: BasicFCN.h:74
virtual double DoDerivative(const double *x, unsigned int icoord) const
PoissonLikelihoodFCN(const PoissonLikelihoodFCN &f)
Copy constructor.
STL namespace.
virtual const BinData & Data() const
access to const reference to the data
Definition: BasicFCN.h:71
static double EvalPoissonBinPdf(const IModelFunctionTempl< double > &func, const BinData &data, const double *p, unsigned int i, double *g)
evaluate the pdf (Poisson) contribution to the logl (return actually log of pdf) and its gradient ...
Definition: FitUtil.h:1493
static double EvalPoissonLogL(const IModelFunctionTempl< double > &func, const BinData &data, const double *p, int iWeight, bool extended, unsigned int &nPoints, ROOT::Fit::ExecutionPolicy executionPolicy, unsigned nChunks=0)
Definition: FitUtil.h:1468
virtual double DoEval(const double *x) const
Evaluation of the function (required by interface)
PoissonLikelihoodFCN & operator=(const PoissonLikelihoodFCN &rhs)
Assignment operator.
Double_t x[n]
Definition: legend1.C:17
virtual BaseObjFunction::Type_t Type() const
get type of fit method function
class evaluating the log likelihood for binned Poisson likelihood fits it is template to distinguish ...
virtual ~PoissonLikelihoodFCN()
Destructor (no operations)
::ROOT::Math::BasicFitMethodFunction< DerivFunType > BaseObjFunction
virtual double DataElement(const double *x, unsigned int i, double *g) const
i-th likelihood element and its gradient
BasicFCN class: base class for the objective functions used in the fits It has a reference to the dat...
Definition: BasicFCN.h:40
PoissonLikelihoodFCN< ROOT::Math::IMultiGradFunction, ROOT::Math::IParamMultiFunction > PoissonLLGradFunction
virtual BaseFunction * Clone() const
clone the function (need to return Base for Windows)
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
Definition: BinData.h:53
PoissonLikelihoodFCN< ROOT::Math::IMultiGenFunction, ROOT::Math::IParamMultiFunction > PoissonLLFunction
::ROOT::Math::IParamMultiFunctionTempl< T > IModelFunction
PoissonLikelihoodFCN(const std::shared_ptr< BinData > &data, const std::shared_ptr< IModelFunction > &func, int weight=0, bool extended=true, const ROOT::Fit::ExecutionPolicy &executionPolicy=ROOT::Fit::ExecutionPolicy::kSerial)
Constructor from unbin data set and model function (pdf)
static void EvalPoissonLogLGradient(const IModelFunctionTempl< double > &func, const BinData &data, const double *p, double *g, unsigned int &nPoints, ROOT::Fit::ExecutionPolicy executionPolicy=ROOT::Fit::ExecutionPolicy::kSerial, unsigned nChunks=0)
Definition: FitUtil.h:1498
TFitResultPtr Fit(FitObject *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
Definition: HFitImpl.cxx:134
virtual void Gradient(const double *x, double *g) const
evaluate gradient
BasicFCN< DerivFunType, ModelFunType, BinData > BaseFCN
std::shared_ptr< IModelFunction > ModelFunctionPtr() const
access to function pointer
Definition: BasicFCN.h:80
static constexpr double g