Logo ROOT  
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 #include <memory>
25 #include <vector>
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  */
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 */
ROOT::Fit::BasicFCN::ModelFunction
virtual const IModelFunction & ModelFunction() const
access to const reference to the model function
Definition: BasicFCN.h:77
ROOT::Fit::BasicFCN< DerivFunType, ROOT::Math::IParamMultiFunction, BinData >::SetData
void SetData(const std::shared_ptr< BinData > &data)
Set the data pointer.
Definition: BasicFCN.h:88
ROOT::Fit::PoissonLikelihoodFCN
class evaluating the log likelihood for binned Poisson likelihood fits it is template to distinguish ...
Definition: PoissonLikelihoodFCN.h:48
ROOT::Fit::BasicFCN< DerivFunType, ROOT::Math::IParamMultiFunction, BinData >::ModelFunctionPtr
std::shared_ptr< IModelFunction > ModelFunctionPtr() const
access to function pointer
Definition: BasicFCN.h:80
HFit::Fit
TFitResultPtr Fit(FitObject *h1, TF1 *f1, Foption_t &option, const ROOT::Math::MinimizerOptions &moption, const char *goption, ROOT::Fit::DataRange &range)
Definition: HFitImpl.cxx:133
ROOT::Fit::BasicFCN< DerivFunType, ROOT::Math::IParamMultiFunction, BinData >::DataPtr
std::shared_ptr< BinData > DataPtr() const
access to data pointer
Definition: BasicFCN.h:74
ROOT::Fit::PoissonLikelihoodFCN::PoissonLikelihoodFCN
PoissonLikelihoodFCN(const PoissonLikelihoodFCN &f)
Copy constructor.
Definition: PoissonLikelihoodFCN.h:93
f
#define f(i)
Definition: RSha256.hxx:104
ROOT::Fit::BinData
Class describing the binned data sets : vectors of x coordinates, y values and optionally error on y ...
Definition: BinData.h:52
ROOT::Fit::BasicFCN< DerivFunType, ROOT::Math::IParamMultiFunction, BinData >::SetModelFunction
void SetModelFunction(const std::shared_ptr< IModelFunction > &func)
Set the function pointer.
Definition: BasicFCN.h:91
BasicFCN.h
FitUtil.h
ROOT::Fit::PoissonLikelihoodFCN::operator=
PoissonLikelihoodFCN & operator=(const PoissonLikelihoodFCN &rhs)
Assignment operator.
Definition: PoissonLikelihoodFCN.h:105
ROOT::Fit::PoissonLikelihoodFCN::NFitPoints
virtual unsigned int NFitPoints() const
Definition: PoissonLikelihoodFCN.h:120
ROOT::Fit::BasicFCN
BasicFCN class: base class for the objective functions used in the fits It has a reference to the dat...
Definition: BasicFCN.h:40
x
Double_t x[n]
Definition: legend1.C:17
ROOT::Fit::FitUtil::Evaluate::EvalPoissonLogLGradient
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:1444
ROOT::Fit::PoissonLikelihoodFCN::DoDerivative
virtual double DoDerivative(const double *x, unsigned int icoord) const
Definition: PoissonLikelihoodFCN.h:171
ROOT::Math::BasicFitMethodFunction< DerivFunType >::UpdateNCalls
virtual void UpdateNCalls() const
update number of calls
Definition: FitMethodFunction.h:90
ROOT::Fit::PoissonLikelihoodFCN::PoissonLikelihoodFCN
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)
Definition: PoissonLikelihoodFCN.h:63
ROOT::Fit::ExecutionPolicy
ExecutionPolicy
Definition: FitExecutionPolicy.h:5
ROOT::Fit::BasicFCN::Data
virtual const DataType & Data() const
access to const reference to the data
Definition: BasicFCN.h:71
ROOT::Fit::PoissonLikelihoodFCN::fIsExtended
bool fIsExtended
Definition: PoissonLikelihoodFCN.h:179
ROOT::Fit::PoissonLikelihoodFCN::~PoissonLikelihoodFCN
virtual ~PoissonLikelihoodFCN()
Destructor (no operations)
Definition: PoissonLikelihoodFCN.h:88
ROOT::Math::IParametricFunctionMultiDimTempl
IParamFunction interface (abstract class) describing multi-dimensional parameteric functions It is a ...
Definition: IParamFunction.h:105
ROOT::Fit::PoissonLLGradFunction
PoissonLikelihoodFCN< ROOT::Math::IMultiGradFunction, ROOT::Math::IParamMultiFunction > PoissonLLGradFunction
Definition: PoissonLikelihoodFCN.h:191
ROOT::Fit::PoissonLikelihoodFCN::fWeight
int fWeight
Definition: PoissonLikelihoodFCN.h:180
ROOT::Fit::PoissonLikelihoodFCN::DataElement
virtual double DataElement(const double *x, unsigned int i, double *g) const
i-th likelihood element and its gradient
Definition: PoissonLikelihoodFCN.h:123
ROOT::Fit::PoissonLikelihoodFCN::Type
virtual BaseObjFunction::Type_t Type() const
get type of fit method function
Definition: PoissonLikelihoodFCN.h:137
ROOT::Fit::FitUtil::Evaluate::EvalPoissonBinPdf
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:1439
ROOT::Fit::PoissonLikelihoodFCN::BaseObjFunction
::ROOT::Math::BasicFitMethodFunction< DerivFunType > BaseObjFunction
Definition: PoissonLikelihoodFCN.h:54
ROOT::Fit::DummyDeleter
Definition: FitData.h:40
ROOT::Fit::PoissonLLFunction
PoissonLikelihoodFCN< ROOT::Math::IMultiGenFunction, ROOT::Math::IParamMultiFunction > PoissonLLFunction
Definition: PoissonLikelihoodFCN.h:190
ROOT::Fit::PoissonLikelihoodFCN::DoEval
virtual double DoEval(const double *x) const
Evaluation of the function (required by interface)
Definition: PoissonLikelihoodFCN.h:164
ROOT::Math::BasicFitMethodFunction< DerivFunType >::Type_t
Type_t
enumeration specyfing the possible fit method types
Definition: FitMethodFunction.h:44
ROOT::Fit::PoissonLikelihoodFCN::Clone
virtual BaseFunction * Clone() const
clone the function (need to return Base for Windows)
Definition: PoissonLikelihoodFCN.h:117
ROOT::Fit::FitUtil::Evaluate::EvalPoissonLogL
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:1414
ROOT::Fit::PoissonLikelihoodFCN::UseSumOfWeights
void UseSumOfWeights()
Definition: PoissonLikelihoodFCN.h:142
double
double
Definition: Converters.cxx:921
IParamFunction.h
ROOT::Fit::PoissonLikelihoodFCN::Gradient
virtual void Gradient(const double *x, double *g) const
evaluate gradient
Definition: PoissonLikelihoodFCN.h:129
ROOT::Fit::PoissonLikelihoodFCN::fGrad
std::vector< double > fGrad
Definition: PoissonLikelihoodFCN.h:184
ROOT::Fit::PoissonLikelihoodFCN::IsWeighted
bool IsWeighted() const
Definition: PoissonLikelihoodFCN.h:139
ROOT::Fit::PoissonLikelihoodFCN::T
ModelFunType::BackendType T
Definition: PoissonLikelihoodFCN.h:51
BinData.h
ROOT::Fit::PoissonLikelihoodFCN::fExecutionPolicy
::ROOT::Fit::ExecutionPolicy fExecutionPolicy
Definition: PoissonLikelihoodFCN.h:186
ROOT::Fit::PoissonLikelihoodFCN::IModelFunction
::ROOT::Math::IParamMultiFunctionTempl< T > IModelFunction
Definition: PoissonLikelihoodFCN.h:57
ROOT::Math::BasicFitMethodFunction< DerivFunType >::BaseFunction
FunctionType::BaseFunc BaseFunction
Definition: FitMethodFunction.h:41
ROOT::Fit::PoissonLikelihoodFCN::UseSumOfWeightSquare
void UseSumOfWeightSquare(bool on=true)
Definition: PoissonLikelihoodFCN.h:149
ROOT::Fit::PoissonLikelihoodFCN::Type_t
BaseObjFunction::Type_t Type_t
Definition: PoissonLikelihoodFCN.h:58
ROOT::Math::BasicFitMethodFunction< DerivFunType >::kLogLikelihood
@ kLogLikelihood
Definition: FitMethodFunction.h:44
ROOT::Fit::ExecutionPolicy::kSerial
@ kSerial
ROOT::Fit::PoissonLikelihoodFCN::BaseFCN
BasicFCN< DerivFunType, ModelFunType, BinData > BaseFCN
Definition: PoissonLikelihoodFCN.h:52
ROOT::Fit::PoissonLikelihoodFCN::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.
Definition: PoissonLikelihoodFCN.h:75
ROOT::Fit::PoissonLikelihoodFCN::BaseFunction
BaseObjFunction::BaseFunction BaseFunction
Definition: PoissonLikelihoodFCN.h:55
ROOT::Fit::PoissonLikelihoodFCN::fNEffPoints
unsigned int fNEffPoints
Definition: PoissonLikelihoodFCN.h:182
ROOT
VSD Structures.
Definition: StringConv.hxx:21
g
#define g(i)
Definition: RSha256.hxx:105