Logo ROOT   6.10/09
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 FunType>
48 class PoissonLikelihoodFCN : public BasicFCN<FunType,BinData> {
49 
50 public:
51 
53 
54  typedef ::ROOT::Math::BasicFitMethodFunction<FunType> BaseObjFunction;
56 
58 
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 ) :
64  BaseFCN( data, func),
65  fIsExtended(extended),
66  fWeight(weight),
67  fNEffPoints(0),
68  fGrad ( std::vector<double> ( func->NPar() ) )
69  { }
70 
71  /**
72  Constructor from unbin data set and model function (pdf) managed by the users
73  */
74  PoissonLikelihoodFCN (const BinData & data, const IModelFunction & func, int weight = 0, bool extended = true ) :
75  BaseFCN(std::shared_ptr<BinData>(const_cast<BinData*>(&data), DummyDeleter<BinData>()), std::shared_ptr<IModelFunction>(dynamic_cast<IModelFunction*>(func.Clone() ) ) ),
76  fIsExtended(extended),
77  fWeight(weight),
78  fNEffPoints(0),
79  fGrad ( std::vector<double> ( func.NPar() ) )
80  { }
81 
82 
83  /**
84  Destructor (no operations)
85  */
86  virtual ~PoissonLikelihoodFCN () {}
87 
88  /**
89  Copy constructor
90  */
92  BaseFCN(f.DataPtr(), f.ModelFunctionPtr() ),
94  fWeight( f.fWeight ),
96  fGrad( f.fGrad)
97  { }
98 
99  /**
100  Assignment operator
101  */
103  SetData(rhs.DataPtr() );
105  fNEffPoints = rhs.fNEffPoints;
106  fGrad = rhs.fGrad;
107  fIsExtended = rhs.fIsExtended;
108  fWeight = rhs.fWeight;
109  }
110 
111 
112  /// clone the function (need to return Base for Windows)
113  virtual BaseFunction * Clone() const { return new PoissonLikelihoodFCN(*this); }
114 
115  // effective points used in the fit
116  virtual unsigned int NFitPoints() const { return fNEffPoints; }
117 
118  /// i-th likelihood element and its gradient
119  virtual double DataElement(const double * x, unsigned int i, double * g) const {
120  if (i==0) this->UpdateNCalls();
122  }
123 
124  /// evaluate gradient
125  virtual void Gradient(const double *x, double *g) const {
126  // evaluate the chi2 gradient
128  }
129 
130  /// get type of fit method function
131  virtual typename BaseObjFunction::Type_t Type() const { return BaseObjFunction::kLogLikelihood; }
132 
133  bool IsWeighted() const { return (fWeight != 0); }
134 
135  // Use the weights in evaluating the likelihood
137  if (fWeight == 0) return; // do nothing if it was not weighted
138  fWeight = 1;
139  }
140 
141  // Use sum of the weight squared in evaluating the likelihood
142  // (this is needed for calculating the errors)
143  void UseSumOfWeightSquare(bool on = true) {
144  if (fWeight == 0) return; // do nothing if it was not weighted
145  if (on) fWeight = 2;
146  else fWeight = 1;
147  }
148 
149 
150 protected:
151 
152 
153 private:
154 
155  /**
156  Evaluation of the function (required by interface)
157  */
158  virtual double DoEval (const double * x) const {
159  this->UpdateNCalls();
161  }
162 
163  // for derivatives
164  virtual double DoDerivative(const double * x, unsigned int icoord ) const {
165  Gradient(x, &fGrad[0]);
166  return fGrad[icoord];
167  }
168 
169 
170  //data member
171 
172  bool fIsExtended; // flag to indicate if is extended (when false is a Multinomial lieklihood), default is true
173  int fWeight; // flag to indicate if needs to evaluate using weight or weight squared (default weight = 0)
174 
175  mutable unsigned int fNEffPoints; // number of effective points used in the fit
176 
177  mutable std::vector<double> fGrad; // for derivatives
178 
179 };
180 
181  // define useful typedef's
184 
185 
186  } // end namespace Fit
187 
188 } // end namespace ROOT
189 
190 
191 #endif /* ROOT_Fit_PoissonLikelihoodFCN */
virtual void UpdateNCalls() const
update number of calls
Type_t
enumeration specyfing the possible fit method types
virtual unsigned int NFitPoints() const
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
double EvaluatePoissonBinPdf(const IModelFunction &func, const BinData &data, const double *x, unsigned int ipoint, double *g=0)
evaluate the pdf contribution to the Poisson LogL given a model function and the BinPoint data...
Definition: FitUtil.cxx:1105
virtual const IModelFunction & ModelFunction() const
access to const reference to the model function
Definition: BasicFCN.h:76
void SetModelFunction(const std::shared_ptr< IModelFunction > &func)
Set the function pointer.
Definition: BasicFCN.h:90
virtual double DataElement(const double *x, unsigned int i, double *g) const
i-th likelihood element and its gradient
virtual BaseObjFunction::Type_t Type() const
get type of fit method function
STL namespace.
PoissonLikelihoodFCN(const std::shared_ptr< BinData > &data, const std::shared_ptr< IModelFunction > &func, int weight=0, bool extended=true)
Constructor from unbin data set and model function (pdf)
PoissonLikelihoodFCN(const PoissonLikelihoodFCN &f)
Copy constructor.
void EvaluatePoissonLogLGradient(const IModelFunction &func, const BinData &data, const double *x, double *grad)
evaluate the Poisson LogL given a model function and the data at the point x.
Definition: FitUtil.cxx:1389
Double_t x[n]
Definition: legend1.C:17
class evaluating the log likelihood for binned Poisson likelihood fits it is template to distinguish ...
virtual ~PoissonLikelihoodFCN()
Destructor (no operations)
virtual const BinData & Data() const
access to const reference to the data
Definition: BasicFCN.h:70
virtual double DoDerivative(const double *x, unsigned int icoord) const
void SetData(const std::shared_ptr< BinData > &data)
Set the data pointer.
Definition: BasicFCN.h:87
BasicFCN class: base class for the objective functions used in the fits It has a reference to the dat...
Definition: BasicFCN.h:40
std::shared_ptr< IModelFunction > ModelFunctionPtr() const
access to function pointer
Definition: BasicFCN.h:79
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
Definition: BinData.h:53
double EvaluatePoissonLogL(const IModelFunction &func, const BinData &data, const double *x, int iWeight, bool extended, unsigned int &nPoints)
evaluate the Poisson LogL given a model function and the data at the point x.
Definition: FitUtil.cxx:1208
PoissonLikelihoodFCN< ROOT::Math::IMultiGenFunction > PoissonLLFunction
virtual BaseFunction * Clone() const
clone the function (need to return Base for Windows)
double f(double x)
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
double func(double *x, double *p)
Definition: stressTF1.cxx:213
PoissonLikelihoodFCN< ROOT::Math::IMultiGradFunction > PoissonLLGradFunction
IParametricFunctionMultiDim IParamMultiFunction
virtual double DoEval(const double *x) const
Evaluation of the function (required by interface)
BaseObjFunction::BaseFunction BaseFunction
virtual void Gradient(const double *x, double *g) const
evaluate gradient
PoissonLikelihoodFCN & operator=(const PoissonLikelihoodFCN &rhs)
Assignment operator.
BasicFCN< FunType, BinData > BaseFCN
const int NPar
::ROOT::Math::IParamMultiFunction IModelFunction
std::shared_ptr< BinData > DataPtr() const
access to data pointer
Definition: BasicFCN.h:73
::ROOT::Math::BasicFitMethodFunction< FunType > BaseObjFunction
PoissonLikelihoodFCN(const BinData &data, const IModelFunction &func, int weight=0, bool extended=true)
Constructor from unbin data set and model function (pdf) managed by the users.