Logo ROOT   6.08/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 #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 */
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
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
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:80
void SetModelFunction(const std::shared_ptr< IModelFunction > &func)
Set the function pointer.
Definition: BasicFCN.h:94
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:47
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:74
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:1018
virtual ~LogLikelihoodFCN()
Destructor (no operations)
void SetData(const std::shared_ptr< UnBinData > &data)
Set the data pointer.
Definition: BasicFCN.h:91
BasicFCN class: base class for the objective functions used in the fits It has a reference to the dat...
Definition: BasicFCN.h:44
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:893
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:83
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:845
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:77
::ROOT::Math::BasicFitMethodFunction< FunType > BaseObjFunction