Logo ROOT   6.08/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 #ifndef ROOT_Fit_BasicFCN
17 #include "Fit/BasicFCN.h"
18 #endif
19 
20 #ifndef ROOT_Math_IParamFunction
21 #include "Math/IParamFunction.h"
22 #endif
23 
24 #ifndef ROOT_Fit_BinData
25 #include "Fit/BinData.h"
26 #endif
27 
28 #ifndef ROOT_Fit_FitUtil
29 #include "Fit/FitUtil.h"
30 #endif
31 
32 
33 #include <memory>
34 
35 //#define PARALLEL
36 // #ifdef PARALLEL
37 // #ifndef ROOT_Fit_FitUtilParallel
38 // #include "Fit/FitUtilParallel.h"
39 // #endif
40 // #endif
41 
42 namespace ROOT {
43 
44  namespace Fit {
45 
46 
47 //___________________________________________________________________________________
48 /**
49  class evaluating the log likelihood
50  for binned Poisson likelihood fits
51  it is template to distinguish gradient and non-gradient case
52 
53  @ingroup FitMethodFunc
54 */
55 template<class FunType>
56 class PoissonLikelihoodFCN : public BasicFCN<FunType,BinData> {
57 
58 public:
59 
61 
62  typedef ::ROOT::Math::BasicFitMethodFunction<FunType> BaseObjFunction;
64 
66 
67 
68  /**
69  Constructor from unbin data set and model function (pdf)
70  */
71  PoissonLikelihoodFCN (const std::shared_ptr<BinData> & data, const std::shared_ptr<IModelFunction> & func, int weight = 0, bool extended = true ) :
72  BaseFCN( data, func),
73  fIsExtended(extended),
74  fWeight(weight),
75  fNEffPoints(0),
76  fGrad ( std::vector<double> ( func->NPar() ) )
77  { }
78 
79  /**
80  Constructor from unbin data set and model function (pdf) managed by the users
81  */
82  PoissonLikelihoodFCN (const BinData & data, const IModelFunction & func, int weight = 0, bool extended = true ) :
83  BaseFCN(std::shared_ptr<BinData>(const_cast<BinData*>(&data), DummyDeleter<BinData>()), std::shared_ptr<IModelFunction>(dynamic_cast<IModelFunction*>(func.Clone() ) ) ),
84  fIsExtended(extended),
85  fWeight(weight),
86  fNEffPoints(0),
87  fGrad ( std::vector<double> ( func.NPar() ) )
88  { }
89 
90 
91  /**
92  Destructor (no operations)
93  */
94  virtual ~PoissonLikelihoodFCN () {}
95 
96  /**
97  Copy constructor
98  */
100  BaseFCN(f.DataPtr(), f.ModelFunctionPtr() ),
102  fWeight( f.fWeight ),
104  fGrad( f.fGrad)
105  { }
106 
107  /**
108  Assignment operator
109  */
111  SetData(rhs.DataPtr() );
113  fNEffPoints = rhs.fNEffPoints;
114  fGrad = rhs.fGrad;
115  fIsExtended = rhs.fIsExtended;
116  fWeight = rhs.fWeight;
117  }
118 
119 
120  /// clone the function (need to return Base for Windows)
121  virtual BaseFunction * Clone() const { return new PoissonLikelihoodFCN(*this); }
122 
123  // effective points used in the fit
124  virtual unsigned int NFitPoints() const { return fNEffPoints; }
125 
126  /// i-th likelihood element and its gradient
127  virtual double DataElement(const double * x, unsigned int i, double * g) const {
128  if (i==0) this->UpdateNCalls();
130  }
131 
132  /// evaluate gradient
133  virtual void Gradient(const double *x, double *g) const {
134  // evaluate the chi2 gradient
136  }
137 
138  /// get type of fit method function
139  virtual typename BaseObjFunction::Type_t Type() const { return BaseObjFunction::kLogLikelihood; }
140 
141  bool IsWeighted() const { return (fWeight != 0); }
142 
143  // Use the weights in evaluating the likelihood
145  if (fWeight == 0) return; // do nothing if it was not weighted
146  fWeight = 1;
147  }
148 
149  // Use sum of the weight squared in evaluating the likelihood
150  // (this is needed for calculating the errors)
151  void UseSumOfWeightSquare(bool on = true) {
152  if (fWeight == 0) return; // do nothing if it was not weighted
153  if (on) fWeight = 2;
154  else fWeight = 1;
155  }
156 
157 
158 protected:
159 
160 
161 private:
162 
163  /**
164  Evaluation of the function (required by interface)
165  */
166  virtual double DoEval (const double * x) const {
167  this->UpdateNCalls();
169  }
170 
171  // for derivatives
172  virtual double DoDerivative(const double * x, unsigned int icoord ) const {
173  Gradient(x, &fGrad[0]);
174  return fGrad[icoord];
175  }
176 
177 
178  //data member
179 
180  bool fIsExtended; // flag to indicate if is extended (when false is a Multinomial lieklihood), default is true
181  int fWeight; // flag to indicate if needs to evaluate using weight or weight squared (default weight = 0)
182 
183  mutable unsigned int fNEffPoints; // number of effective points used in the fit
184 
185  mutable std::vector<double> fGrad; // for derivatives
186 
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 */
virtual void UpdateNCalls() const
update number of calls
Type_t
enumeration specyfing the possible fit method types
virtual unsigned int NFitPoints() const
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
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:1060
virtual const IModelFunction & ModelFunction() const
access to const reference to the model function
Definition: BasicFCN.h:80
void SetModelFunction(const std::shared_ptr< IModelFunction > &func)
Set the function pointer.
Definition: BasicFCN.h:94
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:1344
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:74
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:91
BasicFCN class: base class for the objective functions used in the fits It has a reference to the dat...
Definition: BasicFCN.h:44
std::shared_ptr< IModelFunction > ModelFunctionPtr() const
access to function pointer
Definition: BasicFCN.h:83
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
Definition: BinData.h:61
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:1163
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:77
::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.