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