Logo ROOT   6.21/01
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  */
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  return *this;
109  }
110 
111 
112  /// clone the function (need to return Base for Windows)
113  virtual BaseFunction * Clone() const { return new LogLikelihoodFCN(*this); }
114 
115 
116  //using BaseObjFunction::operator();
117 
118  // effective points used in the fit
119  virtual unsigned int NFitPoints() const { return fNEffPoints; }
120 
121  /// i-th likelihood contribution and its gradient
122  virtual double DataElement(const double * x, unsigned int i, double * g) const {
123  if (i==0) this->UpdateNCalls();
125  }
126 
127  // need to be virtual to be instantited
128  virtual void Gradient(const double *x, double *g) const {
129  // evaluate the chi2 gradient
132  }
133 
134  /// get type of fit method function
135  virtual typename BaseObjFunction::Type_t Type() const { return BaseObjFunction::kLogLikelihood; }
136 
137 
138  // Use sum of the weight squared in evaluating the likelihood
139  // (this is needed for calculating the errors)
140  void UseSumOfWeightSquare(bool on = true) {
141  if (fWeight == 0) return; // do nothing if it was not weighted
142  if (on) fWeight = 2;
143  else fWeight = 1;
144  }
145 
146 
147 
148 protected:
149 
150 
151 private:
152 
153  /**
154  Evaluation of the function (required by interface)
155  */
156  virtual double DoEval (const double * x) const {
157  this->UpdateNCalls();
159  }
160 
161  // for derivatives
162  virtual double DoDerivative(const double * x, unsigned int icoord ) const {
163  Gradient(x, &fGrad[0]);
164  return fGrad[icoord];
165  }
166 
167 
168  //data member
169  bool fIsExtended; // flag for indicating if likelihood is extended
170  int fWeight; // flag to indicate if needs to evaluate using weight or weight squared (default weight = 0)
171 
172 
173  mutable unsigned int fNEffPoints; // number of effective points used in the fit
174 
175  mutable std::vector<double> fGrad; // for derivatives
176 
178 };
179  // define useful typedef's
180  // using LogLikelihoodFunction_v = LogLikelihoodFCN<ROOT::Math::IMultiGenFunction, ROOT::Math::IParametricFunctionMultiDimTempl<T>>;
183 
184  } // end namespace Fit
185 
186 } // end namespace ROOT
187 
188 
189 #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
Returns the available number of logical cores.
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
#define g(i)
Definition: RSha256.hxx:105
LogLikelihoodFCN(const LogLikelihoodFCN &f)
Copy constructor.
virtual unsigned int NFitPoints() const
LogLikelihoodFCN class for likelihood fits.
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 ...
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
#define f(i)
Definition: RSha256.hxx:104
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
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:1452
::ROOT::Fit::ExecutionPolicy fExecutionPolicy
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
IParamFunction interface (abstract class) describing multi-dimensional parameteric functions It is a ...
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:860
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
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:1407
virtual ~LogLikelihoodFCN()
Destructor (no operations)
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)
::ROOT::Math::BasicFitMethodFunction< DerivFunType > BaseObjFunction