Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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;
46  typedef BasicFCN<DerivFunType,ModelFunType,UnBinData> BaseFCN;
47 
48  typedef ::ROOT::Math::BasicFitMethodFunction<DerivFunType> BaseObjFunction;
49  typedef typename BaseObjFunction::BaseFunction BaseFunction;
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  */
87  LogLikelihoodFCN(const LogLikelihoodFCN & f) :
88  BaseFCN(f.DataPtr(), f.ModelFunctionPtr() ),
89  fIsExtended(f.fIsExtended ),
90  fWeight( f.fWeight ),
91  fNEffPoints( f.fNEffPoints ),
92  fGrad( f.fGrad),
93  fExecutionPolicy(f.fExecutionPolicy)
94  { }
95 
96 
97  /**
98  Assignment operator
99  */
100  LogLikelihoodFCN & operator = (const LogLikelihoodFCN & rhs) {
101  SetData(rhs.DataPtr() );
102  SetModelFunction(rhs.ModelFunctionPtr() );
103  fNEffPoints = rhs.fNEffPoints;
104  fGrad = rhs.fGrad;
105  fIsExtended = rhs.fIsExtended;
106  fWeight = rhs.fWeight;
107  fExecutionPolicy = rhs.fExecutionPolicy;
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();
124  return FitUtil::EvaluatePdf(BaseFCN::ModelFunction(), BaseFCN::Data(), x, i, g);
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
130  FitUtil::Evaluate<typename BaseFCN::T>::EvalLogLGradient(BaseFCN::ModelFunction(), BaseFCN::Data(), x, g,
131  fNEffPoints, fExecutionPolicy);
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();
158  return FitUtil::Evaluate<T>::EvalLogL(BaseFCN::ModelFunction(), BaseFCN::Data(), x, fWeight, fIsExtended, fNEffPoints, fExecutionPolicy);
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 
177  ::ROOT::Fit::ExecutionPolicy fExecutionPolicy; // Execution policy
178 };
179  // define useful typedef's
180  // using LogLikelihoodFunction_v = LogLikelihoodFCN<ROOT::Math::IMultiGenFunction, ROOT::Math::IParametricFunctionMultiDimTempl<T>>;
181  typedef LogLikelihoodFCN<ROOT::Math::IMultiGenFunction, ROOT::Math::IParamMultiFunction> LogLikelihoodFunction;
182  typedef LogLikelihoodFCN<ROOT::Math::IMultiGradFunction, ROOT::Math::IParamMultiFunction> LogLikelihoodGradFunction;
183 
184  } // end namespace Fit
185 
186 } // end namespace ROOT
187 
188 
189 #endif /* ROOT_Fit_LogLikelihoodFCN */