ROOT  6.06/09
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 #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_UnBinData
25 #include "Fit/UnBinData.h"
26 #endif
27 
28 #ifndef ROOT_Fit_FitUtil
29 #include "Fit/FitUtil.h"
30 #endif
31 
32 #ifdef ROOT_FIT_PARALLEL
33 #ifndef ROOT_Fit_FitUtilParallel
34 #include "Fit/FitUtilParallel.h"
35 #endif
36 #endif
37 
38 #include <memory>
39 
40 namespace ROOT {
41 
42  namespace Fit {
43 
44 
45 //___________________________________________________________________________________
46 /**
47  LogLikelihoodFCN class
48  for likelihood fits
49 
50  it is template to distinguish gradient and non-gradient case
51 
52  @ingroup FitMethodFunc
53 */
54 template<class FunType>
55 class LogLikelihoodFCN : public BasicFCN<FunType,UnBinData> {
56 
57 public:
58 
60 
61  typedef ::ROOT::Math::BasicFitMethodFunction<FunType> BaseObjFunction;
63 
65 
66 
67  /**
68  Constructor from unbin data set and model function (pdf)
69  */
70  LogLikelihoodFCN (const std::shared_ptr<UnBinData> & data, const std::shared_ptr<IModelFunction> & func, int weight = 0, bool extended = false) :
71  BaseFCN( data, func),
72  fIsExtended(extended),
73  fWeight(weight),
74  fNEffPoints(0),
75  fGrad ( std::vector<double> ( func->NPar() ) )
76  {}
77 
78  /**
79  Constructor from unbin data set and model function (pdf) for object managed by users
80  */
81  LogLikelihoodFCN (const UnBinData & data, const IModelFunction & func, int weight = 0, bool extended = false) :
82  BaseFCN(std::shared_ptr<UnBinData>(const_cast<UnBinData*>(&data), DummyDeleter<UnBinData>()), std::shared_ptr<IModelFunction>(dynamic_cast<IModelFunction*>(func.Clone() ) ) ),
83  fIsExtended(extended),
84  fWeight(weight),
85  fNEffPoints(0),
86  fGrad ( std::vector<double> ( func.NPar() ) )
87  {}
88 
89  /**
90  Destructor (no operations)
91  */
92  virtual ~LogLikelihoodFCN () {}
93 
94  /**
95  Copy constructor
96  */
98  BaseFCN(f.DataPtr(), f.ModelFunctionPtr() ),
100  fWeight( f.fWeight ),
102  fGrad( f.fGrad)
103  { }
104 
105 
106  /**
107  Assignment operator
108  */
110  SetData(rhs.DataPtr() );
112  fNEffPoints = rhs.fNEffPoints;
113  fGrad = rhs.fGrad;
114  fIsExtended = rhs.fIsExtended;
115  fWeight = rhs.fWeight;
116  }
117 
118 
119  /// clone the function (need to return Base for Windows)
120  virtual BaseFunction * Clone() const { return new LogLikelihoodFCN(*this); }
121 
122 
123  //using BaseObjFunction::operator();
124 
125  // effective points used in the fit
126  virtual unsigned int NFitPoints() const { return fNEffPoints; }
127 
128  /// i-th likelihood contribution and its gradient
129  virtual double DataElement(const double * x, unsigned int i, double * g) const {
130  if (i==0) this->UpdateNCalls();
132  }
133 
134 
135  // need to be virtual to be instantited
136  virtual void Gradient(const double *x, double *g) const {
137  // evaluate the chi2 gradient
139  }
140 
141  /// get type of fit method function
142  virtual typename BaseObjFunction::Type_t Type() const { return BaseObjFunction::kLogLikelihood; }
143 
144 
145  // Use sum of the weight squared in evaluating the likelihood
146  // (this is needed for calculating the errors)
147  void UseSumOfWeightSquare(bool on = true) {
148  if (fWeight == 0) return; // do nothing if it was not weighted
149  if (on) fWeight = 2;
150  else fWeight = 1;
151  }
152 
153 
154 
155 protected:
156 
157 
158 private:
159 
160  /**
161  Evaluation of the function (required by interface)
162  */
163  virtual double DoEval (const double * x) const {
164  this->UpdateNCalls();
165 
166 #ifdef ROOT_FIT_PARALLEL
168 #else
170 #endif
171  }
172 
173  // for derivatives
174  virtual double DoDerivative(const double * x, unsigned int icoord ) const {
175  Gradient(x, &fGrad[0]);
176  return fGrad[icoord];
177  }
178 
179 
180  //data member
181  bool fIsExtended; // flag for indicating if likelihood is extended
182  int fWeight; // flag to indicate if needs to evaluate using weight or weight squared (default weight = 0)
183 
184 
185  mutable unsigned int fNEffPoints; // number of effective points used in the fit
186 
187  mutable std::vector<double> fGrad; // for derivatives
188 
189 
190 };
191 
192  // define useful typedef's
195 
196  } // end namespace Fit
197 
198 } // end namespace ROOT
199 
200 
201 #endif /* ROOT_Fit_LogLikelihoodFCN */
LogLikelihoodFCN(const UnBinData &data, const IModelFunction &func, int weight=0, bool extended=false)
Constructor from unbin data set and model function (pdf) for object managed by users.
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
LogLikelihoodFCN(const LogLikelihoodFCN &f)
Copy constructor.
virtual const IModelFunction & ModelFunction() const
access to const reference to the model function
Definition: BasicFCN.h:79
virtual double DataElement(const double *x, unsigned int i, double *g) const
i-th likelihood contribution and its gradient
void SetModelFunction(const std::shared_ptr< IModelFunction > &func)
Set the function pointer.
Definition: BasicFCN.h:93
LogLikelihoodFCN class for likelihood fits.
::ROOT::Math::IParamMultiFunction IModelFunction
LogLikelihoodFCN & operator=(const LogLikelihoodFCN &rhs)
Assignment operator.
Class describing the unbinned data sets (just x coordinates values) of any dimensions.
Definition: UnBinData.h:47
virtual const UnBinData & Data() const
access to const reference to the data
Definition: BasicFCN.h:73
STL namespace.
virtual BaseFunction * Clone() const
clone the function (need to return Base for Windows)
virtual BaseObjFunction::Type_t Type() const
get type of fit method function
Double_t x[n]
Definition: legend1.C:17
void EvaluateLogLGradient(const IModelFunction &func, const UnBinData &data, const double *x, double *grad, unsigned int &nPoints)
evaluate the LogL gradient given a model function and the data at the point x.
Definition: FitUtil.cxx:1016
virtual ~LogLikelihoodFCN()
Destructor (no operations)
virtual double DoDerivative(const double *x, unsigned int icoord) const
void SetData(const std::shared_ptr< UnBinData > &data)
Set the data pointer.
Definition: BasicFCN.h:90
BasicFCN class: base class for the objective functions used in the fits It has a reference to the dat...
Definition: BasicFCN.h:43
BaseObjFunction::BaseFunction BaseFunction
double EvaluateLogL(const IModelFunction &func, const UnBinData &data, const double *x, int iWeight, bool extended, unsigned int &nPoints)
evaluate the LogL given a model function and the data at the point x.
Definition: FitUtil.cxx:891
void UseSumOfWeightSquare(bool on=true)
LogLikelihoodFCN< ROOT::Math::IMultiGradFunction > LogLikelihoodGradFunction
std::vector< double > fGrad
virtual double DoEval(const double *x) const
Evaluation of the function (required by interface)
LogLikelihoodFCN(const std::shared_ptr< UnBinData > &data, const std::shared_ptr< IModelFunction > &func, int weight=0, bool extended=false)
Constructor from unbin data set and model function (pdf)
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:132
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:843
LogLikelihoodFCN< ROOT::Math::IMultiGenFunction > LogLikelihoodFunction
double func(double *x, double *p)
Definition: stressTF1.cxx:213
virtual unsigned int NFitPoints() const
std::shared_ptr< IModelFunction > ModelFunctionPtr() const
access to function pointer
Definition: BasicFCN.h:82
BasicFCN< FunType, UnBinData > BaseFCN
IParametricFunctionMultiDim IParamMultiFunction
virtual void UpdateNCalls() const
update number of calls
virtual void Gradient(const double *x, double *g) const
const int NPar
Type_t
enumeration specyfing the possible fit method types
::ROOT::Math::BasicFitMethodFunction< FunType > BaseObjFunction
std::shared_ptr< UnBinData > DataPtr() const
access to data pointer
Definition: BasicFCN.h:76