Logo ROOT   6.12/07
Reference Guide
LogLikelihoodFCN.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 LogLikelihoodFCN
12 
13 #ifndef ROOT_Fit_LogLikelihoodFCN
14 #define ROOT_Fit_LogLikelihoodFCN
15 
16 #include "Fit/BasicFCN.h"
17 
18 #include "Math/IParamFunction.h"
19 
20 #include "Fit/UnBinData.h"
21 
22 #include "Fit/FitUtil.h"
23 
24 #include <memory>
25 
26 namespace ROOT {
27 
28  namespace Fit {
29 
30 
31 //___________________________________________________________________________________
32 /**
33  LogLikelihoodFCN class
34  for likelihood fits
35 
36  it is template to distinguish gradient and non-gradient case
37 
38  @ingroup FitMethodFunc
39 */
40 template<class DerivFunType,class ModelFunType = ROOT::Math::IParamMultiFunction>
41 class LogLikelihoodFCN : public BasicFCN<DerivFunType,ModelFunType,UnBinData> {
42 
43 public:
44 
45  typedef typename ModelFunType::BackendType T;
47 
48  typedef ::ROOT::Math::BasicFitMethodFunction<DerivFunType> BaseObjFunction;
50 
51  typedef ::ROOT::Math::IParamMultiFunctionTempl<T> IModelFunction;
52  typedef typename BaseObjFunction::Type_t Type_t;
53 
54 
55  /**
56  Constructor from unbin data set and model function (pdf)
57  */
58  LogLikelihoodFCN (const std::shared_ptr<UnBinData> & data, const std::shared_ptr<IModelFunction> & func, int weight = 0, bool extended = false, const ROOT::Fit::ExecutionPolicy &executionPolicy = ROOT::Fit::ExecutionPolicy::kSerial) :
59  BaseFCN( data, func),
60  fIsExtended(extended),
61  fWeight(weight),
62  fNEffPoints(0),
63  fGrad ( std::vector<double> ( func->NPar() ) ),
64  fExecutionPolicy(executionPolicy)
65  {}
66 
67  /**
68  Constructor from unbin data set and model function (pdf) for object managed by users
69  */
70  LogLikelihoodFCN (const UnBinData & data, const IModelFunction & func, int weight = 0, bool extended = false, const ROOT::Fit::ExecutionPolicy &executionPolicy = ROOT::Fit::ExecutionPolicy::kSerial) :
71  BaseFCN(std::shared_ptr<UnBinData>(const_cast<UnBinData*>(&data), DummyDeleter<UnBinData>()), std::shared_ptr<IModelFunction>(dynamic_cast<IModelFunction*>(func.Clone() ) ) ),
72  fIsExtended(extended),
73  fWeight(weight),
74  fNEffPoints(0),
75  fGrad ( std::vector<double> ( func.NPar() ) ),
76  fExecutionPolicy(executionPolicy)
77  {}
78 
79  /**
80  Destructor (no operations)
81  */
82  virtual ~LogLikelihoodFCN () {}
83 
84  /**
85  Copy constructor
86  */
88  BaseFCN(f.DataPtr(), f.ModelFunctionPtr() ),
90  fWeight( f.fWeight ),
92  fGrad( f.fGrad),
94  { }
95 
96 
97  /**
98  Assignment operator
99  */
101  SetData(rhs.DataPtr() );
103  fNEffPoints = rhs.fNEffPoints;
104  fGrad = rhs.fGrad;
105  fIsExtended = rhs.fIsExtended;
106  fWeight = rhs.fWeight;
108  }
109 
110 
111  /// clone the function (need to return Base for Windows)
112  virtual BaseFunction * Clone() const { return new LogLikelihoodFCN(*this); }
113 
114 
115  //using BaseObjFunction::operator();
116 
117  // effective points used in the fit
118  virtual unsigned int NFitPoints() const { return fNEffPoints; }
119 
120  /// i-th likelihood contribution and its gradient
121  virtual double DataElement(const double * x, unsigned int i, double * g) const {
122  if (i==0) this->UpdateNCalls();
124  }
125 
126  // need to be virtual to be instantited
127  virtual void Gradient(const double *x, double *g) const {
128  // evaluate the chi2 gradient
131  }
132 
133  /// get type of fit method function
134  virtual typename BaseObjFunction::Type_t Type() const { return BaseObjFunction::kLogLikelihood; }
135 
136 
137  // Use sum of the weight squared in evaluating the likelihood
138  // (this is needed for calculating the errors)
139  void UseSumOfWeightSquare(bool on = true) {
140  if (fWeight == 0) return; // do nothing if it was not weighted
141  if (on) fWeight = 2;
142  else fWeight = 1;
143  }
144 
145 
146 
147 protected:
148 
149 
150 private:
151 
152  /**
153  Evaluation of the function (required by interface)
154  */
155  virtual double DoEval (const double * x) const {
156  this->UpdateNCalls();
158  }
159 
160  // for derivatives
161  virtual double DoDerivative(const double * x, unsigned int icoord ) const {
162  Gradient(x, &fGrad[0]);
163  return fGrad[icoord];
164  }
165 
166 
167  //data member
168  bool fIsExtended; // flag for indicating if likelihood is extended
169  int fWeight; // flag to indicate if needs to evaluate using weight or weight squared (default weight = 0)
170 
171 
172  mutable unsigned int fNEffPoints; // number of effective points used in the fit
173 
174  mutable std::vector<double> fGrad; // for derivatives
175 
177 };
178  // define useful typedef's
179  // using LogLikelihoodFunction_v = LogLikelihoodFCN<ROOT::Math::IMultiGenFunction, ROOT::Math::IParametricFunctionMultiDimTempl<T>>;
182 
183  } // end namespace Fit
184 
185 } // end namespace ROOT
186 
187 
188 #endif /* ROOT_Fit_LogLikelihoodFCN */
::ROOT::Math::IParamMultiFunctionTempl< T > IModelFunction
virtual void UpdateNCalls() const
update number of calls
Type_t
enumeration specyfing the possible fit method types
virtual const IModelFunction & ModelFunction() const
access to const reference to the model function
Definition: BasicFCN.h:77
void SetData(const std::shared_ptr< UnBinData > &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
LogLikelihoodFCN & operator=(const LogLikelihoodFCN &rhs)
Assignment operator.
std::shared_ptr< UnBinData > DataPtr() const
access to data pointer
Definition: BasicFCN.h:74
static double EvalLogL(const IModelFunctionTempl< double > &func, const UnBinData &data, const double *p, int iWeight, bool extended, unsigned int &nPoints, ROOT::Fit::ExecutionPolicy executionPolicy, unsigned nChunks=0)
Definition: FitUtil.h:1461
LogLikelihoodFCN(const LogLikelihoodFCN &f)
Copy constructor.
virtual unsigned int NFitPoints() const
LogLikelihoodFCN class for likelihood fits.
void UseSumOfWeightSquare(bool on=true)
virtual BaseFunction * Clone() const
clone the function (need to return Base for Windows)
Class describing the unbinned data sets (just x coordinates values) of any dimensions.
Definition: UnBinData.h:42
ROOT::Fit::ExecutionPolicy fExecutionPolicy
STL namespace.
virtual const UnBinData & Data() const
access to const reference to the data
Definition: BasicFCN.h:71
virtual BaseObjFunction::Type_t Type() const
get type of fit method function
BaseObjFunction::BaseFunction BaseFunction
Double_t x[n]
Definition: legend1.C:17
BasicFCN< DerivFunType, ModelFunType, UnBinData > BaseFCN
LogLikelihoodFCN(const std::shared_ptr< UnBinData > &data, const std::shared_ptr< IModelFunction > &func, int weight=0, bool extended=false, const ROOT::Fit::ExecutionPolicy &executionPolicy=ROOT::Fit::ExecutionPolicy::kSerial)
Constructor from unbin data set and model function (pdf)
LogLikelihoodFCN< ROOT::Math::IMultiGradFunction, ROOT::Math::IParamMultiFunction > LogLikelihoodGradFunction
BasicFCN class: base class for the objective functions used in the fits It has a reference to the dat...
Definition: BasicFCN.h:40
BaseObjFunction::Type_t Type_t
LogLikelihoodFCN< ROOT::Math::IMultiGenFunction, ROOT::Math::IParamMultiFunction > LogLikelihoodFunction
virtual double DoDerivative(const double *x, unsigned int icoord) const
static void EvalLogLGradient(const IModelFunctionTempl< double > &func, const UnBinData &data, const double *p, double *g, unsigned int &nPoints, ROOT::Fit::ExecutionPolicy executionPolicy=ROOT::Fit::ExecutionPolicy::kSerial, unsigned nChunks=0)
Definition: FitUtil.h:1506
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
std::vector< double > fGrad
double EvaluatePdf(const IModelFunction &func, const UnBinData &data, const double *x, unsigned int ipoint, double *g=0)
evaluate the pdf contribution to the LogL given a model function and the BinPoint data...
Definition: FitUtil.cxx:850
virtual void Gradient(const double *x, double *g) const
virtual double DataElement(const double *x, unsigned int i, double *g) const
i-th likelihood contribution and its gradient
virtual double DoEval(const double *x) const
Evaluation of the function (required by interface)
ModelFunType::BackendType T
std::shared_ptr< IModelFunction > ModelFunctionPtr() const
access to function pointer
Definition: BasicFCN.h:80
LogLikelihoodFCN(const UnBinData &data, const IModelFunction &func, int weight=0, bool extended=false, const ROOT::Fit::ExecutionPolicy &executionPolicy=ROOT::Fit::ExecutionPolicy::kSerial)
Constructor from unbin data set and model function (pdf) for object managed by users.
virtual ~LogLikelihoodFCN()
Destructor (no operations)
::ROOT::Math::BasicFitMethodFunction< DerivFunType > BaseObjFunction
static constexpr double g